Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2024-04-08 - Prevent command injection by bypassing shell wrapper
**Vulnerability:** Command injection risk via string interpolation into a shell command wrapper.
**Learning:** `toolExists` in `CacheCategory.swift` used `shell("/usr/bin/which \(tool)")`, executing via `/bin/bash -c`. Although the input was currently hardcoded, this pattern creates a severe vulnerability if dynamic inputs are ever passed.
**Prevention:** Always use direct execution via `Process()` and pass inputs inside `process.arguments` instead of interpolating strings into a shell command.
24 changes: 22 additions & 2 deletions Sources/Cacheout/Models/CacheCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,28 @@ 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()
let pipe = Pipe()

process.executableURL = URL(fileURLWithPath: "/usr/bin/which")
process.arguments = [tool]
process.standardOutput = pipe
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()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
process.waitUntilExit()
guard process.terminationStatus == 0 else { return false }
let output = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)
return output != nil && !output!.isEmpty
} catch {
return false
}
}

private func runProbe(_ command: String) -> String? {
Expand Down