Skip to content

The headless browser for AI agents and web scraping

fix(js): keep the performance time origin out of the future

aech9b32a2c6827f
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 7beeb6b5..dbf2282f 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -14361,7 +14361,9 @@ globalThis.__obscura_init = function() {
var memValues = globalThis.__obscura_stealth ? [4, 8] : [0.25, 0.5, 1, 2, 4, 8];
globalThis.__obscura_mem = memValues[Math.floor(_fpRand(401) * memValues.length)];
- const t0 = Date.now() + Math.floor(_fpRand(641) * 100) - 50;
+ // A navigation start precedes the wall clock, so skew into the past only: an
+ // origin ahead of it makes performance.now() and the rAF timestamp negative.
+ const t0 = Date.now() - 1 - Math.floor(_fpRand(641) * 100);
globalThis.performance.timeOrigin = t0;
globalThis.performance.timing = { navigationStart: t0, domContentLoadedEventEnd: t0, loadEventEnd: t0 };
var _totalHeap = 15000000 + Math.floor(_fpRand(620) * 85000000);
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 1fb9423d..5c683a9f 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -3895,6 +3895,24 @@ mod tests {
);
}
+ #[test]
+ fn time_origin_never_lands_in_the_future() {
+ // __obscura_init deletes itself, so a realm yields one draw of the
+ // origin jitter. Build a fresh runtime per draw, do not hoist this out.
+ for _ in 0..40 {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let skew = rt
+ .evaluate("performance.timeOrigin - Date.now()")
+ .unwrap()
+ .as_f64()
+ .unwrap();
+ assert!(
+ skew <= 0.0,
+ "performance.timeOrigin is {skew} ms ahead of Date.now()"
+ );
+ }
+ }
+
#[test]
fn childnode_helpers_coerce_non_string_primitives_to_text() {
let mut rt =