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.

Python: count time.localtime, strftime, ctime and datetime.today as clock reads (#1420)

Hashem Alkhanaizia7944bc449a0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ff9177a6..8c7ab86e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
## Unreleased
+- Python: `time.localtime()`, `time.gmtime()`, `time.strftime()`, `time.ctime()`,
+ `time.asctime()` and `datetime.today()` count as reading the clock in `bridge` and `analyze`.
+ `analyze` also counts `datetime.today()` after `from datetime import datetime`, which resolves to
+ `datetime.datetime.today`. Before, a function that called them was bridged as pure (#1403).
- PHP: `gmdate()`, `idate()`, `getdate()`, `localtime()`, `mktime()`, `gmmktime()`, `strtotime()`
and `gettimeofday()` count as reading the clock, as `date()` and `time()` do. Before, a function
that called them was bridged as pure, and `analyze` left them out of "Clock and random numbers"
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 35a912ed..739d7fa3 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -246,6 +246,14 @@ fn clock_and_random(lang: Lang) -> &'static [&'static str] {
"datetime.now(",
"datetime.utcnow(",
"date.today(",
+ // `date.today(` does not match inside `datetime.today(`, as a match starts at a name.
+ "datetime.today(",
+ // With no time given, these read the current time.
+ "time.localtime(",
+ "time.gmtime(",
+ "time.strftime(",
+ "time.ctime(",
+ "time.asctime(",
"random.",
"uuid.uuid",
"secrets.",
@@ -1541,6 +1549,10 @@ mod tests {
}
// The clock matters when nothing is passed: one recorded case is one moment.
assert!(reason(Lang::Python, &[], "def f():\n return time.time()").is_some());
+ for body in ["time.strftime('%Y')", "datetime.today()"] {
+ let code = format!("def f():\n return {body}");
+ assert!(reason(Lang::Python, &[], &code).is_some(), "{body}");
+ }
assert!(
reason(
Lang::Csharp,
diff --git a/src/lang/python.rs b/src/lang/python.rs
index a4366126..c07f9ab3 100644
--- a/src/lang/python.rs
+++ b/src/lang/python.rs
@@ -519,12 +519,18 @@ pub fn scan(source: &str) -> FileFacts {
"globals" | "locals" | "vars" => add(&mut facts, Risk::Reflection, node),
"time.time"
| "time.time_ns"
+ | "time.localtime"
+ | "time.gmtime"
+ | "time.strftime"
+ | "time.ctime"
+ | "time.asctime"
| "datetime.now"
| "datetime.utcnow"
| "datetime.today"
| "date.today"
| "datetime.datetime.now"
| "datetime.datetime.utcnow"
+ | "datetime.datetime.today"
| "datetime.date.today"
| "uuid.uuid4"
| "os.urandom" => add(&mut facts, Risk::ClockAndRandom, node),
@@ -2405,6 +2411,14 @@ mod tests {
"import datetime as dt\ndef f():\n return dt.datetime.now()\n",
Risk::ClockAndRandom,
),
+ (
+ "from datetime import datetime\ndef f():\n return datetime.today()\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ "import time\ndef f():\n return time.localtime().tm_year\n",
+ Risk::ClockAndRandom,
+ ),
(
"import os\ndef f():\n return os.getenv('USER')\n",
Risk::SharedState,