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"))