Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(scriptview): split parseConfig validation and share mask sort order (#442)

fx-kurumi3911c02ec216
diff --git a/internal/scriptview/scriptview.go b/internal/scriptview/scriptview.go
index 197b0d83..ea92e702 100644
--- a/internal/scriptview/scriptview.go
+++ b/internal/scriptview/scriptview.go
@@ -66,47 +66,68 @@ func parseConfig(raw string) (Config, error) {
if err := decoder.Decode(new(any)); err != io.EOF {
return config, fmt.Errorf("workspace script view configuration must contain one JSON object")
}
+ if err := validateConfigMode(config); err != nil {
+ return config, err
+ }
+ if err := validateMaskLists(config.Masks, config.Hide); err != nil {
+ return config, err
+ }
+ // Children must be detached before their parent when a legacy container has
+ // both a directory mask and a file mask below it.
+ slices.SortFunc(config.Masks, maskOrder(true))
+ slices.SortFunc(config.Hide, maskOrder(false))
+ return config, nil
+}
+
+// validateConfigMode enforces that refresh requests never execute commands or
+// detach masks, and that script executions never modify canonical masks.
+func validateConfigMode(config Config) error {
if config.Refresh {
if len(config.Argv) != 0 || len(config.Masks) != 0 {
- return config, fmt.Errorf("instruction mask refresh cannot execute commands or detach masks")
+ return fmt.Errorf("instruction mask refresh cannot execute commands or detach masks")
}
} else {
if len(config.Hide) != 0 {
- return config, fmt.Errorf("workspace script commands cannot modify canonical instruction masks")
+ return fmt.Errorf("workspace script commands cannot modify canonical instruction masks")
}
if len(config.Argv) == 0 || config.Argv[0] == "" {
- return config, fmt.Errorf("workspace script view command is empty")
+ return fmt.Errorf("workspace script view command is empty")
}
}
for _, argument := range config.Argv {
if strings.IndexByte(argument, 0) >= 0 {
- return config, fmt.Errorf("workspace script view command contains a null byte")
+ return fmt.Errorf("workspace script view command contains a null byte")
}
}
- for _, list := range [][]string{config.Masks, config.Hide} {
+ return nil
+}
+
+// validateMaskLists rejects invalid or duplicate entries within each list.
+func validateMaskLists(lists ...[]string) error {
+ for _, list := range lists {
seen := make(map[string]bool, len(list))
for _, target := range list {
if !ValidMask(target) || seen[target] {
- return config, fmt.Errorf("invalid or duplicate workspace instruction mask %q", target)
+ return fmt.Errorf("invalid or duplicate workspace instruction mask %q", target)
}
seen[target] = true
}
}
- // Children must be detached before their parent when a legacy container has
- // both a directory mask and a file mask below it.
- slices.SortFunc(config.Masks, func(a, b string) int {
- if len(a) != len(b) {
- return len(b) - len(a)
- }
- return strings.Compare(a, b)
- })
- slices.SortFunc(config.Hide, func(a, b string) int {
+ return nil
+}
+
+// maskOrder sorts mask paths by length (longest or shortest first), breaking
+// ties lexically so the order is deterministic.
+func maskOrder(longestFirst bool) func(a, b string) int {
+ return func(a, b string) int {
if len(a) != len(b) {
+ if longestFirst {
+ return len(b) - len(a)
+ }
return len(a) - len(b)
}
return strings.Compare(a, b)
- })
- return config, nil
+ }
}
// ValidMask accepts only repository instruction paths that the platform masks.
diff --git a/internal/scriptview/scriptview_parse_config_test.go b/internal/scriptview/scriptview_parse_config_test.go
new file mode 100644
index 00000000..c53f99c8
--- /dev/null
+++ b/internal/scriptview/scriptview_parse_config_test.go
@@ -0,0 +1,69 @@
+package scriptview
+
+import (
+ "slices"
+ "strings"
+ "testing"
+)
+
+func TestMaskOrderSortsByLengthThenLexically(t *testing.T) {
+ input := []string{"/workspace/b/CLAUDE.md", "/workspace/.claude", "/workspace/a/CLAUDE.md", "/workspace/AGENTS.md"}
+
+ longest := slices.Clone(input)
+ slices.SortFunc(longest, maskOrder(true))
+ wantLongest := []string{"/workspace/a/CLAUDE.md", "/workspace/b/CLAUDE.md", "/workspace/AGENTS.md", "/workspace/.claude"}
+ if !slices.Equal(longest, wantLongest) {
+ t.Fatalf("longest first = %v, want %v", longest, wantLongest)
+ }
+
+ shortest := slices.Clone(input)
+ slices.SortFunc(shortest, maskOrder(false))
+ wantShortest := []string{"/workspace/.claude", "/workspace/AGENTS.md", "/workspace/a/CLAUDE.md", "/workspace/b/CLAUDE.md"}
+ if !slices.Equal(shortest, wantShortest) {
+ t.Fatalf("shortest first = %v, want %v", shortest, wantShortest)
+ }
+}
+
+func TestValidateMaskListsChecksEachListIndependently(t *testing.T) {
+ if err := validateMaskLists([]string{"/workspace/CLAUDE.md"}, []string{"/workspace/CLAUDE.md"}); err != nil {
+ t.Fatalf("same mask in different lists should be accepted: %v", err)
+ }
+ if err := validateMaskLists(nil, nil); err != nil {
+ t.Fatalf("empty lists should be accepted: %v", err)
+ }
+ for _, list := range [][]string{
+ {"/workspace/CLAUDE.md", "/workspace/CLAUDE.md"},
+ {"/workspace/README.md"},
+ {"/workspace/.git/CLAUDE.md"},
+ } {
+ err := validateMaskLists(list)
+ if err == nil || !strings.Contains(err.Error(), "invalid or duplicate workspace instruction mask") {
+ t.Fatalf("validateMaskLists(%v) = %v, want invalid/duplicate error", list, err)
+ }
+ }
+}
+
+func TestValidateConfigModeMessages(t *testing.T) {
+ cases := []struct {
+ config Config
+ want string
+ }{
+ {Config{Refresh: true, Argv: []string{"sh"}}, "instruction mask refresh cannot execute commands or detach masks"},
+ {Config{Refresh: true, Masks: []string{"/workspace/CLAUDE.md"}}, "instruction mask refresh cannot execute commands or detach masks"},
+ {Config{Argv: []string{"sh"}, Hide: []string{"/workspace/CLAUDE.md"}}, "workspace script commands cannot modify canonical instruction masks"},
+ {Config{}, "workspace script view command is empty"},
+ {Config{Argv: []string{""}}, "workspace script view command is empty"},
+ {Config{Argv: []string{"sh", "a\x00b"}}, "workspace script view command contains a null byte"},
+ }
+ for _, tc := range cases {
+ err := validateConfigMode(tc.config)
+ if err == nil || err.Error() != tc.want {
+ t.Fatalf("validateConfigMode(%+v) = %v, want %q", tc.config, err, tc.want)
+ }
+ }
+ for _, ok := range []Config{{Refresh: true, Hide: []string{"/workspace/CLAUDE.md"}}, {Argv: []string{"sh", "-c", "true"}}} {
+ if err := validateConfigMode(ok); err != nil {
+ t.Fatalf("validateConfigMode(%+v) = %v, want nil", ok, err)
+ }
+ }
+}