Open source CapCut alternative.
TypeScript
fix(app): run bundled CLI tools without a consent prompt; gate command-exec flags
David Le47d632c851c3
diff --git a/apps/Donkey/Sources/DonkeyRuntime/BundledTools.swift b/apps/Donkey/Sources/DonkeyRuntime/BundledTools.swiftindex 13945c71..8fa2c0be 100644--- a/apps/Donkey/Sources/DonkeyRuntime/BundledTools.swift+++ b/apps/Donkey/Sources/DonkeyRuntime/BundledTools.swift@@ -7,6 +7,15 @@ import Foundation/// `bundled-tools.json` (in the app's resource bundle) is the single source of truth for the version,/// download URL, and checksum; scripts/publish-bundled-tools.sh builds the artifact and rewrites it.public enum BundledTools {+ /// Bare executable names of the tools shipped in the bundle — the mandatory media set from+ /// scripts/publish-bundled-tools.sh plus the document tools fetched in fetch-bundled-tools.sh. Donkey+ /// resolves these from `installDirectory`, which `shellEnvironment()` puts first on PATH, so capability+ /// skills run them by bare name. This is the single Swift home for the list; keep it in step with the+ /// fetch/publish scripts when the bundle gains or drops a tool.+ public static let executableNames: Set<String> = [+ "ffmpeg", "ffprobe", "yt-dlp", "lit", "pdf-fill", "qpdf", "exiftool"+ ]+/// The manifest describing the published tools bundle. An empty `sha256` means nothing is published/// yet, so callers should treat the bundle as unavailable rather than downloading something unverified.public struct Manifest: Decodable, Sendable, Equatable {diff --git a/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift b/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swiftindex d1776d64..654e516f 100644--- a/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift+++ b/apps/Donkey/Sources/DonkeyRuntime/ShellCommandClassifier.swift@@ -124,10 +124,28 @@ public enum ShellCommandClassifier {let firstArg = tokens.first.map { $0.lowercased() }+ // A command-execution flag runs an arbitrary command the same way `| sh` does, and it can ride+ // inside an otherwise-trusted tool — find's `-exec`, yt-dlp's `--exec`/`--exec-before-download`.+ // Gate it high-risk wherever it appears, BEFORE the read/bundled-tool classifications below, so the+ // escape hatch is never run silently no matter which executable carries it.+ if tokens.contains(where: isCommandExecutionFlag) {+ return ShellCommandClassification(tier: .highRisk, signature: "\(executable) -exec", reason: "runs an arbitrary command")+ }+if let reason = highRiskExecutables[executable] {return ShellCommandClassification(tier: .highRisk, signature: executable, reason: reason)}+ // Tools Donkey ships in its own bundled-tools directory (first on PATH) are a first-party+ // capability surface — the media and document skills run them by bare name, the same way Donkey's+ // built-in file tools write files. They run immediately instead of being mis-scored as an+ // unrecognized "write" and gated on every clip or conversion. The escape-hatch flags that would+ // make one dangerous are already gated above; a dangerous wrapper (`yt-dlp … | sh`, a device+ // redirect) is caught by the whole-command checks in `classify` before any segment reaches here.+ if BundledTools.executableNames.contains(executable) {+ return ShellCommandClassification(tier: .read, signature: executable, reason: nil)+ }+// Per-tool subcommand/flag refinement (read vs write of the same tool).if let refined = refine(executable: executable, tokens: tokens, firstArg: firstArg) {return refined@@ -333,6 +351,19 @@ public enum ShellCommandClassifier {"xargs", "env", "time", "nice", "nohup", "command", "builtin", "stdbuf", "timeout", "caffeinate"]+ /// Flags that make a tool run an arbitrary command — `| sh` smuggled into an argument. Gated+ /// high-risk wherever they appear, so neither a trusted bundled tool nor a read-tier tool (find)+ /// can silently spawn a subcommand through them.+ private static let commandExecutionFlags: Set<String> = [+ "-exec", "-execdir", "--exec", "--exec-before-download"+ ]++ /// True when a token is a command-execution flag, including its `--exec=CMD` and `--exec-…` variants.+ private static func isCommandExecutionFlag(_ token: String) -> Bool {+ let lower = token.lowercased()+ return commandExecutionFlags.contains(lower) || lower.hasPrefix("--exec=") || lower.hasPrefix("--exec-")+ }+/// Read-only inspection tools — safe to run without a prompt. Tools whose/// read/write split depends on a subcommand (defaults, pmset, networksetup,/// scutil, plutil, sed) are handled in `refine` and intentionally absent here.diff --git a/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift b/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swiftindex 4a84f52a..adf7979c 100644--- a/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift+++ b/apps/Donkey/Tests/DonkeyRuntimeTests/ShellCommandClassifierTests.swift@@ -42,6 +42,34 @@ struct ShellCommandClassifierTests {#expect(tier("cat x && open -a Safari") == .reversibleWrite)}+ @Test+ func bundledCapabilityToolsRunWithoutAPrompt() {+ // The motivating bug: clipping a YouTube video and burning in subtitles is the literal task —+ // the bundled, signed media tools must not raise an Approve/Deny gate as "unrecognized command".+ #expect(tier("yt-dlp --download-sections '*15:00-16:00' -f 'best[ext=mp4]' -o clip.mp4 'https://youtu.be/x'") == .read)+ #expect(tier("ffmpeg -i clip.mp4 -vf \"subtitles=subs.srt\" -c:a copy out.mp4") == .read)+ #expect(tier("ffprobe -v error -show_entries format=duration -of csv=p=0 out.mp4") == .read)+ #expect(tier("qpdf --decrypt in.pdf out.pdf") == .read)+ #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)+ // 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)+ }++ @Test+ func commandExecutionFlagsAreHighRiskEvenOnTrustedTools() {+ // yt-dlp's --exec runs an arbitrary command after download — it must never run silently, even+ // though yt-dlp is otherwise a trusted bundled tool that runs read-tier. No `| sh` or separator+ // here, so this exercises the flag gate specifically, not the whole-command substring check.+ #expect(tier("yt-dlp --exec 'open -a Calculator' 'https://youtu.be/x'") == .highRisk)+ #expect(tier("yt-dlp --exec-before-download 'touch /tmp/x' 'https://youtu.be/x'") == .highRisk)+ // find runs read-tier, but -exec/-execdir turn it into a command runner — a latent escape hatch+ // the gate also closes.+ #expect(tier("find . -name '*.tmp' -exec rm {} ;") == .highRisk)+ #expect(tier("find . -type d -execdir chmod 700 {} ;") == .highRisk)+ }+@Testfunc readSubcommandsOfWriteCapableToolsAreRead() {#expect(tier("defaults read com.apple.dock") == .read)