From 30b4c28e061ec059ff0fc218cc1e4c26a899119d Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 21 Nov 2026 13:47:37 +1100 Subject: [PATCH] fix(events): a source save keeps its stored verification (#2021) A PUT that names no verification keeps the stored scheme, token hash and sealed secret, and an hmac save with a blank secret keeps the sealed one. The sources page gains an Edit action that reuses the add dialog. --- modules/events/api.go | 53 ++++++++++++++++++++++++++---------- ui/src/events/SourceList.vue | 39 ++++++++++++++++++++------ 2 files changed, 70 insertions(+), 22 deletions(-) diff --git a/modules/events/api.go b/modules/events/api.go index 2106688ef..e49cc7dd6 100644 --- a/modules/events/api.go +++ b/modules/events/api.go @@ -76,35 +76,60 @@ type sourceCreated struct { URL string `json:"url" doc:"Where the sender posts its events"` } -// putSource creates a source or replaces how it verifies deliveries. A token -// source's token is returned once and stored only as its hash. An hmac -// source's secret is sealed and never returned. +// putSource creates a source or changes it. A save that names no verification +// keeps the stored scheme and its credentials. A token is returned once, when +// a token scheme is chosen, and stored only as its hash. An hmac secret is +// sealed and never returned; leaving it blank keeps the sealed one. func (m *Module) putSource(ctx context.Context, in *putSourceInput) (*httpapi.Reply[sourceCreated], error) { if !validName.MatchString(in.Name) || !validName.MatchString(in.Body.Connector) { return nil, huma.Error400BadRequest("a source needs a lowercase name and a connector") } + existing, err := m.store.Source(ctx, m.orgID, in.Name) + if err != nil && !errors.Is(err, store.ErrNotFound) { + return nil, huma.Error500InternalServerError("couldn't read the source", err) + } + found := err == nil + created := sourceCreated{Source: in.Name, URL: "/hooks/" + in.Name} src := store.Source{OrgID: m.orgID, Name: in.Name, Connector: in.Body.Connector, Verification: in.Body.Verification} - if src.Verification == "" { + switch { + case src.Verification == "" && found: + // Naming no scheme keeps the stored one with its token or secret, so + // a save can't downgrade a source. + src = existing + src.Connector = in.Body.Connector + case src.Verification == "": src.Verification = store.Token } - created := sourceCreated{Source: in.Name, URL: "/hooks/" + in.Name} - switch src.Verification { - case store.Token: + if src.Verification == store.Token && (!found || in.Body.Verification != "") { token, err := newToken() if err != nil { return nil, huma.Error500InternalServerError("couldn't make a token", err) } src.TokenSHA256, created.Token = httpjson.TokenHash(token), token + } + switch src.Verification { + case store.Token, store.None: + if in.Body.Verification != "" && src.Verification == store.None { + src.TokenSHA256 = "" + } case store.HMAC: - if in.Body.HMACHeader == "" || in.Body.HMACSecret == "" { + if in.Body.Verification != "" { + src.HMACHeader, src.HMACPrefix, src.TokenSHA256 = in.Body.HMACHeader, in.Body.HMACPrefix, "" + switch { + case in.Body.HMACSecret != "": + sealed, err := m.sealer.Seal(secretLabel(m.orgID, in.Name), []byte(in.Body.HMACSecret)) + if err != nil { + return nil, huma.Error500InternalServerError("couldn't seal the secret", err) + } + src.HMACSecretSealed = sealed + case found && existing.Verification == store.HMAC: + // A blank secret keeps the sealed one. + src.HMACSecretSealed = existing.HMACSecretSealed + } + } + if src.HMACHeader == "" || len(src.HMACSecretSealed) == 0 { return nil, huma.Error400BadRequest("an hmac source needs the signature header and a secret") } - sealed, err := m.sealer.Seal(secretLabel(m.orgID, in.Name), []byte(in.Body.HMACSecret)) - if err != nil { - return nil, huma.Error500InternalServerError("couldn't seal the secret", err) - } - src.HMACHeader, src.HMACPrefix, src.HMACSecretSealed = in.Body.HMACHeader, in.Body.HMACPrefix, sealed - case store.None: default: return nil, huma.Error400BadRequest("verification is token, hmac or none") } diff --git a/ui/src/events/SourceList.vue b/ui/src/events/SourceList.vue index ad524898d..d701a85df 100644 --- a/ui/src/events/SourceList.vue +++ b/ui/src/events/SourceList.vue @@ -55,17 +55,39 @@ async function put(name: string, connector: string, verification?: SourceVerific } const adding = ref(false); +const editing = ref(null); const blank = { name: "", connector: "", verification: "token" as SourceVerification["verification"], hmac_header: "", hmac_prefix: "", hmac_secret: "" }; const draft = ref({ ...blank }); async function add(): Promise { const { name, connector, ...verification } = draft.value; - if (await put(name.trim(), connector, verification)) { + // Saving an unchanged token source must not rotate its token. + const keepsToken = editing.value?.verification === "token" && verification.verification === "token"; + if (await put(name.trim(), connector, keepsToken ? undefined : verification)) { adding.value = false; + editing.value = null; draft.value = { ...blank }; } } +// A blank secret keeps the stored one, so it is required only when a source +// has none yet. +const secretOptional = computed(() => editing.value?.verification === "hmac" && editing.value.hmac_secret_set); const addDisabled = computed(() => busy.value || !draft.value.name || !draft.value.connector - || (draft.value.verification === "hmac" && (!draft.value.hmac_header || !draft.value.hmac_secret))); + || (draft.value.verification === "hmac" && (!draft.value.hmac_header || (!draft.value.hmac_secret && !secretOptional.value)))); +function openAdd(): void { + editing.value = null; + draft.value = { ...blank }; + adding.value = true; + actionError.value = ""; +} +function openEdit(source: EventSource_): void { + editing.value = source; + draft.value = { + name: source.name, connector: source.connector, verification: source.verification, + hmac_header: source.hmac_header ?? "", hmac_prefix: source.hmac_prefix ?? "", hmac_secret: "", + }; + adding.value = true; + actionError.value = ""; +} const keyFieldsOf = ref(null); const keyFields = ref(""); @@ -104,7 +126,7 @@ const connectorNames = computed(() => (connectors.data.value ?? []).map((c) => c