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.

TS/JS: count process.hrtime and crypto.randomInt as clock and randomness (#1390)

jakalijeyc26399a136a3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 04439c99..19c5310c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -223,6 +223,9 @@
(#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).
+- TS/JS: `process.hrtime()`, `process.hrtime.bigint()` and `crypto.randomInt()` count as reading
+ the clock or randomness in `analyze` and `bridge`, and `analyze` now counts `crypto.randomBytes()`
+ too, which only `bridge` did (#1376 by @jakalijey).
- C: `random`, `arc4random`, `arc4random_uniform`, `arc4random_buf`, `drand48`, `erand48`,
`lrand48`, `nrand48`, `mrand48`, `jrand48` and `rand_r` count as reading randomness, as `rand`
already did, in `analyze` and `bridge` (#1372 by @jakalijey).
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 8e2c5bc0..82198e0a 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -208,6 +208,9 @@ fn clock_and_random(lang: Lang) -> &'static [&'static str] {
"crypto.randomUUID(",
"crypto.getRandomValues(",
"crypto.randomBytes(",
+ "crypto.randomInt(",
+ // Also `process.hrtime.bigint()`.
+ "process.hrtime",
],
Lang::Python => &[
"time.time(",
@@ -1454,6 +1457,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 ["process.hrtime.bigint()", "crypto.randomInt(6)"] {
+ let code = format!("export function f() {{\n return {body}\n}}\n");
+ assert!(reason(Lang::Ts, &[], &code).is_some(), "{body}");
+ }
assert!(
reason(
Lang::C,
diff --git a/src/ts/scan.rs b/src/ts/scan.rs
index 82ebd95b..7263d4cd 100644
--- a/src/ts/scan.rs
+++ b/src/ts/scan.rs
@@ -515,6 +515,15 @@ fn identifier<'a>(expr: &Expression<'a>) -> Option<&'a str> {
}
}
+/// Whether `expr` is `process.hrtime`, the object of `process.hrtime.bigint()`.
+fn is_process_hrtime(expr: &Expression) -> bool {
+ matches!(
+ expr,
+ Expression::StaticMemberExpression(m)
+ if identifier(&m.object) == Some("process") && m.property.name == "hrtime"
+ )
+}
+
fn string_arg(arg: Option<&Argument>) -> Option<String> {
match arg {
Some(Argument::StringLiteral(s)) => Some(s.value.display().to_string()),
@@ -586,7 +595,15 @@ impl<'a> Visit<'a> for Collector {
(Some("Promise"), _) => self.add(Risk::Async, it.span),
(Some("Math"), "random")
| (Some("Date" | "performance"), "now")
- | (Some("crypto"), "randomUUID" | "getRandomValues") => {
+ | (Some("process"), "hrtime")
+ | (
+ Some("crypto"),
+ "randomUUID" | "getRandomValues" | "randomInt" | "randomBytes",
+ ) => {
+ self.add(Risk::ClockAndRandom, it.span);
+ }
+ // `process.hrtime.bigint()`
+ (None, "bigint") if is_process_hrtime(&member.object) => {
self.add(Risk::ClockAndRandom, it.span);
}
(Some("Math"), "round")
@@ -984,6 +1001,22 @@ mod tests {
Risk::NumberSemantics,
),
("export const a = () => process.env.HOME", Risk::SharedState),
+ (
+ "export const a = () => process.hrtime()",
+ Risk::ClockAndRandom,
+ ),
+ (
+ "export const a = () => process.hrtime.bigint()",
+ Risk::ClockAndRandom,
+ ),
+ (
+ "export const a = () => crypto.randomInt(6)",
+ Risk::ClockAndRandom,
+ ),
+ (
+ "export const a = () => crypto.randomBytes(4)",
+ Risk::ClockAndRandom,
+ ),
(
"let counter = 0\nexport const a = () => ++counter",
Risk::SharedState,