`using` bindings can be silently mutated, violating spec const-like semantics
Categories
(Core :: JavaScript Engine, defect, P2)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox156 | --- | fixed |
People
(Reporter: jandem, Assigned: debadree333)
References
(Blocks 1 open bug)
Details
(Keywords: ai-involved)
Attachments
(1 external file)
Summary
using declarations must behave like const: assignments must throw TypeError. SpiderMonkey enforces this for statically-resolved assignments (Bug 2000184 added BindingKind::Using to isConst() in NameAnalysisTypes.h), but two runtime paths bypass the static check:
- Sloppy-mode direct
eval()— resolves dynamically through the environment chain.AddToEnvironmentMap()injs/src/vm/Scope.cpponly marksConstandNamedLambdaCalleenon-writable;Usingfalls through to the writable default. - Debugger API —
DebugEnvironmentProxy::handleUnaliasedAccess()injs/src/vm/EnvironmentObject.cpponly checksBindingKind::Const, soDebugger.Environment.setVariable()silently mutatesusingbindings.
V8 throws TypeError: Assignment to using variable. for the eval case.
There is also a dead-code gap in LexicalScopeHasClosedOverBindings() (js/src/frontend/Parser.cpp) which omits BindingKind::Using — currently unreachable (only called for named-lambda scopes) but worth fixing for defense-in-depth.
Test case
// Reproducer 1: sloppy-mode direct eval
function testEval() {
{
using x = { [Symbol.dispose]() {} };
eval("x = 2");
print("using via eval: typeof=" + typeof x + " value=" + x);
}
}
testEval();
// Actual: using via eval: typeof=number value=2
// Expected: TypeError: invalid assignment to const 'x'
// Reproducer 2: Debugger.Environment.setVariable
var g = newGlobal({ newCompartment: true });
g.eval(`
function f() {
{ using x = { [Symbol.dispose]() {} }; debugger; print(typeof x + " " + x); }
}
`);
var dbg = Debugger(g);
dbg.onDebuggerStatement = function (frame) {
frame.environment.setVariable("x", 99);
};
g.f();
// Actual: number 99
// Expected: TypeError from setVariable
Suggested fix
--- a/js/src/vm/Scope.cpp
+++ b/js/src/vm/Scope.cpp
@@ -111,6 +111,7 @@ static bool AddToEnvironmentMap(JSContext* cx, const JSClass* clasp,
PropertyFlags propFlags = {PropertyFlag::Enumerable};
switch (bindKind) {
case BindingKind::Const:
+ case BindingKind::Using:
case BindingKind::NamedLambdaCallee:
// Non-writable.
break;
--- a/js/src/vm/EnvironmentObject.cpp
+++ b/js/src/vm/EnvironmentObject.cpp
@@ -1745,7 +1745,8 @@
if (!bi) {
return true;
}
- if (action == SET && bi.kind() == BindingKind::Const) {
+ if (action == SET && (bi.kind() == BindingKind::Const ||
+ bi.kind() == BindingKind::Using)) {
ReportRuntimeLexicalError(cx, JSMSG_BAD_CONST_ASSIGN, id);
return false;
}
@@ -1870,7 +1871,8 @@
if (!bi) {
return true;
}
- if (action == SET && bi.kind() == BindingKind::Const) {
+ if (action == SET && (bi.kind() == BindingKind::Const ||
+ bi.kind() == BindingKind::Using)) {
ReportRuntimeLexicalError(cx, JSMSG_BAD_CONST_ASSIGN, id);
return false;
}
--- a/js/src/frontend/Parser.cpp
+++ b/js/src/frontend/Parser.cpp
@@ -1555,6 +1555,7 @@ bool LexicalScopeHasClosedOverBindings(ParseContext* pc,
switch (bi.kind()) {
case BindingKind::Let:
case BindingKind::Const:
+ case BindingKind::Using:
if (allBindingsClosedOver || bi.closedOver()) {
return true;
}
| Reporter | ||
Comment 1•5 months ago
|
||
Debadree, I'm adding a needinfo in case you want to take a look at this :)
Updated•5 months ago
|
| Reporter | ||
Updated•5 months ago
|
| Assignee | ||
Comment 4•3 months ago
|
||
Explicit Resource Management declarations create immutable lexical bindings, but the environment and Debugger mutation paths only handled const bindings as non-writable. This let the different binding representations disagree about assignment semantics.
Classify BindingKind::Using with const when determining closed-over lexical scopes, defining environment properties, and servicing Debugger environment writes. Cover direct eval assignment plus aliased and unaliased Debugger environments, and make the Debugger assertions non-vacuous by verifying that both frame paths were observed.
Updated•2 months ago
|
Comment 6•2 months ago
|
||
| bugherder | ||
Updated•2 months ago
|
Description
•