From 2416f6e2c563ee3583adf1e9f1e5942ce33cf2dc Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 31 Mar 2026 06:38:34 +0000 Subject: [PATCH] Fix command injection vulnerability in toolExists Co-authored-by: acebytes <2820910+acebytes@users.noreply.github.com> --- .jules/sentinel.md | 4 ++++ Sources/Cacheout/Models/CacheCategory.swift | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .jules/sentinel.md diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 0000000..509cdbe --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,4 @@ +## 2024-05-20 - Command Injection via String Interpolation in Shell Wrappers +**Vulnerability:** String interpolation used to inject dynamic variables into shell wrapper commands (`shell("/usr/bin/which \(tool)")`), leading to severe command injection vulnerabilities. +**Learning:** Shell wrapper functions (like those calling `/bin/bash -c`) inherently trust the entire string passed to them, meaning any dynamically injected content can break out of the intended command structure if it contains shell metacharacters. +**Prevention:** Avoid string interpolation in shell wrappers entirely. When dynamic inputs are required, execute the command directly using `Foundation.Process` (e.g., via `/usr/bin/env`) and pass the dynamic inputs strictly as elements in the `process.arguments` array. diff --git a/Sources/Cacheout/Models/CacheCategory.swift b/Sources/Cacheout/Models/CacheCategory.swift index 7b3d942..ba20985 100644 --- a/Sources/Cacheout/Models/CacheCategory.swift +++ b/Sources/Cacheout/Models/CacheCategory.swift @@ -186,8 +186,23 @@ struct CacheCategory: Identifiable, Hashable { } private func toolExists(_ tool: String) -> Bool { - let result = shell("/usr/bin/which \(tool)") - return result != nil && !result!.isEmpty + let process = Process() + process.executableURL = URL(fileURLWithPath: "/usr/bin/env") + process.arguments = ["which", tool] + process.standardOutput = FileHandle.nullDevice + process.standardError = FileHandle.nullDevice + process.environment = [ + "PATH": "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin", + "HOME": FileManager.default.homeDirectoryForCurrentUser.path + ] + + do { + try process.run() + process.waitUntilExit() + return process.terminationStatus == 0 + } catch { + return false + } } private func runProbe(_ command: String) -> String? {