Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

Swift: count NSHomeDirectory, NSTemporaryDirectory, getenv and the home directory as reading the environment (#1365)

jakalijeycc61c1998268
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 23b1f7d3..74f9a0aa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -188,6 +188,11 @@
fails. Before, a Python unit built on `BeautifulSoup(html, "html.parser")`, which no crate
reproduces, could only meet the cap by dropping behavior. The skill and TASK.md tell the agent
to ask the user instead of writing one (#1189 by @sungkilhan221).
+- Swift: `NSHomeDirectory()`, `NSTemporaryDirectory()`, `getenv()` and
+ `FileManager.default.homeDirectoryForCurrentUser` count as reading the environment, so `bridge`
+ keeps a function that reads one in Swift and `analyze` lists it. Before, only `ProcessInfo` and
+ the current directory were seen, and the recording held this machine's folders and variables
+ (#1348 by @jakalijey).
- C#: `Directory.GetCurrentDirectory()` and `Path.GetTempPath()` count as reading the environment,
as `Environment.CurrentDirectory` already did, so `bridge` keeps a function that builds a path from
one in C# and `analyze` lists it. Before, its recording held this machine's folders (#1347 by
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 70266bb6..5a3be92c 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -183,7 +183,15 @@ fn environment(lang: Lang) -> &'static [&'static str] {
"Path.GetTempPath(",
],
Lang::Powershell => &["$env:", "[environment]::", "get-location", "$pwd", "$home"],
- Lang::Swift => &["ProcessInfo", "FileManager.default.currentDirectoryPath"],
+ Lang::Swift => &[
+ "ProcessInfo",
+ "FileManager.default.currentDirectoryPath",
+ // In full: a name after `.` is skipped, so `homeDirectoryForCurrentUser` alone never matches.
+ "FileManager.default.homeDirectoryForCurrentUser",
+ "NSHomeDirectory(",
+ "NSTemporaryDirectory(",
+ "getenv(",
+ ],
Lang::C => &["getenv(", "getcwd("],
}
}
@@ -1587,6 +1595,18 @@ mod tests {
"func f() -> Date { return Date() }\n",
Risk::ClockAndRandom,
),
+ (
+ Lang::Swift,
+ "x.swift",
+ "public func configPath(_ name: String) -> String {\n return NSHomeDirectory() + \"/\" + name\n}\n",
+ Risk::SharedState,
+ ),
+ (
+ Lang::Swift,
+ "x.swift",
+ "public func home() -> String {\n guard let value = getenv(\"HOME\") else { return \"\" }\n return String(cString: value)\n}\n",
+ Risk::SharedState,
+ ),
];
for (lang, path, code, risk) in cases {
let facts = crate::lang::scan(*lang, path, code);