GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(cli): forward a root --allow-escalation into exec, reject it else… · Twigpine/zero@033e3e8

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • internal/cli
    • app.go
    • root_flag_order_test.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎internal/cli/app.go‎

Copy file name to clipboardExpand all lines: internal/cli/app.go
+32-17Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,23 @@ func runWithDeps(args []string, stdout io.Writer, stderr io.Writer, deps appDeps
323323
return runInteractiveTUI(stderr, deps, agent.PermissionModeAsk, addDirs, theme, allowEscalation)
324324
}
325325

326-
// --add-dir grants an extra write root, and only the interactive TUI and
327-
// exec dispatch paths consume one. Fail loud everywhere else rather than
328-
// silently discarding an explicit grant — including help/version, which
329-
// run no agent and could only ignore it. The allowlist names exactly the
330-
// cases below that forward addDirs; a future subcommand is rejected by
331-
// default until it opts in here.
332-
if len(addDirs) > 0 {
333-
switch args[0] {
334-
case "--skip-permissions-unsafe", "-p", "--prompt", "exec":
335-
// Forwarded by the matching case below.
336-
default:
337-
return writeAppError(stderr, "--add-dir is only supported for the interactive TUI and exec", 1)
338-
}
326+
// --add-dir grants an extra write root and --allow-escalation opts a run
327+
// into mid-run model escalation; only the interactive TUI and exec consume
328+
// either. Both are forwarded to exec below and rejected loudly everywhere
329+
// else rather than silently discarded, including help/version, which run
330+
// no agent and could only ignore them. The allowlist names exactly the
331+
// cases below that forward; a future subcommand is rejected by default
332+
// until it opts in here.
333+
forwardsRootFlags := false
334+
switch args[0] {
335+
case "--skip-permissions-unsafe", "-p", "--prompt", "exec":
336+
forwardsRootFlags = true
337+
}
338+
if len(addDirs) > 0 && !forwardsRootFlags {
339+
return writeAppError(stderr, "--add-dir is only supported for the interactive TUI and exec", 1)
340+
}
341+
if allowEscalation && !forwardsRootFlags {
342+
return writeAppError(stderr, "--allow-escalation is only supported for the interactive TUI and exec", 1)
339343
}
340344

341345
switch args[0] {
@@ -412,16 +416,16 @@ func runWithDeps(args []string, stdout io.Writer, stderr io.Writer, deps appDeps
412416
if len(args) < 2 {
413417
return writePromptRequired(stderr)
414418
}
415-
// Forward leading --add-dir occurrences so exec's own parser collects them.
419+
// Forward the root flags exec consumes so its own parser collects them.
416420
// Use the inline --prompt=<value> form so a prompt whose first character is a
417421
// dash (e.g. `zero -p "-foo"`) is taken verbatim instead of being mistaken for
418422
// a flag and rejected with "--prompt requires a value" (matches the cron path).
419-
execArgs := append(addDirFlagArgs(addDirs), "--prompt="+args[1])
423+
execArgs := append(rootFlagArgs(addDirs, allowEscalation), "--prompt="+args[1])
420424
execArgs = append(execArgs, args[2:]...)
421425
return runExec(execArgs, stdout, stderr, deps)
422426
case "exec":
423-
// Forward leading --add-dir occurrences so exec's own parser collects them.
424-
return runExec(append(addDirFlagArgs(addDirs), args[1:]...), stdout, stderr, deps)
427+
// Forward the root flags exec consumes so its own parser collects them.
428+
return runExec(append(rootFlagArgs(addDirs, allowEscalation), args[1:]...), stdout, stderr, deps)
425429
case "completions":
426430
return runCompletions(args[1:], stdout, stderr)
427431
case "daemon":
@@ -1393,6 +1397,17 @@ func addDirFlagArgs(addDirs []string) []string {
13931397
return flags
13941398
}
13951399

1400+
// rootFlagArgs re-synthesises the root flags exec consumes, in the spelling
1401+
// its own parser accepts, so a flag written before the subcommand reaches
1402+
// the run exactly as one written after it would.
1403+
func rootFlagArgs(addDirs []string, allowEscalation bool) []string {
1404+
flags := addDirFlagArgs(addDirs)
1405+
if allowEscalation {
1406+
flags = append(flags, "--allow-escalation")
1407+
}
1408+
return flags
1409+
}
1410+
13961411
// splitLeadingAddDirFlags strips leading --add-dir flags from the root
13971412
// argument list (zero --add-dir <path> [--add-dir <path>] [subcommand …]).
13981413
// Subcommands like exec parse their own --add-dir occurrences.
Collapse file

‎internal/cli/root_flag_order_test.go‎

Copy file name to clipboardExpand all lines: internal/cli/root_flag_order_test.go
+135-14Lines changed: 135 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,55 @@
11
package cli
22

33
import (
4+
"bytes"
5+
"context"
46
"path/filepath"
57
"strings"
68
"testing"
79

810
"github.com/Gitlawb/zero/internal/agent"
11+
"github.com/Gitlawb/zero/internal/config"
12+
"github.com/Gitlawb/zero/internal/sandbox"
13+
"github.com/Gitlawb/zero/internal/tui"
14+
"github.com/Gitlawb/zero/internal/zeroruntime"
915
)
1016

17+
// rootFlagOrderings is every ordering of the given flag groups, each group
18+
// kept intact so a flag stays next to its value.
19+
func rootFlagOrderings(groups [][]string) [][]string {
20+
if len(groups) == 0 {
21+
return [][]string{{}}
22+
}
23+
var orderings [][]string
24+
for index, group := range groups {
25+
rest := append(append([][]string{}, groups[:index]...), groups[index+1:]...)
26+
for _, tail := range rootFlagOrderings(rest) {
27+
orderings = append(orderings, append(append([]string{}, group...), tail...))
28+
}
29+
}
30+
return orderings
31+
}
32+
1133
// ROOT FLAGS COMPOSE IN ANY ORDER. Each leading-flag splitter stops at the first
1234
// token it does not own, so running them once in a fixed sequence made the
1335
// order the operator wrote them in load-bearing: `zero --allow-escalation
1436
// --theme auto` stranded `--theme auto` as an unknown command and exited with
1537
// an argument error instead of launching. Every ordering of the three root
16-
// flags, on the ask path and the unsafe path, has to reach the TUI with all
17-
// three applied.
38+
// flags, with --skip-permissions-unsafe absent or at any position, has to reach
39+
// the TUI with all three applied.
1840
func TestRootFlagsComposeInAnyOrder(t *testing.T) {
1941
extra := t.TempDir()
2042
resolvedExtra, err := filepath.EvalSymlinks(extra)
2143
if err != nil {
2244
t.Fatal(err)
2345
}
24-
for _, args := range [][]string{
25-
{"--allow-escalation", "--theme", "auto", "--add-dir", extra},
26-
{"--allow-escalation", "--add-dir", extra, "--theme", "auto"},
27-
{"--theme", "auto", "--allow-escalation", "--add-dir", extra},
28-
{"--add-dir", extra, "--allow-escalation", "--theme", "auto"},
29-
{"--add-dir", extra, "--theme", "auto", "--allow-escalation"},
30-
{"--allow-escalation", "--add-dir", extra, "--theme", "auto", "--allow-escalation"},
31-
{"--skip-permissions-unsafe", "--allow-escalation", "--theme", "auto", "--add-dir", extra},
32-
{"--allow-escalation", "--skip-permissions-unsafe", "--add-dir", extra, "--theme", "auto"},
33-
{"--theme", "auto", "--skip-permissions-unsafe", "--allow-escalation", "--add-dir", extra},
34-
{"--add-dir", extra, "--theme", "auto", "--allow-escalation", "--skip-permissions-unsafe"},
35-
} {
46+
groups := [][]string{{"--add-dir", extra}, {"--theme", "auto"}, {"--allow-escalation"}}
47+
cases := rootFlagOrderings(groups)
48+
cases = append(cases, rootFlagOrderings(append(groups, []string{"--skip-permissions-unsafe"}))...)
49+
if len(cases) != 6+24 {
50+
t.Fatalf("SETUP INVALID: %d orderings, want 30", len(cases))
51+
}
52+
for _, args := range cases {
3653
t.Run(strings.Join(args, " "), func(t *testing.T) {
3754
options := captureTUIOptions(t, args...)
3855
wantMode := agent.PermissionModeAsk
@@ -88,3 +105,107 @@ func TestRootThemeLastOccurrenceWinsAcrossOtherFlags(t *testing.T) {
88105
})
89106
}
90107
}
108+
109+
// THE ROOT FLAG REACHES EXEC. A --allow-escalation written before the
110+
// subcommand is forwarded the way --add-dir is, so `zero --allow-escalation -p
111+
// "..."` runs with the opt-in the help text promises instead of silently
112+
// without it. The negative control is the same run without the flag, which
113+
// must not advertise escalate_model.
114+
func TestRootAllowEscalationForwardsIntoExec(t *testing.T) {
115+
for _, testCase := range []struct {
116+
name string
117+
args []string
118+
}{
119+
{"exec subcommand", []string{"--allow-escalation", "exec", "say hi"}},
120+
{"prompt flag", []string{"--allow-escalation", "-p", "say hi"}},
121+
} {
122+
t.Run(testCase.name, func(t *testing.T) {
123+
if !execAdvertisesEscalateModel(t, testCase.args) {
124+
t.Fatalf("%v ran without escalate_model advertised: the root flag was dropped before exec", testCase.args)
125+
}
126+
if execAdvertisesEscalateModel(t, testCase.args[1:]) {
127+
t.Fatalf("%v advertised escalate_model without the flag", testCase.args[1:])
128+
}
129+
})
130+
}
131+
}
132+
133+
// Everywhere else the flag is rejected loudly rather than discarded, the way
134+
// --add-dir is: help and version run no agent and could only ignore it.
135+
func TestRootAllowEscalationIsRejectedWhereNothingConsumesIt(t *testing.T) {
136+
for _, args := range [][]string{
137+
{"--allow-escalation", "version"},
138+
{"--allow-escalation", "help"},
139+
{"--allow-escalation", "completions", "bash"},
140+
} {
141+
t.Run(strings.Join(args, " "), func(t *testing.T) {
142+
var stdout, stderr bytes.Buffer
143+
launched := false
144+
exitCode := runWithDeps(args, &stdout, &stderr, appDeps{
145+
getwd: func() (string, error) { return t.TempDir(), nil },
146+
runTUI: func(context.Context, tui.Options) int { launched = true; return exitSuccess },
147+
})
148+
if exitCode == exitSuccess || launched {
149+
t.Fatalf("exit = %d launched = %v: the flag was discarded instead of rejected", exitCode, launched)
150+
}
151+
if !strings.Contains(stderr.String(), "--allow-escalation is only supported for the interactive TUI and exec") {
152+
t.Fatalf("stderr does not name the flag: %s", stderr.String())
153+
}
154+
})
155+
}
156+
}
157+
158+
// execAdvertisesEscalateModel dispatches args through the root command with a
159+
// provider that records the tools advertised on the first request, and reports
160+
// whether escalate_model was among them.
161+
func execAdvertisesEscalateModel(t *testing.T, args []string) bool {
162+
t.Helper()
163+
t.Setenv("XDG_DATA_HOME", t.TempDir())
164+
provider := &toolListingProvider{}
165+
var stdout, stderr bytes.Buffer
166+
exitCode := runWithDeps(args, &stdout, &stderr, appDeps{
167+
getwd: func() (string, error) { return t.TempDir(), nil },
168+
resolveConfig: func(string, config.Overrides) (config.ResolvedConfig, error) {
169+
return execResolvedConfig(), nil
170+
},
171+
newProvider: func(config.ProviderProfile) (zeroruntime.Provider, error) {
172+
return provider, nil
173+
},
174+
newSandboxStore: func() (*sandbox.GrantStore, error) {
175+
return sandbox.NewGrantStore(sandbox.StoreOptions{FilePath: filepath.Join(t.TempDir(), "sandbox-grants.json")})
176+
},
177+
})
178+
if exitCode != exitSuccess {
179+
t.Fatalf("%v: exit = %d, stderr = %s", args, exitCode, stderr.String())
180+
}
181+
if provider.requests == 0 {
182+
t.Fatalf("SETUP INVALID: %v never reached the provider", args)
183+
}
184+
for _, name := range provider.toolNames {
185+
if name == "escalate_model" {
186+
return true
187+
}
188+
}
189+
return false
190+
}
191+
192+
// toolListingProvider answers every request with a one-line text reply and
193+
// keeps the tool names advertised on the first one.
194+
type toolListingProvider struct {
195+
requests int
196+
toolNames []string
197+
}
198+
199+
func (provider *toolListingProvider) StreamCompletion(_ context.Context, request zeroruntime.CompletionRequest) (<-chan zeroruntime.StreamEvent, error) {
200+
provider.requests++
201+
if provider.requests == 1 {
202+
for _, tool := range request.Tools {
203+
provider.toolNames = append(provider.toolNames, tool.Name)
204+
}
205+
}
206+
ch := make(chan zeroruntime.StreamEvent, 2)
207+
ch <- zeroruntime.StreamEvent{Type: zeroruntime.StreamEventText, Content: "hi"}
208+
ch <- zeroruntime.StreamEvent{Type: zeroruntime.StreamEventDone}
209+
close(ch)
210+
return ch, nil
211+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.