@zvenigora/ng-eval-core 0.12.1 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +160 -3
- package/README.md +270 -7
- package/fesm2022/zvenigora-ng-eval-core.mjs +816 -61
- package/fesm2022/zvenigora-ng-eval-core.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -738,6 +738,12 @@ const afterVisitor = (node, st) => {
|
|
|
738
738
|
* by controlling access to dangerous property names and ensuring safe
|
|
739
739
|
* object operations.
|
|
740
740
|
*/
|
|
741
|
+
// Direct, not through the `classes/eval` barrel. `instanceof` needs the class
|
|
742
|
+
// itself and not its type, and this module is imported by most visitors, so
|
|
743
|
+
// the narrowest edge is the right one. Nothing here reads the binding at
|
|
744
|
+
// module scope - only inside {@link safeSetProperty} - so the import cycle
|
|
745
|
+
// the barrels already carry (`eval-scope.ts` imports the visitors barrel) is
|
|
746
|
+
// not deepened into an initialisation order.
|
|
741
747
|
/**
|
|
742
748
|
* Set of property names that are dangerous for prototype pollution attacks
|
|
743
749
|
*/
|
|
@@ -852,6 +858,78 @@ const isDangerousConstructor = (obj) => {
|
|
|
852
858
|
}
|
|
853
859
|
return false;
|
|
854
860
|
};
|
|
861
|
+
/**
|
|
862
|
+
* Security: refuses a member write whose target is an evaluation context -
|
|
863
|
+
* **by class** (`docs/a35/plan.md` Revision 16, and `docs/backlog.md` A42).
|
|
864
|
+
*
|
|
865
|
+
* `this.k = v` is a member write whose target is the `EvalContext` itself: the
|
|
866
|
+
* member visitor resolves `this.k` through the context's chain, and for a key
|
|
867
|
+
* nothing in that chain holds, `getThis` answers undefined and the receiver
|
|
868
|
+
* falls back to the context object. Up to 0.12.1 the write then landed on the
|
|
869
|
+
* instance as an own property. Three things followed, and the third is why
|
|
870
|
+
* this refusal is the fix rather than one of the routes being closed:
|
|
871
|
+
*
|
|
872
|
+
* - **Capture.** An own property shadows the prototype's method, so a planted
|
|
873
|
+
* function under a method's name was invoked by the evaluator itself - with
|
|
874
|
+
* the real context as its `this`. `context-method.ts` closes that at the
|
|
875
|
+
* sites that remain; closing it here closes it at the source.
|
|
876
|
+
* - **Denial of service.** A plant over `push` that evaluates anything
|
|
877
|
+
* recurses through `arrow-function-expression.ts` until the stack is gone,
|
|
878
|
+
* and it persists on the instance - so every later evaluation on a reused
|
|
879
|
+
* context throws. `@zvenigora/ng-eval-signals` and
|
|
880
|
+
* `@zvenigora/ng-eval-forms` both reuse one context by design.
|
|
881
|
+
* - **Integrity.** `_original`, `_scopes`, `_priorScopes`, `_lookups` and
|
|
882
|
+
* `_options` are own properties, so a write could replace the caller's own
|
|
883
|
+
* object or the scope stack outright. No amount of method resolution
|
|
884
|
+
* reaches that; refusing the write does.
|
|
885
|
+
*
|
|
886
|
+
* **Nothing legitimate is lost.** The property it used to create was not
|
|
887
|
+
* readable by any expression: `EvalContext.get` resolves through the scopes,
|
|
888
|
+
* the original, the prior scopes and the lookups, and never off the instance.
|
|
889
|
+
* So the write was invisible from the inside and reachable only as the three
|
|
890
|
+
* defects above. A write to a context *key* is `k = v`, which goes through
|
|
891
|
+
* `EvalContext.set` and the write policy a subclass may override.
|
|
892
|
+
*
|
|
893
|
+
* By class rather than by identity, so it covers a subclass - a signal
|
|
894
|
+
* context - and a context other than the walk's own, which is the shape the
|
|
895
|
+
* cross-context disclosure took.
|
|
896
|
+
*/
|
|
897
|
+
const refuseContextWrite = (target) => {
|
|
898
|
+
if (target instanceof EvalContext) {
|
|
899
|
+
throw new Error(`Write to the evaluation context is blocked for security reasons`);
|
|
900
|
+
}
|
|
901
|
+
};
|
|
902
|
+
/**
|
|
903
|
+
* Security: refuses an evaluation context **as a value**, by class.
|
|
904
|
+
*
|
|
905
|
+
* The companion to {@link refuseContextWrite}, and the load-bearing one: the
|
|
906
|
+
* context cannot be written to, and it cannot be obtained either. It is
|
|
907
|
+
* raised from the value funnel in `visitor-result.ts`, so it covers every
|
|
908
|
+
* value the walk pushes, whatever node pushed it.
|
|
909
|
+
*
|
|
910
|
+
* **It is at the funnel because a member read was not enough.** The first
|
|
911
|
+
* version refused a context only as a member read's object and value, and
|
|
912
|
+
* the step's second code review measured four ways round it, two of them
|
|
913
|
+
* pure language and needing nothing from the host: object spread
|
|
914
|
+
* (`({...c})._original`) and destructuring both copy the context's own
|
|
915
|
+
* enumerable fields into a plain object, from which the caller's object is an
|
|
916
|
+
* ordinary read; `Object.values` and `JSON.stringify` do the same with a
|
|
917
|
+
* host-supplied native; and a bare identifier simply returned the context to
|
|
918
|
+
* the consumer. The spread copy also handed out the **live** scope `Stack` by
|
|
919
|
+
* reference, so pushing onto its array poisoned every later evaluation on
|
|
920
|
+
* that context - the third stack invariant broken from outside any visitor.
|
|
921
|
+
*
|
|
922
|
+
* Refusing the value closes all of those at once, because each of them has to
|
|
923
|
+
* get the context onto the walk's stack first.
|
|
924
|
+
*
|
|
925
|
+
* The one value that may be a context is the positional `this`, which the
|
|
926
|
+
* member visitor consumes as its object and never returns; the funnel
|
|
927
|
+
* exempts exactly that node, and `this` anywhere else is already refused by
|
|
928
|
+
* `this-position.ts`.
|
|
929
|
+
*/
|
|
930
|
+
const refuseContextRead = () => {
|
|
931
|
+
throw new Error(`Access to the evaluation context is blocked for security reasons`);
|
|
932
|
+
};
|
|
855
933
|
/**
|
|
856
934
|
* Safely sets a property on an object with prototype pollution protection.
|
|
857
935
|
* Refuses a dangerous target or key, then assigns - so a setter runs, and a
|
|
@@ -866,6 +944,7 @@ const safeSetProperty = (target, key, value) => {
|
|
|
866
944
|
if (!target || (typeof target !== 'object' && typeof target !== 'function')) {
|
|
867
945
|
throw new Error(`Cannot set property on non-object: ${typeof target}`);
|
|
868
946
|
}
|
|
947
|
+
refuseContextWrite(target);
|
|
869
948
|
if (isDangerousProperty(key)) {
|
|
870
949
|
throw new Error(`Access to dangerous property "${String(key)}" is blocked for security reasons`);
|
|
871
950
|
}
|
|
@@ -1127,6 +1206,66 @@ function getPropertyLookupCacheStats() {
|
|
|
1127
1206
|
};
|
|
1128
1207
|
}
|
|
1129
1208
|
|
|
1209
|
+
/**
|
|
1210
|
+
* Security: `this` by position (`docs/a35/plan.md` § 8 q4, step 7).
|
|
1211
|
+
*
|
|
1212
|
+
* `this` is evaluated only as the object of a member access. There `this.k`,
|
|
1213
|
+
* `this["k"]` and `this.fn()` resolve `k` and `fn` against the context, as an
|
|
1214
|
+
* identifier does (`member-expression.ts`). Anywhere else it throws. Up to
|
|
1215
|
+
* 0.12.1 it evaluated to the walk's `EvalContext` wherever it was written,
|
|
1216
|
+
* which is the route `docs/backlog.md` A39 records.
|
|
1217
|
+
*
|
|
1218
|
+
* The position is a mark on the AST node, as the funnel's exemptions are
|
|
1219
|
+
* (`dangerous-values.ts`): `evaluateMember` marks its object before it visits
|
|
1220
|
+
* it, and the `this` and identifier visitors refuse a `this` that is not
|
|
1221
|
+
* marked. A mark records the node's place in the AST, which never changes, so
|
|
1222
|
+
* it is never cleared. Weak, so a cached AST that is dropped takes its marks
|
|
1223
|
+
* with it.
|
|
1224
|
+
*
|
|
1225
|
+
* Keyed by node object, so it assumes a tree, as `dangerous-values.ts`'s marks
|
|
1226
|
+
* do: a host-built AST that reused one `this` node as a member's object and
|
|
1227
|
+
* elsewhere would see the other place refused until the member had been
|
|
1228
|
+
* visited once, and allowed after.
|
|
1229
|
+
*/
|
|
1230
|
+
const positionalNodes = new WeakSet();
|
|
1231
|
+
/**
|
|
1232
|
+
* Marks a member's `object` when it can be a `this`: a
|
|
1233
|
+
* `ThisExpression`, or an identifier the identifier visitor may resolve as
|
|
1234
|
+
* `this`. Case-sensitively that is one spelt `this`, which only a host-built
|
|
1235
|
+
* AST holds. Under `caseInsensitive` it is any identifier, with no name test:
|
|
1236
|
+
* the visitor's own test, `equalIgnoreCase`, decides which is `this`, and a
|
|
1237
|
+
* mark is read only for a node it has decided is one. So the two cannot
|
|
1238
|
+
* disagree on a name, and a member access does not pay a locale comparison
|
|
1239
|
+
* per identifier object, which this path otherwise would.
|
|
1240
|
+
*
|
|
1241
|
+
* Under `preserveParens` the object's parentheses are stripped first, as the
|
|
1242
|
+
* write visitors strip a target's (`docs/backlog-retired.md` A2): `(this).k`
|
|
1243
|
+
* is the member read `this.k` is, and the walk passes through the parentheses
|
|
1244
|
+
* to the `this` inside. Nothing else is stripped, so in `(0, this).k` the
|
|
1245
|
+
* object is the sequence, and the `this` under it is refused.
|
|
1246
|
+
*
|
|
1247
|
+
* Runs once per member access, before its object is visited.
|
|
1248
|
+
*/
|
|
1249
|
+
const markThisPositional = (object, caseInsensitive) => {
|
|
1250
|
+
const node = unwrapParentheses(object);
|
|
1251
|
+
if (node.type === 'ThisExpression'
|
|
1252
|
+
|| (node.type === 'Identifier' && (caseInsensitive || node.name === 'this'))) {
|
|
1253
|
+
positionalNodes.add(node);
|
|
1254
|
+
}
|
|
1255
|
+
};
|
|
1256
|
+
/** Whether `node` is a `this` that {@link markThisPositional} marked. */
|
|
1257
|
+
const isThisPositional = (node) => positionalNodes.has(node);
|
|
1258
|
+
/**
|
|
1259
|
+
* Refuses a `this` that is not a member's object: as a value, an argument, an
|
|
1260
|
+
* operand, `typeof`'s operand included, or anything else that is not the
|
|
1261
|
+
* object of a member access.
|
|
1262
|
+
*/
|
|
1263
|
+
const refuseThisValue = (node) => {
|
|
1264
|
+
if (!isThisPositional(node)) {
|
|
1265
|
+
throw new Error('Access to "this" outside a member access is blocked for security reasons');
|
|
1266
|
+
}
|
|
1267
|
+
};
|
|
1268
|
+
|
|
1130
1269
|
// import { getCachedVisitorResult, setCachedVisitorResult } from './visitor-result-cache';
|
|
1131
1270
|
/**
|
|
1132
1271
|
* Reconstructs the dotted source path of a member chain (`a.b.c`).
|
|
@@ -1159,6 +1298,21 @@ const readPath = (node) => {
|
|
|
1159
1298
|
parts.unshift(current.name);
|
|
1160
1299
|
return parts.join('.');
|
|
1161
1300
|
};
|
|
1301
|
+
/*
|
|
1302
|
+
* **There is no by-class refusal of a context in this visitor, and its
|
|
1303
|
+
* removal is deliberate** (`docs/a35/plan.md` M20). One stood here, on the
|
|
1304
|
+
* member's object and on the value it read, and the value funnel
|
|
1305
|
+
* (`visitor-result.ts`) now covers both: a context is refused as it is
|
|
1306
|
+
* pushed, whatever node pushed it, so it cannot reach a member read at all -
|
|
1307
|
+
* the funnel's one exemption is the walk's own context in an object
|
|
1308
|
+
* position, which the branch below handles by identity.
|
|
1309
|
+
*
|
|
1310
|
+
* It was removed rather than kept as depth because it was measured on both
|
|
1311
|
+
* counts: removing it turned no case red (probe D of the § 9 matrix), and it
|
|
1312
|
+
* cost ~2.5% of the member-read path, which runs for every `a.b`. A check
|
|
1313
|
+
* that covers nothing and is not free is not depth, it is overhead that
|
|
1314
|
+
* reads like protection.
|
|
1315
|
+
*/
|
|
1162
1316
|
const memberExpressionVisitor = (node, st, callback) => {
|
|
1163
1317
|
beforeVisitor(node, st);
|
|
1164
1318
|
// Disable visitor result caching for now due to context sensitivity issues
|
|
@@ -1175,7 +1329,9 @@ const memberExpressionVisitor = (node, st, callback) => {
|
|
|
1175
1329
|
afterVisitor(node, st);
|
|
1176
1330
|
};
|
|
1177
1331
|
const evaluateMember = (node, st, callback) => {
|
|
1178
|
-
// Get the object
|
|
1332
|
+
// Get the object. A `this` is evaluated only here, as a member's object, so
|
|
1333
|
+
// it is marked before it is visited (`this-position.ts`).
|
|
1334
|
+
markThisPositional(node.object, !!st.options?.caseInsensitive);
|
|
1179
1335
|
callback(node.object, st);
|
|
1180
1336
|
const obj = popVisitorResult(node, st);
|
|
1181
1337
|
const object = (node.optional ? (obj || {}) : obj);
|
|
@@ -1225,7 +1381,14 @@ const evaluateMember = (node, st, callback) => {
|
|
|
1225
1381
|
value
|
|
1226
1382
|
});
|
|
1227
1383
|
}
|
|
1228
|
-
|
|
1384
|
+
// The fourth slot is the receiver's *origin*, not its identity: true means
|
|
1385
|
+
// the evaluator chose this receiver out of the context's own resolution
|
|
1386
|
+
// chain, which is what `callMember` guards (`context-receiver.ts`). The
|
|
1387
|
+
// identity alone cannot say it - `getThis` answers a key the original
|
|
1388
|
+
// holds with the original, which is an ordinary host object - so the fact
|
|
1389
|
+
// is carried from here, where it is known, rather than re-derived at the
|
|
1390
|
+
// call, where it is not (`docs/a35/plan.md` § 8 q7).
|
|
1391
|
+
return [thisValue ?? object, contextKey, value, true];
|
|
1229
1392
|
}
|
|
1230
1393
|
else if (object instanceof EvalScope) {
|
|
1231
1394
|
// A global scope reads its object as the context does, with a bare
|
|
@@ -1247,9 +1410,16 @@ const evaluateMember = (node, st, callback) => {
|
|
|
1247
1410
|
value
|
|
1248
1411
|
});
|
|
1249
1412
|
}
|
|
1250
|
-
|
|
1413
|
+
// A namespaced or global prior scope is not the context's own chain: its
|
|
1414
|
+
// object, or the `thisArg` its host declared, is the receiver it has
|
|
1415
|
+
// always been.
|
|
1416
|
+
return [thisValue ?? object, key, value, false];
|
|
1251
1417
|
}
|
|
1252
1418
|
else {
|
|
1419
|
+
// Not the walk's own context, and not a scope. A context cannot reach
|
|
1420
|
+
// here at all: the value funnel refuses one as it is pushed, and the one
|
|
1421
|
+
// it exempts is the walk's own, which the first branch took.
|
|
1422
|
+
//
|
|
1253
1423
|
// For regular objects, we need both case-insensitive lookup AND prototype pollution protection
|
|
1254
1424
|
let value;
|
|
1255
1425
|
// The key the lookup actually matched, reported to the read hooks. The
|
|
@@ -1347,7 +1517,7 @@ const evaluateMember = (node, st, callback) => {
|
|
|
1347
1517
|
value
|
|
1348
1518
|
});
|
|
1349
1519
|
}
|
|
1350
|
-
return [object, key, value];
|
|
1520
|
+
return [object, key, value, false];
|
|
1351
1521
|
}
|
|
1352
1522
|
};
|
|
1353
1523
|
|
|
@@ -1394,6 +1564,227 @@ const evaluateArray = (node, items, st, callback) => {
|
|
|
1394
1564
|
return elements;
|
|
1395
1565
|
};
|
|
1396
1566
|
|
|
1567
|
+
/**
|
|
1568
|
+
* `key` as the receiver's class defines it - unbound, so the caller supplies
|
|
1569
|
+
* the receiver and nothing is allocated per call.
|
|
1570
|
+
*
|
|
1571
|
+
* **The read starts at the prototype, which is the whole mechanism.** Reading
|
|
1572
|
+
* it off the receiver would find an own property first, and an own property is
|
|
1573
|
+
* what an expression can create. Starting one link up skips the instance and
|
|
1574
|
+
* finds what the class - or a subclass, since the read walks the chain from
|
|
1575
|
+
* there - actually defines.
|
|
1576
|
+
*
|
|
1577
|
+
* **Resolved per call, and not cached per prototype, which was measured.** A
|
|
1578
|
+
* cache keyed by prototype is one entry per class and reads cheaply, but it
|
|
1579
|
+
* freezes the class's methods at the first evaluation of the process: a
|
|
1580
|
+
* prototype patched afterwards is never seen again. That is not hypothetical
|
|
1581
|
+
* - `performance.spec.ts`'s read-emission guard spies on
|
|
1582
|
+
* `EvalContext.prototype.getKey`, and under the cached form the spy was
|
|
1583
|
+
* installed after the entry was taken and was never called, so a test that
|
|
1584
|
+
* asserts real behaviour went red for a reason that had nothing to do with it.
|
|
1585
|
+
* Worse, a spy installed *before* an entry was taken would have been cached
|
|
1586
|
+
* and outlived its own test. A property read off the prototype is cheaper than
|
|
1587
|
+
* the `WeakMap` lookup the cache needs anyway (M19), so the cache cost
|
|
1588
|
+
* correctness and bought nothing.
|
|
1589
|
+
*
|
|
1590
|
+
* The chain read can in principle invoke a prototype accessor, which the
|
|
1591
|
+
* descriptor walk this replaces could not. Nothing on the chain of either
|
|
1592
|
+
* class is an accessor under these names, and an expression cannot add one:
|
|
1593
|
+
* `__proto__`, `constructor` and `prototype` are refused by
|
|
1594
|
+
* `prototype-pollution-guard.ts`, as a member of `this` and through the view
|
|
1595
|
+
* alike. A non-function - an accessor's value included - yields undefined, and
|
|
1596
|
+
* the caller gives the named refusal rather than a raw `TypeError`.
|
|
1597
|
+
*
|
|
1598
|
+
* @param receiver - The context, or a scope of its chain.
|
|
1599
|
+
* @param key - The method name.
|
|
1600
|
+
* @returns The class's method, or undefined when the class defines none.
|
|
1601
|
+
*/
|
|
1602
|
+
const prototypeMethod = (receiver, key) => {
|
|
1603
|
+
const proto = Object.getPrototypeOf(receiver);
|
|
1604
|
+
if (proto === null) {
|
|
1605
|
+
return undefined;
|
|
1606
|
+
}
|
|
1607
|
+
const method = proto[key];
|
|
1608
|
+
return typeof method === 'function' ? method : undefined;
|
|
1609
|
+
};
|
|
1610
|
+
/**
|
|
1611
|
+
* The one refusal for a context member the evaluator will not reach: a key the
|
|
1612
|
+
* view does not forward, and a method the receiver's class does not define.
|
|
1613
|
+
*
|
|
1614
|
+
* Shared so the two cannot drift, and so neither ends as a raw `TypeError`
|
|
1615
|
+
* from calling undefined - which names nothing and reads as a library fault.
|
|
1616
|
+
*/
|
|
1617
|
+
const refuseContextAccess = (key) => {
|
|
1618
|
+
throw new Error(`Access to "${String(key)}" on the evaluation context is blocked for security reasons`);
|
|
1619
|
+
};
|
|
1620
|
+
/**
|
|
1621
|
+
* The resolved method, or the refusal.
|
|
1622
|
+
*
|
|
1623
|
+
* Every wrapper below then calls it with `.call` and its own fixed arity
|
|
1624
|
+
* rather than one shared `apply` over an argument array - which is tidier,
|
|
1625
|
+
* and **not** where the cost of this indirection is. Measured (M19): the
|
|
1626
|
+
* `apply` form and this one are within noise of each other, as are resolving
|
|
1627
|
+
* per call and caching the resolution per prototype. What costs is replacing a
|
|
1628
|
+
* monomorphic `context.get(k)` with a call through a value, which cannot be
|
|
1629
|
+
* inlined; see the plan's M19 for the four shapes measured and the decision
|
|
1630
|
+
* taken on them.
|
|
1631
|
+
*/
|
|
1632
|
+
const methodOf = (receiver, key) => {
|
|
1633
|
+
const method = prototypeMethod(receiver, key);
|
|
1634
|
+
if (!method) {
|
|
1635
|
+
return refuseContextAccess(key);
|
|
1636
|
+
}
|
|
1637
|
+
return method;
|
|
1638
|
+
};
|
|
1639
|
+
/**
|
|
1640
|
+
* `EvalContext.scopeHolding` as the class defines it - at the write path's
|
|
1641
|
+
* two sites, which is where this resolution is kept.
|
|
1642
|
+
*/
|
|
1643
|
+
const contextScopeHolding = (context, key) => methodOf(context, 'scopeHolding').call(context, key);
|
|
1644
|
+
/**
|
|
1645
|
+
* `EvalContext.set` as the class defines it - the method `eval-signals`
|
|
1646
|
+
* overrides to enforce its read-only policy, which is why resolving it off the
|
|
1647
|
+
* chain rather than off the instance matters twice over.
|
|
1648
|
+
*/
|
|
1649
|
+
const contextSet = (context, key, value) => {
|
|
1650
|
+
methodOf(context, 'set').call(context, key, value);
|
|
1651
|
+
};
|
|
1652
|
+
/**
|
|
1653
|
+
* `EvalContext.setInScope` as the class defines it.
|
|
1654
|
+
*/
|
|
1655
|
+
const contextSetInScope = (context, key, value) => methodOf(context, 'setInScope').call(context, key, value);
|
|
1656
|
+
/**
|
|
1657
|
+
* `EvalContext.push` as the class defines it, for a context that may be
|
|
1658
|
+
* undefined.
|
|
1659
|
+
*
|
|
1660
|
+
* The optional receiver is deliberate: every scope push in the evaluator is
|
|
1661
|
+
* written `st.context?.push(...)`, and its pop is paired with it by a
|
|
1662
|
+
* `finally`. Both wrappers no-op on an absent context, so a walk with no
|
|
1663
|
+
* context pushes nothing and pops nothing, exactly as the optional chain did.
|
|
1664
|
+
*/
|
|
1665
|
+
const contextPush = (context, scope, options) => {
|
|
1666
|
+
if (!context) {
|
|
1667
|
+
return;
|
|
1668
|
+
}
|
|
1669
|
+
// The pair is checked once per `EvalState`, not here - see
|
|
1670
|
+
// {@link refuseUnbalancedScopeMethods}. A scope push runs per block and
|
|
1671
|
+
// per loop iteration, and the context cannot change under a state, so
|
|
1672
|
+
// asking per push would pay for a guarantee that is already settled.
|
|
1673
|
+
methodOf(context, 'push').call(context, scope, options);
|
|
1674
|
+
};
|
|
1675
|
+
/**
|
|
1676
|
+
* Refuses a context whose class defines `push` but no `pop`, at the point
|
|
1677
|
+
* the state is built.
|
|
1678
|
+
*
|
|
1679
|
+
* **Because the pop cannot refuse for itself.** Every pop in the evaluator is
|
|
1680
|
+
* the `finally` of a `try` opened after its push, so a throw raised there
|
|
1681
|
+
* would replace whatever error the body was propagating; {@link contextPop}
|
|
1682
|
+
* is therefore tolerant. That tolerance has exactly one failure mode - a
|
|
1683
|
+
* class with `push` and no `pop` pushes a scope nothing ever pops, on a
|
|
1684
|
+
* context that may back any number of evaluations, with nothing thrown. The
|
|
1685
|
+
* scope stack is the one invariant of the three with no net anywhere else in
|
|
1686
|
+
* the system, so the pair is refused up front instead, where a throw costs
|
|
1687
|
+
* nothing and names the cause.
|
|
1688
|
+
*
|
|
1689
|
+
* No context `EvalContext.fromContext` produces has this shape; a hand-built
|
|
1690
|
+
* object cast to `EvalContext` does.
|
|
1691
|
+
*/
|
|
1692
|
+
const refuseUnbalancedScopeMethods = (context) => {
|
|
1693
|
+
if (context && prototypeMethod(context, 'push') && !prototypeMethod(context, 'pop')) {
|
|
1694
|
+
refuseContextAccess('pop');
|
|
1695
|
+
}
|
|
1696
|
+
};
|
|
1697
|
+
/**
|
|
1698
|
+
* `EvalContext.pop` as the class defines it, for a context that may be
|
|
1699
|
+
* undefined - see {@link contextPush} for why both take one.
|
|
1700
|
+
*/
|
|
1701
|
+
const contextPop = (context) => {
|
|
1702
|
+
if (!context) {
|
|
1703
|
+
return;
|
|
1704
|
+
}
|
|
1705
|
+
// Tolerant where {@link contextPush} refuses, and the asymmetry is the
|
|
1706
|
+
// point: every pop in the evaluator is the `finally` of a `try` opened
|
|
1707
|
+
// after its push, so a throw raised here would **replace** whatever error
|
|
1708
|
+
// the body was already propagating. A class that defines no `pop` defines
|
|
1709
|
+
// no `push` either in any shape this library builds, so nothing was pushed
|
|
1710
|
+
// and not popping is the correct outcome rather than a silent drift.
|
|
1711
|
+
const pop = prototypeMethod(context, 'pop');
|
|
1712
|
+
if (pop) {
|
|
1713
|
+
pop.call(context);
|
|
1714
|
+
}
|
|
1715
|
+
};
|
|
1716
|
+
/**
|
|
1717
|
+
* The context's member-write policy, as its class defines it.
|
|
1718
|
+
*
|
|
1719
|
+
* Unresolved is the documented opt-out and not a refusal: `EvalContext`
|
|
1720
|
+
* declares `checkMemberWrite` and defines nothing, and a context without a
|
|
1721
|
+
* policy is asked nothing. What is refused is a policy an *expression*
|
|
1722
|
+
* supplied - a planted own `checkMemberWrite` would otherwise be invoked with
|
|
1723
|
+
* the real context as its `this`, before every member write, and would see
|
|
1724
|
+
* every write the walk makes.
|
|
1725
|
+
*/
|
|
1726
|
+
const contextCheckMemberWrite = (context, write) => {
|
|
1727
|
+
if (!context) {
|
|
1728
|
+
return;
|
|
1729
|
+
}
|
|
1730
|
+
const policy = prototypeMethod(context, 'checkMemberWrite');
|
|
1731
|
+
if (policy) {
|
|
1732
|
+
policy.call(context, write);
|
|
1733
|
+
}
|
|
1734
|
+
};
|
|
1735
|
+
/**
|
|
1736
|
+
* Whether the context's class defines a member-write policy - the opt-in
|
|
1737
|
+
* `EvalState` decides `createdObjects` by.
|
|
1738
|
+
*
|
|
1739
|
+
* Asked of the class for the same reason the call is: a planted own
|
|
1740
|
+
* `checkMemberWrite` must not turn the walk's object recording on and then be
|
|
1741
|
+
* the policy that reads it.
|
|
1742
|
+
*/
|
|
1743
|
+
const hasMemberWritePolicy = (context) => !!context && prototypeMethod(context, 'checkMemberWrite') !== undefined;
|
|
1744
|
+
/**
|
|
1745
|
+
* Refuses a member-write policy held as an **instance property**, loudly,
|
|
1746
|
+
* when the context's class defines none.
|
|
1747
|
+
*
|
|
1748
|
+
* **Because the alternative is failing open in silence.** `checkMemberWrite`
|
|
1749
|
+
* is declared on `EvalContext` as an optional method, so supplying one on the
|
|
1750
|
+
* instance reads like the way to do it. Since the policy and the opt-in are
|
|
1751
|
+
* both taken from the class, such a policy is inert: no error, no
|
|
1752
|
+
* `createdObjects`, and every write it existed to refuse goes through. A
|
|
1753
|
+
* security opt-in that quietly does nothing is worse than a construction
|
|
1754
|
+
* error, so this is the error.
|
|
1755
|
+
*
|
|
1756
|
+
* **Three shapes are instance properties, and the message has to say so.** A
|
|
1757
|
+
* plain `context.checkMemberWrite = fn`; an assignment in a subclass's
|
|
1758
|
+
* constructor; and - the one an honest consumer is most likely to write - a
|
|
1759
|
+
* **class field**, `class C extends EvalContext { checkMemberWrite = (w) => … }`,
|
|
1760
|
+
* which TypeScript defines on the instance and not on the prototype. The
|
|
1761
|
+
* first version of this message told the author to declare it on a subclass,
|
|
1762
|
+
* which the class-field author had already done, so it sent them in a
|
|
1763
|
+
* circle. It now names the condition (an instance property) and the remedy (a
|
|
1764
|
+
* method), which is the distinction that matters.
|
|
1765
|
+
*
|
|
1766
|
+
* The security reason for reading the class does not apply to the party left
|
|
1767
|
+
* making this mistake. An *expression* can no longer supply one at all - the
|
|
1768
|
+
* write is refused by `prototype-pollution-guard.ts` - so whoever holds one
|
|
1769
|
+
* on an instance now is a host that meant it, and the right answer to a host
|
|
1770
|
+
* that meant it is to say so precisely.
|
|
1771
|
+
*
|
|
1772
|
+
* Raised from `EvalState`'s constructor, so it fires once per evaluation
|
|
1773
|
+
* built around such a context.
|
|
1774
|
+
*/
|
|
1775
|
+
const refuseInstanceMemberWritePolicy = (context) => {
|
|
1776
|
+
if (!context
|
|
1777
|
+
|| !Object.prototype.hasOwnProperty.call(context, 'checkMemberWrite')
|
|
1778
|
+
|| prototypeMethod(context, 'checkMemberWrite')) {
|
|
1779
|
+
return;
|
|
1780
|
+
}
|
|
1781
|
+
const className = context.constructor?.name ?? 'EvalContext';
|
|
1782
|
+
throw new Error(`"checkMemberWrite" is an instance property of this context - a class field, a constructor `
|
|
1783
|
+
+ `assignment, or a direct assignment - and the evaluator does not use one: the policy is `
|
|
1784
|
+
+ `resolved from the prototype chain. Declare it as a method on ${className}: `
|
|
1785
|
+
+ `class ${className} extends EvalContext { override checkMemberWrite(write) { ... } }`);
|
|
1786
|
+
};
|
|
1787
|
+
|
|
1397
1788
|
/**
|
|
1398
1789
|
* Whether `value` can be held in a `WeakSet` - an object or a function.
|
|
1399
1790
|
*/
|
|
@@ -1432,7 +1823,7 @@ const recordCreated = (created, value) => {
|
|
|
1432
1823
|
* @param key - The property about to be written.
|
|
1433
1824
|
*/
|
|
1434
1825
|
const consultMemberWrite = (st, created, target, key) => {
|
|
1435
|
-
st.context
|
|
1826
|
+
contextCheckMemberWrite(st.context, {
|
|
1436
1827
|
target,
|
|
1437
1828
|
key,
|
|
1438
1829
|
createdByEvaluation: isObjectLike(target) && created.has(target),
|
|
@@ -1482,6 +1873,107 @@ const FUNCTION_BIND = Function.prototype.bind;
|
|
|
1482
1873
|
* passes none for `null` or `undefined`, and throws for a primitive.
|
|
1483
1874
|
*/
|
|
1484
1875
|
const argumentsOf = (list) => isObjectLike(list) ? Array.prototype.slice.call(list) : [];
|
|
1876
|
+
/**
|
|
1877
|
+
* `Reflect`'s writers, which {@link MUTATING_BUILT_INS} does not list.
|
|
1878
|
+
*
|
|
1879
|
+
* That table decides what a context's `checkMemberWrite` is asked about, and
|
|
1880
|
+
* widening it would change what a published policy sees. This set feeds
|
|
1881
|
+
* {@link WRITE_SITES} alone, so the context refusal covers `Reflect` without
|
|
1882
|
+
* altering the policy's surface. Each of these writes its **first** argument,
|
|
1883
|
+
* as `Object`'s writers do.
|
|
1884
|
+
*/
|
|
1885
|
+
const REFLECT_WRITERS = new Set([
|
|
1886
|
+
Reflect.set,
|
|
1887
|
+
Reflect.defineProperty,
|
|
1888
|
+
Reflect.deleteProperty,
|
|
1889
|
+
Reflect.setPrototypeOf,
|
|
1890
|
+
Reflect.preventExtensions,
|
|
1891
|
+
]);
|
|
1892
|
+
const WRITE_SITES = (() => {
|
|
1893
|
+
const table = new Map();
|
|
1894
|
+
// **A later entry must never silently overwrite an earlier one.** Three
|
|
1895
|
+
// sources feed this table, and a function in two of them would take the
|
|
1896
|
+
// last writer's meaning: putting `Reflect.apply` into `REFLECT_WRITERS` -
|
|
1897
|
+
// an easy mistake, since it is a `Reflect` function whose first argument
|
|
1898
|
+
// is a target - would demote it from a hop to a writer and reopen C1
|
|
1899
|
+
// exactly, with no case able to show the cause. So a collision is a load
|
|
1900
|
+
// error rather than a silent demotion.
|
|
1901
|
+
const add = (fn, site) => {
|
|
1902
|
+
if (table.has(fn)) {
|
|
1903
|
+
throw new Error(`member-write-policy: ${String(fn)} is listed as two kinds of write site`);
|
|
1904
|
+
}
|
|
1905
|
+
table.set(fn, site);
|
|
1906
|
+
};
|
|
1907
|
+
add(FUNCTION_CALL, 0 /* Site.CallOrBind */);
|
|
1908
|
+
add(FUNCTION_BIND, 0 /* Site.CallOrBind */);
|
|
1909
|
+
add(FUNCTION_APPLY, 1 /* Site.Apply */);
|
|
1910
|
+
add(Reflect.apply, 2 /* Site.ReflectApply */);
|
|
1911
|
+
add(Reflect.construct, 3 /* Site.ReflectConstruct */);
|
|
1912
|
+
for (const [fn, builtIn] of MUTATING_BUILT_INS) {
|
|
1913
|
+
add(fn, builtIn.writes === 'receiver' ? 4 /* Site.WritesReceiver */ : 5 /* Site.WritesFirstArgument */);
|
|
1914
|
+
}
|
|
1915
|
+
for (const fn of REFLECT_WRITERS) {
|
|
1916
|
+
add(fn, 5 /* Site.WritesFirstArgument */);
|
|
1917
|
+
}
|
|
1918
|
+
return table;
|
|
1919
|
+
})();
|
|
1920
|
+
/**
|
|
1921
|
+
* Security: refuses a **native** write into an evaluation context, by class -
|
|
1922
|
+
* the other half of `refuseContextWrite` (`docs/a35/plan.md` Revision 16,
|
|
1923
|
+
* item 2's enumeration).
|
|
1924
|
+
*
|
|
1925
|
+
* `safeSetProperty` covers every write an expression makes through the
|
|
1926
|
+
* evaluator: dotted and computed member assignment, compound assignment and
|
|
1927
|
+
* the update expressions. It does not cover a write made by **native code**
|
|
1928
|
+
* the expression merely calls - `Object.assign(c, { push: f })`,
|
|
1929
|
+
* `Object.defineProperty`, `Reflect.set`, `Object.setPrototypeOf`,
|
|
1930
|
+
* `Object.freeze`, and each of them reached through `call`, `apply` or
|
|
1931
|
+
* `bind`. Measured, all of those planted an own property, replaced the
|
|
1932
|
+
* prototype or froze the context (the plan's X-a to X-g).
|
|
1933
|
+
*
|
|
1934
|
+
* **Reachable only where a host supplies both halves**: a native namespace -
|
|
1935
|
+
* `Object` or `Reflect` - in the evaluation data, *and* the context itself
|
|
1936
|
+
* under some key, since `this` outside a member access is refused by
|
|
1937
|
+
* `this-position.ts` and a context can no longer be captured. Narrow, and
|
|
1938
|
+
* still a write to the context, so it is refused rather than documented.
|
|
1939
|
+
*
|
|
1940
|
+
* Asked before every call, unlike the policy consultation below, which a
|
|
1941
|
+
* context without a write policy never reaches. The cost is one map lookup
|
|
1942
|
+
* and one set lookup per call.
|
|
1943
|
+
*
|
|
1944
|
+
* Not asked for a tag: a tagged template's first argument is always the
|
|
1945
|
+
* template's strings array, so no tag can hand a context to one of these.
|
|
1946
|
+
*/
|
|
1947
|
+
const refuseContextMethodWrite = (fn, receiver, args) => {
|
|
1948
|
+
const site = WRITE_SITES.get(fn);
|
|
1949
|
+
if (site === undefined) {
|
|
1950
|
+
return;
|
|
1951
|
+
}
|
|
1952
|
+
switch (site) {
|
|
1953
|
+
// `f.call(this, …)` and `f.bind(this, …)` take the function as their
|
|
1954
|
+
// receiver; `Reflect.apply(f, this, [...])` and `Reflect.construct` take
|
|
1955
|
+
// it as their first argument. Each resolves to the same question of the
|
|
1956
|
+
// function it is about to run.
|
|
1957
|
+
case 0 /* Site.CallOrBind */:
|
|
1958
|
+
refuseContextMethodWrite(receiver, args[0], args.slice(1));
|
|
1959
|
+
return;
|
|
1960
|
+
case 1 /* Site.Apply */:
|
|
1961
|
+
refuseContextMethodWrite(receiver, args[0], argumentsOf(args[1]));
|
|
1962
|
+
return;
|
|
1963
|
+
case 2 /* Site.ReflectApply */:
|
|
1964
|
+
refuseContextMethodWrite(args[0], args[1], argumentsOf(args[2]));
|
|
1965
|
+
return;
|
|
1966
|
+
case 3 /* Site.ReflectConstruct */:
|
|
1967
|
+
refuseContextMethodWrite(args[0], undefined, argumentsOf(args[1]));
|
|
1968
|
+
return;
|
|
1969
|
+
case 4 /* Site.WritesReceiver */:
|
|
1970
|
+
refuseContextWrite(receiver);
|
|
1971
|
+
return;
|
|
1972
|
+
case 5 /* Site.WritesFirstArgument */:
|
|
1973
|
+
refuseContextWrite(args[0]);
|
|
1974
|
+
return;
|
|
1975
|
+
}
|
|
1976
|
+
};
|
|
1485
1977
|
/**
|
|
1486
1978
|
* Asks the context whether a call may run, when the function is a built-in
|
|
1487
1979
|
* that writes into an object it is handed, through
|
|
@@ -1528,7 +2020,7 @@ const consultMethodWrite = (st, created, fn, receiver, args) => {
|
|
|
1528
2020
|
return;
|
|
1529
2021
|
}
|
|
1530
2022
|
const target = builtIn.writes === 'receiver' ? receiver : args[0];
|
|
1531
|
-
st.context
|
|
2023
|
+
contextCheckMemberWrite(st.context, {
|
|
1532
2024
|
target,
|
|
1533
2025
|
key: undefined,
|
|
1534
2026
|
createdByEvaluation: isObjectLike(target) && created.has(target),
|
|
@@ -1536,6 +2028,169 @@ const consultMethodWrite = (st, created, fn, receiver, args) => {
|
|
|
1536
2028
|
});
|
|
1537
2029
|
};
|
|
1538
2030
|
|
|
2031
|
+
/**
|
|
2032
|
+
* Security: the receiver every function the evaluator **resolves from the
|
|
2033
|
+
* context's chain** is called with (`docs/a35/plan.md` § 8 q6, step 8).
|
|
2034
|
+
*
|
|
2035
|
+
* Resolved from the chain, not held by the context: the distinction is the
|
|
2036
|
+
* step's code review, and it is not a nicety. A function an *expression*
|
|
2037
|
+
* planted on the context is held by it and is not resolved from its chain -
|
|
2038
|
+
* and such a plant is never called through this view at all. It is refused
|
|
2039
|
+
* before it can be: `context-method.ts` resolves every method the evaluator
|
|
2040
|
+
* invokes off the class, so the plant is not invoked, by the view or by the
|
|
2041
|
+
* evaluator (`docs/a35/plan.md` Revision 16).
|
|
2042
|
+
*
|
|
2043
|
+
* Four receivers used to hand a function the context or the object behind it:
|
|
2044
|
+
* a bare call's, a bare tag's, and `this.fn()`'s for a key a lookup resolves
|
|
2045
|
+
* (the `EvalContext`) or a key the original holds (the original itself). The
|
|
2046
|
+
* context exposes the caller's own object - as an own field, and through a
|
|
2047
|
+
* public getter on its prototype - so a native method that returns its `this`
|
|
2048
|
+
* returned it as data, and native code then reached whatever the host had put
|
|
2049
|
+
* in the context. No value passed through the walk's stack on that route, so
|
|
2050
|
+
* the funnel never saw it, and no `this` was written, so step 7's positional
|
|
2051
|
+
* refusal did not either. That is `docs/backlog.md` A39's second half.
|
|
2052
|
+
*
|
|
2053
|
+
* Each of those four now receives this view instead. Which receivers those
|
|
2054
|
+
* are is decided where it is known - `evaluateMember` carries the receiver's
|
|
2055
|
+
* origin out in its fourth slot - and not by an identity test at the call,
|
|
2056
|
+
* which can see the context but not the original (`docs/a35/plan.md` § 8 q7).
|
|
2057
|
+
*
|
|
2058
|
+
* **What it forwards, and why only that.** `push` and `pop` are documented to
|
|
2059
|
+
* consumers: `@zvenigora/ng-eval-signals`' README has a source function push a
|
|
2060
|
+
* scope onto the context through its `this`, and its async-signal suite
|
|
2061
|
+
* exercises it. Those two are taken from the context's class, by
|
|
2062
|
+
* {@link boundMethod}, and bound to the real context; nothing else is
|
|
2063
|
+
* reachable - which is the whole contract, and it is narrower than what
|
|
2064
|
+
* 0.12.1 handed out, so it ships as a breaking change.
|
|
2065
|
+
*
|
|
2066
|
+
* **The proxy target is a frozen stand-in, not the context, and that is the
|
|
2067
|
+
* design.** A proxy forwards every operation it does not trap to its target,
|
|
2068
|
+
* and a property read is not the only way out of an object: the prototype
|
|
2069
|
+
* carries the getter, `ownKeys` names the fields, and a descriptor carries a
|
|
2070
|
+
* field's value. Over the real context each of those would have to be trapped,
|
|
2071
|
+
* and the one forgotten would be the way through. Over an empty, frozen,
|
|
2072
|
+
* null-prototype target they answer with nothing by construction, so only
|
|
2073
|
+
* `get` and `has` are trapped and they answer from the allowlist alone. The
|
|
2074
|
+
* spec's B1, B2 and B3 cases are those three ways out, and its S cases run
|
|
2075
|
+
* them against each receiver. Nothing lands on the shared target either: a
|
|
2076
|
+
* write is refused because it is frozen, which is what makes sharing sound.
|
|
2077
|
+
*
|
|
2078
|
+
* **A read of anything else throws, rather than answering undefined.** Both
|
|
2079
|
+
* were measured against all three suites: the throw turned no pre-existing
|
|
2080
|
+
* case red anywhere, so it costs nothing a silent undefined would have saved,
|
|
2081
|
+
* and it fails loudly at the read instead of somewhere downstream where the
|
|
2082
|
+
* cause is no longer visible. A write throws too, which is the frozen
|
|
2083
|
+
* target's doing rather than a trap's; up to 0.12.1 it landed on the context.
|
|
2084
|
+
* Both are breaking, and the CHANGELOG entry states them.
|
|
2085
|
+
*
|
|
2086
|
+
* **Built fresh for every call, and retained by nothing.** So two calls - even
|
|
2087
|
+
* in one walk, on one context - receive two different views, and a function
|
|
2088
|
+
* must not key anything on its `this`. That is a decision taken from a
|
|
2089
|
+
* measurement (`docs/a35/plan.md` N-d): the first form cached one view per
|
|
2090
|
+
* context in a `WeakMap`, and on the ordinary one-shot path, where every
|
|
2091
|
+
* evaluation builds a fresh context, the cache never hit and its entries cost
|
|
2092
|
+
* 400-1,100 ns each to retain - a bare call ran ~45% slower than before the
|
|
2093
|
+
* guard. Every cached variant measured - a closure for the lookup, no `bind`,
|
|
2094
|
+
* no `Proxy` at all - stayed 37-59% above it, and isolated, a cached view of
|
|
2095
|
+
* any shape cost 400-1,100 ns against ~25 ns for an uncached `Proxy`. An
|
|
2096
|
+
* uncached view costs a `Proxy`, its handler and two trap closures, all
|
|
2097
|
+
* bounded and constant per call. The binds are deferred into the `get` trap:
|
|
2098
|
+
* most functions never read their `this`, so most views never bind anything.
|
|
2099
|
+
*
|
|
2100
|
+
* `push` and `pop` return `void`, which the allowlist relies on: a method
|
|
2101
|
+
* that returned the context, or a scope, would hand back by its result what
|
|
2102
|
+
* the view withholds by its traps. Check that before widening the list, and
|
|
2103
|
+
* note that {@link boundMethod} is what makes the two *be* those methods.
|
|
2104
|
+
*
|
|
2105
|
+
* **A view that escapes as an evaluation's result fails differently in the
|
|
2106
|
+
* two walks.** `evaluate` returns it, and the consumer's first read of it
|
|
2107
|
+
* throws. `evaluateAsync` runs its result through `awaitAllPromises`, which
|
|
2108
|
+
* tests `value.constructor` - a read, so the refusal is raised there instead,
|
|
2109
|
+
* naming `constructor` rather than anything the expression wrote. Neither
|
|
2110
|
+
* yields the context, so this is a difference in where the refusal surfaces
|
|
2111
|
+
* and not in what is withheld.
|
|
2112
|
+
*/
|
|
2113
|
+
/**
|
|
2114
|
+
* The proxy target: empty, frozen and null-prototyped, so every untrapped
|
|
2115
|
+
* operation answers with nothing of its own. Shared by every view, which is
|
|
2116
|
+
* sound because a proxy's identity comes from the proxy and not its target,
|
|
2117
|
+
* and nothing lands on it: a write is refused because it is frozen.
|
|
2118
|
+
*/
|
|
2119
|
+
const EMPTY_TARGET = Object.freeze(Object.create(null));
|
|
2120
|
+
/**
|
|
2121
|
+
* `push` or `pop` bound to the real context, as the context's **class**
|
|
2122
|
+
* defines it: `context-method.ts` walks the prototype chain and skips the
|
|
2123
|
+
* instance's own properties.
|
|
2124
|
+
*
|
|
2125
|
+
* **Reading `context[key]` instead would be a hole.** An own property shadows
|
|
2126
|
+
* the prototype's method, and an expression can create one - `this.k = v` is a
|
|
2127
|
+
* member write whose target is the `EvalContext` itself, and a plain context
|
|
2128
|
+
* consults no policy about it (`docs/backlog.md` A42). The view would then
|
|
2129
|
+
* forward whatever was planted, bound to the real context, and a host function
|
|
2130
|
+
* that returns what its `this.push(...)` returns would hand the context back
|
|
2131
|
+
* into the walk. That is the step's code review C1, and the chain walk is the
|
|
2132
|
+
* fix.
|
|
2133
|
+
*
|
|
2134
|
+
* That walk is shared with every site where the *evaluator itself* invokes a
|
|
2135
|
+
* method on the context, which the review's second finding is about: the view
|
|
2136
|
+
* was never the only caller, and a plant the view ignored was still invoked by
|
|
2137
|
+
* `program.ts`, `block-statement.ts`, `for-statement.ts` and
|
|
2138
|
+
* `arrow-function-expression.ts` - with the real context as its `this`. One
|
|
2139
|
+
* resolver answers for all of them (`docs/a35/plan.md` Revision 16).
|
|
2140
|
+
*/
|
|
2141
|
+
const boundMethod = (context, key) => prototypeMethod(context, key)?.bind(context);
|
|
2142
|
+
/**
|
|
2143
|
+
* A guarded view of `context` for one call. The methods the view forwards to
|
|
2144
|
+
* the real context are `push` and `pop`, and nothing else.
|
|
2145
|
+
*
|
|
2146
|
+
* @param context - The walk's context, or undefined when the state has none.
|
|
2147
|
+
* @returns A new view, or `context` unchanged when there is none to guard.
|
|
2148
|
+
*/
|
|
2149
|
+
const guardedReceiver = (context) => {
|
|
2150
|
+
if (!context) {
|
|
2151
|
+
return context;
|
|
2152
|
+
}
|
|
2153
|
+
// Resolved and bound on first use rather than here, and memoized per view.
|
|
2154
|
+
// **When it happens does not matter, and that is measured**: the expression
|
|
2155
|
+
// cannot reach the prototype chain at all, since `__proto__`, `constructor`
|
|
2156
|
+
// and `prototype` are refused by `prototype-pollution-guard.ts`, so what the
|
|
2157
|
+
// walk finds is the same whenever it looks. Doing it here instead cost every
|
|
2158
|
+
// bare call two resolutions and two `bind` allocations - ~15% on the
|
|
2159
|
+
// one-shot path, over the ~10% line this change is held to - while most
|
|
2160
|
+
// functions never read their `this` at all. The resolution itself is a
|
|
2161
|
+
// property read off the prototype (`context-method.ts`, where a cache was
|
|
2162
|
+
// measured and rejected); the binds are not, and they are what the
|
|
2163
|
+
// deferral saves.
|
|
2164
|
+
let push;
|
|
2165
|
+
let pop;
|
|
2166
|
+
return new Proxy(EMPTY_TARGET, {
|
|
2167
|
+
get: (_target, key) => {
|
|
2168
|
+
if (key === 'push') {
|
|
2169
|
+
push ??= boundMethod(context, 'push');
|
|
2170
|
+
if (push !== undefined) {
|
|
2171
|
+
return push;
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
else if (key === 'pop') {
|
|
2175
|
+
pop ??= boundMethod(context, 'pop');
|
|
2176
|
+
if (pop !== undefined) {
|
|
2177
|
+
return pop;
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
return refuseContextAccess(key);
|
|
2181
|
+
},
|
|
2182
|
+
has: (_target, key) => {
|
|
2183
|
+
if (key === 'push') {
|
|
2184
|
+
return (push ??= boundMethod(context, 'push')) !== undefined;
|
|
2185
|
+
}
|
|
2186
|
+
if (key === 'pop') {
|
|
2187
|
+
return (pop ??= boundMethod(context, 'pop')) !== undefined;
|
|
2188
|
+
}
|
|
2189
|
+
return false;
|
|
2190
|
+
},
|
|
2191
|
+
});
|
|
2192
|
+
};
|
|
2193
|
+
|
|
1539
2194
|
/**
|
|
1540
2195
|
* Security: List of dangerous functions that should not be callable
|
|
1541
2196
|
*
|
|
@@ -1693,23 +2348,50 @@ const scopeReceiver = (st, object) => {
|
|
|
1693
2348
|
* order: a call's arguments before its callee, a tag before its template's
|
|
1694
2349
|
* expressions.
|
|
1695
2350
|
*/
|
|
1696
|
-
const callMember = (st, [object, propertyName, fn], args, optional) => {
|
|
2351
|
+
const callMember = (st, [object, propertyName, fn, contextResolved], args, optional) => {
|
|
1697
2352
|
const functionName = typeof propertyName === 'string' ? propertyName : undefined;
|
|
1698
2353
|
const receiver = scopeReceiver(st, object);
|
|
1699
2354
|
const created = st.createdObjects;
|
|
1700
2355
|
if (created) {
|
|
2356
|
+
// The write policy is asked about the real write target, never the view:
|
|
2357
|
+
// a policy decides about the object that is written.
|
|
1701
2358
|
consultMethodWrite(st, created, fn, receiver, args);
|
|
1702
2359
|
}
|
|
1703
|
-
|
|
2360
|
+
// Then, whether or not the context polices writes: a native built-in
|
|
2361
|
+
// writing into an evaluation context is refused, because the context is
|
|
2362
|
+
// not the caller's to write (`member-write-policy.ts`). After the
|
|
2363
|
+
// consultation and not before, so a policy still sees the write it would
|
|
2364
|
+
// have seen and its own refusal is still the one that surfaces - the same
|
|
2365
|
+
// order the member-write path has, where `safeSetProperty` refuses after
|
|
2366
|
+
// `consultMemberWrite` has been asked.
|
|
2367
|
+
refuseContextMethodWrite(fn, receiver, args);
|
|
2368
|
+
// A receiver the evaluator resolved out of the context's own chain is
|
|
2369
|
+
// guarded (`context-receiver.ts`): the context itself, for a key only a
|
|
2370
|
+
// lookup resolves, and the original, for a key the original holds. Both
|
|
2371
|
+
// reach `this.fn()`, and guarding only the first left the second open -
|
|
2372
|
+
// the same route one token shorter (`docs/a35/plan.md` § 8 q7, R4).
|
|
2373
|
+
//
|
|
2374
|
+
// A prior scope's declared `thisArg`, and a plain object's own receiver,
|
|
2375
|
+
// are untouched: the host chose those, and `obj.fn()` and `ns.fn()` call
|
|
2376
|
+
// what they always did.
|
|
2377
|
+
const fromContextChain = contextResolved === true
|
|
2378
|
+
&& (receiver === st.context || receiver === st.context?.original);
|
|
2379
|
+
const thisArg = fromContextChain ? guardedReceiver(st.context) : receiver;
|
|
2380
|
+
return safeCall(fn, thisArg, args, optional, functionName);
|
|
1704
2381
|
};
|
|
1705
2382
|
/**
|
|
1706
2383
|
* Enhanced call expression visitor with security checks
|
|
1707
2384
|
*
|
|
1708
2385
|
* A context with a member-write policy is asked, before the call, about a
|
|
1709
2386
|
* built-in that writes into its receiver or its first argument - see
|
|
1710
|
-
* `consultMethodWrite`. The receiver asked about is the
|
|
1711
|
-
*
|
|
1712
|
-
*
|
|
2387
|
+
* `consultMethodWrite`. The receiver asked about is the write target: the
|
|
2388
|
+
* member's object, a prior scope's `thisArg`, or the context itself for a
|
|
2389
|
+
* bare call. A context without a policy pays one field read per call.
|
|
2390
|
+
*
|
|
2391
|
+
* That is no longer the same object the call is made with. A bare call's
|
|
2392
|
+
* `this` is a guarded view of the context (`context-receiver.ts`), while the
|
|
2393
|
+
* policy keeps the real one - a policy decides about the object that is
|
|
2394
|
+
* written, and the view is not it.
|
|
1713
2395
|
*/
|
|
1714
2396
|
const callExpressionVisitor = (node, st, callback) => {
|
|
1715
2397
|
beforeVisitor(node, st);
|
|
@@ -1729,31 +2411,30 @@ const callExpressionVisitor = (node, st, callback) => {
|
|
|
1729
2411
|
if (node.callee.type === 'Identifier') {
|
|
1730
2412
|
functionName = node.callee.name;
|
|
1731
2413
|
}
|
|
2414
|
+
// A bare call's `this` is a guarded view, not the context itself
|
|
2415
|
+
// (`context-receiver.ts`). Built once, and asked about with the same
|
|
2416
|
+
// object the call will use.
|
|
2417
|
+
const thisArg = guardedReceiver(st.context);
|
|
1732
2418
|
const created = st.createdObjects;
|
|
1733
2419
|
if (created) {
|
|
2420
|
+
// The policy is asked about the **real** context, which is its
|
|
2421
|
+
// published contract: a policy decides about the object that would be
|
|
2422
|
+
// written, and `member-write-policy.methods.spec.ts` pins that target.
|
|
1734
2423
|
consultMethodWrite(st, created, caller, st.context, args);
|
|
1735
2424
|
}
|
|
1736
|
-
|
|
2425
|
+
// The refusal is asked about `thisArg`, which is what the call will
|
|
2426
|
+
// actually receive. Passing the real context here instead would refuse a
|
|
2427
|
+
// write that was never going to reach it - a receiver-writing built-in
|
|
2428
|
+
// bare-called gets the view, whose frozen target refuses the write as
|
|
2429
|
+
// step 8 documents - and would report it under the context's name. An
|
|
2430
|
+
// argument that *is* a context is still caught, since a first-argument
|
|
2431
|
+
// writer is matched on `args` whatever its receiver.
|
|
2432
|
+
refuseContextMethodWrite(caller, thisArg, args);
|
|
2433
|
+
const value = safeCall(caller, thisArg, args, node.optional, functionName);
|
|
1737
2434
|
pushVisitorResult(node, st, value);
|
|
1738
2435
|
}
|
|
1739
2436
|
afterVisitor(node, st);
|
|
1740
2437
|
};
|
|
1741
|
-
const evaluateCall = (node, st, callback) => {
|
|
1742
|
-
callback(node.callee, st);
|
|
1743
|
-
const caller = popVisitorResult(node, st);
|
|
1744
|
-
const args = node.arguments.map((argument) => {
|
|
1745
|
-
callback(argument, st);
|
|
1746
|
-
const value = popVisitorResult(node, st);
|
|
1747
|
-
return value;
|
|
1748
|
-
});
|
|
1749
|
-
// Get function name for security checks
|
|
1750
|
-
let functionName;
|
|
1751
|
-
if (node.callee.type === 'Identifier') {
|
|
1752
|
-
functionName = node.callee.name;
|
|
1753
|
-
}
|
|
1754
|
-
const value = safeCall(caller, st.context, args, false, functionName);
|
|
1755
|
-
return value;
|
|
1756
|
-
};
|
|
1757
2438
|
|
|
1758
2439
|
/**
|
|
1759
2440
|
* The prototype of a function literal of each kind, read from this module's
|
|
@@ -1957,11 +2638,13 @@ const readName = (node, fn) => {
|
|
|
1957
2638
|
*
|
|
1958
2639
|
* It sees only what passes through the walk's own stack. A value that reaches
|
|
1959
2640
|
* native code without being pushed is not seen: the elements of an array the
|
|
1960
|
-
* host built, spread into a call,
|
|
1961
|
-
*
|
|
2641
|
+
* host built, spread into a call, which stays a documented limit. The other
|
|
2642
|
+
* such route, through `this` and the receiver of a call
|
|
2643
|
+
* (`docs/backlog-retired.md` A39), is closed in 0.13.0 elsewhere - by
|
|
2644
|
+
* `this-position.ts` and `context-receiver.ts` - not by this check.
|
|
1962
2645
|
*
|
|
1963
2646
|
* Up to 0.12.0 nothing refused such a read: `["1+1"].map(eval)` gave `[2]`
|
|
1964
|
-
* (`docs/backlog.md` A35).
|
|
2647
|
+
* (`docs/backlog-retired.md` A35).
|
|
1965
2648
|
*/
|
|
1966
2649
|
const refuseDangerousRead = (node, value) => {
|
|
1967
2650
|
if (exemptNodes.has(node) || !isDangerousValue(value)) {
|
|
@@ -1970,6 +2653,9 @@ const refuseDangerousRead = (node, value) => {
|
|
|
1970
2653
|
throw new Error(`Access to dangerous function "${readName(node, value)}" is blocked for security reasons`);
|
|
1971
2654
|
};
|
|
1972
2655
|
|
|
2656
|
+
// Direct, not through the `classes/eval` barrel, as
|
|
2657
|
+
// `prototype-pollution-guard.ts` takes it: `instanceof` needs the class and
|
|
2658
|
+
// not its type, and the narrow edge is the one this family uses.
|
|
1973
2659
|
/**
|
|
1974
2660
|
* The two push helpers each carry their own trace-bound call rather than
|
|
1975
2661
|
* sharing one.
|
|
@@ -2003,6 +2689,35 @@ const pushVisitorResult = (node, st, value) => {
|
|
|
2003
2689
|
if (typeof value === 'function') {
|
|
2004
2690
|
refuseDangerousRead(node, value);
|
|
2005
2691
|
}
|
|
2692
|
+
else if (value instanceof EvalContext
|
|
2693
|
+
&& !(value === st.context && isThisPositional(node))) {
|
|
2694
|
+
// An `EvalContext` is not a value an expression may hold
|
|
2695
|
+
// (`prototype-pollution-guard.ts`'s `refuseContextRead`).
|
|
2696
|
+
//
|
|
2697
|
+
// **The exemption is the walk's own context in a member's object
|
|
2698
|
+
// position**, which the member visitor consumes as its object and never
|
|
2699
|
+
// hands on. Both halves are required, and the identity half is the one
|
|
2700
|
+
// the step's third code review added: `markThisPositional` marks a
|
|
2701
|
+
// member's object whenever it *could* resolve to `this`, and under
|
|
2702
|
+
// `caseInsensitive` that is every identifier, with no name test - for a
|
|
2703
|
+
// reason of its own, to keep a locale comparison off the identifier
|
|
2704
|
+
// path. Keyed on the node alone, the exemption was therefore as wide as
|
|
2705
|
+
// that mark, and a host-supplied *foreign* context named before a `.`
|
|
2706
|
+
// passed the funnel unrefused. Keyed on the value too, it is exactly the
|
|
2707
|
+
// one context that has nowhere else to go.
|
|
2708
|
+
//
|
|
2709
|
+
// The `instanceof` is the gate, so the identity compare and the
|
|
2710
|
+
// `WeakSet` lookup run only for a value that is a context at all.
|
|
2711
|
+
//
|
|
2712
|
+
// **This is the only boundary, with nothing behind it.** A by-class
|
|
2713
|
+
// refusal in `member-expression.ts` used to stand second, and it was
|
|
2714
|
+
// removed once this condition made it unreachable and it was measured at
|
|
2715
|
+
// ~2.5% of every member read (`docs/a35/plan.md` M20). So a change to
|
|
2716
|
+
// the condition on this line is a disclosure, not a narrowing, and its
|
|
2717
|
+
// only tripwires are L10 and L11 of
|
|
2718
|
+
// `eval.service.context-write.spec.ts`.
|
|
2719
|
+
refuseContextRead();
|
|
2720
|
+
}
|
|
2006
2721
|
const result = st.result;
|
|
2007
2722
|
result.stack.push(value);
|
|
2008
2723
|
result.addTraceBounded(node, value, st.maxTraceItems);
|
|
@@ -2124,6 +2839,12 @@ const identifierVisitor = (node, st) => {
|
|
|
2124
2839
|
refuseDangerousName(node);
|
|
2125
2840
|
const context = st.context;
|
|
2126
2841
|
const isThis = node.name === 'this';
|
|
2842
|
+
// `this` written as a name, which only a host-built AST holds here, is
|
|
2843
|
+
// refused outside a member's object, as the `this` visitor refuses it
|
|
2844
|
+
// (`this-position.ts`).
|
|
2845
|
+
if (isThis) {
|
|
2846
|
+
refuseThisValue(node);
|
|
2847
|
+
}
|
|
2127
2848
|
const value = isThis
|
|
2128
2849
|
? st.context
|
|
2129
2850
|
: context?.get(node.name);
|
|
@@ -2141,7 +2862,12 @@ const identifierVisitorCaseInsensitive = (node, st) => {
|
|
|
2141
2862
|
refuseDangerousName(node);
|
|
2142
2863
|
const context = st.context;
|
|
2143
2864
|
const isThis = !!equalIgnoreCase(node.name, 'this');
|
|
2144
|
-
|
|
2865
|
+
// `This` is refused outside a member's object, as `this` is
|
|
2866
|
+
// (`this-position.ts`).
|
|
2867
|
+
if (isThis) {
|
|
2868
|
+
refuseThisValue(node);
|
|
2869
|
+
}
|
|
2870
|
+
else {
|
|
2145
2871
|
refuseDangerousMatch(node, st);
|
|
2146
2872
|
}
|
|
2147
2873
|
const value = isThis
|
|
@@ -2488,6 +3214,9 @@ const logicalExpressionVisitor = (node, st, callback) => {
|
|
|
2488
3214
|
|
|
2489
3215
|
const thisExpressionVisitor = (node, st) => {
|
|
2490
3216
|
beforeVisitor(node, st);
|
|
3217
|
+
// Only a member's object pushes the context; anywhere else `this` is
|
|
3218
|
+
// refused (`this-position.ts`).
|
|
3219
|
+
refuseThisValue(node);
|
|
2491
3220
|
const value = st.context;
|
|
2492
3221
|
pushVisitorResult(node, st, value);
|
|
2493
3222
|
afterVisitor(node, st);
|
|
@@ -2628,8 +3357,9 @@ const templateArguments = (node, st, callback) => {
|
|
|
2628
3357
|
* `MemberExpression` is evaluated by `evaluateMember`, not visited, so it
|
|
2629
3358
|
* raises no `before` or `after` node-hook events and leaves no trace entry,
|
|
2630
3359
|
* as a member callee does. Its read event still fires.
|
|
2631
|
-
* - Any other tag is called with the context as `this`, as
|
|
2632
|
-
* and checked under its
|
|
3360
|
+
* - Any other tag is called with a guarded view of the context as `this`, as
|
|
3361
|
+
* a bare call is (`context-receiver.ts`), and checked under its
|
|
3362
|
+
* identifier's name, or none. Up to 0.12.1 that was the context itself.
|
|
2633
3363
|
*
|
|
2634
3364
|
* The tag is evaluated before the template's expressions, as in JavaScript;
|
|
2635
3365
|
* a call evaluates its arguments first.
|
|
@@ -2657,7 +3387,9 @@ const taggedTemplateExpressionVisitor = (node, st, callback) => {
|
|
|
2657
3387
|
if (created) {
|
|
2658
3388
|
consultMethodWrite(st, created, tag, st.context, args);
|
|
2659
3389
|
}
|
|
2660
|
-
|
|
3390
|
+
// A bare tag's `this` is a guarded view, as a bare call's is
|
|
3391
|
+
// (`context-receiver.ts`); the write policy above keeps the real context.
|
|
3392
|
+
const value = safeCall(tag, guardedReceiver(st.context), args, false, tagName);
|
|
2661
3393
|
pushVisitorResult(node, st, value);
|
|
2662
3394
|
}
|
|
2663
3395
|
afterVisitor(node, st);
|
|
@@ -3191,15 +3923,15 @@ const assignToBinding = (st, key, value, name) => {
|
|
|
3191
3923
|
if (!st.context) {
|
|
3192
3924
|
throw new Error(`Context is not set.`);
|
|
3193
3925
|
}
|
|
3194
|
-
const scope = st.context
|
|
3926
|
+
const scope = contextScopeHolding(st.context, key);
|
|
3195
3927
|
if (!scope) {
|
|
3196
|
-
st.context
|
|
3928
|
+
contextSet(st.context, key, value);
|
|
3197
3929
|
return;
|
|
3198
3930
|
}
|
|
3199
3931
|
if (st.isConstBinding(scope, key)) {
|
|
3200
3932
|
throw new Error(`Assignment to constant variable "${name}".`);
|
|
3201
3933
|
}
|
|
3202
|
-
st.context
|
|
3934
|
+
contextSetInScope(st.context, key, value);
|
|
3203
3935
|
};
|
|
3204
3936
|
|
|
3205
3937
|
const assignmentOperators = {
|
|
@@ -3846,16 +4578,23 @@ const arrowFunctionExpressionVisitor = (node, st, callback) => {
|
|
|
3846
4578
|
// evaluation on the same context. The body is re-entrant `evaluate`, which
|
|
3847
4579
|
// rethrows, so the pop needs `finally`.
|
|
3848
4580
|
//
|
|
3849
|
-
// The `try` opens *after* the push, never around it. `
|
|
3850
|
-
//
|
|
3851
|
-
//
|
|
3852
|
-
// evaluatePatterns into a pop as well.
|
|
3853
|
-
|
|
4581
|
+
// The `try` opens *after* the push, never around it. `contextPush` skips
|
|
4582
|
+
// an absent context, as the optional chain it replaced did: bringing it
|
|
4583
|
+
// inside would pair a `finally` pop with a push that never happened, and
|
|
4584
|
+
// would swallow a throw from evaluatePatterns into a pop as well.
|
|
4585
|
+
//
|
|
4586
|
+
// Both take the method from the context's class (`context-method.ts`).
|
|
4587
|
+
// This site is the one an expression could reach twice over: a planted
|
|
4588
|
+
// own `push` would be invoked here with the real context as its `this`,
|
|
4589
|
+
// and - since this closure's body is a re-entrant `evaluate` that pushes
|
|
4590
|
+
// again - a plant that evaluates anything recurses until the stack is
|
|
4591
|
+
// gone, leaving a reused context unusable (`docs/backlog.md` A42).
|
|
4592
|
+
contextPush(st.context, newContext);
|
|
3854
4593
|
try {
|
|
3855
4594
|
return evaluate(node.body, st);
|
|
3856
4595
|
}
|
|
3857
4596
|
finally {
|
|
3858
|
-
st.context
|
|
4597
|
+
contextPop(st.context);
|
|
3859
4598
|
}
|
|
3860
4599
|
};
|
|
3861
4600
|
// A new function per evaluation of this node, so recorded under a
|
|
@@ -3949,14 +4688,19 @@ const dispatchStatement = (statement, st, callback) => {
|
|
|
3949
4688
|
* on the per-walk `EvalState`, and one context can back any number of
|
|
3950
4689
|
* evaluations: a scope this visitor failed to pop would outlive the walk and be
|
|
3951
4690
|
* read first by every later evaluation on that context. The `try` opens *after*
|
|
3952
|
-
* the push, never around it - `
|
|
3953
|
-
*
|
|
3954
|
-
*
|
|
4691
|
+
* the push, never around it - `contextPush` skips an absent context, as the
|
|
4692
|
+
* optional chain it replaced did, so bringing it inside would pair a `finally`
|
|
4693
|
+
* pop with a push that never happened.
|
|
4694
|
+
*
|
|
4695
|
+
* Both go through `context-method.ts`, which takes `push` and `pop` from the
|
|
4696
|
+
* context's class: an expression can plant an own `push` on the context
|
|
4697
|
+
* (`docs/backlog.md` A42), and this site would otherwise invoke it with the
|
|
4698
|
+
* real context as its `this`.
|
|
3955
4699
|
*/
|
|
3956
4700
|
const programVisitor = (node, st, callback) => {
|
|
3957
4701
|
beforeVisitor(node, st);
|
|
3958
4702
|
let completion = EMPTY_COMPLETION;
|
|
3959
|
-
st.context
|
|
4703
|
+
contextPush(st.context, {}, st.options);
|
|
3960
4704
|
try {
|
|
3961
4705
|
for (const statement of node.body) {
|
|
3962
4706
|
const value = dispatchStatement(statement, st, callback);
|
|
@@ -3966,7 +4710,7 @@ const programVisitor = (node, st, callback) => {
|
|
|
3966
4710
|
}
|
|
3967
4711
|
}
|
|
3968
4712
|
finally {
|
|
3969
|
-
st.context
|
|
4713
|
+
contextPop(st.context);
|
|
3970
4714
|
}
|
|
3971
4715
|
pushVisitorResult(node, st, completion);
|
|
3972
4716
|
afterVisitor(node, st);
|
|
@@ -4038,14 +4782,15 @@ const emptyStatementVisitor = (node, st) => {
|
|
|
4038
4782
|
* on the per-walk `EvalState`, and one context can back any number of
|
|
4039
4783
|
* evaluations: a scope this visitor failed to pop would outlive the walk and be
|
|
4040
4784
|
* read first by every later evaluation on that context. The `try` opens *after*
|
|
4041
|
-
* the push, never around it - `
|
|
4042
|
-
*
|
|
4043
|
-
*
|
|
4785
|
+
* the push, never around it - `contextPush` skips an absent context, as the
|
|
4786
|
+
* optional chain it replaced did, so bringing it inside would pair a `finally`
|
|
4787
|
+
* pop with a push that never happened. Both take the method from the context's
|
|
4788
|
+
* class rather than from the instance (`context-method.ts`).
|
|
4044
4789
|
*/
|
|
4045
4790
|
const blockStatementVisitor = (node, st, callback) => {
|
|
4046
4791
|
beforeVisitor(node, st);
|
|
4047
4792
|
let completion = EMPTY_COMPLETION;
|
|
4048
|
-
st.context
|
|
4793
|
+
contextPush(st.context, {}, st.options);
|
|
4049
4794
|
try {
|
|
4050
4795
|
for (const statement of node.body) {
|
|
4051
4796
|
const value = dispatchStatement(statement, st, callback);
|
|
@@ -4055,7 +4800,7 @@ const blockStatementVisitor = (node, st, callback) => {
|
|
|
4055
4800
|
}
|
|
4056
4801
|
}
|
|
4057
4802
|
finally {
|
|
4058
|
-
st.context
|
|
4803
|
+
contextPop(st.context);
|
|
4059
4804
|
}
|
|
4060
4805
|
pushVisitorResult(node, st, completion);
|
|
4061
4806
|
afterVisitor(node, st);
|
|
@@ -4200,14 +4945,16 @@ const ifStatementVisitor = (node, st, callback) => {
|
|
|
4200
4945
|
* precondition argument - a `for` body throwing on iteration 3 - so every exit
|
|
4201
4946
|
* path matters: `init`, `test`, `body` and `update` can each throw, the
|
|
4202
4947
|
* dispatcher can reject the body, and the budget can end the loop from inside
|
|
4203
|
-
* the `try`. The `try` opens *after* the push, never around it - `
|
|
4204
|
-
*
|
|
4205
|
-
*
|
|
4948
|
+
* the `try`. The `try` opens *after* the push, never around it - `contextPush`
|
|
4949
|
+
* skips an absent context, as the optional chain it replaced did, so bringing
|
|
4950
|
+
* it inside would pair a `finally` pop with a push that never happened. Both
|
|
4951
|
+
* take the method from the context's class rather than from the instance
|
|
4952
|
+
* (`context-method.ts`).
|
|
4206
4953
|
*/
|
|
4207
4954
|
const forStatementVisitor = (node, st, callback) => {
|
|
4208
4955
|
beforeVisitor(node, st);
|
|
4209
4956
|
let completion = EMPTY_COMPLETION;
|
|
4210
|
-
st.context
|
|
4957
|
+
contextPush(st.context, {}, st.options);
|
|
4211
4958
|
try {
|
|
4212
4959
|
if (node.init) {
|
|
4213
4960
|
if (node.init.type === 'VariableDeclaration') {
|
|
@@ -4240,7 +4987,7 @@ const forStatementVisitor = (node, st, callback) => {
|
|
|
4240
4987
|
}
|
|
4241
4988
|
}
|
|
4242
4989
|
finally {
|
|
4243
|
-
st.context
|
|
4990
|
+
contextPop(st.context);
|
|
4244
4991
|
}
|
|
4245
4992
|
pushVisitorResult(node, st, completion);
|
|
4246
4993
|
afterVisitor(node, st);
|
|
@@ -4751,7 +5498,7 @@ class EvalContext {
|
|
|
4751
5498
|
* @returns True when a pushed scope bound the key and received the write.
|
|
4752
5499
|
*/
|
|
4753
5500
|
setInScope(key, value) {
|
|
4754
|
-
const scope = this
|
|
5501
|
+
const scope = contextScopeHolding(this, key);
|
|
4755
5502
|
if (!scope) {
|
|
4756
5503
|
return false;
|
|
4757
5504
|
}
|
|
@@ -6025,7 +6772,15 @@ class EvalState {
|
|
|
6025
6772
|
this._options = options;
|
|
6026
6773
|
this._isAsync = isAsync;
|
|
6027
6774
|
this._hooks = adoptHooks(options);
|
|
6028
|
-
|
|
6775
|
+
// Asked of the context's class, not of the instance: an expression can
|
|
6776
|
+
// plant an own `checkMemberWrite` (`docs/backlog-retired.md` A42), and the
|
|
6777
|
+
// opt-in has to agree with what `member-write-policy.ts` will actually
|
|
6778
|
+
// call - which is the class's policy or none (`context-method.ts`). A
|
|
6779
|
+
// policy a *host* assigned to the instance would therefore be inert, so
|
|
6780
|
+
// it is refused here rather than ignored.
|
|
6781
|
+
refuseInstanceMemberWritePolicy(context);
|
|
6782
|
+
refuseUnbalancedScopeMethods(context);
|
|
6783
|
+
this._createdObjects = hasMemberWritePolicy(context)
|
|
6029
6784
|
? new WeakSet()
|
|
6030
6785
|
: undefined;
|
|
6031
6786
|
// Options configure the registry this state owns, never an adopted one -
|