From 817e6885d9572012f133ed3f47a859c3e1df38c5 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 15 Apr 2026 07:44:55 +0000 Subject: [PATCH] Refactor toolExists to use direct Process execution for defense-in-depth Replaced `toolExists` shell wrapper with a direct `Foundation.Process` execution of `/usr/bin/which` where `tool` is passed explicitly in the `arguments` array. Evaluates `process.terminationStatus == 0` after `process.waitUntilExit()`. Standard output/error is mapped to `FileHandle.nullDevice`. Also documented findings in .jules/sentinel.md. 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..dde8efc --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,4 @@ +## 2024-04-15 - Command injection in CacheCategory probes +**Vulnerability:** `CacheCategory.toolExists` passes user-supplied input to a shell via `/usr/bin/which \(tool)`, introducing command injection if the category allows dynamic inputs. +**Learning:** Shell evaluation with string interpolation `\()` is unsafe. Passing dynamic inputs through `/bin/bash -c` risks evaluating shell operators. +**Prevention:** Avoid shell evaluations where direct execution of `Process` with structured `arguments` is possible. diff --git a/Sources/Cacheout/Models/CacheCategory.swift b/Sources/Cacheout/Models/CacheCategory.swift index 7b3d942..c5ee6dc 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/which") + process.arguments = [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? {