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.

PowerShell: count only [datetime]::Now, UtcNow and Today as clock reads (#1383)

jakalijey2da3debe114c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7dd9c1f0..938766b9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -223,6 +223,10 @@
(#1337 by @jakalijey).
from one JVM start to the next. `List.of`, `Collectors.toList`, and a `toMap` or `groupingBy`
given a `TreeMap` or `LinkedHashMap` supplier are not (#1343 by @jakalijey).
+- PowerShell: only `[datetime]::Now`, `UtcNow` and `Today`, and `[datetimeoffset]::Now` and
+ `UtcNow` (with or without `System.`), count as reading the clock. `[datetime]::IsLeapYear`,
+ `DaysInMonth`, `Parse` and the other static members compute from their arguments, so a function
+ that calls one is bridged and `analyze` no longer counts it (#1338 by @jakalijey).
- `app front-door` on a CLI whose recorded cases name no command says which name answers the
root (`commands: none (the root command is `cli`)`) and ends with the `app wire ... --command cli`
that works, where it said `--command <name>` and `app wire` refused every name but that one.
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 2f1eddaa..4b3bb889 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -273,7 +273,23 @@ fn clock_and_random(lang: Lang) -> &'static [&'static str] {
"Random",
"Guid.NewGuid",
],
- Lang::Powershell => &["get-date", "get-random", "new-guid", "[datetime]::"],
+ // Only these static members of `[datetime]` read the clock; `IsLeapYear`, `DaysInMonth`
+ // and `Parse` compute from their arguments.
+ Lang::Powershell => &[
+ "get-date",
+ "get-random",
+ "new-guid",
+ "[datetime]::now",
+ "[datetime]::utcnow",
+ "[datetime]::today",
+ "[system.datetime]::now",
+ "[system.datetime]::utcnow",
+ "[system.datetime]::today",
+ "[datetimeoffset]::now",
+ "[datetimeoffset]::utcnow",
+ "[system.datetimeoffset]::now",
+ "[system.datetimeoffset]::utcnow",
+ ],
Lang::Swift => &[
"Date()",
"Date.now",
@@ -1694,6 +1710,24 @@ mod tests {
"function F { $env:HOME }\n",
Risk::SharedState,
),
+ (
+ Lang::Powershell,
+ "x.ps1",
+ "function F { [datetime]::UtcNow }\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Powershell,
+ "x.ps1",
+ "function F { [datetime]::Today.DayOfWeek.ToString() }\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Powershell,
+ "x.ps1",
+ "function F { [DateTimeOffset]::Now }\n",
+ Risk::ClockAndRandom,
+ ),
(
Lang::Swift,
"x.swift",
@@ -1739,6 +1773,19 @@ mod tests {
facts.risks
);
}
+ for code in [
+ "function F { [datetime]::IsLeapYear(2024) }\n",
+ "function F { [datetime]::DaysInMonth(2024, 2) }\n",
+ "function F { [datetime]::Parse('2024-02-29') }\n",
+ "function F { [datetime]::MinValue }\n",
+ ] {
+ let facts = crate::lang::scan(Lang::Powershell, "x.ps1", code);
+ assert!(
+ !facts.risks.contains_key(&Risk::ClockAndRandom),
+ "{code}: {:?}",
+ facts.risks
+ );
+ }
let facts = crate::lang::scan(
Lang::Ruby,
"x.rb",