The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
tech(M2): Segmentierung mit Anfuehrungszeichen — alle drei Fehlalarme weg
David8b4792df3fec
diff --git a/src/lib/__tests__/shell-command-classify.test.ts b/src/lib/__tests__/shell-command-classify.test.tsindex 10245c34..b5d1429d 100644--- a/src/lib/__tests__/shell-command-classify.test.ts+++ b/src/lib/__tests__/shell-command-classify.test.ts@@ -206,10 +206,11 @@ describe('the --no-verify ban applies to a commit anywhere in the line', () => {expect(refused('git commit --no-verify -m x > /tmp/out')).toBe(true)})- it('refuses even when a quoted separator splits the flag off the commit', () => {- // Der Split ist anfuehrungszeichen-blind: `;` in der Nachricht trennt das- // Flag vom Commit. Deshalb wird --no-verify weiter gegen die GANZE Zeile- // geprueft, nicht je Segment.+ it('refuses when a separator inside the commit message looks like a chain', () => {+ // Die Umgehung, wegen der --no-verify frueher gegen die ganze Zeile+ // geprueft werden musste: `;` in der Nachricht trennte das Flag vom+ // Commit. Der Split zaehlt jetzt Anfuehrungszeichen, also ist das EIN+ // Segment und die Pruefung darf segmentlokal bleiben.expect(refused('git commit -m "a;b" --no-verify')).toBe(true)})@@ -276,24 +277,118 @@ describe('the ban survives git\'s own global options in front of `commit`', () =// faellt jede Zeile mit, die irgendwo das Flag traegt UND irgendwo ein// Commit-Segment hat. Das ist die gewollte Richtung — lieber eine Zeile zu// viel ablehnen als eine Umgehung offen lassen.-describe('accepted false alarms of the whole-line flag test', () => {+// Der Split zaehlt jetzt Anfuehrungszeichen, also darf das Flag SEGMENTLOKAL+// geprueft werden. Damit verschwinden die drei Fehlalarmklassen, die die+// Ganzzeilen-Pruefung erzeugt hat — sie war nur noetig, WEIL der Split blind+// war. Diese beiden Erwartungen standen vorher auf `true` und kippen hier+// bewusst; genau dafuer waren sie festgenagelt.+describe('the false alarms the whole-line flag test used to produce are gone', () => {+ it('an ordinary commit chained with a push that skips the PUSH hook runs', () => {+ // --no-verify gehoert hier zu `git push`. Die Sperre gilt dem Commit-Hook.+ expect(rejectShellCommand('git commit -m x && git push --no-verify')).toBeNull()+ expect(rejectShellCommand('git add -A && git commit -m wip && git push --no-verify')).toBeNull()+ })++ it('an ordinary commit chained with cargo\'s verify build runs', () => {+ expect(rejectShellCommand('git commit -m release && cargo publish --no-verify')).toBeNull()+ })++ it('a commit whose line merely MENTIONS the flag in another command runs', () => {+ expect(rejectShellCommand('echo --no-verify && git commit -m x')).toBeNull()+ })++ it('a quoted chain that only LOOKS like a commit runs', () => {+ // Ein Kommando (echo). Der blinde Split hat daraus zwei gemacht und das+ // zweite als Commit gelesen.+ expect(rejectShellCommand('echo "a && git commit" --no-verify')).toBeNull()+ })+})++// Was der Split kann und was nicht. Die Sicherheitsbedingung: ein falscher+// Split darf das Flag NIEMALS von seinem Commit trennen. Zusammenfassen ist+// harmlos (lehnt mehr ab), Auftrennen ist die Umgehung.+describe('the split counts quotes', () => {const refused = (c: string) => rejectShellCommand(c) !== null- it('refuses a commit whose line merely MENTIONS the flag elsewhere', () => {- // Der Commit selbst ist gewoehnlich; das Flag steht im echo. Ablehnen ist- // richtig: unterscheiden koennte man die beiden nur per Segment, und die- // Segmentpruefung ist genau die Umgehung (`git commit -m "a;b"- // --no-verify`), die oben geschlossen wurde.- expect(refused('echo --no-verify && git commit -m x')).toBe(true)- })-- it('refuses an ordinary commit chained with a push that skips the PUSH hook', () => {- // Bekannter Fehlalarm, im Bericht als offene Frage vermerkt: hier gehoert- // --no-verify zu `git push`, nicht zum Commit, und der Nutzer bekommt eine- // Meldung ueber `git commit --no-verify`, die nicht zu seiner Zeile passt.- // Faellt weg, sobald die Sperre segmentlokal entscheidet — dann kippt diese- // Erwartung bewusst, statt still.- expect(refused('git add -A && git commit -m wip && git push --no-verify')).toBe(true)+ it('a separator inside DOUBLE quotes does not split the command', () => {+ expect(refused('git commit -m "a;b" --no-verify')).toBe(true)+ expect(refused('git commit -m "a && b" --no-verify')).toBe(true)+ expect(refused('git commit -m "a | b" --no-verify')).toBe(true)+ })++ it('a separator inside SINGLE quotes does not split the command', () => {+ expect(refused("git commit -m 'a && b' --no-verify")).toBe(true)+ expect(refused("git commit -m 'a;b' --no-verify")).toBe(true)+ })++ it('a quote inside the other kind of quote is text, not a delimiter', () => {+ expect(refused('git commit -m "it\'s fine; really" --no-verify')).toBe(true)+ expect(refused('git commit -m \'say "hi"; ok\' --no-verify')).toBe(true)+ })++ it('an escaped quote does not open a string', () => {+ expect(refused('git commit -m "a \\" b; c" --no-verify')).toBe(true)+ expect(refused('git commit -m \\" --no-verify')).toBe(true)+ })++ it('a backslash-escaped newline keeps the flag with its commit', () => {+ // Zeilenfortsetzung: fuer die Shell EIN Kommando. Zaehlt der Scan den+ // Backslash nicht, wird der Zeilenumbruch zum Trenner und das Flag landet+ // in einem eigenen Segment — die Sperre saehe es nicht mehr.+ expect(refused('git commit -m x \\\n--no-verify')).toBe(true)+ })++ it('a backslash inside single quotes stays literal (POSIX)', () => {+ expect(refused("git commit -m 'a\\' --no-verify")).toBe(true)+ })++ it('still splits at separators OUTSIDE quotes', () => {+ expect(refused('echo "hello world" && git commit --no-verify -m x')).toBe(true)+ expect(rejectShellCommand('echo "hello world" && git commit -m x')).toBeNull()+ })+})++// Im Zweifel ablehnen. Bei unbalancierten Anfuehrungszeichen ist nicht mehr+// bestimmbar, wo ein Kommando endet — dann wird nicht geraten, sondern auf die+// alte Ganzzeilen-Pruefung zurueckgefallen.+describe('unbalanced quotes fall back to the whole line', () => {+ const refused = (c: string) => rejectShellCommand(c) !== null++ it('refuses a commit hiding behind an unterminated string', () => {+ // Ohne den Rueckfall liefe die ganze Restzeile als EIN Segment, das mit+ // `echo` beginnt — der Commit waere unsichtbar und die Zeile liefe durch.+ expect(refused("echo 'unclosed && git commit --no-verify")).toBe(true)+ expect(refused('echo "unclosed && git commit --no-verify')).toBe(true)+ })++ it('refuses an unterminated commit line that carries the flag', () => {+ expect(refused('git commit -m "a && b --no-verify')).toBe(true)+ })++ it('does not invent a refusal for an unterminated line without the flag', () => {+ expect(rejectShellCommand('echo "unclosed && git commit -m x')).toBeNull()+ })+})++// Der EINE Fehlalarm, der bleibt, und warum: das Flag steht in einem+// Commit-Segment. Ohne echtes Argument-Parsing — also ohne zu wissen, dass+// `-m` sein naechstes Wort als Nachricht frisst — ist die Erwaehnung in der+// Nachricht nicht vom Flag zu unterscheiden. Bewusster Rest.+describe('the one false alarm that stays', () => {+ it('refuses a commit whose MESSAGE mentions the flag', () => {+ expect(rejectShellCommand('git commit -m "nie wieder --no-verify"')).not.toBeNull()+ })+})++// Und das eine Loch, das offen bleibt: ein Wort VOR `git`. Der Segmenttest+// verlangt, dass das Segment mit `git` beginnt; es zu weiten hiesse Wrapper zu+// erraten (sudo, env, nice, time, doas, …), und jede Rateliste ist entweder+// unvollstaendig oder faengt Fremdes. Hier festgehalten, damit es sichtbar ist+// und nicht still: wer es schliesst, kippt diese Erwartungen bewusst.+describe('known hole: a word in front of `git`', () => {+ it('does not see a commit behind a wrapper or an env prefix', () => {+ expect(rejectShellCommand('sudo git commit --no-verify')).toBeNull()+ expect(rejectShellCommand('GIT_DIR=x git commit --no-verify')).toBeNull()})})diff --git a/src/lib/shell-command-classify.ts b/src/lib/shell-command-classify.tsindex 34302209..5c967a02 100644--- a/src/lib/shell-command-classify.ts+++ b/src/lib/shell-command-classify.ts@@ -132,19 +132,128 @@ export function isReadOnlyCommand(command: string): boolean {}/**- * Every command in a chained line.+ * The separator characters {@link CHAINING} names, as single characters:+ * command separators, background, pipes, backtick substitution, newlines and+ * every output redirection. `$(` and `<(` are two characters and handled+ * beside this.+ */+const SEPARATOR_CHARS = ';&|`\n\r>'++/**+ * Every command in a chained line, counting quotes.+ *+ * THE RULE THIS FUNCTION EXISTS FOR — and the reason it is a scan and not a+ * regex: a wrong split must never separate `--no-verify` from the commit it+ * belongs to. MERGING two commands into one segment is harmless; the ban then+ * tests a wider string and can only refuse MORE. SPLITTING one command into+ * two is what lets the flag land in a different segment than its commit, and+ * that is a bypass. So this errs towards merging wherever it is unsure, and+ * when the quotes do not balance it gives up (`null`) rather than guess —+ * the caller then falls back to testing the whole line, which is stricter+ * than any split could be.+ *+ * The separators are the ones {@link CHAINING} already names, so this agrees+ * with `isReadOnlyCommand` on what a separator IS — but only outside a quoted+ * string. That is the entire difference from the blind split this replaces:+ * `git commit -m "a;b" --no-verify` is ONE command, so the flag stays with+ * its commit and the ban no longer has to test the whole line to see it.+ *+ * A backslash escapes the next character outside quotes and inside double+ * quotes (inside single quotes POSIX says it is literal). That is not+ * decoration: a line continuation — `git commit -m x \` + newline ++ * `--no-verify` — is ONE command to the shell, and treating that newline as a+ * separator would put the flag in a segment of its own.+ *+ * Deliberately NOT a shell tokenizer. It knows quotes and backslashes and+ * nothing else: no word splitting, no expansion, no operator precedence.+ */+function quoteAwareSegments(command: string): string[] | null {+ const segments: string[] = []+ let current = ''+ let quote: '"' | "'" | null = null++ for (let i = 0; i < command.length; i++) {+ const ch = command[i]++ // Escape. Inside single quotes a backslash is a literal character, so the+ // escape only applies outside quotes and inside double quotes.+ if (ch === '\\' && quote !== "'" && i + 1 < command.length) {+ current += ch + command[i + 1]+ i+++ continue+ }++ if (quote !== null) {+ current += ch+ if (ch === quote) quote = null+ continue+ }++ if (ch === '"' || ch === "'") {+ quote = ch+ current += ch+ continue+ }++ if (SEPARATOR_CHARS.includes(ch)) {+ segments.push(current)+ current = ''+ continue+ }+ // `$(` and `<(` open a command of their own — a commit hides in there as+ // well as after an `&&`.+ if ((ch === '$' || ch === '<') && command[i + 1] === '(') {+ segments.push(current)+ current = ''+ i+++ continue+ }++ current += ch+ }++ // An opened string that never closed. We cannot say where one command ends,+ // so we say nothing.+ if (quote !== null) return null++ segments.push(current)+ return segments.map((seg) => seg.trim()).filter((seg) => seg.length > 0)+}++/**+ * The blind split — every separator, quotes and all. Only used when+ * {@link quoteAwareSegments} gives up, where over-splitting is the safe+ * direction because the flag is then tested against the whole line anyway.+ */+function blindSegments(command: string): string[] {+ return command.split(CHAINING).map((seg) => seg.trim()).filter((seg) => seg.length > 0)+}++/**+ * Does this segment invoke `git commit`?*- * Split on the SAME {@link CHAINING} pattern `isReadOnlyCommand` refuses on,- * so both functions agree on what a separator is: `;` `&` `|`, a newline,- * every output redirection, and the two substitution openers. The pattern has- * no capture groups, so `split` drops the separators and hands back only the- * commands between them — including the BODY of a `$(…)` or a backtick pair,- * which is a place a commit hides just as well as after an `&&`.+ * It starts with `git` and mentions `commit` as a word — not `^git\s+commit`,+ * which git's own global options walk straight past (`git -C /repo commit`,+ * `git -c a=b commit`, `git --git-dir=… commit`). Skipping those options+ * properly would mean parsing git's option grammar AND shell quoting+ * (`git -c 'a b' commit` defeats any token walk), and an option-skipper that+ * is wrong in the permissive direction is a new bypass rather than a fix.+ * This test needs to know none of it: it is deliberately too coarse, in the+ * safe direction. What it costs is a segment that merely SAYS commit while+ * carrying the flag — `git log --grep=commit --no-verify`, which nobody+ * writes, since `--no-verify` is no `log` flag.*/-function commandSegments(command: string): string[] {- return command.split(CHAINING).map((s) => s.trim()).filter((s) => s.length > 0)+function invokesGitCommit(segment: string): boolean {+ return /^git\b/.test(segment) && /\bcommit\b/.test(segment)+}++function carriesNoVerify(text: string): boolean {+ return /--no-verify\b/.test(text)}+const NO_VERIFY_REFUSAL =+ 'Refused: git commit --no-verify skips the repository hooks. Fix what the hook reports instead of silencing it, then commit normally.'+/*** The one refusal that stays hard: --no-verify on a commit. The old* git_commit tool could not emit it (buildGitCommitCommand never did); with@@ -161,35 +270,32 @@ function commandSegments(command: string): string[] {* case; `commandKind` is left alone because `commandTimeoutMs` and* `commandIcon` read it too.*- * `--no-verify` is deliberately still matched against the WHOLE line rather- * than per segment: the split is quote-blind, so a `;` inside a commit- * message would otherwise put the flag in a different segment from the commit- * (`git commit -m "a;b" --no-verify`) and hand back the very bypass this is- * closing. Testing the wider string can only ever refuse more, never less.+ * The flag is tested PER SEGMENT, which it could not be while the split was+ * quote-blind: a `;` inside a commit message put the flag in a different+ * segment than its commit, so the ban had to test the whole line to still see+ * it — and then refused three shapes that were never the point:+ *+ * git commit -m x && git push --no-verify the PUSH hook is meant+ * git commit -m rel && cargo publish --no-verify cargo's verify build+ * echo --no-verify && git commit -m x the flag is in the echo*- * A segment counts as a commit when it STARTS with `git` and mentions- * `commit` as a word — not `^git\s+commit`, which git's own global options- * walk straight past (`git -C /repo commit`, `git -c a=b commit`,- * `git --git-dir=… commit`). Skipping those options properly would mean- * parsing git's option grammar AND shell quoting (`git -c 'a b' commit`- * defeats any token walk), and an option-skipper that is wrong in the- * permissive direction is a new bypass rather than a fix. This test needs to- * know none of it: it is deliberately too coarse, in the same direction as- * the whole-line flag test. What it costs is a segment that merely SAYS- * commit while the line also carries the flag — `git log --grep=commit- * --no-verify`, which is not a thing anybody writes, since `--no-verify` is- * no `log` flag.+ * A ban that refuses what it does not mean is one people learn to route+ * around, and then they route around it where it was right too. Counting+ * quotes removes the cause, so the test can be as narrow as the rule.** Returns the refusal text, or null when the command may run.*/export function rejectShellCommand(command: string): string | null {- const commits = commandSegments(command).some(- (seg) => /^git\b/.test(seg) && /\bcommit\b/.test(seg),- )- if (commits && /--no-verify\b/.test(command)) {- return 'Refused: git commit --no-verify skips the repository hooks. Fix what the hook reports instead of silencing it, then commit normally.'- }- return null+ const segments = quoteAwareSegments(command)+ const refuse = segments === null+ // Quotes did not balance. We do not guess where one command ends: the+ // line is tested the old way — a commit in ANY (blindly split) segment+ // plus the flag ANYWHERE — which refuses at least as much as any split of+ // it would. In doubt, refuse.+ ? blindSegments(command).some(invokesGitCommit) && carriesNoVerify(command)+ : segments.some((seg) => invokesGitCommit(seg) && carriesNoVerify(seg))++ return refuse ? NO_VERIFY_REFUSAL : null}/** Timeout for the command, in ms. A recognised test run keeps run_tests' cap. */