Files
archie/modules/events/linkcode.go
T
sam e1ada94d78 fix: redact link codes and guard shipped policies
- redact single-use link codes from stored account.link events after handling, with a migration to scrub existing rows
- refuse deletion of shipped org policies and hide the delete action in the UI
- validate agent step models against the workflow model policy at save time, including the default model
2026-11-23 00:38:54 +11:00

65 lines
2.0 KiB
Go

package events
import (
"bytes"
"context"
"encoding/json/jsontext"
json "encoding/json/v2"
"fmt"
"github.com/jmoiron/sqlx"
"github.com/samcharles93/archie-core/modules/events/internal/store"
)
// redactedCode replaces a link code in a stored event, so the record still
// shows that a code was presented.
const redactedCode = "[redacted]"
// minBodyCodeLen is the shortest code redacted from the stored body. Issued
// codes are longer, and replacing a shorter value could rewrite unrelated
// text in the payload.
const minBodyCodeLen = 16
// redactLinkCode returns e with the link code replaced by redactedCode in its
// fields and in its stored body. The sender, name and chat stay, so the event
// still records which channel account asked to link.
func redactLinkCode(e store.Event) (store.Event, error) {
var fields map[string]jsontext.Value
if err := json.Unmarshal(e.Fields, &fields); err != nil {
return e, fmt.Errorf("read link fields: %w", err)
}
var code string
if raw, ok := fields["code"]; ok {
if err := json.Unmarshal(raw, &code); err != nil {
return e, fmt.Errorf("read link code: %w", err)
}
}
if code == "" {
return e, nil
}
fields["code"] = jsontext.Value(`"` + redactedCode + `"`)
redacted, err := json.Marshal(fields)
if err != nil {
return e, fmt.Errorf("write link fields: %w", err)
}
e.Fields = redacted
if len(code) >= minBodyCodeLen {
e.Body = bytes.ReplaceAll(e.Body, []byte(code), []byte(redactedCode))
}
return e, nil
}
// redactHandledLink removes the link code from a handled account.link event
// in the transaction that marks the event dispatched. Every code an event
// presents is redacted, whether it linked the account, had expired or was
// refused, so no event keeps a code past its handling.
func (m *Module) redactHandledLink(ctx context.Context, tx *sqlx.Tx, e store.Event) error {
redacted, err := redactLinkCode(e)
if err != nil {
m.log.WarnContext(ctx, "unreadable link request", "event", e.ID, "error", err)
return nil
}
return store.SetEventPayload(ctx, tx, redacted)
}