A look-only shell command with an & or a bracket inside quotes no longer asks for approval

The read-only check refused any command containing & ; ( ) { } before it understood quoting, so listing "C:\...\GUI Q&A" showed an
approval card. Quoted text is now set aside first (maskQuotes): an expansion inside double quotes ($ or a backtick), an unterminated
quote, a quote glued to other characters and a quoted text that starts with a dash are still not understood, so those and everything
outside quotes ask as before.
This commit is contained in:
Niko1221 committed 2026-10-22 14:46:52 +02:00
1 parent be58604387
commit a7cd2de97a
2 files changed
+69 -3

No files matched your search

+32 -2
View File
@@ -68,8 +68,37 @@ const SAFE_VERSION = /^(node|python|python3|py|npm|pnpm|git|dotnet|cargo|rustc|g
/** Anything that can chain, redirect, expand or start something else is not read-only. */
const UNSAFE_SYNTAX = /[;&<>`${}()]|\r|\n|--%|\bout-file\b|\bset-content\b|\badd-content\b|\binvoke-|\biex\b|\bstart-|\bnew-|\bremove-|\bcopy-|\bmove-|\brename-|\bclear-|\bset-|\bpush-|\bexpand-|\bsave-|\binstall-|\bcurl\b|\bwget\b|\bsudo\b|\bsh\b|\bbash\b|\bpwsh\b|\bpowershell\b|\bcmd\b|-exec\b|-delete\b|-execdir\b/i;
/**
* The command with every quoted text replaced by a plain stand-in, or null when it cannot be read safely. What is inside quotes is data
* (a path like "C:\Users\x\Q&A", a pattern like 'a|b'), so an & ; | ( ) < > or { in it must not decide anything. Two things still can:
* inside double quotes a $ or a backtick expands (a variable, a sub-command), and an unterminated quote means the text was not understood.
* Either one gives null, and the command asks as before.
* @param {string} text
* @returns {string|null}
*/
export function maskQuotes(text) {
let out = '';
for (let i = 0; i < text.length; i++) {
const c = text[i];
if (c !== '"' && c !== "'") { out += c; continue; }
let end = text.indexOf(c, i + 1);
// PowerShell writes a quote inside a quoted text by doubling it ('it''s'): the text goes on past the pair.
while (end >= 0 && text[end + 1] === c) end = text.indexOf(c, end + 2);
if (end < 0) return null;
if (c === '"' && /[$`]/.test(text.slice(i + 1, end))) return null;
// A quoted text is a whole word. One glued to other characters (--out"put"=f, "a"b) or one that starts with a dash could hide an option from the
// word checks below (git diff "--output=f" writes a file), so those are not understood either.
const before = text[i - 1], after = text[end + 1];
if ((before !== undefined && !/[\s|]/.test(before)) || (after !== undefined && !/[\s|]/.test(after)) || text[i + 1] === '-') return null;
out += '"_"';
i = end;
}
return out;
}
/**
* Whether a shell command only reads. Conservative by design: one unrecognised word and the answer is no.
* Quoted text is data and is looked at after it has been set aside (maskQuotes): Get-ChildItem "C:\Users\x\Q&A" only lists.
* @param {string} command
* @returns {boolean}
*/
@@ -77,8 +106,9 @@ export function isReadOnlyShell(command) {
const text = String(command ?? '').trim();
if (!text || text.length > 600) return false;
if (SAFE_VERSION.test(text)) return true;
if (UNSAFE_SYNTAX.test(text)) return false;
const segments = text.split('|').map(s => s.trim());
const masked = maskQuotes(text);
if (masked === null || UNSAFE_SYNTAX.test(masked)) return false;
const segments = masked.split('|').map(s => s.trim());
return segments.every((segment) => {
if (!segment) return false;
const words = segment.split(/\s+/);
+37 -1
View File
@@ -15,7 +15,7 @@ import { createAgentBridge } from '../server/agent/agent-bridge.mjs';
import { resolveHarness } from '../server/agent/harness-runtime.mjs';
import { buildProfile, GATE_PLUGIN } from '../server/agent/profile.mjs';
import { STANDARD_PLUGIN_IDS, REMOVED_PLUGIN_IDS, standardPreset } from '../server/agent/preset.mjs';
import { isReadOnlyShell, manualDecision, READ_ONLY_TOOLS } from '../server/agent/plugins/strata-gate.mjs';
import { isReadOnlyShell, manualDecision, maskQuotes, READ_ONLY_TOOLS } from '../server/agent/plugins/strata-gate.mjs';
import { plainToolError, withPlain } from '../server/agent/events.mjs';
const HARNESS = process.env.STRATA_HARNESS_DIR;
@@ -35,6 +35,42 @@ test('the gate: only commands that provably just look are allowed without a ques
assert.equal(isReadOnlyShell('x'.repeat(700)), false, 'a long command is never trusted');
});
test('the gate: quoted text is data, so an & ( ) ; { } in a quoted path does not make a look-only command ask (2026-10-22)', () => {
for (const ok of [
'Get-ChildItem "C:\\Users\\AI-Server\\Downloads\\GUI Q&A" | Select-Object -First 5 Name',
'Get-Content "C:\\Users\\AI-Server\\Downloads\\GUI Q&A\\a.txt"',
"Get-ChildItem 'C:\\Users\\x\\Notes (old) {draft}; v2'",
'Select-String -Path a.txt "a|b"',
'Select-String needle "C:\\x\\a & b.txt" | Measure-Object',
'git log --oneline "docs/Q&A.md"',
'cat "my file (1).txt"',
"Get-ChildItem 'it''s here'",
]) assert.equal(isReadOnlyShell(ok), true, ok);
// Quoting does not hide anything that acts: outside the quotes the same characters still ask, and inside double quotes an expansion does.
for (const bad of [
'Get-ChildItem C:\\Q&A',
'Get-ChildItem "C:\\a" ; Remove-Item x',
'Get-ChildItem "C:\\a" & calc',
'Get-ChildItem "C:\\a" > out.txt',
'Get-ChildItem "$env:USERPROFILE"',
'Get-ChildItem "$(whoami)"',
'Get-ChildItem "a`b"',
"Get-ChildItem 'unterminated",
'Get-ChildItem "unterminated',
'git diff "--output=out.txt"',
'git diff --out"put"=out.txt',
'git diff --"output"=out.txt',
'Get-Content "a"b',
'"Remove-Item" x',
'Set-Content "a.txt" "x"',
'Get-ChildItem "a" | Remove-Item',
'curl "http://x"',
]) assert.equal(isReadOnlyShell(bad), false, bad);
assert.equal(maskQuotes('Get-ChildItem "C:\\Q&A" | Select-Object Name'), 'Get-ChildItem "_" | Select-Object Name');
assert.equal(maskQuotes("echo 'a' \"b\""), 'echo "_" "_"');
assert.equal(maskQuotes('echo "$HOME"'), null);
});
test('the gate: every tool outside the read-only list asks, including unknown and connected ones', () => {
for (const name of READ_ONLY_TOOLS) assert.equal(manualDecision({ name, arguments: {} }), null, name);
for (const name of ['write', 'edit', 'job_kill', 'mcp__github__create_issue', 'something_new']) assert.equal(manualDecision({ name, arguments: {} })?.kind, 'ask', name);