From 498fc011a45fb341a7c8f9c512848dadbcffae5f Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 01:51:07 +1000 Subject: [PATCH] fix(store): keep an empty binding secret empty so a partial edit cannot erase it UpdateBinding documents that an empty caller Secret is preserved by COALESCE(NULLIF(?, ''), secret), and binding.ValidateForUpdate explicitly makes an empty Secret mean "keep the current one". The sentinel was defeated before the SQL saw it: encryptBindingSecret ran first and, with a binding cipher configured, sealed the empty string into a non-empty "arcie-binding:v1::" envelope. NULLIF(?, '') then no longer matched, COALESCE took the envelope, and a routine PATCH that changed only name/source/ workflow overwrote the shared HMAC secret with the encrypted empty value. Once re-approved the binding could never authenticate an inbound webhook again, and nothing surfaced the loss. Fix it at the producer rather than by re-testing emptiness at the SQL layer: an empty secret is the store's "no secret" sentinel, so it stays empty and the existing COALESCE guard keeps working. decryptBindingSecret already treats an empty stored value as nothing-to-decrypt, so this is symmetric. The only existing coverage, TestUpdateBindingPreservesSecretWhenEmpty, opens the store without a cipher and so never exercised this path. Add a table-driven test over the cipher path covering both directions -- an empty caller secret must preserve the stored secret, and a supplied one must still replace it, so the fix cannot degrade into "updates never change the secret". Found by an adversarial package sweep (temp tracker: persistence-1, .local/issue-tracker/). --- internal/store/bindings.go | 11 ++++++ internal/store/bindings_test.go | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/internal/store/bindings.go b/internal/store/bindings.go index 395e337d4..dd0e095b9 100644 --- a/internal/store/bindings.go +++ b/internal/store/bindings.go @@ -306,10 +306,21 @@ func (s *Store) ArmedBindingsForSource(ctx context.Context, source string) ([]bi // encryptBindingSecret seals a plaintext secret for persistence. With no // cipher configured it returns the value unchanged (legacy plaintext). +// +// An empty secret is the store's sentinel for "no secret" and is returned +// unchanged even when a cipher is installed. Sealing it would yield a +// non-empty envelope, and callers rely on emptiness surviving to the SQL +// layer: UpdateBinding preserves an existing secret through +// COALESCE(NULLIF(?, ”), secret), which cannot fire once encryption has +// turned an empty caller secret into a non-empty value. A partial edit would +// then overwrite the binding's HMAC secret with an encrypted empty string. func (s *Store) encryptBindingSecret(plaintext string) (string, error) { if s.bindingsCipher == nil { return plaintext, nil } + if plaintext == "" { + return "", nil + } return s.bindingsCipher.Encrypt(plaintext) } diff --git a/internal/store/bindings_test.go b/internal/store/bindings_test.go index f5bab4379..c3542e961 100644 --- a/internal/store/bindings_test.go +++ b/internal/store/bindings_test.go @@ -4,6 +4,7 @@ import ( "database/sql" "errors" "path/filepath" + "strings" "testing" "github.com/samcharles93/archie-core/internal/domain/binding" @@ -277,6 +278,67 @@ func TestUpdateBindingPreservesSecretWhenEmpty(t *testing.T) { } } +// TestUpdateBindingSecretWithCipher covers the same empty-secret-means-preserve +// contract as TestUpdateBindingPreservesSecretWhenEmpty, but with an at-rest +// cipher installed -- the configuration every deployment with +// [bindings].encryption_key actually runs. +// +// The sibling test opens the store without a cipher, where +// COALESCE(NULLIF(?, ”), secret) sees a literal empty string and holds. With a +// cipher the secret is encrypted BEFORE that guard runs, so an empty caller +// secret was sealed into a non-empty envelope, NULLIF stopped firing, and a +// name-only edit overwrote the binding's HMAC secret with an encrypted empty +// value -- after which the binding can never authenticate an inbound webhook. +// +// The second case is the other half of the contract: a genuinely supplied +// secret must still replace the stored one, so the fix cannot degrade into +// "updates never change the secret". +func TestUpdateBindingSecretWithCipher(t *testing.T) { + t.Parallel() + + const original = "0123456789abcdef0123456789abcdef" + replacement := strings.Repeat("f", len(original)) + + cases := []struct { + name string + newSecret string + want string + }{ + {"empty caller secret preserves the stored secret", "", original}, + {"a supplied secret replaces the stored secret", replacement, replacement}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + s := openTestWithBindingCipher(t) + b := testBinding("sentry") + b.Secret = original + id, err := s.InsertBinding(t.Context(), b) + if err != nil { + t.Fatalf("InsertBinding: %v", err) + } + + updated := testBinding("sentry") + updated.ID = id + updated.Name = "renamed binding" // an ordinary edit that does not touch the secret + updated.Secret = tc.newSecret + if err := s.UpdateBinding(t.Context(), updated); err != nil { + t.Fatalf("UpdateBinding: %v", err) + } + + got, err := s.GetBinding(t.Context(), id) + if err != nil || got == nil { + t.Fatalf("GetBinding after update: %+v, %v", got, err) + } + if got.Secret != tc.want { + t.Fatalf("secret = %q, want %q", got.Secret, tc.want) + } + }) + } +} + func TestApproveBindingFromPendingApprovalToArmedSucceeds(t *testing.T) { s := openTest(t) id, err := s.InsertBinding(t.Context(), testBinding("sentry"))