From 751044a97bd50bfd64290a77bab20b1fb6e57637 Mon Sep 17 00:00:00 2001 From: Ethan Davidson <31261035+EthanThatOneKid@users.noreply.github.com> Date: Fri, 20 Nov 2026 20:07:42 -0800 Subject: [PATCH] fix(ecmascript): reject top-level await in strict scripts and eval Strict scripts and evals are syntax checked as a sloppy script first to reject module-only syntax such as top-level await, and then re-parsed as a module to get strict mode. The sloppy parse was only treated as failed when the parser panicked, so non-fatal parser errors were dropped. The module re-parse then accepted `await` expressions and `for await`, which compiled to an Await at the top level and hit the "Unexpected yield or await" panic in ExecutionResult::into_js_result. Return the sloppy parse errors as well, so these now throw a SyntaxError like they do in sloppy mode. Fixes #1006 --- .../scripts_and_modules/source_code.rs | 36 ++++++++++++++++++- tests/expectations.json | 2 -- tests/metrics.json | 4 +-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/nova_vm/src/ecmascript/scripts_and_modules/source_code.rs b/nova_vm/src/ecmascript/scripts_and_modules/source_code.rs index 8448bf956..25fe372db 100644 --- a/nova_vm/src/ecmascript/scripts_and_modules/source_code.rs +++ b/nova_vm/src/ecmascript/scripts_and_modules/source_code.rs @@ -166,7 +166,7 @@ impl<'a> SourceCode<'a> { panicked, .. } = sloppy_result; - if panicked { + if panicked || !sloppy_errors.is_empty() { return Err(sloppy_errors); } let SemanticBuilderReturn { @@ -433,6 +433,40 @@ mod test { assert!(!errors.is_empty()); } + #[test] + fn strict_script_with_top_level_await() { + let (mut gc, mut scope) = unsafe { GcScope::create_root() }; + let mut gc = GcScope::new(&mut gc, &mut scope); + let mut agent = Agent::new(AgentOptions::default(), &DefaultHostHooks); + initialize_default_realm(&mut agent, gc.reborrow()); + + for source_type in [ + SourceCodeType::Script { strict: true }, + SourceCodeType::Eval { + direct: false, + strict: true, + }, + ] { + for source in ["await 10;", "for await (let x of []) {}"] { + let source_text = String::from_static_str(&mut agent, source, gc.nogc()); + // SAFETY: tests. + let errors = unsafe { + SourceCode::parse_source( + &mut agent, + source_text, + source_type, + #[cfg(feature = "typescript")] + false, + gc.nogc(), + ) + } + .unwrap_err(); + + assert!(!errors.is_empty()); + } + } + } + #[test] fn parse_and_realloc_source_codes_with_program() { let (mut gc, mut scope) = unsafe { GcScope::create_root() }; diff --git a/tests/expectations.json b/tests/expectations.json index d104ae2b5..2a274874e 100644 --- a/tests/expectations.json +++ b/tests/expectations.json @@ -7439,7 +7439,6 @@ "staging/sm/Array/unscopables.js": "FAIL", "staging/sm/Array/values.js": "FAIL", "staging/sm/ArrayBuffer/slice-species.js": "FAIL", - "staging/sm/AsyncGenerators/for-await-of-error.js": "CRASH", "staging/sm/BigInt/Number-conversion-rounding.js": "FAIL", "staging/sm/BigInt/large-bit-length.js": "TIMEOUT", "staging/sm/Date/dst-offset-caching-1-of-8.js": "TIMEOUT", @@ -7678,7 +7677,6 @@ "staging/sm/TypedArray/sort-non-function.js": "FAIL", "staging/sm/TypedArray/test-integrity-level-detached.js": "FAIL", "staging/sm/TypedArray/toString.js": "FAIL", - "staging/sm/async-functions/await-error.js": "CRASH", "staging/sm/async-functions/await-in-arrow-parameters.js": "FAIL", "staging/sm/async-functions/await-in-parameters-of-async-func.js": "FAIL", "staging/sm/async-functions/toString.js": "FAIL", diff --git a/tests/metrics.json b/tests/metrics.json index 64570d3a7..2a2b1f315 100644 --- a/tests/metrics.json +++ b/tests/metrics.json @@ -1,8 +1,8 @@ { "results": { - "crash": 162, + "crash": 160, "fail": 7572, - "pass": 40692, + "pass": 40694, "skip": 5127, "timeout": 22, "unresolved": 41