Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(security): table-driven side-effect flags in the safe-read classifier

Salman Qureshi806d7355a14d
diff --git a/src/capabilities/permissions.test.ts b/src/capabilities/permissions.test.ts
index ff3e9d8f..7060f9d5 100644
--- a/src/capabilities/permissions.test.ts
+++ b/src/capabilities/permissions.test.ts
@@ -471,3 +471,72 @@ describe('PermissionManager read-side canonicalisation (symlink reads, #104 hard
expect(result.reason).toBe(`Permission denied for read access to ${join(outside, 'id_rsa')}`);
});
});
+
+describe('safe-read classifier: residual side-effect flags (table)', () => {
+ function makePermissions() {
+ const permissions = new PermissionManager();
+ const manifest = permissions.getManifest();
+ manifest.capabilities.shell.enabled = true;
+ manifest.capabilities.shell.blocked = [];
+ const ask = vi.fn().mockResolvedValue('no');
+ permissions.onAsk(ask);
+ permissions.setCurrentContext('web', 'cloud-request-1');
+ return { permissions, ask };
+ }
+
+ const requiresApproval: Array<[string, string]> = [
+ ['find . -fprint0 out.bin', 'find -fprint0 writes a file (the old \\b after -fprint stopped at the digit)'],
+ ['find . -fprint0=out.bin', 'find -fprint0= form'],
+ ['find . -fls out.txt', 'find -fls writes an ls-style listing'],
+ ['find . -type f -fprintf out.txt %p', 'find -fprintf writes a file'],
+ ['tree -o out.txt', 'tree -o writes the listing'],
+ ['tree -ao out.txt', 'tree -o inside a short-flag cluster'],
+ ['tree --output out.txt', 'tree --output'],
+ ['git log --output=hist.txt', 'git log --output= writes a file'],
+ ['git log --output hist.txt', 'git log --output writes a file'],
+ ['git diff --output=d.patch', 'git diff --output= writes a file'],
+ ['git diff --ext-diff', 'git diff --ext-diff runs the configured external diff'],
+ ['git log -p --ext-diff', 'git log --ext-diff runs the configured external diff'],
+ ['git diff --no-index a.txt b.txt', 'git diff --no-index diffs arbitrary paths'],
+ ['rg --pre cat secret', 'rg --pre runs a preprocessor'],
+ ['rg --pre=./leak.sh secret', 'rg --pre= runs a preprocessor'],
+ ['curl -o out.html http://example.com', 'curl -o writes a file'],
+ ['curl -O http://example.com/x.sh', 'curl -O writes the remote name'],
+ ['curl -sSLo out.sh http://example.com', 'curl -o inside a short-flag cluster'],
+ ['curl -sSLJO http://example.com', 'curl -J/-O inside a cluster'],
+ ['curl --output out.html http://example.com', 'curl --output'],
+ ['curl --remote-name http://example.com/x', 'curl --remote-name'],
+ ['curl --output-dir /tmp -O http://example.com/x', 'curl --output-dir'],
+ ['wget -O out.html http://example.com', 'wget -O'],
+ ['wget --output-document=out.html http://example.com', 'wget --output-document='],
+ ['wget -o log.txt http://example.com', 'wget -o writes a log file'],
+ ['wget -P /tmp http://example.com', 'wget -P chooses the download directory'],
+ ['wget -qO- http://example.com', 'wget -O inside a cluster'],
+ ];
+
+ it.each(requiresApproval)('%s → requires approval (%s)', async (command) => {
+ const { permissions, ask } = makePermissions();
+ await expect(permissions.checkShellCommand(command)).resolves.toMatchObject({ allowed: false });
+ expect(ask).toHaveBeenCalledTimes(1);
+ });
+
+ const staysAutoApproved: Array<[string, string]> = [
+ ['find . -name "*.ts" -print0', 'find -print0 only prints'],
+ ['find . -type f -newer README.md', 'plain find'],
+ ['tree -a -L 2', 'tree without an output flag'],
+ ['git log --oneline -n 5', 'plain git log'],
+ ['git log --format=%H', 'git log --format is read-only'],
+ ['git diff --stat', 'plain git diff'],
+ ['git diff --no-ext-diff', '--no-ext-diff disables the external diff'],
+ ['rg --pretty pattern src', 'rg --pretty is not --pre'],
+ ['rg -n pattern src', 'plain rg'],
+ ['ls -o', 'ls has no write flags; -o is a listing format'],
+ ['grep -o pattern file.txt', 'grep -o prints only matches'],
+ ];
+
+ it.each(staysAutoApproved)('%s → auto-approved (%s)', async (command) => {
+ const { permissions, ask } = makePermissions();
+ await expect(permissions.checkShellCommand(command)).resolves.toMatchObject({ allowed: true });
+ expect(ask).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/capabilities/permissions.ts b/src/capabilities/permissions.ts
index 69585a0a..c665dfc0 100644
--- a/src/capabilities/permissions.ts
+++ b/src/capabilities/permissions.ts
@@ -820,14 +820,40 @@ export class PermissionManager {
return { allowed: false, reason: 'Command not in auto-approve list — requires approval', needsApproval: true };
}
+ /**
+ * Per-command flags that turn a "read-only" command into a write or an
+ * exec. Each entry is matched against the segment's command word, then its
+ * flag pattern against the whole segment. Flags are anchored at a token
+ * start and must end at whitespace, `=` or end-of-segment, so `-fprint0`
+ * and `-fls` cannot hide behind a `\b` that stops at the digit (#71 family).
+ */
+ private static readonly SIDE_EFFECT_FLAGS: ReadonlyArray<{ command: RegExp; flags: RegExp }> = [
+ // find: -exec/-execdir/-ok/-okdir run commands; -delete deletes;
+ // -fprint/-fprint0/-fprintf/-fls write attacker-chosen files;
+ // -files0-from dereferences a path list the literal-path gate never saw.
+ { command: /^find(?:\s|$)/, flags: /(?:^|\s)-(?:delete|exec|execdir|ok|okdir|fprintf|fprint0|fprint|fls|files0-from)(?=\s|=|$)/ },
+ // tree -o FILE / --output FILE writes the listing to a file.
+ { command: /^tree(?:\s|$)/, flags: /(?:^|\s)(?:-[A-Za-z]*o[A-Za-z]*|--output)(?=\s|=|$)/ },
+ // curl: -o/-O (also inside a cluster such as -sSLo), --output,
+ // --remote-name(-all), -J/--remote-header-name, --output-dir write files.
+ { command: /^curl(?:\s|$)/, flags: /(?:^|\s)(?:-[A-Za-z]*[oOJ][A-Za-z]*|--output|--output-dir|--remote-name|--remote-name-all|--remote-header-name|--create-dirs)(?=\s|=|$)/ },
+ // wget: -O/--output-document, -o/--output-file, -a/--append-output,
+ // -P/--directory-prefix all choose where it writes.
+ { command: /^wget(?:\s|$)/, flags: /(?:^|\s)(?:-[A-Za-z]*[oOaP][A-Za-z]*|--output-document|--output-file|--append-output|--directory-prefix)(?=\s|=|$)/ },
+ // git log/diff: --output writes the result to a file; --ext-diff runs the
+ // configured external diff program; --no-index diffs arbitrary paths.
+ { command: /^git\s+(?:log|diff|status|branch)(?:\s|$)/, flags: /(?:^|\s)(?:--output|--ext-diff|--no-index)(?=\s|=|$)/ },
+ // rg --pre CMD pipes every file through an arbitrary preprocessor.
+ { command: /^rg(?:\s|$)/, flags: /(?:^|\s)--pre(?=\s|=|$)/ },
+ ];
+
private isSafeReadSegment(segment: string): boolean {
// Redirection turns otherwise read-only commands such as cat/echo into writes.
if (/\d*(?:>{1,2}|<{1,2})|&>/.test(segment)) return false;
- // find's action flags are side-effectful: -exec/-execdir/-ok/-okdir run
- // arbitrary commands, -fprint/-fprintf write files, and -files0-from
- // dereferences a path list at execution time (the literal-path gate never
- // sees those paths).
- if (/^find\b.*(?:^|\s)-(?:delete|exec|execdir|ok|okdir|fprintf|fprint|files0-from)\b/.test(segment)) return false;
+ // Command-specific write/exec flags (find -exec, curl -o, rg --pre, …).
+ for (const rule of PermissionManager.SIDE_EFFECT_FLAGS) {
+ if (rule.command.test(segment) && rule.flags.test(segment)) return false;
+ }
// File-list indirection in any safe-read command (wc/du/sort/…): the
// paths live inside the referenced file, invisible to the literal-path
// gate, so the read can escape the approved scopes.
@@ -844,7 +870,6 @@ export class PermissionManager {
!branchArgs.startsWith('-')
|| /(?:^|\s)(?:-[dDmMcC]\b|--(?:delete|move|copy|edit-description|set-upstream-to|unset-upstream|track)\b)/.test(branchArgs)
)) return false;
- if (/^git\s+diff\b.*(?:--output(?:=|\s)|--no-index\b)/.test(segment)) return false;
return PermissionManager.SAFE_READ_PATTERNS.some((pattern) => this.matchPattern(segment, pattern));
}