Sloppy direct eval of a block-level function throws SyntaxError when an enclosing block has a `using` binding of the same name
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
People
(Reporter: bugmon, Unassigned)
References
(Blocks 1 open bug)
Details
(Keywords: testcase)
Attachments
(4 files)
ScopeContext::cacheEnclosingScopeBindingForEval skips BindingKind::Using when it fills the enclosing-lexical-binding cache for a direct eval, so ParseContext::isVarRedeclaredInEval reports no conflict for a name bound by an enclosing using declaration. The parser then applies Annex B function hoisting and gives the eval script a synthesized var for that name, and the runtime check in CheckEvalDeclarationConflicts finds the using binding on the block environment and throws SyntaxError: redeclaration of const x. The same program with let or const completes, as ECMA-262 Annex B.3.2.3 requires: an existing binding in an enclosing declarative environment suppresses the hoist and no error is specified.
A web page author can observe a spurious SyntaxError in spec-conforming script by running a sloppy direct eval that declares a block-level function whose name matches an enclosing using binding. The demonstrated effect is a wrong exception in SpiderMonkey where V8 completes normally; no crash, assertion failure or memory corruption was observed in the debug shell. The behaviour is the same with --no-jit-backend, so it is a frontend defect and not a JIT miscompilation.
Build Info
- Branch: main
- Revision: bf9757d91e2e53b8ab836f2a08bf4c0275fdcd23
- Timestamp: 2026-10-02T21:18:24+00:00
Affected Code
File: js/src/frontend/Stencil.cpp, line 937-949
// TODO: Optimize cache population for `using` bindings. (Bug 1899502)
case BindingKind::Using:
break;
case BindingKind::Const: {
InputName binding(scope_ref, bi.name());
if (!addToEnclosingLexicalBindingCache(
fc, parserAtoms, input.atomCache, binding,
EnclosingLexicalBindingKind::Const)) {
return false;
}
break;
}
File: js/src/frontend/ParseContext.cpp, line 461-470
bool ParseContext::isVarRedeclaredInEval(TaggedParserAtomIndex name,
ParserBase* parser,
DeclarationKind kind,
Maybe<DeclarationKind>* out) {
auto maybeKind = parser->getCompilationState()
.scopeContext.lookupLexicalBindingInEnclosingScope(name);
if (!maybeKind) {
*out = Nothing();
return true;
}
File: js/src/frontend/ParseContext.cpp, line 416-450
bool ParseContext::computeAnnexBAppliesToLexicalFunctionInInnermostScope(
FunctionBox* funbox, ParserBase* parser, bool* annexBApplies) {
...
if (!isVarRedeclaredInInnermostScope(
name, parser, DeclarationKind::VarForAnnexBLexicalFunction,
&redeclaredKind)) {
return false;
}
...
// If an early error would have occurred already, this function should not
// exhibit Annex B.3.3 semantics.
*annexBApplies = !redeclaredKind;
return true;
}
File: js/src/vm/EnvironmentObject.cpp, line 3915-3924
[[nodiscard]] static bool CheckVarNameConflict(
JSContext* cx, Handle<LexicalEnvironmentObject*> lexicalEnv,
Handle<PropertyName*> name) {
mozilla::Maybe<PropertyInfo> prop = lexicalEnv->lookup(cx, name);
if (prop.isSome()) {
ReportRuntimeRedeclaration(cx, name, prop->writable() ? "let" : "const");
return false;
}
return true;
}
File: js/src/vm/EnvironmentObject.cpp, line 4277-4302
static bool CheckEvalDeclarationConflicts(JSContext* cx, HandleScript script,
HandleObject envChain,
HandleObject varObj) {
...
// Check that a direct eval will not hoist 'var' bindings over lexical
// bindings with the same name.
while (env != varObj) {
if (!CheckVarNameConflictsInEnv(cx, script, env)) {
return false;
}
env = env->enclosingEnvironment();
}
The cache that tells the eval parser which names are lexically bound in enclosing scopes omits using bindings. The eval parser consults only that cache, so it decides Annex B hoisting is allowed and emits a var binding. The runtime check walks the real environment chain, where the using binding is present, and throws.
Exploit Chain
- Script declares
using x = null;inside a block in sloppy-mode code. - In the same block, script calls direct
eval("{ function x() {} }"). - While compiling the eval,
ScopeContext::cacheEnclosingScopeBindingForEvalskips the enclosingusing xbinding, soisVarRedeclaredInEvalfinds no conflict and the parser treats the block function as Annex B hoistable, adding avar xbinding to the eval script. - At eval entry,
CheckEvalDeclarationConflictswalks the environment chain,CheckVarNameConflictfindsxon the block's lexical environment, and SpiderMonkey throwsSyntaxError: redeclaration of const x. - V8 completes the same program with
typeof x === "object", as does SpiderMonkey when the outer binding isletorconst.
Steps to Reproduce
- Use an unmodified tree and build the SpiderMonkey debug shell with /app/mozconfigs/mozconfig.js.linux.debug (binary at /firefox/obj-js-debug/dist/bin/js).
- Run testcase.js through js_shell_differential_evaluator with shell_a=/firefox/obj-js-debug/dist/bin/js (engine_a=spidermonkey), shell_b=/app/tools/d8/d8 (engine_b=v8) and no extra flags; the result is differs: true (SpiderMonkey exit 3, V8 exit 0).
- Repeat with flags_a=["--differential-testing"]; the result is again differs: true.
- No prefs, source patches or external services are needed.
Security Impact
- Severity: Low
- Attacker capability: Script receives a SyntaxError from a sloppy direct eval that the specification says must complete normally. This is a conformance defect in the frontend; no memory corruption, assertion failure or information disclosure was demonstrated.
- Preconditions: Sloppy-mode code containing a
usingdeclaration and a direct eval, in the same or a nested scope below the nearest var scope, that declares a block-level function with the same name.
ASAN Report
No sanitizer report; this class is proven by a divergence in exit status. Verifier's own runs of the unmodified testcase.js:
RUN 1 (default flags) -> differs: true
shell_a argv: ["/firefox/obj-js-debug/dist/bin/js","--fuzzing-safe","/tmp/fx_audit_jsdiff_2j6ixp_w/testcase.js"]
shell_a engine: spidermonkey, exit_code: 3, timed_out: false, succeeded: false
shell_b argv: ["/app/tools/d8/d8","/tmp/fx_audit_jsdiff_2j6ixp_w/testcase.js"]
shell_b engine: v8, exit_code: 0, timed_out: false, succeeded: true
shell_a stderr:
/tmp/fx_audit_jsdiff_2j6ixp_w/testcase.js line 11 > eval:1:1 SyntaxError: redeclaration of const x
Stack:
@/tmp/fx_audit_jsdiff_2j6ixp_w/testcase.js line 11 > eval:1:1
withUsing@/tmp/fx_audit_jsdiff_2j6ixp_w/testcase.js:11:5
@/tmp/fx_audit_jsdiff_2j6ixp_w/testcase.js:19:11
RUN 2 (--differential-testing on the SpiderMonkey side) -> differs: true
shell_a argv: ["/firefox/obj-js-debug/dist/bin/js","--fuzzing-safe","--differential-testing","/tmp/fx_audit_jsdiff_cx9hhu3g/testcase.js"]
shell_a engine: spidermonkey, exit_code: 3, timed_out: false, succeeded: false
shell_b argv: ["/app/tools/d8/d8","/tmp/fx_audit_jsdiff_cx9hhu3g/testcase.js"]
shell_b engine: v8, exit_code: 0, timed_out: false, succeeded: true
shell_a stderr:
/tmp/fx_audit_jsdiff_cx9hhu3g/testcase.js line 11 > eval:1:1 SyntaxError: redeclaration of const x
Stack:
@/tmp/fx_audit_jsdiff_cx9hhu3g/testcase.js line 11 > eval:1:1
withUsing@/tmp/fx_audit_jsdiff_cx9hhu3g/testcase.js:11:1
@/tmp/fx_audit_jsdiff_cx9hhu3g/testcase.js:19:1
Additional verifier runs:
- `using`-only half of the testcase with flags_a=["--differential-testing","--no-jit-backend"] -> differs: true (SpiderMonkey exit 3, V8 exit 0).
- Control with `const` and `let` in place of `using` -> differs: false (both shells exit 0).
| Reporter | ||
Comment 1•1 month ago
|
||
| Reporter | ||
Comment 2•1 month ago
|
||
| Reporter | ||
Comment 3•1 month ago
|
||
| Reporter | ||
Comment 4•1 month ago
|
||
| Reporter | ||
Comment 5•1 month ago
|
||
This bug was filed automatically by an AI-powered security testing tool. The analysis, testcase, and any suggested patches were generated by an AI model and have NOT been verified by a human.
Please review all details carefully before triaging. The issue was confirmed by an independent verifier, but for non-ASAN issues, the verifier has been made less strict. Additionally, the root cause analysis and suggested fix may contain errors.
Updated•1 month ago
|
Updated•1 month ago
|
Updated•1 month ago
|
Description
•