Open source CapCut alternative.
TypeScript
fix(runtime): keep quoted paths intact when classifying shell commands
David Le64957b15bcd7
diff --git a/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift b/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swiftindex 654e516f..6711c791 100644--- a/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift+++ b/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift@@ -276,12 +276,60 @@ public enum ShellCommandClassifier {return segments}- /// Split a segment into whitespace-separated tokens. Leading environment- /// assignments (e.g. `FOO=bar cmd`) are dropped so the executable is found.+ /// Split a segment into tokens on unquoted whitespace, honoring single/double+ /// quotes and backslash escapes the same way `splitSegments` does. A quoted span+ /// containing a space stays one token — a plain whitespace split would break a+ /// bundled tool invoked by its real path (`"~/Library/Application Support/Donkey/+ /// donkey-tools/yt-dlp"`) on the space inside "Application Support", mis-parsing+ /// its executable as `application` and gating a first-party tool that must run+ /// silently. Leading environment assignments (`FOO=bar cmd`) are dropped so the+ /// executable is found.private static func tokenize(_ segment: String) -> [String] {- var tokens = segment- .split(whereSeparator: { $0 == " " || $0 == "\t" })- .map(String.init)+ var tokens: [String] = []+ var current = ""+ var hasToken = false+ var inSingle = false+ var inDouble = false+ var escaped = false+ for character in segment {+ if escaped {+ current.append(character)+ hasToken = true+ escaped = false+ continue+ }+ if character == "\\", !inSingle {+ current.append(character)+ hasToken = true+ escaped = true+ continue+ }+ if character == "'", !inDouble {+ inSingle.toggle()+ current.append(character)+ hasToken = true+ continue+ }+ if character == "\"", !inSingle {+ inDouble.toggle()+ current.append(character)+ hasToken = true+ continue+ }+ if (character == " " || character == "\t"), !inSingle, !inDouble {+ if hasToken {+ tokens.append(current)+ current = ""+ hasToken = false+ }+ continue+ }+ current.append(character)+ hasToken = true+ }+ if hasToken {+ tokens.append(current)+ }while let first = tokens.first, first.contains("="), !first.hasPrefix("-"), isLeadingAssignment(first) {tokens.removeFirst()}diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swiftindex adf7979c..22645d38 100644--- a/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift@@ -53,8 +53,15 @@ struct ShellCommandClassifierTests {#expect(tier("exiftool -all= photo.jpg") == .read)// A path-qualified invocation normalizes to the bare tool name and is still trusted.#expect(tier("/opt/donkey-tools/yt-dlp -P ~/Downloads 'https://youtu.be/x'") == .read)+ // The real install path lives under "Application Support" — a space inside a quoted executable+ // path must not split the token, or the executable mis-parses as `application` and the tool is+ // gated as "unrecognized command". This is the exact command the screenshot prompted on.+ #expect(tier("\"/Users/me/Library/Application Support/Donkey/donkey-tools/yt-dlp\" --ffmpeg-location /opt/homebrew/bin/ffmpeg --download-sections '*15:00-16:00' -o clip.mp4 'https://youtu.be/x'") == .read)+ #expect(tier("'/Users/me/Library/Application Support/Donkey/donkey-tools/ffmpeg' -i clip.mp4 -vf \"subtitles=subs.srt\" out.mp4") == .read)// But a dangerous wrapper around a bundled tool is still caught by the whole-command checks.#expect(tier("yt-dlp -o - 'https://x' | sh") == .highRisk)+ // ...and the escape-hatch flag is still gated even when the bundled tool is at its spaced real path.+ #expect(tier("\"/Users/me/Library/Application Support/Donkey/donkey-tools/yt-dlp\" --exec 'rm -rf ~' 'https://youtu.be/x'") == .highRisk)}@Test