diff --git a/cmd/bd/update.go b/cmd/bd/update.go index 545f62b63..abf0a60ea 100644 --- a/cmd/bd/update.go +++ b/cmd/bd/update.go @@ -108,6 +108,13 @@ pointless).`, } }() + // Refuse a mis-typed flag value that cobra parsed as a positional issue + // id, before any write path (direct or proxied) can apply a partial + // update. See bd-5247. + if err := errStrayFlagValuePositional(args); err != nil { + return HandleErrorRespectJSON("%s", err) + } + if usesProxiedServer() { return runUpdateProxiedServer(cmd, rootCtx, args) } @@ -827,6 +834,21 @@ type updateIDFailure struct { GuardMismatch bool `json:"guard_mismatch,omitempty"` } +// errStrayFlagValuePositional refuses, before any write, a positional argument +// that contains '='. --set-metadata takes ONE key=value per flag, so +// `--set-metadata a=1 b=2` silently turns `b=2` into a positional issue id; +// no issue id contains '=', so such a positional is unambiguously a mis-typed +// flag value. Rejecting it up front prevents a partial write that would apply +// only the pairs that happened to bind to a flag (bd-5247). +func errStrayFlagValuePositional(args []string) error { + for _, arg := range args { + if strings.Contains(arg, "=") { + return fmt.Errorf("positional argument %q contains '=', which no issue id does — this is a mis-typed flag value; repeat the flag per pair, e.g. --set-metadata a=1 --set-metadata b=2", arg) + } + } + return nil +} + // reportUpdateFailures emits a per-ID failure report on stderr and returns a // nonzero exit error — ExitGuardMismatch when every failure is a // --if-assignee/--if-status guard refusal, 1 otherwise. In --json mode the diff --git a/cmd/bd/update_stray_positional_test.go b/cmd/bd/update_stray_positional_test.go new file mode 100644 index 000000000..ff24ee8b9 --- /dev/null +++ b/cmd/bd/update_stray_positional_test.go @@ -0,0 +1,91 @@ +// Tests for bd-5247: `bd update --set-metadata a=1 b=2` silently turns `b=2` +// into a positional issue id (--set-metadata takes one key=value per flag). +// Before this guard, `a=1` was written and only the unbound pairs failed, so a +// caller could not tell a full write from a 1-of-N write. The guard refuses a +// `=`-bearing positional before ANY write, so no partial update lands. +// +// This file MUST NOT carry a cgo build tag: it exercises the default sqlite +// backend via a bd binary built with the gms_pure_go tag, reusing the helpers +// in update_multi_id_exit_test.go (same package). + +package main + +import ( + "encoding/json" + "strings" + "testing" +) + +func TestErrStrayFlagValuePositional(t *testing.T) { + // A positional carrying '=' is a mis-typed flag value and must be refused. + if err := errStrayFlagValuePositional([]string{"test-abc", "probe_b=2"}); err == nil { + t.Fatal("errStrayFlagValuePositional with a '='-bearing positional = nil, want error") + } + // Plain issue ids (no '=') must pass through untouched. + if err := errStrayFlagValuePositional([]string{"test-abc", "test-def"}); err != nil { + t.Fatalf("errStrayFlagValuePositional with plain ids = %v, want nil", err) + } + if err := errStrayFlagValuePositional(nil); err != nil { + t.Fatalf("errStrayFlagValuePositional(nil) = %v, want nil", err) + } +} + +// showStrayMetadata fetches an issue's metadata map via bd show --json. +func showStrayMetadata(t *testing.T, bd, dir, id string) map[string]interface{} { + t.Helper() + stdout, stderr, code := runBDMultiID(t, bd, dir, "show", id, "--json") + if code != 0 { + t.Fatalf("bd show %s failed (exit %d):\nstdout:\n%s\nstderr:\n%s", id, code, stdout, stderr) + } + var details []struct { + ID string `json:"id"` + Metadata map[string]interface{} `json:"metadata"` + } + if err := json.Unmarshal([]byte(stdout), &details); err != nil { + t.Fatalf("parsing show --json for %s: %v\n%s", id, err, stdout) + } + if len(details) != 1 || details[0].ID != id { + t.Fatalf("show --json for %s returned unexpected issues:\n%s", id, stdout) + } + return details[0].Metadata +} + +func TestUpdateStrayMetadataPositionalRefusedBeforeWrite(t *testing.T) { + bd, dir := setupMultiIDUpdateDB(t) + id := createMultiIDUpdateIssue(t, bd, dir, "stray metadata target") + + // Only `probe_a=1` binds to the flag; `probe_b=2` and `probe_c=3` land as + // positional ids. The command must refuse before writing anything. + stdout, stderr, code := runBDMultiID(t, bd, dir, + "update", id, "--set-metadata", "probe_a=1", "probe_b=2", "probe_c=3") + if code == 0 { + t.Errorf("bd update with a '='-bearing positional exited 0, want nonzero\nstdout:\n%s\nstderr:\n%s", stdout, stderr) + } + if !strings.Contains(stderr, "probe_b=2") { + t.Errorf("stderr does not name the mis-typed pair probe_b=2:\n%s", stderr) + } + + // The refusal is before any write: no pair — not even the bound probe_a — + // may have landed. This is the half a message-only fix would leave broken. + meta := showStrayMetadata(t, bd, dir, id) + if len(meta) != 0 { + t.Errorf("metadata is %v, want empty: refusal must prevent the partial write", meta) + } +} + +func TestUpdateSetMetadataRepeatedFlagStillWrites(t *testing.T) { + bd, dir := setupMultiIDUpdateDB(t) + id := createMultiIDUpdateIssue(t, bd, dir, "correct form target") + + // The correct form (one flag per pair) has no stray positional and must + // still apply normally — the guard does not break valid usage. + stdout, stderr, code := runBDMultiID(t, bd, dir, + "update", id, "--set-metadata", "probe_a=1", "--set-metadata", "probe_b=2") + if code != 0 { + t.Fatalf("bd update with repeated --set-metadata exited %d, want 0\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr) + } + meta := showStrayMetadata(t, bd, dir, id) + if meta["probe_a"] == nil || meta["probe_b"] == nil { + t.Errorf("metadata = %v, want both probe_a and probe_b written", meta) + } +}