fix(binding): split ValidateForUpdate so edits can preserve the secret
Real bug found via browser-testing the new bindings UI (t2db.8): UpdateBinding's own store-layer contract documents that an empty Secret means "preserve the existing one" (COALESCE against NULLIF), but handleBindingUpdate validated with the create-time Validate(), which unconditionally requires a full-length secret -- rejecting the exact "leave blank to keep it" edit flow the store was built to allow. Root-cause fix, not a UI workaround: Binding.Validate() (create, unchanged, still requires a real secret -- there's nothing to fall back to on insert) now delegates shared field checks to a private validateCommon(); a new ValidateForUpdate() reuses the same checks but only enforces the secret-length floor when the caller actually supplied a new one. handleBindingUpdate now calls ValidateForUpdate. 3 new domain tests + 1 new API-layer test pin: empty secret accepted on update, a genuinely-short new secret still rejected on update, empty secret still rejected on create.
This commit is contained in:
1 parent
8f9f2d601e
commit
912db8f590
4 files changed
+101
-9
No files matched your search
@@ -68,12 +68,42 @@ type Binding struct {
|
||||
// floor is chosen so a one-character typo cannot arm.
|
||||
const minSecretLen = 16
|
||||
|
||||
// Validate checks a binding is well-formed before it is persisted or
|
||||
// evaluated. A non-empty Name, a non-empty Matcher.Source, a positive
|
||||
// MappingID, a non-empty Workflow, and a sufficiently long Secret are
|
||||
// all required. Workflow existence is checked at the API/store layer
|
||||
// because the domain package does not import the workflow registry.
|
||||
// Validate checks a binding is well-formed before it is newly persisted.
|
||||
// A non-empty Name, a non-empty Matcher.Source, a positive MappingID, a
|
||||
// non-empty Workflow, and a sufficiently long Secret are all required.
|
||||
// Workflow existence is checked at the API/store layer because the domain
|
||||
// package does not import the workflow registry. Use ValidateForUpdate
|
||||
// instead when editing an existing binding, where an empty Secret is a
|
||||
// meaningful "keep the current one," not a missing value.
|
||||
func (b Binding) Validate() error {
|
||||
if err := b.validateCommon(); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(b.Secret) < minSecretLen {
|
||||
return fmt.Errorf("binding: secret must be at least %d bytes", minSecretLen)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateForUpdate is Validate for the edit path: every other field is
|
||||
// checked identically, but Secret is only length-checked when the caller
|
||||
// actually supplied one. UpdateBinding's own store-layer contract already
|
||||
// treats an empty Secret as "preserve the existing one" (COALESCE against
|
||||
// NULLIF) -- rejecting that here at the validation gate before the store
|
||||
// ever sees it would make the store's own documented behavior
|
||||
// unreachable. A non-empty Secret (a genuine change) is still held to the
|
||||
// same floor Validate enforces.
|
||||
func (b Binding) ValidateForUpdate() error {
|
||||
if err := b.validateCommon(); err != nil {
|
||||
return err
|
||||
}
|
||||
if b.Secret != "" && len(b.Secret) < minSecretLen {
|
||||
return fmt.Errorf("binding: secret must be at least %d bytes", minSecretLen)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b Binding) validateCommon() error {
|
||||
if strings.TrimSpace(b.Name) == "" {
|
||||
return fmt.Errorf("binding: name is required")
|
||||
}
|
||||
@@ -86,9 +116,6 @@ func (b Binding) Validate() error {
|
||||
if strings.TrimSpace(b.Workflow) == "" {
|
||||
return fmt.Errorf("binding: workflow is required")
|
||||
}
|
||||
if len(b.Secret) < minSecretLen {
|
||||
return fmt.Errorf("binding: secret must be at least %d bytes", minSecretLen)
|
||||
}
|
||||
if (b.Owner == "") != (b.Repo == "") {
|
||||
return fmt.Errorf("binding: owner and repo must both be set or both be empty")
|
||||
}
|
||||
|
||||
@@ -38,6 +38,42 @@ func TestValidateAcceptsBothOwnerAndRepo(t *testing.T) {
|
||||
// (that's both empty) and it isn't a complete pin, so it must be
|
||||
// rejected at validation time rather than silently guessing which
|
||||
// half was meant.
|
||||
// TestValidateForUpdateAcceptsEmptySecret is the fix for a real bug found
|
||||
// via browser testing (t2db.8): UpdateBinding's own documented contract
|
||||
// treats an empty Secret as "preserve the existing one" (COALESCE against
|
||||
// NULLIF), but the API handler validated with the create-time Validate,
|
||||
// which rejected any edit that didn't retype the full secret -- making
|
||||
// the store's own documented behavior unreachable.
|
||||
func TestValidateForUpdateAcceptsEmptySecret(t *testing.T) {
|
||||
b := validBinding()
|
||||
b.Secret = ""
|
||||
if err := b.ValidateForUpdate(); err != nil {
|
||||
t.Fatalf("ValidateForUpdate() = %v, want nil for an empty (preserve-existing) secret", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateForUpdateStillEnforcesLengthOnANewSecret confirms the fix
|
||||
// isn't a blanket bypass: a genuinely-supplied short secret on update is
|
||||
// still rejected, same floor as create.
|
||||
func TestValidateForUpdateStillEnforcesLengthOnANewSecret(t *testing.T) {
|
||||
b := validBinding()
|
||||
b.Secret = "short"
|
||||
if err := b.ValidateForUpdate(); err == nil {
|
||||
t.Fatal("ValidateForUpdate() = nil, want error for a too-short (but non-empty) secret")
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateRejectsEmptySecretOnCreate pins that the fix is scoped to
|
||||
// update only -- Validate (used at create time, where there is no
|
||||
// existing secret to fall back to) still requires a real one.
|
||||
func TestValidateRejectsEmptySecretOnCreate(t *testing.T) {
|
||||
b := validBinding()
|
||||
b.Secret = ""
|
||||
if err := b.Validate(); err == nil {
|
||||
t.Fatal("Validate() = nil, want error for an empty secret at create time")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRejectsPartialRepoPin(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -144,7 +144,7 @@ func (s *Server) handleBindingUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
Repo: req.Repo,
|
||||
Secret: req.Secret,
|
||||
}
|
||||
if err := b.Validate(); err != nil {
|
||||
if err := b.ValidateForUpdate(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -276,6 +276,35 @@ func TestHandleBindingUpdatePreservesOtherFields(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleBindingUpdateWithEmptySecretPreservesExisting is the API-layer
|
||||
// half of UpdateBinding's own documented contract ("Secret is preserved
|
||||
// when the caller passes an empty string ... so a partial update cannot
|
||||
// erase the HMAC secret"): an edit that never touches the secret field
|
||||
// must not be rejected by Validate's create-time secret-length floor,
|
||||
// since nothing about the existing secret is changing.
|
||||
func TestHandleBindingUpdateWithEmptySecretPreservesExisting(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID))
|
||||
var created binding.Binding
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &created)
|
||||
|
||||
update := validBindingRequest("renamed", "sentry", mappingID)
|
||||
update["secret"] = ""
|
||||
w = doJSON(t, srv, http.MethodPatch, "/api/bindings/"+strconv.FormatInt(created.ID, 10), update)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("patch status = %d, want %d (empty secret must mean 'keep existing', not fail validation); body = %s", w.Code, http.StatusOK, w.Body.String())
|
||||
}
|
||||
|
||||
stored, err := srv.Bindings.GetBinding(t.Context(), created.ID)
|
||||
if err != nil || stored == nil {
|
||||
t.Fatalf("GetBinding: %+v, %v", stored, err)
|
||||
}
|
||||
if stored.Secret != "0123456789abcdef0123456789abcdef" {
|
||||
t.Fatalf("stored secret = %q, want the original secret preserved", stored.Secret)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBindingApproveFromDraftRejected(t *testing.T) {
|
||||
srv := bindingTestServer(t)
|
||||
mappingID := seedMapping(t, srv, "m")
|
||||
|
||||
Reference in new issue
Block a user