Open source CapCut alternative.
TypeScript
fix(app): make the shell command classifier quote-aware
David Le51cf12744705
diff --git a/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift b/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swiftindex adf04bf5..101332b9 100644--- a/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift+++ b/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift@@ -74,8 +74,7 @@ public enum ShellCommandClassifier {// overall tier is the most restrictive segment; the signature/reason// come from the first segment that reached that tier.var worst: ShellCommandClassification?- let separators = CharacterSet(charactersIn: ";|&\n`(")- for rawSegment in command.components(separatedBy: separators) {+ for rawSegment in splitSegments(command) {let tokens = tokenize(rawSegment)guard !tokens.isEmpty else { continue }let classified = classifySegment(tokens)@@ -187,6 +186,50 @@ public enum ShellCommandClassifier {// MARK: - Tokenization+ /// Split a command into segments on shell separators (`;`, `|`, `&`, newline,+ /// backtick) and subshell/substitution opens (`(`), but ONLY when the separator+ /// is outside single/double quotes. A separator inside quotes is a literal part+ /// of an argument — e.g. the `|` in `grep -iE 'a|b'` or the `(` in a quoted glob+ /// pattern — and must not start a new "command" segment, or a read-only pipeline+ /// would be mis-scored as an unrecognized write and gated needlessly.+ static func splitSegments(_ command: String) -> [String] {+ var segments: [String] = []+ var current = ""+ var inSingle = false+ var inDouble = false+ var escaped = false+ for character in command {+ if escaped {+ current.append(character)+ escaped = false+ continue+ }+ if character == "\\", !inSingle {+ current.append(character)+ escaped = true+ continue+ }+ if character == "'", !inDouble {+ inSingle.toggle()+ current.append(character)+ continue+ }+ if character == "\"", !inSingle {+ inDouble.toggle()+ current.append(character)+ continue+ }+ if !inSingle, !inDouble, ";|&\n`(".contains(character) {+ segments.append(current)+ current = ""+ continue+ }+ current.append(character)+ }+ segments.append(current)+ return segments+ }+/// Split a segment into whitespace-separated tokens. Leading environment/// assignments (e.g. `FOO=bar cmd`) are dropped so the executable is found.private static func tokenize(_ segment: String) -> [String] {diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swiftindex 3065ed65..e963560e 100644--- a/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift@@ -27,6 +27,21 @@ struct ShellCommandClassifierTests {#expect(tier("ls -t ~/Downloads/*.pdf | head -1") == .read)}+ @Test+ func separatorsInsideQuotesDoNotEscalateAReadPipeline() {+ // The `|` inside the grep pattern is a literal regex alternation, not a shell+ // pipe — a read-only `ls | grep | head` must stay read, not be mis-scored as an+ // unrecognized write because `\.jpg$` looked like a command segment.+ #expect(tier("ls -t ~/Downloads | grep -iE '\\.png$|\\.jpg$|\\.heic$' | head -1") == .read)+ #expect(tier("grep -E 'foo|bar|baz' file.txt") == .read)+ // Parentheses inside a quoted argument are literal too.+ #expect(tier("grep -E '(foo|bar)' file.txt") == .read)+ #expect(tier("echo 'a; rm -rf /'") == .read)+ // But a real unquoted pipe into a write still escalates.+ #expect(tier("ls | rm") == .highRisk)+ #expect(tier("cat x && open -a Safari") == .reversibleWrite)+ }+@Testfunc readSubcommandsOfWriteCapableToolsAreRead() {#expect(tier("defaults read com.apple.dock") == .read)