Skip to content

Commit 1bac102

Browse files
yetvalsteipete
andauthored
fix(exec): auto-approve recognized read-only boolean flags on default safe bins (#88953)
* fix(exec): auto-approve recognized read-only boolean flags on default safe bins Default safe bins (cut, head, tail, tr, uniq, wc) auto-approve stdin-only text-filter invocations, but the short-option validator only had an accept path for value-consuming flags: a cluster of pure boolean short flags fell through to a terminal reject, so common read-only forms like 'wc -l', 'tr -d', 'uniq -c' and 'sort -n' were force-routed to manual approval even though the bins are stdin-only and the dangerous flags are already denied. Add an allowedBooleanFlags allowlist to the safe-bin profile model and populate it for the default bins with their read-only boolean flags. The short-cluster validator now accepts recognized boolean flags and the long validator reuses its existing boolean-flag accept branch (previously unreachable). Unrecognized short flags (e.g. 'tr -S') stay fail-closed, and denied flags are still rejected first. * fix(exec): keep safe-bin allowedBooleanFlags off the config-facing fixture type The boolean-flag allowlist for default safe bins leaked onto SafeBinProfileFixture, the type used for tools.exec.safeBinProfiles, while the strict zod schema and the config normalizer never accepted or preserved the key. Move allowedBooleanFlags to an internal BuiltinSafeBinProfileFixture used only for the curated built-in profiles; custom config profiles keep the allowedValueFlags/deniedFlags model. Adds tests proving built-in profiles still honor the boolean allowlist and that a custom profile cannot widen it. * fix(exec): keep tail follow approval-gated * test(exec): stabilize safe-bin trust fixtures * test(exec): isolate safe-bin argv fixtures --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 4bc3008 commit 1bac102

6 files changed

Lines changed: 221 additions & 13 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Docs: https://docs.openclaw.ai
2727
### Fixes
2828

2929
- **Chutes OAuth deadlines:** bound token exchange, profile lookup, and refresh requests, and keep issued tokens when optional userinfo enrichment stalls. (#102026) Thanks @Alix-007.
30+
- **Exec safe-bin flags:** auto-approve curated read-only boolean flags for default stdin-only filters while keeping unknown flags, tail follow/retry modes, file operands, and custom profiles fail-closed. (#88953) Thanks @yetval.
3031
- **Model pin hot reload and fallback:** keep explicit `/model` selections authoritative across Telegram config reloads and model fallback, capture one live config snapshot per assembled turn, and leave fallback candidates turn-local instead of persisting them over the user's pin. (#103324, #103417) Thanks @obviyus.
3132
- **Swift protocol initializers:** default every schema-optional generated initializer parameter to `nil` so additive protocol fields no longer break SDK construction call sites.
3233
- **OpenCode Go MiMo catalog:** stop exposing the deprecated `mimo-v2-omni` and `mimo-v2-pro` aliases that reject agent requests, and keep release validation on the active MiMo V2.5 routes. (#103311, #103329) Thanks @krissding.

‎docs/tools/exec-approvals-advanced.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ Validation is deterministic from argv shape only (no host filesystem existence
4646
checks), which prevents file-existence oracle behavior from allow/deny
4747
differences. File-oriented options are denied for default safe bins; long
4848
options validate fail-closed (unknown flags and ambiguous abbreviations are
49-
rejected).
49+
rejected). Recognized read-only boolean flags of the default bins (for example
50+
`wc -l`, `tr -d`, `uniq -c`) are accepted, while unrecognized short flags stay
51+
fail-closed and fall through to manual approval.
5052

5153
Denied flags by safe-bin profile:
5254

@@ -55,6 +57,7 @@ Denied flags by safe-bin profile:
5557
- `grep`: `--dereference-recursive`, `--directories`, `--exclude-from`, `--file`, `--recursive`, `-R`, `-d`, `-f`, `-r`
5658
- `jq`: `--argfile`, `--from-file`, `--library-path`, `--rawfile`, `--slurpfile`, `-L`, `-f`
5759
- `sort`: `--compress-program`, `--files0-from`, `--output`, `--random-source`, `--temporary-directory`, `-T`, `-o`
60+
- `tail`: `--follow`, `--retry`, `-F`, `-f`
5861
- `wc`: `--files0-from`
5962

6063
[//]: # "SAFE_BIN_DENIED_FLAGS:END"

‎src/infra/exec-approvals-safe-bins.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe("exec approvals safe bins", () => {
3434
rawExecutable?: string;
3535
cwd?: string;
3636
setup?: (cwd: string) => void;
37+
trusted?: boolean;
3738
};
3839

3940
function buildDeniedFlagVariantCases(params: {
@@ -279,6 +280,7 @@ describe("exec approvals safe bins", () => {
279280
resolvedPath: "/tmp/evil-bin/jq",
280281
expected: false,
281282
cwd: "/tmp",
283+
trusted: false,
282284
},
283285
...deniedFlagCases,
284286
{
@@ -321,6 +323,70 @@ describe("exec approvals safe bins", () => {
321323
safeBins: ["tr"],
322324
executableName: "tr",
323325
},
326+
{
327+
name: "keeps tail -fn 1 follow mode approval-gated",
328+
argv: ["tail", "-fn", "1"],
329+
resolvedPath: "/usr/bin/tail",
330+
expected: false,
331+
safeBins: ["tail"],
332+
executableName: "tail",
333+
},
334+
{
335+
name: "auto-allows cut only-delimited mode with a field selector",
336+
argv: ["cut", "-s", "-f", "1"],
337+
resolvedPath: "/usr/bin/cut",
338+
expected: true,
339+
safeBins: ["cut"],
340+
executableName: "cut",
341+
},
342+
{
343+
name: "auto-allows head quiet mode",
344+
argv: ["head", "-q"],
345+
resolvedPath: "/usr/bin/head",
346+
expected: true,
347+
safeBins: ["head"],
348+
executableName: "head",
349+
},
350+
{
351+
name: "auto-allows tail quiet mode",
352+
argv: ["tail", "-q"],
353+
resolvedPath: "/usr/bin/tail",
354+
expected: true,
355+
safeBins: ["tail"],
356+
executableName: "tail",
357+
},
358+
{
359+
name: "auto-allows wc line count via boolean flag",
360+
argv: ["wc", "-l"],
361+
resolvedPath: "/usr/bin/wc",
362+
expected: true,
363+
safeBins: ["wc"],
364+
executableName: "wc",
365+
},
366+
{
367+
name: "auto-allows wc word count via boolean long flag",
368+
argv: ["wc", "--words"],
369+
resolvedPath: "/usr/bin/wc",
370+
expected: true,
371+
safeBins: ["wc"],
372+
executableName: "wc",
373+
},
374+
{
375+
name: "auto-allows uniq count via boolean flag",
376+
argv: ["uniq", "-c"],
377+
resolvedPath: "/usr/bin/uniq",
378+
expected: true,
379+
safeBins: ["uniq"],
380+
executableName: "uniq",
381+
},
382+
{
383+
name: "auto-allows tr delete via boolean flag",
384+
argv: ["tr", "-d", "abc"],
385+
resolvedPath: "/usr/bin/tr",
386+
expected: true,
387+
safeBins: ["tr"],
388+
executableName: "tr",
389+
},
324390
];
325391

326392
it.runIf(process.platform !== "win32").each(cases)("$name", (testCase) => {
@@ -337,6 +403,8 @@ describe("exec approvals safe bins", () => {
337403
},
338404
safeBins: normalizeSafeBins(testCase.safeBins ?? [executableName]),
339405
safeBinProfiles: testCase.safeBinProfiles,
406+
// This table isolates argv policy. Dedicated cases below exercise real path trust.
407+
isTrustedSafeBinPathFn: () => testCase.trusted ?? true,
340408
});
341409
expect(ok).toBe(testCase.expected);
342410
});

‎src/infra/exec-safe-bin-policy-profiles.ts‎

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type SafeBinProfile = {
66
minPositional?: number;
77
maxPositional?: number;
88
allowedValueFlags?: ReadonlySet<string>;
9+
allowedBooleanFlags?: ReadonlySet<string>;
910
deniedFlags?: ReadonlySet<string>;
1011
// Precomputed long-option metadata for GNU abbreviation resolution.
1112
knownLongFlags?: readonly string[];
@@ -22,6 +23,10 @@ export type SafeBinProfileFixture = {
2223

2324
export type SafeBinProfileFixtures = Readonly<Record<string, SafeBinProfileFixture>>;
2425

26+
type BuiltinSafeBinProfileFixture = SafeBinProfileFixture & {
27+
allowedBooleanFlags?: readonly string[];
28+
};
29+
2530
const NO_FLAGS: ReadonlySet<string> = new Set();
2631

2732
export const DEFAULT_SAFE_BINS = ["cut", "uniq", "head", "tail", "tr", "wc"] as const;
@@ -36,13 +41,19 @@ const toFlagSet = (flags?: readonly string[]): ReadonlySet<string> => {
3641
export function collectKnownLongFlags(
3742
allowedValueFlags: ReadonlySet<string>,
3843
deniedFlags: ReadonlySet<string>,
44+
allowedBooleanFlags: ReadonlySet<string> = NO_FLAGS,
3945
): string[] {
4046
const known = new Set<string>();
4147
for (const flag of allowedValueFlags) {
4248
if (flag.startsWith("--")) {
4349
known.add(flag);
4450
}
4551
}
52+
for (const flag of allowedBooleanFlags) {
53+
if (flag.startsWith("--")) {
54+
known.add(flag);
55+
}
56+
}
4657
for (const flag of deniedFlags) {
4758
if (flag.startsWith("--")) {
4859
known.add(flag);
@@ -74,14 +85,16 @@ export function buildLongFlagPrefixMap(
7485
return prefixMap;
7586
}
7687

77-
function compileSafeBinProfile(fixture: SafeBinProfileFixture): SafeBinProfile {
88+
function compileSafeBinProfile(fixture: BuiltinSafeBinProfileFixture): SafeBinProfile {
7889
const allowedValueFlags = toFlagSet(fixture.allowedValueFlags);
90+
const allowedBooleanFlags = toFlagSet(fixture.allowedBooleanFlags);
7991
const deniedFlags = toFlagSet(fixture.deniedFlags);
80-
const knownLongFlags = collectKnownLongFlags(allowedValueFlags, deniedFlags);
92+
const knownLongFlags = collectKnownLongFlags(allowedValueFlags, deniedFlags, allowedBooleanFlags);
8193
return {
8294
minPositional: fixture.minPositional,
8395
maxPositional: fixture.maxPositional,
8496
allowedValueFlags,
97+
allowedBooleanFlags,
8598
deniedFlags,
8699
knownLongFlags,
87100
knownLongFlagsSet: new Set(knownLongFlags),
@@ -90,14 +103,14 @@ function compileSafeBinProfile(fixture: SafeBinProfileFixture): SafeBinProfile {
90103
}
91104

92105
function compileSafeBinProfiles(
93-
fixtures: Record<string, SafeBinProfileFixture>,
106+
fixtures: Record<string, BuiltinSafeBinProfileFixture>,
94107
): Record<string, SafeBinProfile> {
95108
return Object.fromEntries(
96109
Object.entries(fixtures).map(([name, fixture]) => [name, compileSafeBinProfile(fixture)]),
97110
) as Record<string, SafeBinProfile>;
98111
}
99112

100-
export const SAFE_BIN_PROFILE_FIXTURES: Record<string, SafeBinProfileFixture> = {
113+
export const SAFE_BIN_PROFILE_FIXTURES: Record<string, BuiltinSafeBinProfileFixture> = {
101114
jq: {
102115
maxPositional: 1,
103116
allowedValueFlags: ["--arg", "--argjson", "--argstr"],
@@ -159,6 +172,14 @@ export const SAFE_BIN_PROFILE_FIXTURES: Record<string, SafeBinProfileFixture> =
159172
"-f",
160173
"-d",
161174
],
175+
allowedBooleanFlags: [
176+
"--complement",
177+
"--only-delimited",
178+
"--zero-terminated",
179+
"-n",
180+
"-s",
181+
"-z",
182+
],
162183
},
163184
sort: {
164185
maxPositional: 0,
@@ -195,10 +216,31 @@ export const SAFE_BIN_PROFILE_FIXTURES: Record<string, SafeBinProfileFixture> =
195216
"-s",
196217
"-w",
197218
],
219+
allowedBooleanFlags: [
220+
"--count",
221+
"--repeated",
222+
"--unique",
223+
"--ignore-case",
224+
"--zero-terminated",
225+
"-c",
226+
"-d",
227+
"-u",
228+
"-i",
229+
"-z",
230+
],
198231
},
199232
head: {
200233
maxPositional: 0,
201234
allowedValueFlags: ["--lines", "--bytes", "-n", "-c"],
235+
allowedBooleanFlags: [
236+
"--quiet",
237+
"--silent",
238+
"--verbose",
239+
"--zero-terminated",
240+
"-q",
241+
"-v",
242+
"-z",
243+
],
202244
},
203245
tail: {
204246
maxPositional: 0,
@@ -211,13 +253,47 @@ export const SAFE_BIN_PROFILE_FIXTURES: Record<string, SafeBinProfileFixture> =
211253
"-n",
212254
"-c",
213255
],
256+
allowedBooleanFlags: [
257+
"--quiet",
258+
"--silent",
259+
"--verbose",
260+
"--zero-terminated",
261+
"-q",
262+
"-v",
263+
"-z",
264+
],
265+
// Follow/retry modes are unbounded and do not belong in auto-approved safe-bin use.
266+
deniedFlags: ["--follow", "--retry", "-F", "-f"],
214267
},
215268
tr: {
216269
minPositional: 1,
217270
maxPositional: 2,
271+
allowedBooleanFlags: [
272+
"--complement",
273+
"--delete",
274+
"--squeeze-repeats",
275+
"--truncate-set1",
276+
"-C",
277+
"-c",
278+
"-d",
279+
"-s",
280+
"-t",
281+
],
218282
},
219283
wc: {
220284
maxPositional: 0,
285+
allowedBooleanFlags: [
286+
"--bytes",
287+
"--chars",
288+
"--lines",
289+
"--max-line-length",
290+
"--words",
291+
"-L",
292+
"-c",
293+
"-l",
294+
"-m",
295+
"-w",
296+
],
221297
deniedFlags: ["--files0-from"],
222298
},
223299
};

‎src/infra/exec-safe-bin-policy-validator.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,27 @@ function consumeShortOptionClusterToken(params: {
101101
cluster: string;
102102
flags: string[];
103103
allowedValueFlags: ReadonlySet<string>;
104+
allowedBooleanFlags: ReadonlySet<string>;
104105
deniedFlags: ReadonlySet<string>;
105106
}): number {
106107
for (let j = 0; j < params.flags.length; j += 1) {
107108
const flag = params.flags[j];
108109
if (params.deniedFlags.has(flag)) {
109110
return -1;
110111
}
111-
if (!params.allowedValueFlags.has(flag)) {
112-
continue;
112+
if (params.allowedValueFlags.has(flag)) {
113+
const inlineValue = params.cluster.slice(j + 1);
114+
if (inlineValue) {
115+
return isSafeLiteralToken(inlineValue) ? params.index + 1 : -1;
116+
}
117+
return isInvalidValueToken(params.args[params.index + 1]) ? -1 : params.index + 2;
113118
}
114-
const inlineValue = params.cluster.slice(j + 1);
115-
if (inlineValue) {
116-
return isSafeLiteralToken(inlineValue) ? params.index + 1 : -1;
119+
if (params.allowedBooleanFlags.has(flag)) {
120+
continue;
117121
}
118-
return isInvalidValueToken(params.args[params.index + 1]) ? -1 : params.index + 2;
122+
return -1;
119123
}
120-
return -1;
124+
return params.index + 1;
121125
}
122126

123127
function consumePositionalToken(token: string, positional: string[]): boolean {
@@ -141,9 +145,11 @@ function validatePositionalCount(positional: string[], profile: SafeBinProfile):
141145

142146
function collectPositionalTokens(args: string[], profile: SafeBinProfile): string[] | null {
143147
const allowedValueFlags = profile.allowedValueFlags ?? NO_FLAGS;
148+
const allowedBooleanFlags = profile.allowedBooleanFlags ?? NO_FLAGS;
144149
const deniedFlags = profile.deniedFlags ?? NO_FLAGS;
145150
const knownLongFlags =
146-
profile.knownLongFlags ?? collectKnownLongFlags(allowedValueFlags, deniedFlags);
151+
profile.knownLongFlags ??
152+
collectKnownLongFlags(allowedValueFlags, deniedFlags, allowedBooleanFlags);
147153
const knownLongFlagsSet = profile.knownLongFlagsSet ?? new Set(knownLongFlags);
148154
const longFlagPrefixMap = profile.longFlagPrefixMap ?? buildLongFlagPrefixMap(knownLongFlags);
149155

@@ -203,6 +209,7 @@ function collectPositionalTokens(args: string[], profile: SafeBinProfile): strin
203209
cluster: token.cluster,
204210
flags: token.flags,
205211
allowedValueFlags,
212+
allowedBooleanFlags,
206213
deniedFlags,
207214
});
208215
if (nextIndex < 0) {

0 commit comments

Comments
 (0)