Open Bug 2078775 Opened 1 month ago Updated 1 month ago

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)

defect

Tracking

()

UNCONFIRMED

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

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

  1. Script declares using x = null; inside a block in sloppy-mode code.
  2. In the same block, script calls direct eval("{ function x() {} }").
  3. While compiling the eval, ScopeContext::cacheEnclosingScopeBindingForEval skips the enclosing using x binding, so isVarRedeclaredInEval finds no conflict and the parser treats the block function as Annex B hoistable, adding a var x binding to the eval script.
  4. At eval entry, CheckEvalDeclarationConflicts walks the environment chain, CheckVarNameConflict finds x on the block's lexical environment, and SpiderMonkey throws SyntaxError: redeclaration of const x.
  5. V8 completes the same program with typeof x === "object", as does SpiderMonkey when the outer binding is let or const.

Steps to Reproduce

  1. 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).
  2. 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).
  3. Repeat with flags_a=["--differential-testing"]; the result is again differs: true.
  4. 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 using declaration 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).
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Attached file testcase.js —
Attached file analysis.txt —

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.

Group: core-security → javascript-core-security
Blocks: sm-security
Severity: -- → S3
Priority: -- → P3
Blocks: 1569081
No longer blocks: sm-security
Group: javascript-core-security
Keywords: sec-other
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: