@zvenigora/ng-eval-core 0.12.0 → 0.13.0
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 +257 -1
- package/fesm2022/zvenigora-ng-eval-core.mjs +1639 -550
- package/fesm2022/zvenigora-ng-eval-core.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -694,50 +694,6 @@ const setContextValue = (context, key, value) => {
|
|
|
694
694
|
}
|
|
695
695
|
};
|
|
696
696
|
|
|
697
|
-
/**
|
|
698
|
-
* The two push helpers each carry their own trace-bound call rather than
|
|
699
|
-
* sharing one.
|
|
700
|
-
*
|
|
701
|
-
* **Why the read is at these two sites.** `st.maxTraceItems` is the *walk's*
|
|
702
|
-
* bound. `EvalResult` cannot read it for itself: it holds a context, and a
|
|
703
|
-
* context's options are not the walk's - the split `CLAUDE.md` documents for
|
|
704
|
-
* `caseInsensitive`. So the option is read where `st` is in hand and passed in.
|
|
705
|
-
*
|
|
706
|
-
* **Why the one line is duplicated rather than factored.** A shared helper put
|
|
707
|
-
* a third frame on a path taken once per node - `push` -> helper ->
|
|
708
|
-
* `addTraceBounded` -> `EvalTrace.add` - to save one identical line. Measured
|
|
709
|
-
* both ways at 50,000 walks of `2 + 3 * a`, the difference was inside
|
|
710
|
-
* run-to-run noise, so this is not a speed claim; it is that the helper cost a
|
|
711
|
-
* frame on the hot path and bought nothing, and that `addTraceBounded`'s own
|
|
712
|
-
* docblock claims one call per push, which only this shape makes true.
|
|
713
|
-
*
|
|
714
|
-
* **The value stack is not bounded and must not be.** It is how visitors return
|
|
715
|
-
* values to each other, and dropping an entry desynchronises every node above
|
|
716
|
-
* it. Only the trace - which nothing in this library reads - is capped.
|
|
717
|
-
*/
|
|
718
|
-
const pushVisitorResult = (node, st, value) => {
|
|
719
|
-
const result = st.result;
|
|
720
|
-
result.stack.push(value);
|
|
721
|
-
result.addTraceBounded(node, value, st.maxTraceItems);
|
|
722
|
-
return value;
|
|
723
|
-
};
|
|
724
|
-
const popVisitorResult = (node, st) => {
|
|
725
|
-
const result = st.result;
|
|
726
|
-
const value = result.stack.pop();
|
|
727
|
-
return value;
|
|
728
|
-
};
|
|
729
|
-
const pushVisitorResultAsync = (node, st, value) => {
|
|
730
|
-
const result = st.result;
|
|
731
|
-
result.stack.push(value);
|
|
732
|
-
result.addTraceBounded(node, value, st.maxTraceItems);
|
|
733
|
-
return value;
|
|
734
|
-
};
|
|
735
|
-
const popVisitorResultAsync = (node, st) => {
|
|
736
|
-
const result = st.result;
|
|
737
|
-
const value = result.stack.pop();
|
|
738
|
-
return value;
|
|
739
|
-
};
|
|
740
|
-
|
|
741
697
|
/**
|
|
742
698
|
* Opens a node: records it as open and fires the registered 'before' hooks.
|
|
743
699
|
*
|
|
@@ -775,80 +731,6 @@ const afterVisitor = (node, st) => {
|
|
|
775
731
|
st.hooks.dispatch('after', node, st, value);
|
|
776
732
|
};
|
|
777
733
|
|
|
778
|
-
/**
|
|
779
|
-
* Strips the `ParenthesizedExpression` wrappers acorn keeps around a node
|
|
780
|
-
* under `preserveParens`, so `(a)` and `((a))` are handled as `a`. A walk
|
|
781
|
-
* passes through them without help, since `acorn-walk`'s base visitor does;
|
|
782
|
-
* only a visitor that branches on a child's *type* has to unwrap it first.
|
|
783
|
-
* @param node - The node, possibly parenthesised.
|
|
784
|
-
* @returns The innermost node that is not a `ParenthesizedExpression`.
|
|
785
|
-
*/
|
|
786
|
-
const unwrapParentheses = (node) => {
|
|
787
|
-
let inner = node;
|
|
788
|
-
while (inner.type === 'ParenthesizedExpression') {
|
|
789
|
-
inner = inner.expression;
|
|
790
|
-
}
|
|
791
|
-
return inner;
|
|
792
|
-
};
|
|
793
|
-
/**
|
|
794
|
-
* Retrieves the key-value pair from an object based on the provided key.
|
|
795
|
-
* @param obj - The object to search for the key-value pair.
|
|
796
|
-
* @param key - The key to search for in the object.
|
|
797
|
-
* @param caseInsesitive - Specifies whether the key comparison should be case-insensitive.
|
|
798
|
-
* @returns The key-value pair as an array, or undefined if the key is not found.
|
|
799
|
-
*/
|
|
800
|
-
const getKeyValue = (obj, key, caseInsesitive) => {
|
|
801
|
-
if (!caseInsesitive || typeof key !== 'string') {
|
|
802
|
-
const value = obj[key];
|
|
803
|
-
return [key, value];
|
|
804
|
-
}
|
|
805
|
-
if (typeof key === 'string') {
|
|
806
|
-
let currentObj = obj;
|
|
807
|
-
do {
|
|
808
|
-
const keys = Object.getOwnPropertyNames(currentObj);
|
|
809
|
-
if (Array.isArray(keys)) {
|
|
810
|
-
const foundKey = keys.find(k => equalIgnoreCase(key, k));
|
|
811
|
-
if (foundKey) {
|
|
812
|
-
const value = currentObj[foundKey];
|
|
813
|
-
return [foundKey, value];
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
} while ((currentObj = Object.getPrototypeOf(currentObj)));
|
|
817
|
-
}
|
|
818
|
-
return undefined;
|
|
819
|
-
};
|
|
820
|
-
/**
|
|
821
|
-
* Compares two values for equality, ignoring case sensitivity.
|
|
822
|
-
* @param a - The first value to compare.
|
|
823
|
-
* @param b - The second value to compare.
|
|
824
|
-
* @returns `true` if the values are equal, `false` otherwise.
|
|
825
|
-
*/
|
|
826
|
-
const equalIgnoreCase = (a, b) => {
|
|
827
|
-
if (typeof a === 'number' || typeof b === 'number') {
|
|
828
|
-
return a === b;
|
|
829
|
-
}
|
|
830
|
-
else if (typeof a === 'symbol' || typeof b === 'symbol') {
|
|
831
|
-
return a === b;
|
|
832
|
-
}
|
|
833
|
-
else if (typeof a === 'string' || typeof b === 'string') {
|
|
834
|
-
return a.localeCompare(b, 'en', { sensitivity: 'base' }) === 0;
|
|
835
|
-
}
|
|
836
|
-
return undefined;
|
|
837
|
-
};
|
|
838
|
-
/**
|
|
839
|
-
* Retrieves the value from an object, ignoring the case of the key.
|
|
840
|
-
* @param obj - The object to retrieve the value from.
|
|
841
|
-
* @param key - The key to search for, ignoring the case.
|
|
842
|
-
* @returns The value associated with the key, or undefined if the key is not found.
|
|
843
|
-
*/
|
|
844
|
-
const getValueIgnoreCase = (obj, key) => {
|
|
845
|
-
const pair = getKeyValue(obj, key, true);
|
|
846
|
-
if (pair) {
|
|
847
|
-
return pair[1];
|
|
848
|
-
}
|
|
849
|
-
return undefined;
|
|
850
|
-
};
|
|
851
|
-
|
|
852
734
|
/**
|
|
853
735
|
* Prototype Pollution Prevention Utilities
|
|
854
736
|
*
|
|
@@ -856,6 +738,12 @@ const getValueIgnoreCase = (obj, key) => {
|
|
|
856
738
|
* by controlling access to dangerous property names and ensuring safe
|
|
857
739
|
* object operations.
|
|
858
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.
|
|
859
747
|
/**
|
|
860
748
|
* Set of property names that are dangerous for prototype pollution attacks
|
|
861
749
|
*/
|
|
@@ -970,6 +858,78 @@ const isDangerousConstructor = (obj) => {
|
|
|
970
858
|
}
|
|
971
859
|
return false;
|
|
972
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
|
+
};
|
|
973
933
|
/**
|
|
974
934
|
* Safely sets a property on an object with prototype pollution protection.
|
|
975
935
|
* Refuses a dangerous target or key, then assigns - so a setter runs, and a
|
|
@@ -984,6 +944,7 @@ const safeSetProperty = (target, key, value) => {
|
|
|
984
944
|
if (!target || (typeof target !== 'object' && typeof target !== 'function')) {
|
|
985
945
|
throw new Error(`Cannot set property on non-object: ${typeof target}`);
|
|
986
946
|
}
|
|
947
|
+
refuseContextWrite(target);
|
|
987
948
|
if (isDangerousProperty(key)) {
|
|
988
949
|
throw new Error(`Access to dangerous property "${String(key)}" is blocked for security reasons`);
|
|
989
950
|
}
|
|
@@ -1104,318 +1065,78 @@ const validateSafeObject = (obj) => {
|
|
|
1104
1065
|
}
|
|
1105
1066
|
};
|
|
1106
1067
|
|
|
1107
|
-
const literals = Registry.fromObject({
|
|
1108
|
-
'undefined': undefined,
|
|
1109
|
-
'null': null,
|
|
1110
|
-
'true': true,
|
|
1111
|
-
'false': false,
|
|
1112
|
-
}, { caseInsensitive: true });
|
|
1113
1068
|
/**
|
|
1114
|
-
*
|
|
1115
|
-
*
|
|
1116
|
-
*
|
|
1117
|
-
*
|
|
1118
|
-
*
|
|
1119
|
-
*
|
|
1120
|
-
* is reconstructed statically.
|
|
1121
|
-
*
|
|
1122
|
-
* `scoped` marks a read of a name bound by any scope on the context's scope
|
|
1123
|
-
* stack when the name is read, whoever pushed it: an arrow-function
|
|
1124
|
-
* parameter, a declaration in the lexical scope `program.ts`,
|
|
1125
|
-
* `block-statement.ts` or `for-statement.ts` pushes, or a binding the caller
|
|
1126
|
-
* pushed with `EvalContext.push` before the walk - which is how `eval-signals`
|
|
1127
|
-
* binds a run's `AbortSignal` under `abortSignalKey`, and why that key never
|
|
1128
|
-
* reaches its `dependencies`. It is asked of `EvalContext.hasInScopes`, which
|
|
1129
|
-
* tests for the binding rather than for its value: a parameter bound to
|
|
1130
|
-
* `undefined` is still a parameter, and reading the flag off the resolved value
|
|
1131
|
-
* would make it vary with the data rather than with the expression.
|
|
1132
|
-
*
|
|
1133
|
-
* It is deliberately *not* set for `priorScopes`: those hold caller-registered,
|
|
1134
|
-
* long-lived objects and are real dependencies, so the discriminator is
|
|
1135
|
-
* lifetime rather than `instanceof EvalScope`. The property is omitted rather
|
|
1136
|
-
* than set to false, so absent reads as "not scoped" the same way it does at
|
|
1137
|
-
* the member emission sites.
|
|
1138
|
-
*
|
|
1139
|
-
* The scope stack is empty only for a walk that does not start at a `Program`
|
|
1140
|
-
* and has nothing pushed by its caller: `program.ts` pushes a scope at the root
|
|
1141
|
-
* of every walk through `EvalService`. `Stack.asArray` allocates, so that empty
|
|
1142
|
-
* case is short-circuited on length.
|
|
1069
|
+
* Strips the `ParenthesizedExpression` wrappers acorn keeps around a node
|
|
1070
|
+
* under `preserveParens`, so `(a)` and `((a))` are handled as `a`. A walk
|
|
1071
|
+
* passes through them without help, since `acorn-walk`'s base visitor does;
|
|
1072
|
+
* only a visitor that branches on a child's *type* has to unwrap it first.
|
|
1073
|
+
* @param node - The node, possibly parenthesised.
|
|
1074
|
+
* @returns The innermost node that is not a `ParenthesizedExpression`.
|
|
1143
1075
|
*/
|
|
1144
|
-
const
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
kind: 'identifier',
|
|
1151
|
-
node,
|
|
1152
|
-
state: st,
|
|
1153
|
-
key,
|
|
1154
|
-
target: st.context,
|
|
1155
|
-
path: node.name,
|
|
1156
|
-
value,
|
|
1157
|
-
...(scoped ? { scoped: true } : {})
|
|
1158
|
-
});
|
|
1076
|
+
const unwrapParentheses = (node) => {
|
|
1077
|
+
let inner = node;
|
|
1078
|
+
while (inner.type === 'ParenthesizedExpression') {
|
|
1079
|
+
inner = inner.expression;
|
|
1080
|
+
}
|
|
1081
|
+
return inner;
|
|
1159
1082
|
};
|
|
1160
1083
|
/**
|
|
1161
|
-
*
|
|
1162
|
-
*
|
|
1163
|
-
*
|
|
1164
|
-
*
|
|
1165
|
-
*
|
|
1166
|
-
* So up to 0.10.x a name on the blocklist resolved to what the context inherits
|
|
1167
|
-
* from `Object.prototype` (`docs/backlog-retired.md` B6): the blocklist guarded
|
|
1168
|
-
* member access to the objects an expression holds, and the context is not one
|
|
1169
|
-
* of them. Every kind of context is refused alike, including those - a
|
|
1170
|
-
* `Registry`, a signal context's source - that hold such a name only as a key
|
|
1171
|
-
* of their own.
|
|
1084
|
+
* Retrieves the key-value pair from an object based on the provided key.
|
|
1085
|
+
* @param obj - The object to search for the key-value pair.
|
|
1086
|
+
* @param key - The key to search for in the object.
|
|
1087
|
+
* @param caseInsesitive - Specifies whether the key comparison should be case-insensitive.
|
|
1088
|
+
* @returns The key-value pair as an array, or undefined if the key is not found.
|
|
1172
1089
|
*/
|
|
1173
|
-
const
|
|
1174
|
-
if (
|
|
1175
|
-
|
|
1090
|
+
const getKeyValue = (obj, key, caseInsesitive) => {
|
|
1091
|
+
if (!caseInsesitive || typeof key !== 'string') {
|
|
1092
|
+
const value = obj[key];
|
|
1093
|
+
return [key, value];
|
|
1094
|
+
}
|
|
1095
|
+
if (typeof key === 'string') {
|
|
1096
|
+
let currentObj = obj;
|
|
1097
|
+
do {
|
|
1098
|
+
const keys = Object.getOwnPropertyNames(currentObj);
|
|
1099
|
+
if (Array.isArray(keys)) {
|
|
1100
|
+
const foundKey = keys.find(k => equalIgnoreCase(key, k));
|
|
1101
|
+
if (foundKey) {
|
|
1102
|
+
const value = currentObj[foundKey];
|
|
1103
|
+
return [foundKey, value];
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
} while ((currentObj = Object.getPrototypeOf(currentObj)));
|
|
1176
1107
|
}
|
|
1108
|
+
return undefined;
|
|
1177
1109
|
};
|
|
1178
1110
|
/**
|
|
1179
|
-
*
|
|
1180
|
-
*
|
|
1181
|
-
*
|
|
1182
|
-
*
|
|
1183
|
-
* The matched key is asked of `getKey`, which answers the key `get` resolves,
|
|
1184
|
-
* and only for a name `mayMatchDangerousProperty` lets through. Any other name
|
|
1185
|
-
* cannot match a blocklisted key, and an identifier must not pay a second
|
|
1186
|
-
* resolution for nothing: the performance spec's read-emission guard asserts
|
|
1187
|
-
* that, with no read hook, `getKey` is not called per identifier.
|
|
1111
|
+
* Compares two values for equality, ignoring case sensitivity.
|
|
1112
|
+
* @param a - The first value to compare.
|
|
1113
|
+
* @param b - The second value to compare.
|
|
1114
|
+
* @returns `true` if the values are equal, `false` otherwise.
|
|
1188
1115
|
*/
|
|
1189
|
-
const
|
|
1190
|
-
if (
|
|
1191
|
-
return;
|
|
1116
|
+
const equalIgnoreCase = (a, b) => {
|
|
1117
|
+
if (typeof a === 'number' || typeof b === 'number') {
|
|
1118
|
+
return a === b;
|
|
1192
1119
|
}
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
throw new Error(`Access to dangerous property "${String(matched)}" is blocked for security reasons`);
|
|
1120
|
+
else if (typeof a === 'symbol' || typeof b === 'symbol') {
|
|
1121
|
+
return a === b;
|
|
1196
1122
|
}
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
if (st.options?.caseInsensitive) {
|
|
1200
|
-
return identifierVisitorCaseInsensitive(node, st);
|
|
1123
|
+
else if (typeof a === 'string' || typeof b === 'string') {
|
|
1124
|
+
return a.localeCompare(b, 'en', { sensitivity: 'base' }) === 0;
|
|
1201
1125
|
}
|
|
1202
|
-
|
|
1203
|
-
refuseDangerousName(node);
|
|
1204
|
-
const context = st.context;
|
|
1205
|
-
const isThis = node.name === 'this';
|
|
1206
|
-
const value = isThis
|
|
1207
|
-
? st.context
|
|
1208
|
-
: context?.get(node.name);
|
|
1209
|
-
// `this` resolves to the context object itself rather than to a key within
|
|
1210
|
-
// it, so it is not a read: reporting it would have a dependency tracker
|
|
1211
|
-
// record the whole context and re-fire on every change to it.
|
|
1212
|
-
if (st.hasHooks && st.hooks.hasReadHooks && !isThis) {
|
|
1213
|
-
emitRead(node, st, value);
|
|
1214
|
-
}
|
|
1215
|
-
pushVisitorResult(node, st, value);
|
|
1216
|
-
afterVisitor(node, st);
|
|
1217
|
-
};
|
|
1218
|
-
const identifierVisitorCaseInsensitive = (node, st) => {
|
|
1219
|
-
beforeVisitor(node, st);
|
|
1220
|
-
refuseDangerousName(node);
|
|
1221
|
-
const context = st.context;
|
|
1222
|
-
const isThis = !!equalIgnoreCase(node.name, 'this');
|
|
1223
|
-
if (!isThis) {
|
|
1224
|
-
refuseDangerousMatch(node, st);
|
|
1225
|
-
}
|
|
1226
|
-
const value = isThis
|
|
1227
|
-
? st.context
|
|
1228
|
-
: context?.get(node.name) ?? literals.get(node.name);
|
|
1229
|
-
if (st.hasHooks && st.hooks.hasReadHooks && !isThis) {
|
|
1230
|
-
emitRead(node, st, value);
|
|
1231
|
-
}
|
|
1232
|
-
pushVisitorResult(node, st, value);
|
|
1233
|
-
afterVisitor(node, st);
|
|
1234
|
-
};
|
|
1235
|
-
|
|
1236
|
-
const literalVisitor = (node, st) => {
|
|
1237
|
-
beforeVisitor(node, st);
|
|
1238
|
-
// A regex literal is a new `RegExp` on every evaluation, as it is in
|
|
1239
|
-
// JavaScript. Acorn builds `node.value` once, at parse time, and every walk of
|
|
1240
|
-
// the parsed tree used to push that one object - a cached `simpleEval` string
|
|
1241
|
-
// and each recompute of a compiled expression alike - so `lastIndex` carried
|
|
1242
|
-
// from one evaluation into the next, and `/a/g.test("a")` answered true, then
|
|
1243
|
-
// false. A copy starts at `lastIndex` 0, and being new it is recorded under a
|
|
1244
|
-
// member-write policy, as a literal is - `EvalContext.checkMemberWrite`. When
|
|
1245
|
-
// acorn could not build the regex in this environment, `value` is null and is
|
|
1246
|
-
// pushed as before.
|
|
1247
|
-
let value = node.value;
|
|
1248
|
-
if (value instanceof RegExp) {
|
|
1249
|
-
value = new RegExp(value);
|
|
1250
|
-
st.createdObjects?.add(value);
|
|
1251
|
-
}
|
|
1252
|
-
pushVisitorResult(node, st, value);
|
|
1253
|
-
afterVisitor(node, st);
|
|
1254
|
-
};
|
|
1255
|
-
|
|
1256
|
-
// import { getCachedVisitorResult, setCachedVisitorResult } from './visitor-result-cache';
|
|
1257
|
-
/**
|
|
1258
|
-
* Converts value to primitive for comparison operations
|
|
1259
|
-
*/
|
|
1260
|
-
const toPrimitive = (value, hint) => {
|
|
1261
|
-
if (value === null || value === undefined)
|
|
1262
|
-
return value;
|
|
1263
|
-
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ||
|
|
1264
|
-
typeof value === 'symbol' || typeof value === 'bigint')
|
|
1265
|
-
return value;
|
|
1266
|
-
// For objects, call valueOf/toString based on hint
|
|
1267
|
-
if (typeof value === 'object') {
|
|
1268
|
-
if (hint === 'number') {
|
|
1269
|
-
const valueOf = value.valueOf;
|
|
1270
|
-
if (typeof valueOf === 'function') {
|
|
1271
|
-
const result = valueOf.call(value);
|
|
1272
|
-
if (typeof result !== 'object')
|
|
1273
|
-
return result;
|
|
1274
|
-
}
|
|
1275
|
-
const toString = value.toString;
|
|
1276
|
-
if (typeof toString === 'function') {
|
|
1277
|
-
return toString.call(value);
|
|
1278
|
-
}
|
|
1279
|
-
}
|
|
1280
|
-
else {
|
|
1281
|
-
const toString = value.toString;
|
|
1282
|
-
if (typeof toString === 'function') {
|
|
1283
|
-
const result = toString.call(value);
|
|
1284
|
-
if (typeof result === 'string')
|
|
1285
|
-
return result;
|
|
1286
|
-
}
|
|
1287
|
-
const valueOf = value.valueOf;
|
|
1288
|
-
if (typeof valueOf === 'function') {
|
|
1289
|
-
const result = valueOf.call(value);
|
|
1290
|
-
if (typeof result !== 'object')
|
|
1291
|
-
return result;
|
|
1292
|
-
}
|
|
1293
|
-
}
|
|
1294
|
-
}
|
|
1295
|
-
return String(value);
|
|
1126
|
+
return undefined;
|
|
1296
1127
|
};
|
|
1297
1128
|
/**
|
|
1298
|
-
*
|
|
1129
|
+
* Retrieves the value from an object, ignoring the case of the key.
|
|
1130
|
+
* @param obj - The object to retrieve the value from.
|
|
1131
|
+
* @param key - The key to search for, ignoring the case.
|
|
1132
|
+
* @returns The value associated with the key, or undefined if the key is not found.
|
|
1299
1133
|
*/
|
|
1300
|
-
const
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
const leftPrim = toPrimitive(left);
|
|
1305
|
-
const rightPrim = toPrimitive(right);
|
|
1306
|
-
if (typeof leftPrim === 'string' || typeof rightPrim === 'string') {
|
|
1307
|
-
return String(leftPrim) + String(rightPrim);
|
|
1308
|
-
}
|
|
1309
|
-
return Number(leftPrim) + Number(rightPrim);
|
|
1310
|
-
}
|
|
1311
|
-
case '-':
|
|
1312
|
-
return Number(toPrimitive(left, 'number')) - Number(toPrimitive(right, 'number'));
|
|
1313
|
-
case '*':
|
|
1314
|
-
return Number(toPrimitive(left, 'number')) * Number(toPrimitive(right, 'number'));
|
|
1315
|
-
case '/':
|
|
1316
|
-
return Number(toPrimitive(left, 'number')) / Number(toPrimitive(right, 'number'));
|
|
1317
|
-
case '%':
|
|
1318
|
-
return Number(toPrimitive(left, 'number')) % Number(toPrimitive(right, 'number'));
|
|
1319
|
-
case '**':
|
|
1320
|
-
return Number(toPrimitive(left, 'number')) ** Number(toPrimitive(right, 'number'));
|
|
1321
|
-
// Bitwise operations - convert to 32-bit integers
|
|
1322
|
-
case '<<':
|
|
1323
|
-
return (Number(toPrimitive(left, 'number')) | 0) << (Number(toPrimitive(right, 'number')) | 0);
|
|
1324
|
-
case '>>':
|
|
1325
|
-
return (Number(toPrimitive(left, 'number')) | 0) >> (Number(toPrimitive(right, 'number')) | 0);
|
|
1326
|
-
case '>>>':
|
|
1327
|
-
return (Number(toPrimitive(left, 'number')) >>> 0) >>> (Number(toPrimitive(right, 'number')) | 0);
|
|
1328
|
-
case '&':
|
|
1329
|
-
return (Number(toPrimitive(left, 'number')) | 0) & (Number(toPrimitive(right, 'number')) | 0);
|
|
1330
|
-
case '^':
|
|
1331
|
-
return (Number(toPrimitive(left, 'number')) | 0) ^ (Number(toPrimitive(right, 'number')) | 0);
|
|
1332
|
-
case '|':
|
|
1333
|
-
return (Number(toPrimitive(left, 'number')) | 0) | (Number(toPrimitive(right, 'number')) | 0);
|
|
1334
|
-
// Comparison operations - follow JavaScript's abstract comparison rules
|
|
1335
|
-
case '==':
|
|
1336
|
-
return left == right;
|
|
1337
|
-
case '!=':
|
|
1338
|
-
return left != right;
|
|
1339
|
-
case '===':
|
|
1340
|
-
return left === right;
|
|
1341
|
-
case '!==':
|
|
1342
|
-
return left !== right;
|
|
1343
|
-
case '<': {
|
|
1344
|
-
const leftComp = toPrimitive(left, 'number');
|
|
1345
|
-
const rightComp = toPrimitive(right, 'number');
|
|
1346
|
-
// If both are strings, do string comparison
|
|
1347
|
-
if (typeof leftComp === 'string' && typeof rightComp === 'string') {
|
|
1348
|
-
return leftComp < rightComp;
|
|
1349
|
-
}
|
|
1350
|
-
return Number(leftComp) < Number(rightComp);
|
|
1351
|
-
}
|
|
1352
|
-
case '<=': {
|
|
1353
|
-
const leftLE = toPrimitive(left, 'number');
|
|
1354
|
-
const rightLE = toPrimitive(right, 'number');
|
|
1355
|
-
if (typeof leftLE === 'string' && typeof rightLE === 'string') {
|
|
1356
|
-
return leftLE <= rightLE;
|
|
1357
|
-
}
|
|
1358
|
-
return Number(leftLE) <= Number(rightLE);
|
|
1359
|
-
}
|
|
1360
|
-
case '>': {
|
|
1361
|
-
const leftGT = toPrimitive(left, 'number');
|
|
1362
|
-
const rightGT = toPrimitive(right, 'number');
|
|
1363
|
-
if (typeof leftGT === 'string' && typeof rightGT === 'string') {
|
|
1364
|
-
return leftGT > rightGT;
|
|
1365
|
-
}
|
|
1366
|
-
return Number(leftGT) > Number(rightGT);
|
|
1367
|
-
}
|
|
1368
|
-
case '>=': {
|
|
1369
|
-
const leftGE = toPrimitive(left, 'number');
|
|
1370
|
-
const rightGE = toPrimitive(right, 'number');
|
|
1371
|
-
if (typeof leftGE === 'string' && typeof rightGE === 'string') {
|
|
1372
|
-
return leftGE >= rightGE;
|
|
1373
|
-
}
|
|
1374
|
-
return Number(leftGE) >= Number(rightGE);
|
|
1375
|
-
}
|
|
1376
|
-
// Special operations
|
|
1377
|
-
case 'in': {
|
|
1378
|
-
if (typeof left === 'string' || typeof left === 'number' || typeof left === 'symbol') {
|
|
1379
|
-
if (right == null) {
|
|
1380
|
-
throw new Error(`Cannot use 'in' operator with null or undefined right operand`);
|
|
1381
|
-
}
|
|
1382
|
-
return left in right;
|
|
1383
|
-
}
|
|
1384
|
-
throw new Error(`Invalid left operand for 'in' operator: ${typeof left}`);
|
|
1385
|
-
}
|
|
1386
|
-
case 'instanceof': {
|
|
1387
|
-
if (typeof right === 'function') {
|
|
1388
|
-
return left instanceof right;
|
|
1389
|
-
}
|
|
1390
|
-
throw new Error(`Right operand of 'instanceof' is not a constructor`);
|
|
1391
|
-
}
|
|
1392
|
-
case '??':
|
|
1393
|
-
return left ?? right;
|
|
1394
|
-
default:
|
|
1395
|
-
throw new Error(`Unsupported binary operator: ${operator}`);
|
|
1134
|
+
const getValueIgnoreCase = (obj, key) => {
|
|
1135
|
+
const pair = getKeyValue(obj, key, true);
|
|
1136
|
+
if (pair) {
|
|
1137
|
+
return pair[1];
|
|
1396
1138
|
}
|
|
1397
|
-
|
|
1398
|
-
/**
|
|
1399
|
-
* Safely evaluates binary expressions with proper type handling
|
|
1400
|
-
*/
|
|
1401
|
-
const binaryExpressionVisitor = (node, st, callback) => {
|
|
1402
|
-
beforeVisitor(node, st);
|
|
1403
|
-
// Disable visitor result caching for now due to context sensitivity issues
|
|
1404
|
-
// const cachedResult = getCachedVisitorResult(node, st.context);
|
|
1405
|
-
// if (cachedResult !== undefined) {
|
|
1406
|
-
// pushVisitorResult(node, st, cachedResult);
|
|
1407
|
-
// afterVisitor(node, st);
|
|
1408
|
-
// return;
|
|
1409
|
-
// }
|
|
1410
|
-
callback(node.left, st);
|
|
1411
|
-
const left = popVisitorResult(node, st);
|
|
1412
|
-
callback(node.right, st);
|
|
1413
|
-
const right = popVisitorResult(node, st);
|
|
1414
|
-
const value = evaluateBinaryOperation(node.operator, left, right);
|
|
1415
|
-
// Disable visitor result caching for now
|
|
1416
|
-
// setCachedVisitorResult(node, st.context, value);
|
|
1417
|
-
pushVisitorResult(node, st, value);
|
|
1418
|
-
afterVisitor(node, st);
|
|
1139
|
+
return undefined;
|
|
1419
1140
|
};
|
|
1420
1141
|
|
|
1421
1142
|
/**
|
|
@@ -1485,6 +1206,66 @@ function getPropertyLookupCacheStats() {
|
|
|
1485
1206
|
};
|
|
1486
1207
|
}
|
|
1487
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
|
+
|
|
1488
1269
|
// import { getCachedVisitorResult, setCachedVisitorResult } from './visitor-result-cache';
|
|
1489
1270
|
/**
|
|
1490
1271
|
* Reconstructs the dotted source path of a member chain (`a.b.c`).
|
|
@@ -1517,6 +1298,21 @@ const readPath = (node) => {
|
|
|
1517
1298
|
parts.unshift(current.name);
|
|
1518
1299
|
return parts.join('.');
|
|
1519
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
|
+
*/
|
|
1520
1316
|
const memberExpressionVisitor = (node, st, callback) => {
|
|
1521
1317
|
beforeVisitor(node, st);
|
|
1522
1318
|
// Disable visitor result caching for now due to context sensitivity issues
|
|
@@ -1533,7 +1329,9 @@ const memberExpressionVisitor = (node, st, callback) => {
|
|
|
1533
1329
|
afterVisitor(node, st);
|
|
1534
1330
|
};
|
|
1535
1331
|
const evaluateMember = (node, st, callback) => {
|
|
1536
|
-
// 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);
|
|
1537
1335
|
callback(node.object, st);
|
|
1538
1336
|
const obj = popVisitorResult(node, st);
|
|
1539
1337
|
const object = (node.optional ? (obj || {}) : obj);
|
|
@@ -1583,7 +1381,14 @@ const evaluateMember = (node, st, callback) => {
|
|
|
1583
1381
|
value
|
|
1584
1382
|
});
|
|
1585
1383
|
}
|
|
1586
|
-
|
|
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];
|
|
1587
1392
|
}
|
|
1588
1393
|
else if (object instanceof EvalScope) {
|
|
1589
1394
|
// A global scope reads its object as the context does, with a bare
|
|
@@ -1605,9 +1410,16 @@ const evaluateMember = (node, st, callback) => {
|
|
|
1605
1410
|
value
|
|
1606
1411
|
});
|
|
1607
1412
|
}
|
|
1608
|
-
|
|
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];
|
|
1609
1417
|
}
|
|
1610
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
|
+
//
|
|
1611
1423
|
// For regular objects, we need both case-insensitive lookup AND prototype pollution protection
|
|
1612
1424
|
let value;
|
|
1613
1425
|
// The key the lookup actually matched, reported to the read hooks. The
|
|
@@ -1705,7 +1517,7 @@ const evaluateMember = (node, st, callback) => {
|
|
|
1705
1517
|
value
|
|
1706
1518
|
});
|
|
1707
1519
|
}
|
|
1708
|
-
return [object, key, value];
|
|
1520
|
+
return [object, key, value, false];
|
|
1709
1521
|
}
|
|
1710
1522
|
};
|
|
1711
1523
|
|
|
@@ -1753,30 +1565,251 @@ const evaluateArray = (node, items, st, callback) => {
|
|
|
1753
1565
|
};
|
|
1754
1566
|
|
|
1755
1567
|
/**
|
|
1756
|
-
*
|
|
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.
|
|
1757
1601
|
*/
|
|
1758
|
-
const
|
|
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
|
+
};
|
|
1759
1610
|
/**
|
|
1760
|
-
*
|
|
1761
|
-
*
|
|
1762
|
-
* For the one recording site whose value may be a primitive: an array or
|
|
1763
|
-
* parameter rest is `args.slice(i)`, which is a string when the destructured
|
|
1764
|
-
* source is one (`let [h, ...t] = "abc"`), and `WeakSet.add` throws on a
|
|
1765
|
-
* primitive. The literal, object-rest and arrow-function sites always create
|
|
1766
|
-
* an object and add directly.
|
|
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.
|
|
1767
1613
|
*
|
|
1768
|
-
*
|
|
1769
|
-
*
|
|
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.
|
|
1770
1616
|
*/
|
|
1771
|
-
const
|
|
1772
|
-
|
|
1773
|
-
|
|
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);
|
|
1774
1636
|
}
|
|
1637
|
+
return method;
|
|
1775
1638
|
};
|
|
1776
1639
|
/**
|
|
1777
|
-
*
|
|
1778
|
-
*
|
|
1779
|
-
|
|
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
|
+
|
|
1788
|
+
/**
|
|
1789
|
+
* Whether `value` can be held in a `WeakSet` - an object or a function.
|
|
1790
|
+
*/
|
|
1791
|
+
const isObjectLike = (value) => (typeof value === 'object' && value !== null) || typeof value === 'function';
|
|
1792
|
+
/**
|
|
1793
|
+
* Records `value` as created by the walk, unless it is a primitive.
|
|
1794
|
+
*
|
|
1795
|
+
* For the one recording site whose value may be a primitive: an array or
|
|
1796
|
+
* parameter rest is `args.slice(i)`, which is a string when the destructured
|
|
1797
|
+
* source is one (`let [h, ...t] = "abc"`), and `WeakSet.add` throws on a
|
|
1798
|
+
* primitive. The literal, object-rest and arrow-function sites always create
|
|
1799
|
+
* an object and add directly.
|
|
1800
|
+
*
|
|
1801
|
+
* @param created - `st.createdObjects`, already read and found set by the caller.
|
|
1802
|
+
* @param value - The value the walk created.
|
|
1803
|
+
*/
|
|
1804
|
+
const recordCreated = (created, value) => {
|
|
1805
|
+
if (isObjectLike(value)) {
|
|
1806
|
+
created.add(value);
|
|
1807
|
+
}
|
|
1808
|
+
};
|
|
1809
|
+
/**
|
|
1810
|
+
* Asks the context whether a member write may happen, through
|
|
1811
|
+
* `EvalContext.checkMemberWrite`. A refusal is a throw, and it propagates from
|
|
1812
|
+
* here unchanged: nothing has been written yet.
|
|
1780
1813
|
*
|
|
1781
1814
|
* Called by the member branches of `assignment-expression.ts` and
|
|
1782
1815
|
* `update-expression.ts`, immediately before `safeSetProperty`, and only after
|
|
@@ -1790,7 +1823,7 @@ const recordCreated = (created, value) => {
|
|
|
1790
1823
|
* @param key - The property about to be written.
|
|
1791
1824
|
*/
|
|
1792
1825
|
const consultMemberWrite = (st, created, target, key) => {
|
|
1793
|
-
st.context
|
|
1826
|
+
contextCheckMemberWrite(st.context, {
|
|
1794
1827
|
target,
|
|
1795
1828
|
key,
|
|
1796
1829
|
createdByEvaluation: isObjectLike(target) && created.has(target),
|
|
@@ -1840,6 +1873,107 @@ const FUNCTION_BIND = Function.prototype.bind;
|
|
|
1840
1873
|
* passes none for `null` or `undefined`, and throws for a primitive.
|
|
1841
1874
|
*/
|
|
1842
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
|
+
};
|
|
1843
1977
|
/**
|
|
1844
1978
|
* Asks the context whether a call may run, when the function is a built-in
|
|
1845
1979
|
* that writes into an object it is handed, through
|
|
@@ -1857,8 +1991,10 @@ const argumentsOf = (list) => isObjectLike(list) ? Array.prototype.slice.call(li
|
|
|
1857
1991
|
* arguments: binding a built-in to an object the walk did not create is
|
|
1858
1992
|
* refused before the bound function exists (`docs/backlog-retired.md` C5).
|
|
1859
1993
|
*
|
|
1860
|
-
* Called by `call-expression.ts` immediately before
|
|
1861
|
-
*
|
|
1994
|
+
* Called by `call-expression.ts` immediately before a call, and by
|
|
1995
|
+
* `tagged-template-expression.ts` immediately before a tag is called, in each
|
|
1996
|
+
* case only after the caller has read `st.createdObjects` and found it set, as
|
|
1997
|
+
* for a member write.
|
|
1862
1998
|
*
|
|
1863
1999
|
* @param st - The state of the walk making the call.
|
|
1864
2000
|
* @param created - `st.createdObjects`, already read by the caller.
|
|
@@ -1884,7 +2020,7 @@ const consultMethodWrite = (st, created, fn, receiver, args) => {
|
|
|
1884
2020
|
return;
|
|
1885
2021
|
}
|
|
1886
2022
|
const target = builtIn.writes === 'receiver' ? receiver : args[0];
|
|
1887
|
-
st.context
|
|
2023
|
+
contextCheckMemberWrite(st.context, {
|
|
1888
2024
|
target,
|
|
1889
2025
|
key: undefined,
|
|
1890
2026
|
createdByEvaluation: isObjectLike(target) && created.has(target),
|
|
@@ -1892,8 +2028,174 @@ const consultMethodWrite = (st, created, fn, receiver, args) => {
|
|
|
1892
2028
|
});
|
|
1893
2029
|
};
|
|
1894
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
|
+
|
|
1895
2194
|
/**
|
|
1896
2195
|
* Security: List of dangerous functions that should not be callable
|
|
2196
|
+
*
|
|
2197
|
+
* Also the names `dangerous-values.ts` reads from `globalThis` into its
|
|
2198
|
+
* identity set, and the names its bound-function rule refuses.
|
|
1897
2199
|
*/
|
|
1898
2200
|
const DANGEROUS_FUNCTIONS = new Set([
|
|
1899
2201
|
'Function',
|
|
@@ -1958,6 +2260,12 @@ const isFunctionSafe = (fn, fnName) => {
|
|
|
1958
2260
|
if (fn === Function) {
|
|
1959
2261
|
return false;
|
|
1960
2262
|
}
|
|
2263
|
+
// A dangerous function by value, under whatever name it was reached:
|
|
2264
|
+
// an alias, a member, a call result, a case variant (`dangerous-values.ts`).
|
|
2265
|
+
// Up to 0.12.0 only the written name was checked (`docs/backlog.md` A35).
|
|
2266
|
+
if (isDangerousValue(fn)) {
|
|
2267
|
+
return false;
|
|
2268
|
+
}
|
|
1961
2269
|
try {
|
|
1962
2270
|
// Check function string for dangerous patterns
|
|
1963
2271
|
const fnString = fn.toString();
|
|
@@ -1979,6 +2287,9 @@ const isFunctionSafe = (fn, fnName) => {
|
|
|
1979
2287
|
* re-raised as a new `Error` with a fixed prefix on its message, which no
|
|
1980
2288
|
* caller could route around, since the call is inside the walk
|
|
1981
2289
|
* (`docs/backlog-retired.md` A6).
|
|
2290
|
+
*
|
|
2291
|
+
* Also the call a tag that is not a member goes through
|
|
2292
|
+
* (`tagged-template-expression.ts`).
|
|
1982
2293
|
*/
|
|
1983
2294
|
const safeCall = (caller, thisArg, args, optional = false, fnName) => {
|
|
1984
2295
|
if (!caller) {
|
|
@@ -1993,92 +2304,765 @@ const safeCall = (caller, thisArg, args, optional = false, fnName) => {
|
|
|
1993
2304
|
if (!isFunctionSafe(caller, fnName)) {
|
|
1994
2305
|
throw new Error(`Function call blocked for security reasons: ${fnName || 'anonymous'}`);
|
|
1995
2306
|
}
|
|
1996
|
-
return caller.apply(thisArg, args);
|
|
2307
|
+
return caller.apply(thisArg, args);
|
|
2308
|
+
};
|
|
2309
|
+
/**
|
|
2310
|
+
* The receiver for a method called on `object`: a prior scope's `thisArg` when
|
|
2311
|
+
* `object` is that scope's own object, `object` otherwise.
|
|
2312
|
+
*
|
|
2313
|
+
* `ns.fn()` reaches `fn` through the namespace, which evaluates to the scope's
|
|
2314
|
+
* object and has to keep doing so - `ns` alone, or `ns === x`, must not see
|
|
2315
|
+
* `thisArg`. So the substitution is made here, at the call, and nowhere
|
|
2316
|
+
* earlier: `evaluateMember`'s first slot is also the target of `ns.x = v`, and
|
|
2317
|
+
* a write belongs on the scope's object. Matched by identity, so it follows the
|
|
2318
|
+
* object rather than the name it was reached by; the first scope holding the
|
|
2319
|
+
* object answers, as in `EvalContext.get`. Up to 0.6.x `thisArg` was never
|
|
2320
|
+
* applied (`docs/backlog-retired.md` A7).
|
|
2321
|
+
*
|
|
2322
|
+
* `this.fn()` does not need this: `EvalContext.getThis` answers it.
|
|
2323
|
+
*
|
|
2324
|
+
* A call with no prior scopes pays one length check.
|
|
2325
|
+
*/
|
|
2326
|
+
const scopeReceiver = (st, object) => {
|
|
2327
|
+
const priorScopes = st.context?.priorScopes;
|
|
2328
|
+
if (!priorScopes?.length) {
|
|
2329
|
+
return object;
|
|
2330
|
+
}
|
|
2331
|
+
for (const scope of priorScopes) {
|
|
2332
|
+
if (scope.context === object) {
|
|
2333
|
+
return scope.options.thisArg ?? object;
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
return object;
|
|
2337
|
+
};
|
|
2338
|
+
/**
|
|
2339
|
+
* Calls the function a member callee reads, from what `evaluateMember`
|
|
2340
|
+
* returned for it: the receiver `scopeReceiver` gives, the context's write
|
|
2341
|
+
* policy asked first, then the sandbox, with the property name as the checked
|
|
2342
|
+
* name - a computed key included, since `obj["fetch"]` reads the key `fetch`.
|
|
2343
|
+
*
|
|
2344
|
+
* Shared by a member call and a member tag (`tagged-template-expression.ts`),
|
|
2345
|
+
* so the two cannot drift. Up to 0.12.0 a tag was applied with the context as
|
|
2346
|
+
* `this` and no check at all (`docs/backlog-retired.md` A36). Each visitor
|
|
2347
|
+
* evaluates the member itself and hands the result in, so each keeps its own
|
|
2348
|
+
* order: a call's arguments before its callee, a tag before its template's
|
|
2349
|
+
* expressions.
|
|
2350
|
+
*/
|
|
2351
|
+
const callMember = (st, [object, propertyName, fn, contextResolved], args, optional) => {
|
|
2352
|
+
const functionName = typeof propertyName === 'string' ? propertyName : undefined;
|
|
2353
|
+
const receiver = scopeReceiver(st, object);
|
|
2354
|
+
const created = st.createdObjects;
|
|
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.
|
|
2358
|
+
consultMethodWrite(st, created, fn, receiver, args);
|
|
2359
|
+
}
|
|
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);
|
|
2381
|
+
};
|
|
2382
|
+
/**
|
|
2383
|
+
* Enhanced call expression visitor with security checks
|
|
2384
|
+
*
|
|
2385
|
+
* A context with a member-write policy is asked, before the call, about a
|
|
2386
|
+
* built-in that writes into its receiver or its first argument - see
|
|
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.
|
|
2395
|
+
*/
|
|
2396
|
+
const callExpressionVisitor = (node, st, callback) => {
|
|
2397
|
+
beforeVisitor(node, st);
|
|
2398
|
+
const args = evaluateArray(node, node.arguments, st, callback);
|
|
2399
|
+
if (node.callee.type === 'MemberExpression') {
|
|
2400
|
+
const value = callMember(st, evaluateMember(node.callee, st, callback), args, node.callee.optional);
|
|
2401
|
+
pushVisitorResult(node, st, value);
|
|
2402
|
+
}
|
|
2403
|
+
else {
|
|
2404
|
+
// The callee's own value is checked by `safeCall`, with the call's
|
|
2405
|
+
// message; what is read under it still passes the funnel.
|
|
2406
|
+
exemptFromFunnel(node.callee);
|
|
2407
|
+
callback(node.callee, st);
|
|
2408
|
+
const caller = popVisitorResult(node, st);
|
|
2409
|
+
// Try to get function name for security checks
|
|
2410
|
+
let functionName;
|
|
2411
|
+
if (node.callee.type === 'Identifier') {
|
|
2412
|
+
functionName = node.callee.name;
|
|
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);
|
|
2418
|
+
const created = st.createdObjects;
|
|
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.
|
|
2423
|
+
consultMethodWrite(st, created, caller, st.context, args);
|
|
2424
|
+
}
|
|
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);
|
|
2434
|
+
pushVisitorResult(node, st, value);
|
|
2435
|
+
}
|
|
2436
|
+
afterVisitor(node, st);
|
|
2437
|
+
};
|
|
2438
|
+
|
|
2439
|
+
/**
|
|
2440
|
+
* The prototype of a function literal of each kind, read from this module's
|
|
2441
|
+
* own literals. Never built from source text through `Function` or `eval`:
|
|
2442
|
+
* that fails under a CSP without `unsafe-eval`, and a sandbox must not
|
|
2443
|
+
* construct code to defend itself.
|
|
2444
|
+
*/
|
|
2445
|
+
const literalPrototypes = () => ({
|
|
2446
|
+
AsyncFunction: Object.getPrototypeOf(async function () { }),
|
|
2447
|
+
GeneratorFunction: Object.getPrototypeOf(function* () { }),
|
|
2448
|
+
AsyncGeneratorFunction: Object.getPrototypeOf(async function* () { }),
|
|
2449
|
+
});
|
|
2450
|
+
/**
|
|
2451
|
+
* Reads the constructor off each prototype in `prototypes`.
|
|
2452
|
+
*
|
|
2453
|
+
* A constructor that is `Function` itself is not captured, and is recorded
|
|
2454
|
+
* as such: a realm whose bundler downlevels async functions - Jest's, or an
|
|
2455
|
+
* Angular zone.js build - makes an async literal a plain function, and the
|
|
2456
|
+
* real constructor is no global there. An expression reaches it only through
|
|
2457
|
+
* some value's `constructor`, which the guard refuses, so only a host could
|
|
2458
|
+
* put it in the context.
|
|
2459
|
+
*
|
|
2460
|
+
* @param prototypes - The prototype of each kind; this module's literals by
|
|
2461
|
+
* default. A spec injects its own.
|
|
2462
|
+
*/
|
|
2463
|
+
const captureFunctionConstructors = (prototypes = literalPrototypes()) => {
|
|
2464
|
+
const captured = new Map();
|
|
2465
|
+
const notCaptured = [];
|
|
2466
|
+
for (const name of Object.keys(prototypes)) {
|
|
2467
|
+
const prototype = prototypes[name];
|
|
2468
|
+
const constructor = prototype ? prototype.constructor : undefined;
|
|
2469
|
+
if (typeof constructor === 'function' && constructor !== Function) {
|
|
2470
|
+
captured.set(name, constructor);
|
|
2471
|
+
}
|
|
2472
|
+
else {
|
|
2473
|
+
notCaptured.push(name);
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
return { captured, notCaptured };
|
|
2477
|
+
};
|
|
2478
|
+
/** The prefix `Function.prototype.bind` gives a bound function's `name`. */
|
|
2479
|
+
const BOUND_PREFIX = 'bound ';
|
|
2480
|
+
/** The realm, read by name. */
|
|
2481
|
+
const realmGlobals = globalThis;
|
|
2482
|
+
/**
|
|
2483
|
+
* The prefix under which zone.js keeps the original of a global it patched:
|
|
2484
|
+
* the realm's `__Zone_symbol_prefix` when that is a string, as zone.js reads
|
|
2485
|
+
* it, and `__zone_symbol__` otherwise.
|
|
2486
|
+
*/
|
|
2487
|
+
const zoneOriginalPrefix = () => {
|
|
2488
|
+
const prefix = realmGlobals['__Zone_symbol_prefix'];
|
|
2489
|
+
return typeof prefix === 'string' ? prefix : '__zone_symbol__';
|
|
2490
|
+
};
|
|
2491
|
+
/**
|
|
2492
|
+
* Weak, so a function that was once a listed global - a test's fake timer, a
|
|
2493
|
+
* patcher's wrapper - is not kept alive by having been captured.
|
|
2494
|
+
*/
|
|
2495
|
+
let dangerousValues;
|
|
2496
|
+
let listedNames;
|
|
2497
|
+
/** The `setTimeout` the set last captured: the sentinel. */
|
|
2498
|
+
let capturedSetTimeout;
|
|
2499
|
+
/**
|
|
2500
|
+
* Adds each call-list name's current global value, and zone.js's saved
|
|
2501
|
+
* original of it where there is one. Adds only: a value captured earlier
|
|
2502
|
+
* stays, so a context that took it before a replacement is still refused.
|
|
2503
|
+
*/
|
|
2504
|
+
const addGlobals = (set) => {
|
|
2505
|
+
const zonePrefix = zoneOriginalPrefix();
|
|
2506
|
+
for (const name of DANGEROUS_FUNCTIONS) {
|
|
2507
|
+
for (const value of [realmGlobals[name], realmGlobals[zonePrefix + name]]) {
|
|
2508
|
+
if (typeof value === 'function') {
|
|
2509
|
+
set.add(value);
|
|
2510
|
+
}
|
|
2511
|
+
}
|
|
2512
|
+
}
|
|
2513
|
+
capturedSetTimeout = realmGlobals['setTimeout'];
|
|
2514
|
+
};
|
|
2515
|
+
/**
|
|
2516
|
+
* The identity set, captured at the first check and never at module load: an
|
|
2517
|
+
* eval-core module evaluated before zone.js would otherwise hold the native
|
|
2518
|
+
* timers and miss the wrappers every later context holds.
|
|
2519
|
+
*
|
|
2520
|
+
* Each check compares the last captured `setTimeout` with the current one.
|
|
2521
|
+
* When `setTimeout` has been replaced - zone.js loaded late, or any other
|
|
2522
|
+
* patcher - the current value of every listed global is added. Another listed
|
|
2523
|
+
* global replaced on its own is not seen until `setTimeout` changes.
|
|
2524
|
+
*
|
|
2525
|
+
* Assigned only once it is filled, so a throw part-way through leaves no
|
|
2526
|
+
* half-captured set behind; the next check captures again.
|
|
2527
|
+
*/
|
|
2528
|
+
const dangerousValueSet = () => {
|
|
2529
|
+
if (!dangerousValues) {
|
|
2530
|
+
const set = new WeakSet();
|
|
2531
|
+
addGlobals(set);
|
|
2532
|
+
for (const constructor of captureFunctionConstructors().captured.values()) {
|
|
2533
|
+
set.add(constructor);
|
|
2534
|
+
}
|
|
2535
|
+
dangerousValues = set;
|
|
2536
|
+
}
|
|
2537
|
+
else if (realmGlobals['setTimeout'] !== capturedSetTimeout) {
|
|
2538
|
+
addGlobals(dangerousValues);
|
|
2539
|
+
}
|
|
2540
|
+
return dangerousValues;
|
|
2541
|
+
};
|
|
2542
|
+
/**
|
|
2543
|
+
* The names the bound-function rule refuses: the call list, `import`
|
|
2544
|
+
* included, and the three function constructors.
|
|
2545
|
+
*/
|
|
2546
|
+
const boundListedNames = () => (listedNames ??= new Set([
|
|
2547
|
+
...DANGEROUS_FUNCTIONS, 'AsyncFunction', 'GeneratorFunction', 'AsyncGeneratorFunction',
|
|
2548
|
+
]));
|
|
2549
|
+
/**
|
|
2550
|
+
* Whether `fn` is a bound function whose name, stripped of every `bound `
|
|
2551
|
+
* prefix, is a listed name: `eval.bind(null)` is named `"bound eval"`, and
|
|
2552
|
+
* binding it again gives `"bound bound eval"`. A heuristic: a host's own
|
|
2553
|
+
* function bound under a listed name is refused as well. An unbound function
|
|
2554
|
+
* is never refused by its name here.
|
|
2555
|
+
*
|
|
2556
|
+
* A `name` that cannot be read - a revoked proxy - is not a listed name; the
|
|
2557
|
+
* call sandbox's source test refuses a function it cannot read.
|
|
2558
|
+
*/
|
|
2559
|
+
const isBoundListedFunction = (fn) => {
|
|
2560
|
+
let name;
|
|
2561
|
+
try {
|
|
2562
|
+
name = fn.name;
|
|
2563
|
+
}
|
|
2564
|
+
catch {
|
|
2565
|
+
return false;
|
|
2566
|
+
}
|
|
2567
|
+
if (typeof name !== 'string' || !name.startsWith(BOUND_PREFIX)) {
|
|
2568
|
+
return false;
|
|
2569
|
+
}
|
|
2570
|
+
let target = name;
|
|
2571
|
+
while (target.startsWith(BOUND_PREFIX)) {
|
|
2572
|
+
target = target.slice(BOUND_PREFIX.length);
|
|
2573
|
+
}
|
|
2574
|
+
return boundListedNames().has(target);
|
|
2575
|
+
};
|
|
2576
|
+
/**
|
|
2577
|
+
* Whether `value` is a dangerous function: a member of the realm's identity
|
|
2578
|
+
* set, or a bound function of a listed name. Anything that is not a function
|
|
2579
|
+
* is not.
|
|
2580
|
+
*/
|
|
2581
|
+
const isDangerousValue = (value) => {
|
|
2582
|
+
if (typeof value !== 'function') {
|
|
2583
|
+
return false;
|
|
2584
|
+
}
|
|
2585
|
+
return dangerousValueSet().has(value) || isBoundListedFunction(value);
|
|
2586
|
+
};
|
|
2587
|
+
/**
|
|
2588
|
+
* The nodes whose own pushed value the funnel leaves to the site that uses
|
|
2589
|
+
* it. Weak, so a cached AST that is dropped takes its marks with it.
|
|
2590
|
+
*
|
|
2591
|
+
* Keyed by node object, so it assumes a tree: no node object in two places.
|
|
2592
|
+
* acorn never shares one, and nothing in this library builds nodes. A
|
|
2593
|
+
* host-built AST that reused one object as both a callee and an argument
|
|
2594
|
+
* would see the argument refused until the callee had been visited once, and
|
|
2595
|
+
* exempt after.
|
|
2596
|
+
*/
|
|
2597
|
+
const exemptNodes = new WeakSet();
|
|
2598
|
+
/**
|
|
2599
|
+
* Exempts `node`'s own pushed value from {@link refuseDangerousRead}, and
|
|
2600
|
+
* nothing under it.
|
|
2601
|
+
*
|
|
2602
|
+
* - The call, `new` and tag visitors mark the callee or tag they visit. Its
|
|
2603
|
+
* value is checked by that site's sandbox, with that site's message (K1).
|
|
2604
|
+
* - The unary visitor marks `typeof`'s operand, so `typeof eval` is still
|
|
2605
|
+
* `"function"`.
|
|
2606
|
+
*
|
|
2607
|
+
* Only the node itself: in `(0, eval)("1+1")` the callee is the sequence, so
|
|
2608
|
+
* the identifier `eval` under it is refused when it is pushed. A mark records
|
|
2609
|
+
* the node's place in the AST, which never changes, so it is never cleared.
|
|
2610
|
+
*/
|
|
2611
|
+
const exemptFromFunnel = (node) => {
|
|
2612
|
+
exemptNodes.add(node);
|
|
2613
|
+
};
|
|
2614
|
+
/**
|
|
2615
|
+
* The name a refused read reports: the identifier, the member's property name
|
|
2616
|
+
* where the node holds it, or the function's own `name`.
|
|
2617
|
+
*/
|
|
2618
|
+
const readName = (node, fn) => {
|
|
2619
|
+
if (node.type === 'Identifier') {
|
|
2620
|
+
return node.name;
|
|
2621
|
+
}
|
|
2622
|
+
if (node.type === 'MemberExpression') {
|
|
2623
|
+
if (!node.computed && node.property.type === 'Identifier') {
|
|
2624
|
+
return node.property.name;
|
|
2625
|
+
}
|
|
2626
|
+
if (node.property.type === 'Literal') {
|
|
2627
|
+
return String(node.property.value);
|
|
2628
|
+
}
|
|
2629
|
+
}
|
|
2630
|
+
return typeof fn.name === 'string' && fn.name ? fn.name : 'anonymous';
|
|
2631
|
+
};
|
|
2632
|
+
/**
|
|
2633
|
+
* The funnel (`docs/a35/plan.md` § 3.3): refuses a dangerous function pushed
|
|
2634
|
+
* by any node but an exempt one, so no visitor - including one not yet
|
|
2635
|
+
* written - can hand it on as an argument, a receiver, an element, a branch
|
|
2636
|
+
* or the expression's result. `pushVisitorResult` calls it for a function
|
|
2637
|
+
* value only.
|
|
2638
|
+
*
|
|
2639
|
+
* It sees only what passes through the walk's own stack. A value that reaches
|
|
2640
|
+
* native code without being pushed is not seen: the elements of an array the
|
|
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.
|
|
2645
|
+
*
|
|
2646
|
+
* Up to 0.12.0 nothing refused such a read: `["1+1"].map(eval)` gave `[2]`
|
|
2647
|
+
* (`docs/backlog-retired.md` A35).
|
|
2648
|
+
*/
|
|
2649
|
+
const refuseDangerousRead = (node, value) => {
|
|
2650
|
+
if (exemptNodes.has(node) || !isDangerousValue(value)) {
|
|
2651
|
+
return;
|
|
2652
|
+
}
|
|
2653
|
+
throw new Error(`Access to dangerous function "${readName(node, value)}" is blocked for security reasons`);
|
|
2654
|
+
};
|
|
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.
|
|
2659
|
+
/**
|
|
2660
|
+
* The two push helpers each carry their own trace-bound call rather than
|
|
2661
|
+
* sharing one.
|
|
2662
|
+
*
|
|
2663
|
+
* **Why the read is at these two sites.** `st.maxTraceItems` is the *walk's*
|
|
2664
|
+
* bound. `EvalResult` cannot read it for itself: it holds a context, and a
|
|
2665
|
+
* context's options are not the walk's - the split `CLAUDE.md` documents for
|
|
2666
|
+
* `caseInsensitive`. So the option is read where `st` is in hand and passed in.
|
|
2667
|
+
*
|
|
2668
|
+
* **Why the one line is duplicated rather than factored.** A shared helper put
|
|
2669
|
+
* a third frame on a path taken once per node - `push` -> helper ->
|
|
2670
|
+
* `addTraceBounded` -> `EvalTrace.add` - to save one identical line. Measured
|
|
2671
|
+
* both ways at 50,000 walks of `2 + 3 * a`, the difference was inside
|
|
2672
|
+
* run-to-run noise, so this is not a speed claim; it is that the helper cost a
|
|
2673
|
+
* frame on the hot path and bought nothing, and that `addTraceBounded`'s own
|
|
2674
|
+
* docblock claims one call per push, which only this shape makes true.
|
|
2675
|
+
*
|
|
2676
|
+
* **The value stack is not bounded and must not be.** It is how visitors return
|
|
2677
|
+
* values to each other, and dropping an entry desynchronises every node above
|
|
2678
|
+
* it. Only the trace - which nothing in this library reads - is capped.
|
|
2679
|
+
*
|
|
2680
|
+
* **Every function value pushed here passes the funnel**, `refuseDangerousRead`
|
|
2681
|
+
* (`docs/a35/plan.md` § 3.3): a dangerous function is refused wherever it was
|
|
2682
|
+
* read, unless the node pushing it is a callee, a tag or `typeof`'s operand.
|
|
2683
|
+
* The call is behind `typeof value === 'function'`, so a value that is not a
|
|
2684
|
+
* function pays one comparison and no frame on this per-node path. `await`
|
|
2685
|
+
* pushes its promise here too, which is not a function.
|
|
2686
|
+
* `pushVisitorResultAsync` has no caller, and no check.
|
|
2687
|
+
*/
|
|
2688
|
+
const pushVisitorResult = (node, st, value) => {
|
|
2689
|
+
if (typeof value === 'function') {
|
|
2690
|
+
refuseDangerousRead(node, value);
|
|
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
|
+
}
|
|
2721
|
+
const result = st.result;
|
|
2722
|
+
result.stack.push(value);
|
|
2723
|
+
result.addTraceBounded(node, value, st.maxTraceItems);
|
|
2724
|
+
return value;
|
|
2725
|
+
};
|
|
2726
|
+
const popVisitorResult = (node, st) => {
|
|
2727
|
+
const result = st.result;
|
|
2728
|
+
const value = result.stack.pop();
|
|
2729
|
+
return value;
|
|
2730
|
+
};
|
|
2731
|
+
const pushVisitorResultAsync = (node, st, value) => {
|
|
2732
|
+
const result = st.result;
|
|
2733
|
+
result.stack.push(value);
|
|
2734
|
+
result.addTraceBounded(node, value, st.maxTraceItems);
|
|
2735
|
+
return value;
|
|
2736
|
+
};
|
|
2737
|
+
const popVisitorResultAsync = (node, st) => {
|
|
2738
|
+
const result = st.result;
|
|
2739
|
+
const value = result.stack.pop();
|
|
2740
|
+
return value;
|
|
2741
|
+
};
|
|
2742
|
+
|
|
2743
|
+
const literals = Registry.fromObject({
|
|
2744
|
+
'undefined': undefined,
|
|
2745
|
+
'null': null,
|
|
2746
|
+
'true': true,
|
|
2747
|
+
'false': false,
|
|
2748
|
+
}, { caseInsensitive: true });
|
|
2749
|
+
/**
|
|
2750
|
+
* Reports a resolved identifier read to the hook layer.
|
|
2751
|
+
*
|
|
2752
|
+
* The key is the one the context actually matched, so a case-insensitive
|
|
2753
|
+
* lookup reports the corrected spelling; `getKey` returns undefined for a name
|
|
2754
|
+
* the context does not hold - a literal-registry resolution, or a miss - and
|
|
2755
|
+
* the source name stands in. The path is always the source spelling, since it
|
|
2756
|
+
* is reconstructed statically.
|
|
2757
|
+
*
|
|
2758
|
+
* `scoped` marks a read of a name bound by any scope on the context's scope
|
|
2759
|
+
* stack when the name is read, whoever pushed it: an arrow-function
|
|
2760
|
+
* parameter, a declaration in the lexical scope `program.ts`,
|
|
2761
|
+
* `block-statement.ts` or `for-statement.ts` pushes, or a binding the caller
|
|
2762
|
+
* pushed with `EvalContext.push` before the walk - which is how `eval-signals`
|
|
2763
|
+
* binds a run's `AbortSignal` under `abortSignalKey`, and why that key never
|
|
2764
|
+
* reaches its `dependencies`. It is asked of `EvalContext.hasInScopes`, which
|
|
2765
|
+
* tests for the binding rather than for its value: a parameter bound to
|
|
2766
|
+
* `undefined` is still a parameter, and reading the flag off the resolved value
|
|
2767
|
+
* would make it vary with the data rather than with the expression.
|
|
2768
|
+
*
|
|
2769
|
+
* It is deliberately *not* set for `priorScopes`: those hold caller-registered,
|
|
2770
|
+
* long-lived objects and are real dependencies, so the discriminator is
|
|
2771
|
+
* lifetime rather than `instanceof EvalScope`. The property is omitted rather
|
|
2772
|
+
* than set to false, so absent reads as "not scoped" the same way it does at
|
|
2773
|
+
* the member emission sites.
|
|
2774
|
+
*
|
|
2775
|
+
* The scope stack is empty only for a walk that does not start at a `Program`
|
|
2776
|
+
* and has nothing pushed by its caller: `program.ts` pushes a scope at the root
|
|
2777
|
+
* of every walk through `EvalService`. `Stack.asArray` allocates, so that empty
|
|
2778
|
+
* case is short-circuited on length.
|
|
2779
|
+
*/
|
|
2780
|
+
const emitRead = (node, st, value) => {
|
|
2781
|
+
const key = st.context?.getKey(node.name) ?? node.name;
|
|
2782
|
+
const scoped = !!st.context
|
|
2783
|
+
&& st.context.scopes.length > 0
|
|
2784
|
+
&& st.context.hasInScopes(node.name);
|
|
2785
|
+
st.hooks.dispatchRead({
|
|
2786
|
+
kind: 'identifier',
|
|
2787
|
+
node,
|
|
2788
|
+
state: st,
|
|
2789
|
+
key,
|
|
2790
|
+
target: st.context,
|
|
2791
|
+
path: node.name,
|
|
2792
|
+
value,
|
|
2793
|
+
...(scoped ? { scoped: true } : {})
|
|
2794
|
+
});
|
|
2795
|
+
};
|
|
2796
|
+
/**
|
|
2797
|
+
* Refuses an identifier whose name is on the prototype-pollution blocklist,
|
|
2798
|
+
* before any lookup, with the member visitor's error.
|
|
2799
|
+
*
|
|
2800
|
+
* An identifier is resolved against the context, and `EvalContext.get` reads a
|
|
2801
|
+
* plain object there - the original, or a scope - with a bare property access.
|
|
2802
|
+
* So up to 0.10.x a name on the blocklist resolved to what the context inherits
|
|
2803
|
+
* from `Object.prototype` (`docs/backlog-retired.md` B6): the blocklist guarded
|
|
2804
|
+
* member access to the objects an expression holds, and the context is not one
|
|
2805
|
+
* of them. Every kind of context is refused alike, including those - a
|
|
2806
|
+
* `Registry`, a signal context's source - that hold such a name only as a key
|
|
2807
|
+
* of their own.
|
|
2808
|
+
*/
|
|
2809
|
+
const refuseDangerousName = (node) => {
|
|
2810
|
+
if (isDangerousProperty(node.name)) {
|
|
2811
|
+
throw new Error(`Access to dangerous property "${node.name}" is blocked for security reasons`);
|
|
2812
|
+
}
|
|
2813
|
+
};
|
|
2814
|
+
/**
|
|
2815
|
+
* Under `caseInsensitive`, refuses an identifier whose lookup matched a key on
|
|
2816
|
+
* the blocklist - `CONSTRUCTOR` over a context holding `constructor` - as the
|
|
2817
|
+
* member visitor re-checks `foundKey` for another object.
|
|
2818
|
+
*
|
|
2819
|
+
* The matched key is asked of `getKey`, which answers the key `get` resolves,
|
|
2820
|
+
* and only for a name `mayMatchDangerousProperty` lets through. Any other name
|
|
2821
|
+
* cannot match a blocklisted key, and an identifier must not pay a second
|
|
2822
|
+
* resolution for nothing: the performance spec's read-emission guard asserts
|
|
2823
|
+
* that, with no read hook, `getKey` is not called per identifier.
|
|
2824
|
+
*/
|
|
2825
|
+
const refuseDangerousMatch = (node, st) => {
|
|
2826
|
+
if (!st.context || !mayMatchDangerousProperty(node.name)) {
|
|
2827
|
+
return;
|
|
2828
|
+
}
|
|
2829
|
+
const matched = st.context.getKey(node.name);
|
|
2830
|
+
if (isDangerousProperty(matched)) {
|
|
2831
|
+
throw new Error(`Access to dangerous property "${String(matched)}" is blocked for security reasons`);
|
|
2832
|
+
}
|
|
2833
|
+
};
|
|
2834
|
+
const identifierVisitor = (node, st) => {
|
|
2835
|
+
if (st.options?.caseInsensitive) {
|
|
2836
|
+
return identifierVisitorCaseInsensitive(node, st);
|
|
2837
|
+
}
|
|
2838
|
+
beforeVisitor(node, st);
|
|
2839
|
+
refuseDangerousName(node);
|
|
2840
|
+
const context = st.context;
|
|
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
|
+
}
|
|
2848
|
+
const value = isThis
|
|
2849
|
+
? st.context
|
|
2850
|
+
: context?.get(node.name);
|
|
2851
|
+
// `this` resolves to the context object itself rather than to a key within
|
|
2852
|
+
// it, so it is not a read: reporting it would have a dependency tracker
|
|
2853
|
+
// record the whole context and re-fire on every change to it.
|
|
2854
|
+
if (st.hasHooks && st.hooks.hasReadHooks && !isThis) {
|
|
2855
|
+
emitRead(node, st, value);
|
|
2856
|
+
}
|
|
2857
|
+
pushVisitorResult(node, st, value);
|
|
2858
|
+
afterVisitor(node, st);
|
|
2859
|
+
};
|
|
2860
|
+
const identifierVisitorCaseInsensitive = (node, st) => {
|
|
2861
|
+
beforeVisitor(node, st);
|
|
2862
|
+
refuseDangerousName(node);
|
|
2863
|
+
const context = st.context;
|
|
2864
|
+
const isThis = !!equalIgnoreCase(node.name, 'this');
|
|
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 {
|
|
2871
|
+
refuseDangerousMatch(node, st);
|
|
2872
|
+
}
|
|
2873
|
+
const value = isThis
|
|
2874
|
+
? st.context
|
|
2875
|
+
: context?.get(node.name) ?? literals.get(node.name);
|
|
2876
|
+
if (st.hasHooks && st.hooks.hasReadHooks && !isThis) {
|
|
2877
|
+
emitRead(node, st, value);
|
|
2878
|
+
}
|
|
2879
|
+
pushVisitorResult(node, st, value);
|
|
2880
|
+
afterVisitor(node, st);
|
|
2881
|
+
};
|
|
2882
|
+
|
|
2883
|
+
const literalVisitor = (node, st) => {
|
|
2884
|
+
beforeVisitor(node, st);
|
|
2885
|
+
// A regex literal is a new `RegExp` on every evaluation, as it is in
|
|
2886
|
+
// JavaScript. Acorn builds `node.value` once, at parse time, and every walk of
|
|
2887
|
+
// the parsed tree used to push that one object - a cached `simpleEval` string
|
|
2888
|
+
// and each recompute of a compiled expression alike - so `lastIndex` carried
|
|
2889
|
+
// from one evaluation into the next, and `/a/g.test("a")` answered true, then
|
|
2890
|
+
// false. A copy starts at `lastIndex` 0, and being new it is recorded under a
|
|
2891
|
+
// member-write policy, as a literal is - `EvalContext.checkMemberWrite`. When
|
|
2892
|
+
// acorn could not build the regex in this environment, `value` is null and is
|
|
2893
|
+
// pushed as before.
|
|
2894
|
+
let value = node.value;
|
|
2895
|
+
if (value instanceof RegExp) {
|
|
2896
|
+
value = new RegExp(value);
|
|
2897
|
+
st.createdObjects?.add(value);
|
|
2898
|
+
}
|
|
2899
|
+
pushVisitorResult(node, st, value);
|
|
2900
|
+
afterVisitor(node, st);
|
|
1997
2901
|
};
|
|
2902
|
+
|
|
2903
|
+
// import { getCachedVisitorResult, setCachedVisitorResult } from './visitor-result-cache';
|
|
1998
2904
|
/**
|
|
1999
|
-
*
|
|
2000
|
-
* `object` is that scope's own object, `object` otherwise.
|
|
2001
|
-
*
|
|
2002
|
-
* `ns.fn()` reaches `fn` through the namespace, which evaluates to the scope's
|
|
2003
|
-
* object and has to keep doing so - `ns` alone, or `ns === x`, must not see
|
|
2004
|
-
* `thisArg`. So the substitution is made here, at the call, and nowhere
|
|
2005
|
-
* earlier: `evaluateMember`'s first slot is also the target of `ns.x = v`, and
|
|
2006
|
-
* a write belongs on the scope's object. Matched by identity, so it follows the
|
|
2007
|
-
* object rather than the name it was reached by; the first scope holding the
|
|
2008
|
-
* object answers, as in `EvalContext.get`. Up to 0.6.x `thisArg` was never
|
|
2009
|
-
* applied (`docs/backlog-retired.md` A7).
|
|
2010
|
-
*
|
|
2011
|
-
* `this.fn()` does not need this: `EvalContext.getThis` answers it.
|
|
2012
|
-
*
|
|
2013
|
-
* A call with no prior scopes pays one length check.
|
|
2905
|
+
* Converts value to primitive for comparison operations
|
|
2014
2906
|
*/
|
|
2015
|
-
const
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2907
|
+
const toPrimitive = (value, hint) => {
|
|
2908
|
+
if (value === null || value === undefined)
|
|
2909
|
+
return value;
|
|
2910
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ||
|
|
2911
|
+
typeof value === 'symbol' || typeof value === 'bigint')
|
|
2912
|
+
return value;
|
|
2913
|
+
// For objects, call valueOf/toString based on hint
|
|
2914
|
+
if (typeof value === 'object') {
|
|
2915
|
+
if (hint === 'number') {
|
|
2916
|
+
const valueOf = value.valueOf;
|
|
2917
|
+
if (typeof valueOf === 'function') {
|
|
2918
|
+
const result = valueOf.call(value);
|
|
2919
|
+
if (typeof result !== 'object')
|
|
2920
|
+
return result;
|
|
2921
|
+
}
|
|
2922
|
+
const toString = value.toString;
|
|
2923
|
+
if (typeof toString === 'function') {
|
|
2924
|
+
return toString.call(value);
|
|
2925
|
+
}
|
|
2926
|
+
}
|
|
2927
|
+
else {
|
|
2928
|
+
const toString = value.toString;
|
|
2929
|
+
if (typeof toString === 'function') {
|
|
2930
|
+
const result = toString.call(value);
|
|
2931
|
+
if (typeof result === 'string')
|
|
2932
|
+
return result;
|
|
2933
|
+
}
|
|
2934
|
+
const valueOf = value.valueOf;
|
|
2935
|
+
if (typeof valueOf === 'function') {
|
|
2936
|
+
const result = valueOf.call(value);
|
|
2937
|
+
if (typeof result !== 'object')
|
|
2938
|
+
return result;
|
|
2939
|
+
}
|
|
2023
2940
|
}
|
|
2024
2941
|
}
|
|
2025
|
-
return
|
|
2942
|
+
return String(value);
|
|
2026
2943
|
};
|
|
2027
2944
|
/**
|
|
2028
|
-
*
|
|
2029
|
-
*
|
|
2030
|
-
* A context with a member-write policy is asked, before the call, about a
|
|
2031
|
-
* built-in that writes into its receiver or its first argument - see
|
|
2032
|
-
* `consultMethodWrite`. The receiver asked about is the one the call is made
|
|
2033
|
-
* with: the member's object, a prior scope's `thisArg`, or the context itself
|
|
2034
|
-
* for a bare call. A context without a policy pays one field read per call.
|
|
2945
|
+
* Safely evaluates binary operations with proper type coercion
|
|
2035
2946
|
*/
|
|
2036
|
-
const
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2947
|
+
const evaluateBinaryOperation = (operator, left, right) => {
|
|
2948
|
+
switch (operator) {
|
|
2949
|
+
case '+': {
|
|
2950
|
+
// Handle string concatenation and numeric addition according to JavaScript rules
|
|
2951
|
+
const leftPrim = toPrimitive(left);
|
|
2952
|
+
const rightPrim = toPrimitive(right);
|
|
2953
|
+
if (typeof leftPrim === 'string' || typeof rightPrim === 'string') {
|
|
2954
|
+
return String(leftPrim) + String(rightPrim);
|
|
2955
|
+
}
|
|
2956
|
+
return Number(leftPrim) + Number(rightPrim);
|
|
2046
2957
|
}
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2958
|
+
case '-':
|
|
2959
|
+
return Number(toPrimitive(left, 'number')) - Number(toPrimitive(right, 'number'));
|
|
2960
|
+
case '*':
|
|
2961
|
+
return Number(toPrimitive(left, 'number')) * Number(toPrimitive(right, 'number'));
|
|
2962
|
+
case '/':
|
|
2963
|
+
return Number(toPrimitive(left, 'number')) / Number(toPrimitive(right, 'number'));
|
|
2964
|
+
case '%':
|
|
2965
|
+
return Number(toPrimitive(left, 'number')) % Number(toPrimitive(right, 'number'));
|
|
2966
|
+
case '**':
|
|
2967
|
+
return Number(toPrimitive(left, 'number')) ** Number(toPrimitive(right, 'number'));
|
|
2968
|
+
// Bitwise operations - convert to 32-bit integers
|
|
2969
|
+
case '<<':
|
|
2970
|
+
return (Number(toPrimitive(left, 'number')) | 0) << (Number(toPrimitive(right, 'number')) | 0);
|
|
2971
|
+
case '>>':
|
|
2972
|
+
return (Number(toPrimitive(left, 'number')) | 0) >> (Number(toPrimitive(right, 'number')) | 0);
|
|
2973
|
+
case '>>>':
|
|
2974
|
+
return (Number(toPrimitive(left, 'number')) >>> 0) >>> (Number(toPrimitive(right, 'number')) | 0);
|
|
2975
|
+
case '&':
|
|
2976
|
+
return (Number(toPrimitive(left, 'number')) | 0) & (Number(toPrimitive(right, 'number')) | 0);
|
|
2977
|
+
case '^':
|
|
2978
|
+
return (Number(toPrimitive(left, 'number')) | 0) ^ (Number(toPrimitive(right, 'number')) | 0);
|
|
2979
|
+
case '|':
|
|
2980
|
+
return (Number(toPrimitive(left, 'number')) | 0) | (Number(toPrimitive(right, 'number')) | 0);
|
|
2981
|
+
// Comparison operations - follow JavaScript's abstract comparison rules
|
|
2982
|
+
case '==':
|
|
2983
|
+
return left == right;
|
|
2984
|
+
case '!=':
|
|
2985
|
+
return left != right;
|
|
2986
|
+
case '===':
|
|
2987
|
+
return left === right;
|
|
2988
|
+
case '!==':
|
|
2989
|
+
return left !== right;
|
|
2990
|
+
case '<': {
|
|
2991
|
+
const leftComp = toPrimitive(left, 'number');
|
|
2992
|
+
const rightComp = toPrimitive(right, 'number');
|
|
2993
|
+
// If both are strings, do string comparison
|
|
2994
|
+
if (typeof leftComp === 'string' && typeof rightComp === 'string') {
|
|
2995
|
+
return leftComp < rightComp;
|
|
2996
|
+
}
|
|
2997
|
+
return Number(leftComp) < Number(rightComp);
|
|
2057
2998
|
}
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2999
|
+
case '<=': {
|
|
3000
|
+
const leftLE = toPrimitive(left, 'number');
|
|
3001
|
+
const rightLE = toPrimitive(right, 'number');
|
|
3002
|
+
if (typeof leftLE === 'string' && typeof rightLE === 'string') {
|
|
3003
|
+
return leftLE <= rightLE;
|
|
3004
|
+
}
|
|
3005
|
+
return Number(leftLE) <= Number(rightLE);
|
|
2061
3006
|
}
|
|
2062
|
-
|
|
2063
|
-
|
|
3007
|
+
case '>': {
|
|
3008
|
+
const leftGT = toPrimitive(left, 'number');
|
|
3009
|
+
const rightGT = toPrimitive(right, 'number');
|
|
3010
|
+
if (typeof leftGT === 'string' && typeof rightGT === 'string') {
|
|
3011
|
+
return leftGT > rightGT;
|
|
3012
|
+
}
|
|
3013
|
+
return Number(leftGT) > Number(rightGT);
|
|
3014
|
+
}
|
|
3015
|
+
case '>=': {
|
|
3016
|
+
const leftGE = toPrimitive(left, 'number');
|
|
3017
|
+
const rightGE = toPrimitive(right, 'number');
|
|
3018
|
+
if (typeof leftGE === 'string' && typeof rightGE === 'string') {
|
|
3019
|
+
return leftGE >= rightGE;
|
|
3020
|
+
}
|
|
3021
|
+
return Number(leftGE) >= Number(rightGE);
|
|
3022
|
+
}
|
|
3023
|
+
// Special operations
|
|
3024
|
+
case 'in': {
|
|
3025
|
+
if (typeof left === 'string' || typeof left === 'number' || typeof left === 'symbol') {
|
|
3026
|
+
if (right == null) {
|
|
3027
|
+
throw new Error(`Cannot use 'in' operator with null or undefined right operand`);
|
|
3028
|
+
}
|
|
3029
|
+
return left in right;
|
|
3030
|
+
}
|
|
3031
|
+
throw new Error(`Invalid left operand for 'in' operator: ${typeof left}`);
|
|
3032
|
+
}
|
|
3033
|
+
case 'instanceof': {
|
|
3034
|
+
if (typeof right === 'function') {
|
|
3035
|
+
return left instanceof right;
|
|
3036
|
+
}
|
|
3037
|
+
throw new Error(`Right operand of 'instanceof' is not a constructor`);
|
|
3038
|
+
}
|
|
3039
|
+
case '??':
|
|
3040
|
+
return left ?? right;
|
|
3041
|
+
default:
|
|
3042
|
+
throw new Error(`Unsupported binary operator: ${operator}`);
|
|
2064
3043
|
}
|
|
2065
|
-
afterVisitor(node, st);
|
|
2066
3044
|
};
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
//
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
const
|
|
2081
|
-
|
|
3045
|
+
/**
|
|
3046
|
+
* Safely evaluates binary expressions with proper type handling
|
|
3047
|
+
*/
|
|
3048
|
+
const binaryExpressionVisitor = (node, st, callback) => {
|
|
3049
|
+
beforeVisitor(node, st);
|
|
3050
|
+
// Disable visitor result caching for now due to context sensitivity issues
|
|
3051
|
+
// const cachedResult = getCachedVisitorResult(node, st.context);
|
|
3052
|
+
// if (cachedResult !== undefined) {
|
|
3053
|
+
// pushVisitorResult(node, st, cachedResult);
|
|
3054
|
+
// afterVisitor(node, st);
|
|
3055
|
+
// return;
|
|
3056
|
+
// }
|
|
3057
|
+
callback(node.left, st);
|
|
3058
|
+
const left = popVisitorResult(node, st);
|
|
3059
|
+
callback(node.right, st);
|
|
3060
|
+
const right = popVisitorResult(node, st);
|
|
3061
|
+
const value = evaluateBinaryOperation(node.operator, left, right);
|
|
3062
|
+
// Disable visitor result caching for now
|
|
3063
|
+
// setCachedVisitorResult(node, st.context, value);
|
|
3064
|
+
pushVisitorResult(node, st, value);
|
|
3065
|
+
afterVisitor(node, st);
|
|
2082
3066
|
};
|
|
2083
3067
|
|
|
2084
3068
|
/**
|
|
@@ -2165,6 +3149,11 @@ const conditionalExpressionVisitor = (node, st, callback) => {
|
|
|
2165
3149
|
|
|
2166
3150
|
const unaryExpressionVisitor = (node, st, callback) => {
|
|
2167
3151
|
beforeVisitor(node, st);
|
|
3152
|
+
// `typeof eval` reads a dangerous function to name its type, and hands it
|
|
3153
|
+
// on to nothing, so its operand is exempt from the funnel.
|
|
3154
|
+
if (node.operator === 'typeof') {
|
|
3155
|
+
exemptFromFunnel(node.argument);
|
|
3156
|
+
}
|
|
2168
3157
|
callback(node.argument, st);
|
|
2169
3158
|
const argument = popVisitorResult(node, st);
|
|
2170
3159
|
const value = node.operator === '-' ? -argument :
|
|
@@ -2225,6 +3214,9 @@ const logicalExpressionVisitor = (node, st, callback) => {
|
|
|
2225
3214
|
|
|
2226
3215
|
const thisExpressionVisitor = (node, st) => {
|
|
2227
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);
|
|
2228
3220
|
const value = st.context;
|
|
2229
3221
|
pushVisitorResult(node, st, value);
|
|
2230
3222
|
afterVisitor(node, st);
|
|
@@ -2263,6 +3255,12 @@ const isConstructorSafe = (constructor, constructorName) => {
|
|
|
2263
3255
|
constructor === (async function* () { }).constructor) {
|
|
2264
3256
|
return false;
|
|
2265
3257
|
}
|
|
3258
|
+
// A dangerous function by value, under whatever name it was reached
|
|
3259
|
+
// (`dangerous-values.ts`). Up to 0.12.0 only a bare identifier's name was
|
|
3260
|
+
// checked (`docs/backlog.md` A35).
|
|
3261
|
+
if (isDangerousValue(constructor)) {
|
|
3262
|
+
return false;
|
|
3263
|
+
}
|
|
2266
3264
|
try {
|
|
2267
3265
|
// Check constructor string for dangerous patterns
|
|
2268
3266
|
const constructorString = constructor.toString();
|
|
@@ -2300,6 +3298,9 @@ const safeNew = (constructor, args, constructorName) => {
|
|
|
2300
3298
|
const newExpressionVisitor = (node, st, callback) => {
|
|
2301
3299
|
beforeVisitor(node, st);
|
|
2302
3300
|
const args = evaluateArray(node, node.arguments, st, callback);
|
|
3301
|
+
// The callee's own value is checked by `safeNew`, with the constructor's
|
|
3302
|
+
// message; what is read under it still passes the funnel.
|
|
3303
|
+
exemptFromFunnel(node.callee);
|
|
2303
3304
|
callback(node.callee, st);
|
|
2304
3305
|
const constructor = popVisitorResult(node, st);
|
|
2305
3306
|
// Try to get constructor name for security checks
|
|
@@ -2334,20 +3335,63 @@ const templateLiteralVisitor = (node, st, callback) => {
|
|
|
2334
3335
|
afterVisitor(node, st);
|
|
2335
3336
|
};
|
|
2336
3337
|
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
3338
|
+
/**
|
|
3339
|
+
* The tag's arguments: the template's cooked strings, then the value of each
|
|
3340
|
+
* of its expressions, evaluated in order.
|
|
3341
|
+
*/
|
|
3342
|
+
const templateArguments = (node, st, callback) => {
|
|
3343
|
+
const values = node.quasi.expressions.map((expression) => {
|
|
2342
3344
|
callback(expression, st);
|
|
2343
|
-
|
|
2344
|
-
return value;
|
|
2345
|
-
});
|
|
2346
|
-
const quasi = node.quasi.quasis.map((templateElement) => {
|
|
2347
|
-
return templateElement.value.cooked;
|
|
3345
|
+
return popVisitorResult(node, st);
|
|
2348
3346
|
});
|
|
2349
|
-
const
|
|
2350
|
-
|
|
3347
|
+
const strings = node.quasi.quasis.map((templateElement) => templateElement.value.cooked);
|
|
3348
|
+
return [strings, ...values];
|
|
3349
|
+
};
|
|
3350
|
+
/**
|
|
3351
|
+
* Calls the tag as a call calls its callee (`call-expression.ts`): past the
|
|
3352
|
+
* context's write policy, then through the call sandbox.
|
|
3353
|
+
*
|
|
3354
|
+
* - A member tag goes through `callMember`, as a member call does: `obj.m`
|
|
3355
|
+
* is called with `obj` as `this` - or a prior scope's `thisArg` - and checked
|
|
3356
|
+
* under the property name, a computed key included. Its own
|
|
3357
|
+
* `MemberExpression` is evaluated by `evaluateMember`, not visited, so it
|
|
3358
|
+
* raises no `before` or `after` node-hook events and leaves no trace entry,
|
|
3359
|
+
* as a member callee does. Its read event still fires.
|
|
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.
|
|
3363
|
+
*
|
|
3364
|
+
* The tag is evaluated before the template's expressions, as in JavaScript;
|
|
3365
|
+
* a call evaluates its arguments first.
|
|
3366
|
+
*
|
|
3367
|
+
* Up to 0.12.0 the tag was applied directly, with the context as `this` and no
|
|
3368
|
+
* check at all, so `` Function`return 7`() `` ran (`docs/backlog-retired.md`
|
|
3369
|
+
* A36).
|
|
3370
|
+
*/
|
|
3371
|
+
const taggedTemplateExpressionVisitor = (node, st, callback) => {
|
|
3372
|
+
beforeVisitor(node, st);
|
|
3373
|
+
if (node.tag.type === 'MemberExpression') {
|
|
3374
|
+
const member = evaluateMember(node.tag, st, callback);
|
|
3375
|
+
const args = templateArguments(node, st, callback);
|
|
3376
|
+
const value = callMember(st, member, args, false);
|
|
3377
|
+
pushVisitorResult(node, st, value);
|
|
3378
|
+
}
|
|
3379
|
+
else {
|
|
3380
|
+
// As a call's callee: the tag's own value is checked by `safeCall`.
|
|
3381
|
+
exemptFromFunnel(node.tag);
|
|
3382
|
+
callback(node.tag, st);
|
|
3383
|
+
const tag = popVisitorResult(node, st);
|
|
3384
|
+
const args = templateArguments(node, st, callback);
|
|
3385
|
+
const tagName = node.tag.type === 'Identifier' ? node.tag.name : undefined;
|
|
3386
|
+
const created = st.createdObjects;
|
|
3387
|
+
if (created) {
|
|
3388
|
+
consultMethodWrite(st, created, tag, st.context, args);
|
|
3389
|
+
}
|
|
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);
|
|
3393
|
+
pushVisitorResult(node, st, value);
|
|
3394
|
+
}
|
|
2351
3395
|
afterVisitor(node, st);
|
|
2352
3396
|
};
|
|
2353
3397
|
|
|
@@ -2447,6 +3491,7 @@ const getDefaultVisitors = () => {
|
|
|
2447
3491
|
visitors['AssignmentExpression'] = assignmentExpressionVisitor;
|
|
2448
3492
|
visitors['UpdateExpression'] = updateExpressionVisitor;
|
|
2449
3493
|
visitors['ArrowFunctionExpression'] = arrowFunctionExpressionVisitor;
|
|
3494
|
+
visitors['SequenceExpression'] = sequenceExpressionVisitor;
|
|
2450
3495
|
// The statement family. `Program` is the root of every walk that arrives
|
|
2451
3496
|
// through `EvalService`, whose parser options leave `extractExpressions` off.
|
|
2452
3497
|
visitors['Program'] = programVisitor;
|
|
@@ -2878,15 +3923,15 @@ const assignToBinding = (st, key, value, name) => {
|
|
|
2878
3923
|
if (!st.context) {
|
|
2879
3924
|
throw new Error(`Context is not set.`);
|
|
2880
3925
|
}
|
|
2881
|
-
const scope = st.context
|
|
3926
|
+
const scope = contextScopeHolding(st.context, key);
|
|
2882
3927
|
if (!scope) {
|
|
2883
|
-
st.context
|
|
3928
|
+
contextSet(st.context, key, value);
|
|
2884
3929
|
return;
|
|
2885
3930
|
}
|
|
2886
3931
|
if (st.isConstBinding(scope, key)) {
|
|
2887
3932
|
throw new Error(`Assignment to constant variable "${name}".`);
|
|
2888
3933
|
}
|
|
2889
|
-
st.context
|
|
3934
|
+
contextSetInScope(st.context, key, value);
|
|
2890
3935
|
};
|
|
2891
3936
|
|
|
2892
3937
|
const assignmentOperators = {
|
|
@@ -3533,16 +4578,23 @@ const arrowFunctionExpressionVisitor = (node, st, callback) => {
|
|
|
3533
4578
|
// evaluation on the same context. The body is re-entrant `evaluate`, which
|
|
3534
4579
|
// rethrows, so the pop needs `finally`.
|
|
3535
4580
|
//
|
|
3536
|
-
// The `try` opens *after* the push, never around it. `
|
|
3537
|
-
//
|
|
3538
|
-
//
|
|
3539
|
-
// evaluatePatterns into a pop as well.
|
|
3540
|
-
|
|
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);
|
|
3541
4593
|
try {
|
|
3542
4594
|
return evaluate(node.body, st);
|
|
3543
4595
|
}
|
|
3544
4596
|
finally {
|
|
3545
|
-
st.context
|
|
4597
|
+
contextPop(st.context);
|
|
3546
4598
|
}
|
|
3547
4599
|
};
|
|
3548
4600
|
// A new function per evaluation of this node, so recorded under a
|
|
@@ -3636,14 +4688,19 @@ const dispatchStatement = (statement, st, callback) => {
|
|
|
3636
4688
|
* on the per-walk `EvalState`, and one context can back any number of
|
|
3637
4689
|
* evaluations: a scope this visitor failed to pop would outlive the walk and be
|
|
3638
4690
|
* read first by every later evaluation on that context. The `try` opens *after*
|
|
3639
|
-
* the push, never around it - `
|
|
3640
|
-
*
|
|
3641
|
-
*
|
|
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`.
|
|
3642
4699
|
*/
|
|
3643
4700
|
const programVisitor = (node, st, callback) => {
|
|
3644
4701
|
beforeVisitor(node, st);
|
|
3645
4702
|
let completion = EMPTY_COMPLETION;
|
|
3646
|
-
st.context
|
|
4703
|
+
contextPush(st.context, {}, st.options);
|
|
3647
4704
|
try {
|
|
3648
4705
|
for (const statement of node.body) {
|
|
3649
4706
|
const value = dispatchStatement(statement, st, callback);
|
|
@@ -3653,7 +4710,7 @@ const programVisitor = (node, st, callback) => {
|
|
|
3653
4710
|
}
|
|
3654
4711
|
}
|
|
3655
4712
|
finally {
|
|
3656
|
-
st.context
|
|
4713
|
+
contextPop(st.context);
|
|
3657
4714
|
}
|
|
3658
4715
|
pushVisitorResult(node, st, completion);
|
|
3659
4716
|
afterVisitor(node, st);
|
|
@@ -3725,14 +4782,15 @@ const emptyStatementVisitor = (node, st) => {
|
|
|
3725
4782
|
* on the per-walk `EvalState`, and one context can back any number of
|
|
3726
4783
|
* evaluations: a scope this visitor failed to pop would outlive the walk and be
|
|
3727
4784
|
* read first by every later evaluation on that context. The `try` opens *after*
|
|
3728
|
-
* the push, never around it - `
|
|
3729
|
-
*
|
|
3730
|
-
*
|
|
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`).
|
|
3731
4789
|
*/
|
|
3732
4790
|
const blockStatementVisitor = (node, st, callback) => {
|
|
3733
4791
|
beforeVisitor(node, st);
|
|
3734
4792
|
let completion = EMPTY_COMPLETION;
|
|
3735
|
-
st.context
|
|
4793
|
+
contextPush(st.context, {}, st.options);
|
|
3736
4794
|
try {
|
|
3737
4795
|
for (const statement of node.body) {
|
|
3738
4796
|
const value = dispatchStatement(statement, st, callback);
|
|
@@ -3742,7 +4800,7 @@ const blockStatementVisitor = (node, st, callback) => {
|
|
|
3742
4800
|
}
|
|
3743
4801
|
}
|
|
3744
4802
|
finally {
|
|
3745
|
-
st.context
|
|
4803
|
+
contextPop(st.context);
|
|
3746
4804
|
}
|
|
3747
4805
|
pushVisitorResult(node, st, completion);
|
|
3748
4806
|
afterVisitor(node, st);
|
|
@@ -3887,14 +4945,16 @@ const ifStatementVisitor = (node, st, callback) => {
|
|
|
3887
4945
|
* precondition argument - a `for` body throwing on iteration 3 - so every exit
|
|
3888
4946
|
* path matters: `init`, `test`, `body` and `update` can each throw, the
|
|
3889
4947
|
* dispatcher can reject the body, and the budget can end the loop from inside
|
|
3890
|
-
* the `try`. The `try` opens *after* the push, never around it - `
|
|
3891
|
-
*
|
|
3892
|
-
*
|
|
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`).
|
|
3893
4953
|
*/
|
|
3894
4954
|
const forStatementVisitor = (node, st, callback) => {
|
|
3895
4955
|
beforeVisitor(node, st);
|
|
3896
4956
|
let completion = EMPTY_COMPLETION;
|
|
3897
|
-
st.context
|
|
4957
|
+
contextPush(st.context, {}, st.options);
|
|
3898
4958
|
try {
|
|
3899
4959
|
if (node.init) {
|
|
3900
4960
|
if (node.init.type === 'VariableDeclaration') {
|
|
@@ -3927,12 +4987,33 @@ const forStatementVisitor = (node, st, callback) => {
|
|
|
3927
4987
|
}
|
|
3928
4988
|
}
|
|
3929
4989
|
finally {
|
|
3930
|
-
st.context
|
|
4990
|
+
contextPop(st.context);
|
|
3931
4991
|
}
|
|
3932
4992
|
pushVisitorResult(node, st, completion);
|
|
3933
4993
|
afterVisitor(node, st);
|
|
3934
4994
|
};
|
|
3935
4995
|
|
|
4996
|
+
/**
|
|
4997
|
+
* Evaluates a sequence, `(a, b, c)`: each expression in order, the value the
|
|
4998
|
+
* last one's.
|
|
4999
|
+
*
|
|
5000
|
+
* Registered rather than left to `acorn-walk`'s base walker, which walked the
|
|
5001
|
+
* expressions and popped none of them, so every value but the last stayed on
|
|
5002
|
+
* the stack: `(0, 1)` left one entry and `((1, 2), 3)` two, a reused state
|
|
5003
|
+
* accumulated them, and no hook saw the node (`docs/backlog-retired.md` A37).
|
|
5004
|
+
* Each value is popped as it arrives, so the visitor leaves exactly one.
|
|
5005
|
+
*/
|
|
5006
|
+
const sequenceExpressionVisitor = (node, st, callback) => {
|
|
5007
|
+
beforeVisitor(node, st);
|
|
5008
|
+
let value;
|
|
5009
|
+
for (const expression of node.expressions) {
|
|
5010
|
+
callback(expression, st);
|
|
5011
|
+
value = popVisitorResult(node, st);
|
|
5012
|
+
}
|
|
5013
|
+
pushVisitorResult(node, st, value);
|
|
5014
|
+
afterVisitor(node, st);
|
|
5015
|
+
};
|
|
5016
|
+
|
|
3936
5017
|
const defaultEvalScopeOptions = {
|
|
3937
5018
|
global: false,
|
|
3938
5019
|
caseInsensitive: false,
|
|
@@ -4417,7 +5498,7 @@ class EvalContext {
|
|
|
4417
5498
|
* @returns True when a pushed scope bound the key and received the write.
|
|
4418
5499
|
*/
|
|
4419
5500
|
setInScope(key, value) {
|
|
4420
|
-
const scope = this
|
|
5501
|
+
const scope = contextScopeHolding(this, key);
|
|
4421
5502
|
if (!scope) {
|
|
4422
5503
|
return false;
|
|
4423
5504
|
}
|
|
@@ -5691,7 +6772,15 @@ class EvalState {
|
|
|
5691
6772
|
this._options = options;
|
|
5692
6773
|
this._isAsync = isAsync;
|
|
5693
6774
|
this._hooks = adoptHooks(options);
|
|
5694
|
-
|
|
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)
|
|
5695
6784
|
? new WeakSet()
|
|
5696
6785
|
: undefined;
|
|
5697
6786
|
// Options configure the registry this state owns, never an adopted one -
|