fix(update): require explicit opt-in for empty stdin/file DESCRIPTION rewrites
Guard against accidental description blanking when a producer fails but still pipes empty input into bd update. Empty DESCRIPTION replacement from --body-file, --description-file, --stdin, and dash shorthand now requires --allow-empty-description. Inline --description "" unchanged. Closes #2713 Co-Authored-By: Gary <gary@algorune.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
0469a9ab18
commit
f8152a1320
3 changed files with 199 additions and 0 deletions
|
|
@ -92,6 +92,9 @@ create, update, show, or close operation).`,
|
|||
}
|
||||
description, descChanged := getDescriptionFlag(cmd)
|
||||
if descChanged {
|
||||
if err := validateDescriptionUpdate(cmd, description, descChanged); err != nil {
|
||||
FatalErrorRespectJSON("%v", err)
|
||||
}
|
||||
updates["description"] = description
|
||||
}
|
||||
design, designChanged := getDesignFlag(cmd)
|
||||
|
|
@ -586,6 +589,7 @@ func init() {
|
|||
updateCmd.Flags().String("title", "", "New title")
|
||||
updateCmd.Flags().StringP("type", "t", "", "New type (bug|feature|task|epic|chore|decision); custom types require types.custom config")
|
||||
registerCommonIssueFlags(updateCmd)
|
||||
updateCmd.Flags().Bool("allow-empty-description", false, "Allow empty description replacement when reading from stdin or file")
|
||||
updateCmd.Flags().String("spec-id", "", "Link to specification document")
|
||||
updateCmd.Flags().String("acceptance-criteria", "", "DEPRECATED: use --acceptance")
|
||||
_ = updateCmd.Flags().MarkHidden("acceptance-criteria") // Only fails if flag missing (caught in tests)
|
||||
|
|
|
|||
48
cmd/bd/update_description_guard.go
Normal file
48
cmd/bd/update_description_guard.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func descriptionUsesExternalInput(cmd *cobra.Command) bool {
|
||||
if stdinFlag, _ := cmd.Flags().GetBool("stdin"); stdinFlag {
|
||||
return true
|
||||
}
|
||||
if cmd.Flags().Changed("body-file") || cmd.Flags().Changed("description-file") {
|
||||
return true
|
||||
}
|
||||
if cmd.Flags().Changed("description") {
|
||||
desc, _ := cmd.Flags().GetString("description")
|
||||
if desc == "-" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if cmd.Flags().Changed("body") {
|
||||
body, _ := cmd.Flags().GetString("body")
|
||||
if body == "-" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if cmd.Flags().Changed("message") {
|
||||
message, _ := cmd.Flags().GetString("message")
|
||||
if message == "-" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func validateDescriptionUpdate(cmd *cobra.Command, description string, descChanged bool) error {
|
||||
if !descChanged || description != "" || !descriptionUsesExternalInput(cmd) {
|
||||
return nil
|
||||
}
|
||||
|
||||
allowEmptyDescription, _ := cmd.Flags().GetBool("allow-empty-description")
|
||||
if allowEmptyDescription {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("empty description from stdin/file requires --allow-empty-description (or use an explicit inline empty value like --description \"\")")
|
||||
}
|
||||
147
cmd/bd/update_description_guard_test.go
Normal file
147
cmd/bd/update_description_guard_test.go
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func newUpdateDescriptionGuardCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "test",
|
||||
Run: func(cmd *cobra.Command, args []string) {},
|
||||
}
|
||||
registerCommonIssueFlags(cmd)
|
||||
cmd.Flags().Bool("allow-empty-description", false, "Allow empty description replacement when reading from stdin or file")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func withTestStdin(t *testing.T, content string, fn func()) {
|
||||
t.Helper()
|
||||
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create pipe: %v", err)
|
||||
}
|
||||
|
||||
oldStdin := os.Stdin
|
||||
os.Stdin = r
|
||||
t.Cleanup(func() { os.Stdin = oldStdin })
|
||||
|
||||
go func() {
|
||||
_, _ = w.WriteString(content)
|
||||
_ = w.Close()
|
||||
}()
|
||||
|
||||
fn()
|
||||
}
|
||||
|
||||
func TestValidateDescriptionUpdateRejectsEmptyStdinWithoutOptIn(t *testing.T) {
|
||||
withTestStdin(t, "", func() {
|
||||
cmd := newUpdateDescriptionGuardCmd()
|
||||
if err := cmd.ParseFlags([]string{"--stdin"}); err != nil {
|
||||
t.Fatalf("failed to parse flags: %v", err)
|
||||
}
|
||||
|
||||
description, changed := getDescriptionFlag(cmd)
|
||||
err := validateDescriptionUpdate(cmd, description, changed)
|
||||
if err == nil {
|
||||
t.Fatal("expected empty stdin description to be rejected")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "--allow-empty-description") {
|
||||
t.Fatalf("expected opt-in guidance in error, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidateDescriptionUpdateRejectsEmptyDashShorthandWithoutOptIn(t *testing.T) {
|
||||
withTestStdin(t, "", func() {
|
||||
cmd := newUpdateDescriptionGuardCmd()
|
||||
if err := cmd.ParseFlags([]string{"--description", "-"}); err != nil {
|
||||
t.Fatalf("failed to parse flags: %v", err)
|
||||
}
|
||||
|
||||
description, changed := getDescriptionFlag(cmd)
|
||||
err := validateDescriptionUpdate(cmd, description, changed)
|
||||
if err == nil {
|
||||
t.Fatal("expected empty dash shorthand description to be rejected")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "--allow-empty-description") {
|
||||
t.Fatalf("expected opt-in guidance in error, got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidateDescriptionUpdateRejectsEmptyBodyFileWithoutOptIn(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
filePath := filepath.Join(tmpDir, "empty.md")
|
||||
if err := os.WriteFile(filePath, []byte{}, 0644); err != nil {
|
||||
t.Fatalf("failed to write empty file: %v", err)
|
||||
}
|
||||
|
||||
cmd := newUpdateDescriptionGuardCmd()
|
||||
if err := cmd.ParseFlags([]string{"--body-file", filePath}); err != nil {
|
||||
t.Fatalf("failed to parse flags: %v", err)
|
||||
}
|
||||
|
||||
description, changed := getDescriptionFlag(cmd)
|
||||
err := validateDescriptionUpdate(cmd, description, changed)
|
||||
if err == nil {
|
||||
t.Fatal("expected empty body file description to be rejected")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "--allow-empty-description") {
|
||||
t.Fatalf("expected opt-in guidance in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDescriptionUpdateAllowsEmptyStdinWithOptIn(t *testing.T) {
|
||||
withTestStdin(t, "", func() {
|
||||
cmd := newUpdateDescriptionGuardCmd()
|
||||
if err := cmd.ParseFlags([]string{"--stdin", "--allow-empty-description"}); err != nil {
|
||||
t.Fatalf("failed to parse flags: %v", err)
|
||||
}
|
||||
|
||||
description, changed := getDescriptionFlag(cmd)
|
||||
if err := validateDescriptionUpdate(cmd, description, changed); err != nil {
|
||||
t.Fatalf("expected opt-in empty stdin to succeed, got: %v", err)
|
||||
}
|
||||
if description != "" {
|
||||
t.Fatalf("expected empty description, got %q", description)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidateDescriptionUpdateAllowsNonEmptyStdinWithoutOptIn(t *testing.T) {
|
||||
withTestStdin(t, "Updated from stdin\n", func() {
|
||||
cmd := newUpdateDescriptionGuardCmd()
|
||||
if err := cmd.ParseFlags([]string{"--stdin"}); err != nil {
|
||||
t.Fatalf("failed to parse flags: %v", err)
|
||||
}
|
||||
|
||||
description, changed := getDescriptionFlag(cmd)
|
||||
if err := validateDescriptionUpdate(cmd, description, changed); err != nil {
|
||||
t.Fatalf("expected non-empty stdin to succeed, got: %v", err)
|
||||
}
|
||||
if description != "Updated from stdin\n" {
|
||||
t.Fatalf("expected stdin content to round-trip, got %q", description)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidateDescriptionUpdateAllowsExplicitInlineEmpty(t *testing.T) {
|
||||
cmd := newUpdateDescriptionGuardCmd()
|
||||
if err := cmd.ParseFlags([]string{"--description", ""}); err != nil {
|
||||
t.Fatalf("failed to parse flags: %v", err)
|
||||
}
|
||||
|
||||
description, changed := getDescriptionFlag(cmd)
|
||||
if err := validateDescriptionUpdate(cmd, description, changed); err != nil {
|
||||
t.Fatalf("expected explicit inline empty description to succeed, got: %v", err)
|
||||
}
|
||||
if description != "" {
|
||||
t.Fatalf("expected empty inline description, got %q", description)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue