@bascik/language-server 0.1.0-rc.5 → 0.1.0-rc.6
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/README.md +72 -30
- package/dist/analyzer.d.ts +70 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +625 -75
- package/dist/analyzer.js.map +1 -1
- package/dist/api-rules.d.ts +7 -0
- package/dist/api-rules.d.ts.map +1 -1
- package/dist/api-rules.js +235 -16
- package/dist/api-rules.js.map +1 -1
- package/dist/bin.js +4 -1
- package/dist/bin.js.map +1 -1
- package/dist/check.d.ts +14 -1
- package/dist/check.d.ts.map +1 -1
- package/dist/check.js +116 -10
- package/dist/check.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +27 -8
- package/dist/cli.js.map +1 -1
- package/dist/compatibility-rules.json +21 -3
- package/dist/component-metadata.d.ts.map +1 -1
- package/dist/component-metadata.js +3 -1
- package/dist/component-metadata.js.map +1 -1
- package/dist/component-usages.d.ts +47 -0
- package/dist/component-usages.d.ts.map +1 -0
- package/dist/component-usages.js +495 -0
- package/dist/component-usages.js.map +1 -0
- package/dist/config-reader.d.ts +41 -0
- package/dist/config-reader.d.ts.map +1 -0
- package/dist/config-reader.js +461 -0
- package/dist/config-reader.js.map +1 -0
- package/dist/config.d.ts +10 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +25 -30
- package/dist/config.js.map +1 -1
- package/dist/hand-written-escaper.d.ts +35 -0
- package/dist/hand-written-escaper.d.ts.map +1 -0
- package/dist/hand-written-escaper.js +739 -0
- package/dist/hand-written-escaper.js.map +1 -0
- package/dist/module-specifiers.d.ts +25 -0
- package/dist/module-specifiers.d.ts.map +1 -1
- package/dist/module-specifiers.js +234 -165
- package/dist/module-specifiers.js.map +1 -1
- package/dist/name-groups.d.ts +36 -0
- package/dist/name-groups.d.ts.map +1 -0
- package/dist/name-groups.js +198 -0
- package/dist/name-groups.js.map +1 -0
- package/dist/plain-holes.d.ts +116 -0
- package/dist/plain-holes.d.ts.map +1 -0
- package/dist/plain-holes.js +1970 -0
- package/dist/plain-holes.js.map +1 -0
- package/dist/project.d.ts +58 -1
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +146 -8
- package/dist/project.js.map +1 -1
- package/dist/references.d.ts +47 -0
- package/dist/references.d.ts.map +1 -0
- package/dist/references.js +241 -0
- package/dist/references.js.map +1 -0
- package/dist/script-modules.d.ts +60 -0
- package/dist/script-modules.d.ts.map +1 -0
- package/dist/script-modules.js +382 -0
- package/dist/script-modules.js.map +1 -0
- package/dist/server-script-rules.d.ts +94 -1
- package/dist/server-script-rules.d.ts.map +1 -1
- package/dist/server-script-rules.js +2572 -230
- package/dist/server-script-rules.js.map +1 -1
- package/dist/server-script-templates.d.ts +250 -0
- package/dist/server-script-templates.d.ts.map +1 -0
- package/dist/server-script-templates.js +1685 -0
- package/dist/server-script-templates.js.map +1 -0
- package/dist/server.d.ts +191 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +822 -50
- package/dist/server.js.map +1 -1
- package/dist/start.d.ts +10 -1
- package/dist/start.d.ts.map +1 -1
- package/dist/start.js +14 -2
- package/dist/start.js.map +1 -1
- package/dist/string-typed-flow.d.ts +26 -0
- package/dist/string-typed-flow.d.ts.map +1 -0
- package/dist/string-typed-flow.js +496 -0
- package/dist/string-typed-flow.js.map +1 -0
- package/dist/typescript-blocks.d.ts +205 -0
- package/dist/typescript-blocks.d.ts.map +1 -0
- package/dist/typescript-blocks.js +464 -0
- package/dist/typescript-blocks.js.map +1 -0
- package/dist/typescript-editor-mode.d.ts +28 -0
- package/dist/typescript-editor-mode.d.ts.map +1 -0
- package/dist/typescript-editor-mode.js +161 -0
- package/dist/typescript-editor-mode.js.map +1 -0
- package/dist/typescript-engine.d.ts +115 -0
- package/dist/typescript-engine.d.ts.map +1 -0
- package/dist/typescript-engine.js +286 -0
- package/dist/typescript-engine.js.map +1 -0
- package/dist/typescript-features.d.ts +136 -0
- package/dist/typescript-features.d.ts.map +1 -0
- package/dist/typescript-features.js +374 -0
- package/dist/typescript-features.js.map +1 -0
- package/dist/typescript-loader.d.ts +55 -0
- package/dist/typescript-loader.d.ts.map +1 -0
- package/dist/typescript-loader.js +202 -0
- package/dist/typescript-loader.js.map +1 -0
- package/dist/typescript-lsp.d.ts +8 -0
- package/dist/typescript-lsp.d.ts.map +1 -0
- package/dist/typescript-lsp.js +115 -0
- package/dist/typescript-lsp.js.map +1 -0
- package/dist/typescript-service.d.ts +350 -0
- package/dist/typescript-service.d.ts.map +1 -0
- package/dist/typescript-service.js +1465 -0
- package/dist/typescript-service.js.map +1 -0
- package/dist/typescript-worker.d.ts +11 -0
- package/dist/typescript-worker.d.ts.map +1 -0
- package/dist/typescript-worker.js +107 -0
- package/dist/typescript-worker.js.map +1 -0
- package/package.json +20 -7
|
@@ -0,0 +1,1970 @@
|
|
|
1
|
+
const LITERAL_WORDS = new Set([
|
|
2
|
+
"true",
|
|
3
|
+
"false",
|
|
4
|
+
"null",
|
|
5
|
+
"undefined",
|
|
6
|
+
"NaN",
|
|
7
|
+
"Infinity",
|
|
8
|
+
]);
|
|
9
|
+
/** Methods that return text or a number made only from their receiver and arguments. */
|
|
10
|
+
const STRING_METHODS = new Set("toFixed toPrecision toExponential toString toUpperCase toLowerCase toLocaleUpperCase toLocaleLowerCase toLocaleString trim trimStart trimEnd slice substring substr padStart padEnd charAt at repeat normalize replace replaceAll concat valueOf toISOString toDateString toTimeString toUTCString toLocaleDateString toLocaleTimeString".split(" "));
|
|
11
|
+
/** Methods that read request data without changing it. */
|
|
12
|
+
const REQUEST_READ_METHODS = new Set([
|
|
13
|
+
...STRING_METHODS,
|
|
14
|
+
"get",
|
|
15
|
+
"getAll",
|
|
16
|
+
"has",
|
|
17
|
+
"json",
|
|
18
|
+
"text",
|
|
19
|
+
"formData",
|
|
20
|
+
]);
|
|
21
|
+
/**
|
|
22
|
+
* Members of request data (the request, a URL, context) that are text,
|
|
23
|
+
* not objects other code could change: url, method, a URL's parts.
|
|
24
|
+
*/
|
|
25
|
+
const REQUEST_TEXT_MEMBERS = new Set("url method href origin protocol username password host hostname port pathname search hash remoteIp mode credentials cache redirect referrer referrerPolicy integrity destination keepalive bodyUsed size".split(" "));
|
|
26
|
+
/** Calls that can rewrite any built-in or global: a file that makes one gets no wrap fix. */
|
|
27
|
+
const REWRITING_MEMBERS = {
|
|
28
|
+
Object: new Set(["defineProperty", "defineProperties", "setPrototypeOf"]),
|
|
29
|
+
Reflect: new Set(["defineProperty", "set", "setPrototypeOf"]),
|
|
30
|
+
};
|
|
31
|
+
/** Globals whose call returns text made only from their arguments. */
|
|
32
|
+
const STRING_CALLS = new Set(["String", "encodeURIComponent", "encodeURI"]);
|
|
33
|
+
/** Globals whose call returns a number or boolean, whatever the arguments. */
|
|
34
|
+
const NUMBER_CALLS = new Set(["Number", "Boolean", "parseInt", "parseFloat"]);
|
|
35
|
+
const ASSIGN_OPS = new Set([
|
|
36
|
+
"=",
|
|
37
|
+
"+=",
|
|
38
|
+
"-=",
|
|
39
|
+
"*=",
|
|
40
|
+
"/=",
|
|
41
|
+
"%=",
|
|
42
|
+
"**=",
|
|
43
|
+
"<<=",
|
|
44
|
+
">>=",
|
|
45
|
+
">>>=",
|
|
46
|
+
"&=",
|
|
47
|
+
"|=",
|
|
48
|
+
"^=",
|
|
49
|
+
"&&=",
|
|
50
|
+
"||=",
|
|
51
|
+
"??=",
|
|
52
|
+
]);
|
|
53
|
+
const OP_CHARS = new Set("=+-*/%&|^<>!?~");
|
|
54
|
+
const NOT_PARAMS_BEFORE = new Set(["if", "for", "while", "switch", "with"]);
|
|
55
|
+
const PARAM_MODIFIERS = new Set([
|
|
56
|
+
"public",
|
|
57
|
+
"private",
|
|
58
|
+
"protected",
|
|
59
|
+
"readonly",
|
|
60
|
+
"override",
|
|
61
|
+
]);
|
|
62
|
+
const MEMBER_MODIFIERS = new Set([
|
|
63
|
+
"async",
|
|
64
|
+
"static",
|
|
65
|
+
"get",
|
|
66
|
+
"set",
|
|
67
|
+
"public",
|
|
68
|
+
"private",
|
|
69
|
+
"protected",
|
|
70
|
+
"override",
|
|
71
|
+
"readonly",
|
|
72
|
+
"abstract",
|
|
73
|
+
"declare",
|
|
74
|
+
"accessor",
|
|
75
|
+
]);
|
|
76
|
+
const TYPE_DECLARATIONS = new Set([
|
|
77
|
+
"class",
|
|
78
|
+
"enum",
|
|
79
|
+
"interface",
|
|
80
|
+
"type",
|
|
81
|
+
"namespace",
|
|
82
|
+
"module",
|
|
83
|
+
"declare",
|
|
84
|
+
]);
|
|
85
|
+
const UNKNOWN_WORDS = new Set([
|
|
86
|
+
"this",
|
|
87
|
+
"super",
|
|
88
|
+
"arguments",
|
|
89
|
+
"globalThis",
|
|
90
|
+
"window",
|
|
91
|
+
"self",
|
|
92
|
+
"function",
|
|
93
|
+
"class",
|
|
94
|
+
"yield",
|
|
95
|
+
"import",
|
|
96
|
+
"async",
|
|
97
|
+
"delete",
|
|
98
|
+
]);
|
|
99
|
+
const PLAIN_PARAMETER_TYPE = /^\s*\|?\s*(?:number|bigint|boolean|true|false)(?:\s*\|\s*(?:number|bigint|boolean|true|false))*\s*$/;
|
|
100
|
+
/**
|
|
101
|
+
* Whether a string or template literal's value, its escapes decoded,
|
|
102
|
+
* holds `&` or `<`: html would escape it, so an entity (`—`) or a
|
|
103
|
+
* tag written as `\x3cb>` would show as text after wrapping.
|
|
104
|
+
*/
|
|
105
|
+
export function holdsMarkupCharacter(raw) {
|
|
106
|
+
if (raw.includes("&") || raw.includes("<"))
|
|
107
|
+
return true;
|
|
108
|
+
if (!raw.includes("\\"))
|
|
109
|
+
return false;
|
|
110
|
+
const decoded = raw.replace(/\\(?:x([0-9a-fA-F]{2})|u\{([0-9a-fA-F]+)\}|u([0-9a-fA-F]{4})|([0-3][0-7]{0,2}|[4-7][0-7]?)|([\s\S]))/g, (_m, x, braced, u, octal, other) => {
|
|
111
|
+
if (x !== undefined)
|
|
112
|
+
return String.fromCharCode(parseInt(x, 16));
|
|
113
|
+
if (braced !== undefined) {
|
|
114
|
+
const code = parseInt(braced, 16);
|
|
115
|
+
return code <= 0x10ffff ? String.fromCodePoint(code) : "";
|
|
116
|
+
}
|
|
117
|
+
if (u !== undefined)
|
|
118
|
+
return String.fromCharCode(parseInt(u, 16));
|
|
119
|
+
if (octal !== undefined)
|
|
120
|
+
return String.fromCharCode(parseInt(octal, 8));
|
|
121
|
+
return other === "\n" || other === "\r" ? "" : (other ?? "");
|
|
122
|
+
});
|
|
123
|
+
return decoded.includes("&") || decoded.includes("<");
|
|
124
|
+
}
|
|
125
|
+
/** Built-ins the checker trusts by name, while the file leaves them alone. */
|
|
126
|
+
const TRUSTED_GLOBALS = new Set([
|
|
127
|
+
"String",
|
|
128
|
+
"encodeURIComponent",
|
|
129
|
+
"encodeURI",
|
|
130
|
+
"Number",
|
|
131
|
+
"Boolean",
|
|
132
|
+
"parseInt",
|
|
133
|
+
"parseFloat",
|
|
134
|
+
"Math",
|
|
135
|
+
"Date",
|
|
136
|
+
"process",
|
|
137
|
+
"URL",
|
|
138
|
+
"URLSearchParams",
|
|
139
|
+
]);
|
|
140
|
+
/** Names for the global object: writing through one can replace any built-in. */
|
|
141
|
+
const GLOBAL_OBJECTS = new Set(["globalThis", "global", "window", "self"]);
|
|
142
|
+
/** Names that reach an object's prototype. */
|
|
143
|
+
const PROTOTYPE_WORDS = new Set([
|
|
144
|
+
"prototype",
|
|
145
|
+
"__proto__",
|
|
146
|
+
"setPrototypeOf",
|
|
147
|
+
"getPrototypeOf",
|
|
148
|
+
]);
|
|
149
|
+
const PRIM = { k: "prim" };
|
|
150
|
+
const HTML = { k: "html" };
|
|
151
|
+
const HTML_AS_TEXT = "an html value turned into text (by String(), a string method, + or a template), which html would escape";
|
|
152
|
+
/**
|
|
153
|
+
* Whether the text between tokens of a number holds only digits, a radix or exponent, white space and
|
|
154
|
+
* comments (and, with `signs`, + and -). A hand-written scan: a regular expression with comments in a
|
|
155
|
+
* repeated group backtracks exponentially on a run of slashes.
|
|
156
|
+
*/
|
|
157
|
+
export function isNumberText(text, signs = false) {
|
|
158
|
+
let i = 0;
|
|
159
|
+
while (i < text.length) {
|
|
160
|
+
const c = text[i];
|
|
161
|
+
if (c === "/" && text[i + 1] === "/") {
|
|
162
|
+
const end = text.indexOf("\n", i + 2);
|
|
163
|
+
if (end === -1)
|
|
164
|
+
return true;
|
|
165
|
+
i = end;
|
|
166
|
+
}
|
|
167
|
+
else if (c === "/" && text[i + 1] === "*") {
|
|
168
|
+
const end = text.indexOf("*/", i + 2);
|
|
169
|
+
if (end === -1)
|
|
170
|
+
return false;
|
|
171
|
+
i = end + 2;
|
|
172
|
+
}
|
|
173
|
+
else if (/[\s\d._xXoObBnaAcCdDeEfF]/.test(c) ||
|
|
174
|
+
(signs && (c === "+" || c === "-"))) {
|
|
175
|
+
i++;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
const MARKUP_CHARACTER = "a literal with & or < in its value (written out or as an escape such as \\x3c), which html would escape, so an entity or a tag in it would show as text";
|
|
184
|
+
/** How many names, values and helpers deep a check follows. */
|
|
185
|
+
const MAX_DEPTH = 200;
|
|
186
|
+
/**
|
|
187
|
+
* Builds the checker for one script: `(from, to)` gives undefined when the
|
|
188
|
+
* code between the offsets (a hole's expression) is provably plain data,
|
|
189
|
+
* or what it reads that is not.
|
|
190
|
+
*/
|
|
191
|
+
export function plainHoleChecker(body, tokens, opts) {
|
|
192
|
+
const n = tokens.length;
|
|
193
|
+
const punct = (k, v) => k >= 0 &&
|
|
194
|
+
k < n &&
|
|
195
|
+
tokens[k].type === "punctuation" &&
|
|
196
|
+
tokens[k].value === v;
|
|
197
|
+
const word = (k, v) => k >= 0 &&
|
|
198
|
+
k < n &&
|
|
199
|
+
tokens[k].type === "identifier" &&
|
|
200
|
+
(v === undefined || tokens[k].value === v);
|
|
201
|
+
const adjacent = (k) => k > 0 && k < n && tokens[k].start === tokens[k - 1].end;
|
|
202
|
+
// Brackets: partners and the innermost open bracket around each token.
|
|
203
|
+
const partner = new Int32Array(n).fill(-1);
|
|
204
|
+
const parentOpen = new Int32Array(n).fill(-1);
|
|
205
|
+
{
|
|
206
|
+
const stack = [];
|
|
207
|
+
for (let k = 0; k < n; k++) {
|
|
208
|
+
parentOpen[k] = stack.length > 0 ? stack[stack.length - 1] : -1;
|
|
209
|
+
const t = tokens[k];
|
|
210
|
+
if (t.type !== "punctuation")
|
|
211
|
+
continue;
|
|
212
|
+
if (t.value === "(" || t.value === "[" || t.value === "{")
|
|
213
|
+
stack.push(k);
|
|
214
|
+
else if ((t.value === ")" || t.value === "]" || t.value === "}") &&
|
|
215
|
+
stack.length > 0) {
|
|
216
|
+
const o = stack.pop();
|
|
217
|
+
partner[o] = k;
|
|
218
|
+
partner[k] = o;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/** The first token index at or after an offset. */
|
|
223
|
+
const tokenAt = (offset) => {
|
|
224
|
+
let lo = 0;
|
|
225
|
+
let hi = n;
|
|
226
|
+
while (lo < hi) {
|
|
227
|
+
const mid = (lo + hi) >> 1;
|
|
228
|
+
if (tokens[mid].start < offset)
|
|
229
|
+
lo = mid + 1;
|
|
230
|
+
else
|
|
231
|
+
hi = mid;
|
|
232
|
+
}
|
|
233
|
+
return lo;
|
|
234
|
+
};
|
|
235
|
+
/** The operator that starts at token k (adjacent punctuation), as text. */
|
|
236
|
+
const opAt = (k) => {
|
|
237
|
+
let s = "";
|
|
238
|
+
for (let j = k; j < n && j < k + 4; j++) {
|
|
239
|
+
const t = tokens[j];
|
|
240
|
+
if (t.type !== "punctuation" || !OP_CHARS.has(t.value))
|
|
241
|
+
break;
|
|
242
|
+
if (j > k && !adjacent(j))
|
|
243
|
+
break;
|
|
244
|
+
s += t.value;
|
|
245
|
+
}
|
|
246
|
+
return s;
|
|
247
|
+
};
|
|
248
|
+
/** Whether an operator assigns: `=`, `+=`, `??=`, or one followed by a unary (`=-1`). */
|
|
249
|
+
const isAssignOp = (op) => {
|
|
250
|
+
if (op.startsWith("++") || op.startsWith("--"))
|
|
251
|
+
return true;
|
|
252
|
+
for (let len = Math.min(4, op.length); len > 0; len--)
|
|
253
|
+
if (ASSIGN_OPS.has(op.slice(0, len)) &&
|
|
254
|
+
op[len] !== "=" &&
|
|
255
|
+
op[len] !== ">")
|
|
256
|
+
return true;
|
|
257
|
+
return false;
|
|
258
|
+
};
|
|
259
|
+
const isAssignAt = (k) => isAssignOp(opAt(k));
|
|
260
|
+
const isPrefixUpdate = (k) => k >= 2 &&
|
|
261
|
+
adjacent(k - 1) &&
|
|
262
|
+
((punct(k - 1, "+") && punct(k - 2, "+")) ||
|
|
263
|
+
(punct(k - 1, "-") && punct(k - 2, "-")));
|
|
264
|
+
const isSpread = (k) => punct(k - 1, ".") && punct(k - 2, ".") && punct(k - 3, ".");
|
|
265
|
+
// The first token of each template hole: a read there is a hole's value.
|
|
266
|
+
const holeFirst = new Set();
|
|
267
|
+
for (const [from, to] of opts.holes) {
|
|
268
|
+
const k = tokenAt(from);
|
|
269
|
+
if (k < n && tokens[k].start < to)
|
|
270
|
+
holeFirst.add(k);
|
|
271
|
+
}
|
|
272
|
+
// Import clauses bind names; export clauses and export declarations
|
|
273
|
+
// hand names to other modules.
|
|
274
|
+
const imported = new Uint8Array(n);
|
|
275
|
+
const exported = new Uint8Array(n);
|
|
276
|
+
for (let k = 0; k < n; k++) {
|
|
277
|
+
if (word(k, "import") && !punct(k - 1, ".") && !punct(k + 1, "(")) {
|
|
278
|
+
if (punct(k + 1, "."))
|
|
279
|
+
continue;
|
|
280
|
+
let j = k + 1;
|
|
281
|
+
for (; j < n && tokens[j].type !== "string" && !punct(j, ";"); j++)
|
|
282
|
+
imported[j] = 1;
|
|
283
|
+
k = j;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (!word(k, "export") || punct(k - 1, "."))
|
|
287
|
+
continue;
|
|
288
|
+
if (punct(k + 1, "{") && partner[k + 1] !== -1) {
|
|
289
|
+
for (let j = k + 2; j < partner[k + 1]; j++)
|
|
290
|
+
exported[j] = 1;
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
let j = k + 1;
|
|
294
|
+
if (word(j, "default")) {
|
|
295
|
+
j++;
|
|
296
|
+
if (word(j) && !punct(j + 1, "(") && !punct(j + 1, "."))
|
|
297
|
+
exported[j] = 1;
|
|
298
|
+
}
|
|
299
|
+
if (word(j, "async"))
|
|
300
|
+
j++;
|
|
301
|
+
if (word(j, "function") || word(j, "class"))
|
|
302
|
+
j++;
|
|
303
|
+
else if (word(j, "const") || word(j, "let") || word(j, "var"))
|
|
304
|
+
j++;
|
|
305
|
+
if (punct(j, "*"))
|
|
306
|
+
j++;
|
|
307
|
+
if (word(j))
|
|
308
|
+
exported[j] = 1;
|
|
309
|
+
}
|
|
310
|
+
// Parameter lists: each bound name, whether its type makes it plain,
|
|
311
|
+
// and whether it holds request data.
|
|
312
|
+
const params = new Map();
|
|
313
|
+
// A parameter list's owner: the outermost local function whose name
|
|
314
|
+
// comes before the list and whose body after it. Outermost, because a
|
|
315
|
+
// binding's type annotation (`const f: (s: number) => string = ...`)
|
|
316
|
+
// reads like a function of its own. Lists are met in source order, so
|
|
317
|
+
// one sweep with a stack of the functions still open finds each owner.
|
|
318
|
+
const byStart = [...opts.functions].sort((x, y) => x.start - y.start);
|
|
319
|
+
const open = [];
|
|
320
|
+
let nextFn = 0;
|
|
321
|
+
const ownerOf = (offset) => {
|
|
322
|
+
while (nextFn < byStart.length && byStart[nextFn].start <= offset) {
|
|
323
|
+
const f = byStart[nextFn++];
|
|
324
|
+
while (open.length > 0 && open[open.length - 1].body <= f.start)
|
|
325
|
+
open.pop();
|
|
326
|
+
open.push(f);
|
|
327
|
+
}
|
|
328
|
+
while (open.length > 0 && open[open.length - 1].body <= offset)
|
|
329
|
+
open.pop();
|
|
330
|
+
return open.length > 0 && open[0].body > offset ? open[0] : undefined;
|
|
331
|
+
};
|
|
332
|
+
/** For `)` at `c`: whether a function body or arrow follows (past a return type). */
|
|
333
|
+
const bodyFollows = (c) => {
|
|
334
|
+
if (punct(c + 1, "=") && punct(c + 2, ">"))
|
|
335
|
+
return true;
|
|
336
|
+
if (punct(c + 1, "{"))
|
|
337
|
+
return true;
|
|
338
|
+
if (!punct(c + 1, ":"))
|
|
339
|
+
return false;
|
|
340
|
+
for (let j = c + 2; j < n && j <= c + 32; j++) {
|
|
341
|
+
if (punct(j, "=") && punct(j + 1, ">"))
|
|
342
|
+
return true;
|
|
343
|
+
if (punct(j, "{")) {
|
|
344
|
+
// `): { a: string } {` is a type; `): Foo {` a body.
|
|
345
|
+
const close = partner[j];
|
|
346
|
+
if (close !== -1 && (punct(close + 1, "{") || punct(close + 1, "=")))
|
|
347
|
+
return true;
|
|
348
|
+
return !word(j - 1, "return");
|
|
349
|
+
}
|
|
350
|
+
if (punct(j, "(") || punct(j, "[") || punct(j, "<")) {
|
|
351
|
+
if (partner[j] !== -1)
|
|
352
|
+
j = partner[j];
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
if (punct(j, ";") || punct(j, ",") || punct(j, ")") || punct(j, "}"))
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
return false;
|
|
359
|
+
};
|
|
360
|
+
const addParams = (p, isCatch) => {
|
|
361
|
+
const c = partner[p];
|
|
362
|
+
const owner = isCatch ? undefined : ownerOf(tokens[p].start);
|
|
363
|
+
let names = [];
|
|
364
|
+
let pattern = false;
|
|
365
|
+
let rest = false;
|
|
366
|
+
let part = 0;
|
|
367
|
+
let typeFrom = -1;
|
|
368
|
+
let typeTo = -1;
|
|
369
|
+
let index = 0;
|
|
370
|
+
let defaultFrom = -1;
|
|
371
|
+
const finish = (endToken) => {
|
|
372
|
+
const type = typeFrom !== -1 ? body.slice(typeFrom, typeTo) : undefined;
|
|
373
|
+
const plain = !pattern &&
|
|
374
|
+
!rest &&
|
|
375
|
+
typeof type === "string" &&
|
|
376
|
+
PLAIN_PARAMETER_TYPE.test(type);
|
|
377
|
+
for (const k of names) {
|
|
378
|
+
const name = tokens[k].value;
|
|
379
|
+
params.set(k, {
|
|
380
|
+
plain,
|
|
381
|
+
request: opts.requestTime &&
|
|
382
|
+
!pattern &&
|
|
383
|
+
!rest &&
|
|
384
|
+
!isCatch &&
|
|
385
|
+
(name === "request" || name === "context"),
|
|
386
|
+
catch: isCatch,
|
|
387
|
+
owner,
|
|
388
|
+
index,
|
|
389
|
+
defaultRange: defaultFrom !== -1 ? [defaultFrom, endToken] : undefined,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
names = [];
|
|
393
|
+
pattern = rest = false;
|
|
394
|
+
part = 0;
|
|
395
|
+
typeFrom = typeTo = -1;
|
|
396
|
+
defaultFrom = -1;
|
|
397
|
+
index++;
|
|
398
|
+
};
|
|
399
|
+
for (let j = p + 1; j < c; j++) {
|
|
400
|
+
const t = tokens[j];
|
|
401
|
+
if (t.type === "punctuation") {
|
|
402
|
+
if (t.value === ",") {
|
|
403
|
+
finish(j);
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
if (part === 0 && t.value === "." && isSpread(j + 1)) {
|
|
407
|
+
rest = true;
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
if (part === 0 && t.value === ":") {
|
|
411
|
+
part = 1;
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
if (part !== 2 && t.value === "=" && !punct(j + 1, ">")) {
|
|
415
|
+
part = 2;
|
|
416
|
+
defaultFrom = j + 1;
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
if (t.value === "(" || t.value === "[" || t.value === "{") {
|
|
420
|
+
const end = partner[j] === -1 ? c : partner[j];
|
|
421
|
+
if (part === 0 && t.value !== "(") {
|
|
422
|
+
pattern = true;
|
|
423
|
+
for (let q = j + 1; q < end; q++)
|
|
424
|
+
if (tokens[q].type === "identifier" &&
|
|
425
|
+
!punct(q + 1, ":") &&
|
|
426
|
+
!punct(q - 1, "."))
|
|
427
|
+
names.push(q);
|
|
428
|
+
}
|
|
429
|
+
if (part === 1) {
|
|
430
|
+
if (typeFrom === -1)
|
|
431
|
+
typeFrom = t.start;
|
|
432
|
+
typeTo = tokens[end].end;
|
|
433
|
+
}
|
|
434
|
+
j = end;
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (part === 1 && t.value !== "?") {
|
|
438
|
+
if (typeFrom === -1)
|
|
439
|
+
typeFrom = t.start;
|
|
440
|
+
typeTo = t.end;
|
|
441
|
+
}
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
if (part === 0 && t.type === "identifier") {
|
|
445
|
+
if (t.value === "this") {
|
|
446
|
+
part = 1;
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (PARAM_MODIFIERS.has(t.value) && word(j + 1))
|
|
450
|
+
continue;
|
|
451
|
+
names.push(j);
|
|
452
|
+
}
|
|
453
|
+
else if (part === 1) {
|
|
454
|
+
if (typeFrom === -1)
|
|
455
|
+
typeFrom = t.start;
|
|
456
|
+
typeTo = t.end;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
finish(c);
|
|
460
|
+
};
|
|
461
|
+
for (let k = 0; k < n; k++) {
|
|
462
|
+
if (punct(k, "(") && partner[k] !== -1) {
|
|
463
|
+
const before = k > 0 ? tokens[k - 1] : undefined;
|
|
464
|
+
const isCatch = word(k - 1, "catch");
|
|
465
|
+
if (isCatch ||
|
|
466
|
+
word(k - 1, "function") ||
|
|
467
|
+
(bodyFollows(partner[k]) &&
|
|
468
|
+
!(before?.type === "identifier" && NOT_PARAMS_BEFORE.has(before.value))))
|
|
469
|
+
addParams(k, isCatch);
|
|
470
|
+
}
|
|
471
|
+
else if (word(k) &&
|
|
472
|
+
punct(k + 1, "=") &&
|
|
473
|
+
punct(k + 2, ">") &&
|
|
474
|
+
adjacent(k + 2) &&
|
|
475
|
+
!punct(k - 1, "."))
|
|
476
|
+
params.set(k, { plain: false, request: false, catch: false, index: 0 });
|
|
477
|
+
}
|
|
478
|
+
// The outermost bracket of a chain of nested `{` and `[`: a pattern root.
|
|
479
|
+
const patternRoot = new Int32Array(n).fill(-1);
|
|
480
|
+
for (let k = 0; k < n; k++) {
|
|
481
|
+
if (!punct(k, "{") && !punct(k, "["))
|
|
482
|
+
continue;
|
|
483
|
+
const p = parentOpen[k];
|
|
484
|
+
patternRoot[k] =
|
|
485
|
+
p !== -1 && (punct(p, "{") || punct(p, "[")) ? patternRoot[p] : k;
|
|
486
|
+
}
|
|
487
|
+
/** `[` after a value is an index, not an array or a pattern. */
|
|
488
|
+
const isIndex = (o) => punct(o, "[") &&
|
|
489
|
+
o > 0 &&
|
|
490
|
+
(tokens[o - 1].type === "identifier"
|
|
491
|
+
? !/^(?:return|typeof|void|in|of|case|await|yield|const|let|var|new|delete|instanceof)$/.test(tokens[o - 1].value)
|
|
492
|
+
: tokens[o - 1].type === "string" ||
|
|
493
|
+
punct(o - 1, ")") ||
|
|
494
|
+
punct(o - 1, "]"));
|
|
495
|
+
// Each reference to a name, by name. Property names, object keys and
|
|
496
|
+
// method names are not references.
|
|
497
|
+
const occurrences = new Map();
|
|
498
|
+
for (let k = 0; k < n; k++) {
|
|
499
|
+
if (tokens[k].type !== "identifier")
|
|
500
|
+
continue;
|
|
501
|
+
if (punct(k - 1, ".") && !isSpread(k))
|
|
502
|
+
continue;
|
|
503
|
+
const prev = k > 0 ? tokens[k - 1] : undefined;
|
|
504
|
+
const afterOpenOrComma = punct(k - 1, "{") ||
|
|
505
|
+
punct(k - 1, ",") ||
|
|
506
|
+
punct(k - 1, ";") ||
|
|
507
|
+
punct(k - 1, "}") ||
|
|
508
|
+
(prev?.type === "identifier" && MEMBER_MODIFIERS.has(prev.value));
|
|
509
|
+
const insideBraces = parentOpen[k] !== -1 && punct(parentOpen[k], "{");
|
|
510
|
+
// { key: value } and { key?: type }.
|
|
511
|
+
if (insideBraces &&
|
|
512
|
+
(punct(k - 1, "{") || punct(k - 1, ",")) &&
|
|
513
|
+
(punct(k + 1, ":") || (punct(k + 1, "?") && punct(k + 2, ":"))))
|
|
514
|
+
continue;
|
|
515
|
+
// A method: name(...) { or name(...): type {.
|
|
516
|
+
if (afterOpenOrComma &&
|
|
517
|
+
insideBraces &&
|
|
518
|
+
punct(k + 1, "(") &&
|
|
519
|
+
partner[k + 1] !== -1 &&
|
|
520
|
+
bodyFollows(partner[k + 1]) &&
|
|
521
|
+
!(punct(partner[k + 1] + 1, "=") && punct(partner[k + 1] + 2, ">")))
|
|
522
|
+
continue;
|
|
523
|
+
const name = tokens[k].value;
|
|
524
|
+
let list = occurrences.get(name);
|
|
525
|
+
if (!list)
|
|
526
|
+
occurrences.set(name, (list = []));
|
|
527
|
+
list.push(k);
|
|
528
|
+
}
|
|
529
|
+
/** A function by its name's offset. */
|
|
530
|
+
const functionAt = new Map(opts.functions.map((f) => [f.start, f]));
|
|
531
|
+
// Each `return`'s innermost local function.
|
|
532
|
+
const returnsOf = new Map();
|
|
533
|
+
{
|
|
534
|
+
const sorted = [...opts.functions].sort((a, b) => a.start - b.start || b.end - a.end);
|
|
535
|
+
const stack = [];
|
|
536
|
+
let f = 0;
|
|
537
|
+
for (let k = 0; k < n; k++) {
|
|
538
|
+
const at = tokens[k].start;
|
|
539
|
+
while (f < sorted.length && sorted[f].start <= at) {
|
|
540
|
+
while (stack.length > 0 &&
|
|
541
|
+
stack[stack.length - 1].end <= sorted[f].start)
|
|
542
|
+
stack.pop();
|
|
543
|
+
stack.push(sorted[f++]);
|
|
544
|
+
}
|
|
545
|
+
while (stack.length > 0 && stack[stack.length - 1].end <= at)
|
|
546
|
+
stack.pop();
|
|
547
|
+
if (word(k, "return") && stack.length > 0) {
|
|
548
|
+
const owner = stack[stack.length - 1];
|
|
549
|
+
if (at < owner.body)
|
|
550
|
+
continue;
|
|
551
|
+
const list = returnsOf.get(owner) ?? [];
|
|
552
|
+
list.push(k);
|
|
553
|
+
returnsOf.set(owner, list);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Where an expression that starts at token `k` ends (exclusive): a `;`
|
|
559
|
+
* or `,` at its depth, a bracket that closes around it, or a line break
|
|
560
|
+
* that ends the statement.
|
|
561
|
+
*/
|
|
562
|
+
const CONTINUES_LINE = /[-+*/%&|^<>=?:.,!~([{]$/;
|
|
563
|
+
const CONTINUES_NEXT = /^[-+*/%&|^<>=?:.([`]/;
|
|
564
|
+
const expressionEnd = (k, stopAtComma) => {
|
|
565
|
+
let j = k;
|
|
566
|
+
for (; j < n; j++) {
|
|
567
|
+
const t = tokens[j];
|
|
568
|
+
if (t.type === "punctuation") {
|
|
569
|
+
if (t.value === ";" || (stopAtComma && t.value === ","))
|
|
570
|
+
return j;
|
|
571
|
+
if (t.value === ")" || t.value === "]" || t.value === "}")
|
|
572
|
+
return j;
|
|
573
|
+
if ((t.value === "(" || t.value === "[" || t.value === "{") &&
|
|
574
|
+
partner[j] !== -1) {
|
|
575
|
+
j = partner[j];
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
if (j + 1 < n) {
|
|
580
|
+
const gap = body.slice(tokens[j].end, tokens[j + 1].start);
|
|
581
|
+
const nl = gap.indexOf("\n");
|
|
582
|
+
if (nl !== -1) {
|
|
583
|
+
const line = (tokens[j].value + gap.slice(0, nl))
|
|
584
|
+
.replace(/\/\/.*$/, "")
|
|
585
|
+
.trimEnd();
|
|
586
|
+
const nextLine = gap.slice(nl).replace(/^\s+/, "");
|
|
587
|
+
const next = nextLine === "" ? tokens[j + 1].value : nextLine;
|
|
588
|
+
const lastIsOp = tokens[j].type === "punctuation" &&
|
|
589
|
+
line === tokens[j].value &&
|
|
590
|
+
CONTINUES_LINE.test(line);
|
|
591
|
+
const lineEndsInOp = line.length > tokens[j].value.length && CONTINUES_LINE.test(line);
|
|
592
|
+
if (!lastIsOp && !lineEndsInOp && !CONTINUES_NEXT.test(next))
|
|
593
|
+
return j + 1;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
return j;
|
|
598
|
+
};
|
|
599
|
+
const nameMemo = new Map();
|
|
600
|
+
/** The hole being checked, so a range inside it never reaches the template around it. */
|
|
601
|
+
let holeLo = -1;
|
|
602
|
+
let holeHi = -1;
|
|
603
|
+
let depth = 0;
|
|
604
|
+
const helperMemo = new Map();
|
|
605
|
+
/** Local functions that may return an html value. */
|
|
606
|
+
const helperHtml = new Set();
|
|
607
|
+
const unknown = (what, why) => ({
|
|
608
|
+
ok: false,
|
|
609
|
+
cause: { what, why },
|
|
610
|
+
});
|
|
611
|
+
/** The tokens after the value at k that form its member chain: [end, assigned, calls]. */
|
|
612
|
+
const chainAfter = (k) => {
|
|
613
|
+
let j = k + 1;
|
|
614
|
+
let members = 0;
|
|
615
|
+
const methods = [];
|
|
616
|
+
for (;;) {
|
|
617
|
+
if (punct(j, "!") && !punct(j + 1, "=")) {
|
|
618
|
+
j++;
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
if (punct(j, "?") && punct(j + 1, ".") && adjacent(j + 1))
|
|
622
|
+
j++;
|
|
623
|
+
if (punct(j, ".") && word(j + 1)) {
|
|
624
|
+
members++;
|
|
625
|
+
if (punct(j + 2, "("))
|
|
626
|
+
methods.push(tokens[j + 1].value);
|
|
627
|
+
j += 2;
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
if (punct(j, "[") && partner[j] !== -1) {
|
|
631
|
+
members++;
|
|
632
|
+
j = partner[j] + 1;
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
635
|
+
if (punct(j, "(") && partner[j] !== -1) {
|
|
636
|
+
j = partner[j] + 1;
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
return { end: j, members, assigned: isAssignAt(j), methods };
|
|
642
|
+
};
|
|
643
|
+
/** Whether the `(` at o opens a call's arguments. */
|
|
644
|
+
const isCallParen = (o) => punct(o, "(") &&
|
|
645
|
+
o > 0 &&
|
|
646
|
+
(tokens[o - 1].type === "identifier"
|
|
647
|
+
? !/^(?:if|while|switch|for|return|typeof|await|case|in|of|void|delete|catch|with|yield)$/.test(tokens[o - 1].value)
|
|
648
|
+
: punct(o - 1, ")") || punct(o - 1, "]"));
|
|
649
|
+
/** Whether a bare read at k hands the value on: an argument, an alias, a return, an element. */
|
|
650
|
+
const handsOn = (k) => {
|
|
651
|
+
if (holeFirst.has(k))
|
|
652
|
+
return false;
|
|
653
|
+
if (isSpread(k))
|
|
654
|
+
return true;
|
|
655
|
+
if (word(k - 1, "return") || word(k - 1, "yield") || word(k - 1, "export"))
|
|
656
|
+
return true;
|
|
657
|
+
if (punct(k - 1, "=")) {
|
|
658
|
+
const op = opAt(k - 1);
|
|
659
|
+
if (op === "=" &&
|
|
660
|
+
!(k >= 2 && adjacent(k - 1) && OP_CHARS.has(tokens[k - 2].value)))
|
|
661
|
+
return true;
|
|
662
|
+
}
|
|
663
|
+
if (punct(k - 1, ">") && punct(k - 2, "=") && adjacent(k - 1))
|
|
664
|
+
return true;
|
|
665
|
+
if (punct(k - 1, "("))
|
|
666
|
+
return isCallParen(k - 1);
|
|
667
|
+
if (punct(k - 1, "["))
|
|
668
|
+
return !isIndex(k - 1);
|
|
669
|
+
if (punct(k - 1, "{"))
|
|
670
|
+
return true;
|
|
671
|
+
if (punct(k - 1, ",")) {
|
|
672
|
+
const p = parentOpen[k];
|
|
673
|
+
if (p === -1)
|
|
674
|
+
return false;
|
|
675
|
+
if (punct(p, "("))
|
|
676
|
+
return isCallParen(p);
|
|
677
|
+
if (punct(p, "["))
|
|
678
|
+
return !isIndex(p);
|
|
679
|
+
return true;
|
|
680
|
+
}
|
|
681
|
+
if (punct(k - 1, ":")) {
|
|
682
|
+
const p = parentOpen[k];
|
|
683
|
+
return p !== -1 && punct(p, "{");
|
|
684
|
+
}
|
|
685
|
+
return false;
|
|
686
|
+
};
|
|
687
|
+
// Built-ins the file changes: a write to one or one of its members, or
|
|
688
|
+
// the object handed to other code (Object.assign, defineProperty). A
|
|
689
|
+
// write through the global object can replace any of them, and a
|
|
690
|
+
// prototype reached anywhere can change what any method returns.
|
|
691
|
+
const patched = new Set();
|
|
692
|
+
let everyGlobalPatched = false;
|
|
693
|
+
let prototypeReach;
|
|
694
|
+
{
|
|
695
|
+
/** The member chain after the name at k: whether it calls, and whether it names prototype. */
|
|
696
|
+
const chainShape = (k) => {
|
|
697
|
+
let call = false;
|
|
698
|
+
let prototype = false;
|
|
699
|
+
for (let j = k + 1; j < n;) {
|
|
700
|
+
if (punct(j, "?") && punct(j + 1, "."))
|
|
701
|
+
j++;
|
|
702
|
+
if (punct(j, ".") && word(j + 1)) {
|
|
703
|
+
if (PROTOTYPE_WORDS.has(tokens[j + 1].value))
|
|
704
|
+
prototype = true;
|
|
705
|
+
j += 2;
|
|
706
|
+
}
|
|
707
|
+
else if (punct(j, "[") && partner[j] !== -1) {
|
|
708
|
+
if (partner[j] === j + 2 &&
|
|
709
|
+
(tokens[j + 1].type === "string" ||
|
|
710
|
+
tokens[j + 1].type === "template") &&
|
|
711
|
+
PROTOTYPE_WORDS.has(tokens[j + 1].value))
|
|
712
|
+
prototype = true;
|
|
713
|
+
j = partner[j] + 1;
|
|
714
|
+
}
|
|
715
|
+
else if (punct(j, "(") && partner[j] !== -1) {
|
|
716
|
+
call = true;
|
|
717
|
+
j = partner[j] + 1;
|
|
718
|
+
}
|
|
719
|
+
else
|
|
720
|
+
break;
|
|
721
|
+
}
|
|
722
|
+
return { call, prototype };
|
|
723
|
+
};
|
|
724
|
+
const declaresName = (list) => list.some((k) => {
|
|
725
|
+
const prev = k > 0 ? tokens[k - 1] : undefined;
|
|
726
|
+
return (params.has(k) ||
|
|
727
|
+
imported[k] === 1 ||
|
|
728
|
+
(prev?.type === "identifier" &&
|
|
729
|
+
/^(?:const|let|var|function|class)$/.test(prev.value)));
|
|
730
|
+
});
|
|
731
|
+
for (const [name, list] of occurrences) {
|
|
732
|
+
const trusted = TRUSTED_GLOBALS.has(name);
|
|
733
|
+
const globalObject = GLOBAL_OBJECTS.has(name);
|
|
734
|
+
if (!trusted && !globalObject)
|
|
735
|
+
continue;
|
|
736
|
+
if (declaresName(list))
|
|
737
|
+
continue;
|
|
738
|
+
for (const k of list) {
|
|
739
|
+
const chain = chainAfter(k);
|
|
740
|
+
const shape = chainShape(k);
|
|
741
|
+
const handed = !shape.call && handsOn(k);
|
|
742
|
+
if (shape.prototype && (chain.assigned || handed))
|
|
743
|
+
prototypeReach ??= {
|
|
744
|
+
what: body.slice(tokens[k].start, tokens[chain.end - 1].end),
|
|
745
|
+
why: "a prototype this file changes, which can change what any method returns",
|
|
746
|
+
};
|
|
747
|
+
if (globalObject) {
|
|
748
|
+
// Only a direct method call (globalThis.fetch(...)) leaves it alone.
|
|
749
|
+
if (!(chain.members === 1 && shape.call && !chain.assigned))
|
|
750
|
+
everyGlobalPatched = true;
|
|
751
|
+
continue;
|
|
752
|
+
}
|
|
753
|
+
if (chain.assigned ||
|
|
754
|
+
isPrefixUpdate(k) ||
|
|
755
|
+
(handed &&
|
|
756
|
+
(chain.members === 0 ||
|
|
757
|
+
(name === "process" &&
|
|
758
|
+
chain.members === 1 &&
|
|
759
|
+
word(k + 2, "env")))))
|
|
760
|
+
patched.add(name);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
// A prototype reached any other way: "".__proto__, setPrototypeOf, or
|
|
764
|
+
// X.prototype on any object that is not only read through a method.
|
|
765
|
+
if (prototypeReach === undefined)
|
|
766
|
+
for (let k = 0; k < n; k++) {
|
|
767
|
+
const t = tokens[k];
|
|
768
|
+
if (!PROTOTYPE_WORDS.has(t.value))
|
|
769
|
+
continue;
|
|
770
|
+
const keyed = t.type === "string" || t.type === "template";
|
|
771
|
+
if (keyed && !(punct(k - 1, "[") && punct(k + 1, "]")))
|
|
772
|
+
continue;
|
|
773
|
+
if (t.type !== "identifier" && !keyed)
|
|
774
|
+
continue;
|
|
775
|
+
const chain = chainAfter(keyed ? k + 1 : k);
|
|
776
|
+
if (t.value === "prototype" &&
|
|
777
|
+
t.type === "identifier" &&
|
|
778
|
+
!chain.assigned &&
|
|
779
|
+
chain.methods.length > 0)
|
|
780
|
+
continue;
|
|
781
|
+
prototypeReach = {
|
|
782
|
+
what: t.value,
|
|
783
|
+
why: "a prototype this file reaches, which can change what any method returns",
|
|
784
|
+
};
|
|
785
|
+
break;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
// Out of scope by design: code that rewrites built-ins or globals.
|
|
789
|
+
// Rather than follow eval, Function, defineProperty, Reflect.set or
|
|
790
|
+
// setPrototypeOf, a file that calls one gets no fix for any hole.
|
|
791
|
+
let rewrites;
|
|
792
|
+
for (let k = 0; k < n && rewrites === undefined; k++) {
|
|
793
|
+
const t = tokens[k];
|
|
794
|
+
if (t.type !== "identifier" || punct(k - 1, "."))
|
|
795
|
+
continue;
|
|
796
|
+
let what;
|
|
797
|
+
if (t.value === "eval" && punct(k + 1, "("))
|
|
798
|
+
what = "eval()";
|
|
799
|
+
else if (t.value === "Function" &&
|
|
800
|
+
(punct(k + 1, "(") || word(k - 1, "new")))
|
|
801
|
+
what = word(k - 1, "new") ? "new Function()" : "Function()";
|
|
802
|
+
else if (REWRITING_MEMBERS[t.value] !== undefined &&
|
|
803
|
+
punct(k + 1, ".") &&
|
|
804
|
+
word(k + 2) &&
|
|
805
|
+
REWRITING_MEMBERS[t.value].has(tokens[k + 2].value))
|
|
806
|
+
what = `${t.value}.${tokens[k + 2].value}()`;
|
|
807
|
+
if (what !== undefined)
|
|
808
|
+
rewrites = {
|
|
809
|
+
what,
|
|
810
|
+
why: "code that can rewrite built-ins or globals, so no hole in this file is proven plain",
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
/** Why a trusted built-in is not trusted in this file, if it is not. */
|
|
814
|
+
const builtinChanged = (name) => everyGlobalPatched
|
|
815
|
+
? {
|
|
816
|
+
what: name,
|
|
817
|
+
why: "a built-in, and this file writes through the global object, which can replace it",
|
|
818
|
+
}
|
|
819
|
+
: patched.has(name)
|
|
820
|
+
? { what: name, why: "a built-in this file changes" }
|
|
821
|
+
: undefined;
|
|
822
|
+
// Request data the script changes or hands to other code: a header it
|
|
823
|
+
// sets, appends or deletes, params or searchParams it writes, or the
|
|
824
|
+
// request, its headers, params or a URL made from it passed to a
|
|
825
|
+
// function. Other code may write markup into it, so no value read from
|
|
826
|
+
// request data keeps the fix.
|
|
827
|
+
let requestTaint;
|
|
828
|
+
if (opts.requestTime) {
|
|
829
|
+
/** Names that hold a request object, and whether its members are text (params). */
|
|
830
|
+
const roots = new Map();
|
|
831
|
+
for (const [k, p] of params)
|
|
832
|
+
if (p.request)
|
|
833
|
+
roots.set(tokens[k].value, false);
|
|
834
|
+
/** The member chain after a request object at k: where it ends, whether on an object, and whether it changes one. */
|
|
835
|
+
const walk = (k, textMembers) => {
|
|
836
|
+
let object = true;
|
|
837
|
+
let text = textMembers;
|
|
838
|
+
let changes = false;
|
|
839
|
+
let j = k + 1;
|
|
840
|
+
for (;;) {
|
|
841
|
+
if (punct(j, "!") && !punct(j + 1, "=")) {
|
|
842
|
+
j++;
|
|
843
|
+
continue;
|
|
844
|
+
}
|
|
845
|
+
if (punct(j, "?") && punct(j + 1, ".")) {
|
|
846
|
+
j++;
|
|
847
|
+
if (punct(j + 1, "(") || punct(j + 1, "["))
|
|
848
|
+
j++;
|
|
849
|
+
}
|
|
850
|
+
if (punct(j, ".") && word(j + 1)) {
|
|
851
|
+
const member = tokens[j + 1].value;
|
|
852
|
+
const callAt = punct(j + 2, "(")
|
|
853
|
+
? j + 2
|
|
854
|
+
: punct(j + 2, "?") && punct(j + 3, ".") && punct(j + 4, "(")
|
|
855
|
+
? j + 4
|
|
856
|
+
: -1;
|
|
857
|
+
if (callAt !== -1 && partner[callAt] !== -1) {
|
|
858
|
+
if (object && !REQUEST_READ_METHODS.has(member))
|
|
859
|
+
changes = true;
|
|
860
|
+
object = false;
|
|
861
|
+
j = partner[callAt] + 1;
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
if (object) {
|
|
865
|
+
if (text || REQUEST_TEXT_MEMBERS.has(member))
|
|
866
|
+
object = false;
|
|
867
|
+
else if (member === "params")
|
|
868
|
+
text = true;
|
|
869
|
+
}
|
|
870
|
+
j += 2;
|
|
871
|
+
continue;
|
|
872
|
+
}
|
|
873
|
+
if (punct(j, "[") && partner[j] !== -1) {
|
|
874
|
+
object = false;
|
|
875
|
+
j = partner[j] + 1;
|
|
876
|
+
continue;
|
|
877
|
+
}
|
|
878
|
+
if (punct(j, "(") && partner[j] !== -1) {
|
|
879
|
+
if (object)
|
|
880
|
+
changes = true;
|
|
881
|
+
object = false;
|
|
882
|
+
j = partner[j] + 1;
|
|
883
|
+
continue;
|
|
884
|
+
}
|
|
885
|
+
break;
|
|
886
|
+
}
|
|
887
|
+
return { end: j, object, text, changes };
|
|
888
|
+
};
|
|
889
|
+
// Aliases, in source order: const h = request.headers, const u = new
|
|
890
|
+
// URL(...), const { headers } = request.
|
|
891
|
+
for (let k = 0; k < n; k++) {
|
|
892
|
+
if (!word(k, "const"))
|
|
893
|
+
continue;
|
|
894
|
+
if (word(k + 1) && punct(k + 2, "=") && !punct(k + 3, "=")) {
|
|
895
|
+
const v = k + 3;
|
|
896
|
+
// The value starts right after `=` (not inside a template).
|
|
897
|
+
if (v >= n || body.slice(tokens[k + 2].end, tokens[v].start).trim())
|
|
898
|
+
continue;
|
|
899
|
+
let w;
|
|
900
|
+
if (word(v, "new") &&
|
|
901
|
+
(word(v + 1, "URL") || word(v + 1, "URLSearchParams")) &&
|
|
902
|
+
punct(v + 2, "(") &&
|
|
903
|
+
partner[v + 2] !== -1)
|
|
904
|
+
w = walk(partner[v + 2], false);
|
|
905
|
+
else if (word(v) && roots.has(tokens[v].value))
|
|
906
|
+
w = walk(v, roots.get(tokens[v].value));
|
|
907
|
+
if (w?.object && expressionEnd(v, true) === w.end)
|
|
908
|
+
roots.set(tokens[k + 1].value, w.text);
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
const close = punct(k + 1, "{") ? partner[k + 1] : -1;
|
|
912
|
+
if (close === -1 || !punct(close + 1, "=") || !word(close + 2))
|
|
913
|
+
continue;
|
|
914
|
+
const from = roots.get(tokens[close + 2].value);
|
|
915
|
+
if (from === undefined)
|
|
916
|
+
continue;
|
|
917
|
+
const w = walk(close + 2, from);
|
|
918
|
+
if (!w.object || w.text)
|
|
919
|
+
continue;
|
|
920
|
+
for (let j = k + 2; j < close; j++) {
|
|
921
|
+
if (punct(j, "{") || punct(j, "[")) {
|
|
922
|
+
requestTaint ??= {
|
|
923
|
+
what: body.slice(tokens[k].start, tokens[close].end).slice(0, 60),
|
|
924
|
+
why: "request data destructured in a way Bascik does not follow",
|
|
925
|
+
};
|
|
926
|
+
break;
|
|
927
|
+
}
|
|
928
|
+
if (!word(j) || !(punct(j - 1, "{") || punct(j - 1, ",")))
|
|
929
|
+
continue;
|
|
930
|
+
const key = tokens[j].value;
|
|
931
|
+
const local = punct(j + 1, ":") && word(j + 2) ? j + 2 : j;
|
|
932
|
+
if (!REQUEST_TEXT_MEMBERS.has(key))
|
|
933
|
+
roots.set(tokens[local].value, key === "params");
|
|
934
|
+
j = local;
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
for (const [name, text] of roots) {
|
|
938
|
+
if (requestTaint !== undefined)
|
|
939
|
+
break;
|
|
940
|
+
for (const k of occurrences.get(name) ?? []) {
|
|
941
|
+
if (params.has(k))
|
|
942
|
+
continue;
|
|
943
|
+
if (word(k - 1, "const") ||
|
|
944
|
+
word(k - 1, "let") ||
|
|
945
|
+
word(k - 1, "var") ||
|
|
946
|
+
word(k - 1, "function"))
|
|
947
|
+
continue;
|
|
948
|
+
const open = parentOpen[k];
|
|
949
|
+
if (open !== -1 &&
|
|
950
|
+
(punct(open, "{") || punct(open, "[")) &&
|
|
951
|
+
!isIndex(open)) {
|
|
952
|
+
const root = patternRoot[open];
|
|
953
|
+
if (word(root - 1, "const") ||
|
|
954
|
+
word(root - 1, "let") ||
|
|
955
|
+
word(root - 1, "var"))
|
|
956
|
+
continue;
|
|
957
|
+
}
|
|
958
|
+
const w = walk(k, text);
|
|
959
|
+
const shown = (why) => ({
|
|
960
|
+
what: body
|
|
961
|
+
.slice(tokens[k].start, tokens[Math.max(k, w.end - 1)].end)
|
|
962
|
+
.slice(0, 60),
|
|
963
|
+
why,
|
|
964
|
+
});
|
|
965
|
+
if (w.changes) {
|
|
966
|
+
requestTaint = shown("request data this script changes, so a value read from it may be markup the script wrote");
|
|
967
|
+
break;
|
|
968
|
+
}
|
|
969
|
+
if (isAssignAt(w.end) || isPrefixUpdate(k) || word(k - 1, "delete")) {
|
|
970
|
+
requestTaint = shown("request data this script writes to, so a value read from it may be markup the script wrote");
|
|
971
|
+
break;
|
|
972
|
+
}
|
|
973
|
+
if (!w.object)
|
|
974
|
+
continue;
|
|
975
|
+
const alias = punct(k - 1, "=") &&
|
|
976
|
+
word(k - 2) &&
|
|
977
|
+
word(k - 3, "const") &&
|
|
978
|
+
expressionEnd(k, true) === w.end;
|
|
979
|
+
const handed = handsOn(k) ||
|
|
980
|
+
(!holeFirst.has(k) &&
|
|
981
|
+
(punct(k - 1, "?") ||
|
|
982
|
+
punct(k - 1, ":") ||
|
|
983
|
+
punct(k - 1, "|") ||
|
|
984
|
+
punct(k - 1, "&")));
|
|
985
|
+
if (!alias && handed) {
|
|
986
|
+
requestTaint = shown("request data this script hands to other code, which may change it");
|
|
987
|
+
break;
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
/** The value of a `const NAME =` or `const NAME: Type =` declaration at k: token range. */
|
|
993
|
+
const constValue = (k) => {
|
|
994
|
+
let eq = -1;
|
|
995
|
+
if (punct(k + 1, "=") && !punct(k + 2, "=") && !punct(k + 2, ">"))
|
|
996
|
+
eq = k + 1;
|
|
997
|
+
else if (punct(k + 1, ":")) {
|
|
998
|
+
let angle = 0;
|
|
999
|
+
for (let j = k + 2; j < n && j <= k + 200; j++) {
|
|
1000
|
+
if (tokens[j].type !== "punctuation")
|
|
1001
|
+
continue;
|
|
1002
|
+
const v = tokens[j].value;
|
|
1003
|
+
if ((v === "(" || v === "[" || v === "{") && partner[j] !== -1)
|
|
1004
|
+
j = partner[j];
|
|
1005
|
+
else if (v === "<")
|
|
1006
|
+
angle++;
|
|
1007
|
+
else if (v === ">")
|
|
1008
|
+
angle = Math.max(0, angle - 1);
|
|
1009
|
+
else if (v === "=") {
|
|
1010
|
+
if (punct(j + 1, ">"))
|
|
1011
|
+
j++;
|
|
1012
|
+
else if (punct(j + 1, "="))
|
|
1013
|
+
return undefined;
|
|
1014
|
+
else if (angle === 0) {
|
|
1015
|
+
eq = j;
|
|
1016
|
+
break;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
else if (v === ";" || v === ")" || v === "]" || v === "}")
|
|
1020
|
+
return undefined;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
if (eq === -1)
|
|
1024
|
+
return undefined;
|
|
1025
|
+
return [eq + 1, expressionEnd(eq + 1, true)];
|
|
1026
|
+
};
|
|
1027
|
+
/** Whether tokens [a, b) are an object or array literal of only literals. */
|
|
1028
|
+
const isTable = (a, b) => {
|
|
1029
|
+
if (!(punct(a, "{") || punct(a, "[")) || partner[a] === -1)
|
|
1030
|
+
return false;
|
|
1031
|
+
const close = partner[a];
|
|
1032
|
+
if (close !== b - 1 &&
|
|
1033
|
+
!(close === b - 3 && word(b - 2, "as") && word(b - 1, "const")))
|
|
1034
|
+
return false;
|
|
1035
|
+
const object = punct(a, "{");
|
|
1036
|
+
for (let j = a + 1; j < close; j++) {
|
|
1037
|
+
const t = tokens[j];
|
|
1038
|
+
if (t.type === "punctuation") {
|
|
1039
|
+
if (t.value === "," || t.value === ":" || t.value === "-")
|
|
1040
|
+
continue;
|
|
1041
|
+
return false;
|
|
1042
|
+
}
|
|
1043
|
+
if (t.type === "string") {
|
|
1044
|
+
if (holdsMarkupCharacter(t.value))
|
|
1045
|
+
return false;
|
|
1046
|
+
if (object &&
|
|
1047
|
+
!punct(j + 1, ":") &&
|
|
1048
|
+
!punct(j + 1, ",") &&
|
|
1049
|
+
j + 1 !== close)
|
|
1050
|
+
return false;
|
|
1051
|
+
continue;
|
|
1052
|
+
}
|
|
1053
|
+
if (t.type === "identifier") {
|
|
1054
|
+
if (LITERAL_WORDS.has(t.value) && !punct(j + 1, ":"))
|
|
1055
|
+
continue;
|
|
1056
|
+
if (object && punct(j + 1, ":"))
|
|
1057
|
+
continue;
|
|
1058
|
+
return false;
|
|
1059
|
+
}
|
|
1060
|
+
return false;
|
|
1061
|
+
}
|
|
1062
|
+
// Numbers, comments and white space only between the tokens.
|
|
1063
|
+
for (let j = a; j < close; j++) {
|
|
1064
|
+
let gap = body.slice(tokens[j].end, tokens[j + 1].start);
|
|
1065
|
+
if (tokens[j].type === "string")
|
|
1066
|
+
gap = gap.slice(1);
|
|
1067
|
+
if (tokens[j + 1].type === "string")
|
|
1068
|
+
gap = gap.slice(0, -1);
|
|
1069
|
+
if (!isNumberText(gap, true))
|
|
1070
|
+
return false;
|
|
1071
|
+
}
|
|
1072
|
+
return true;
|
|
1073
|
+
};
|
|
1074
|
+
/** Calls `each(from, to)` for each top-level comma-separated piece of tokens [a, b); false stops. */
|
|
1075
|
+
const eachArgument = (a, b, each) => {
|
|
1076
|
+
let start = a;
|
|
1077
|
+
for (let j = a; j <= b; j++) {
|
|
1078
|
+
if (j < b && !punct(j, ",")) {
|
|
1079
|
+
if ((punct(j, "(") || punct(j, "[") || punct(j, "{")) &&
|
|
1080
|
+
partner[j] !== -1 &&
|
|
1081
|
+
partner[j] < b)
|
|
1082
|
+
j = partner[j];
|
|
1083
|
+
continue;
|
|
1084
|
+
}
|
|
1085
|
+
if (!each(start, j))
|
|
1086
|
+
return false;
|
|
1087
|
+
start = j + 1;
|
|
1088
|
+
}
|
|
1089
|
+
return true;
|
|
1090
|
+
};
|
|
1091
|
+
/**
|
|
1092
|
+
* Evaluates call arguments, tokens [a, b): each must be plain (an
|
|
1093
|
+
* options object of literals counts), and none an html value, which a
|
|
1094
|
+
* call turns into text.
|
|
1095
|
+
*/
|
|
1096
|
+
const evalArgs = (a, b) => {
|
|
1097
|
+
let failed;
|
|
1098
|
+
eachArgument(a, b, (from, to) => {
|
|
1099
|
+
if (from >= to || isTable(from, to))
|
|
1100
|
+
return true;
|
|
1101
|
+
const r = evalRange(from, to);
|
|
1102
|
+
if (!r.ok)
|
|
1103
|
+
failed = r;
|
|
1104
|
+
else if (r.kind.k === "html")
|
|
1105
|
+
failed = unknown(body.slice(tokens[from].start, tokens[to - 1].end).slice(0, 40), HTML_AS_TEXT);
|
|
1106
|
+
return failed === undefined;
|
|
1107
|
+
});
|
|
1108
|
+
return failed ?? { ok: true, kind: PRIM, end: b };
|
|
1109
|
+
};
|
|
1110
|
+
/** Each const table's keys whose value is a number literal. */
|
|
1111
|
+
const tableNumbers = new Map();
|
|
1112
|
+
const tableNumber = (name, key) => {
|
|
1113
|
+
let map = tableNumbers.get(name);
|
|
1114
|
+
if (map === undefined) {
|
|
1115
|
+
map = null;
|
|
1116
|
+
const decls = (occurrences.get(name) ?? []).filter((k) => word(k - 1, "const"));
|
|
1117
|
+
const range = decls.length === 1 ? constValue(decls[0]) : undefined;
|
|
1118
|
+
if (nameVerdict(name).k === "table" &&
|
|
1119
|
+
range !== undefined &&
|
|
1120
|
+
punct(range[0], "{")) {
|
|
1121
|
+
map = new Map();
|
|
1122
|
+
const close = partner[range[0]];
|
|
1123
|
+
for (let j = range[0] + 1; j < close; j++) {
|
|
1124
|
+
if (tokens[j].type === "punctuation" ||
|
|
1125
|
+
!punct(j + 1, ":") ||
|
|
1126
|
+
!(punct(j - 1, "{") || punct(j - 1, ",")))
|
|
1127
|
+
continue;
|
|
1128
|
+
const next = j + 2;
|
|
1129
|
+
const text = body.slice(tokens[j + 1].end, tokens[next].start);
|
|
1130
|
+
const isNumber = (punct(next, ",") || next === close) &&
|
|
1131
|
+
isNumberText(text) &&
|
|
1132
|
+
/\d/.test(text);
|
|
1133
|
+
map.set(tokens[j].value, isNumber && map.get(tokens[j].value) !== false);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
tableNumbers.set(name, map);
|
|
1137
|
+
}
|
|
1138
|
+
return map?.get(key) === true;
|
|
1139
|
+
};
|
|
1140
|
+
const numericMemo = new Map();
|
|
1141
|
+
/** Whether a name holds a number: a number const, or a checked number parameter. */
|
|
1142
|
+
const numericName = (name) => {
|
|
1143
|
+
const memo = numericMemo.get(name);
|
|
1144
|
+
if (memo !== undefined)
|
|
1145
|
+
return memo === true;
|
|
1146
|
+
numericMemo.set(name, "pending");
|
|
1147
|
+
let ok = nameVerdict(name).k === "prim";
|
|
1148
|
+
for (const k of ok ? (occurrences.get(name) ?? []) : []) {
|
|
1149
|
+
if (word(k - 1, "const")) {
|
|
1150
|
+
const range = constValue(k);
|
|
1151
|
+
ok =
|
|
1152
|
+
range !== undefined &&
|
|
1153
|
+
numericSpan(range[0], range[1], tokens[range[0] - 1].end, range[1] < n ? tokens[range[1]].start : body.length);
|
|
1154
|
+
}
|
|
1155
|
+
else if (params.has(k))
|
|
1156
|
+
ok = params.get(k).plain;
|
|
1157
|
+
else if (word(k - 1, "function"))
|
|
1158
|
+
ok = false;
|
|
1159
|
+
if (!ok)
|
|
1160
|
+
break;
|
|
1161
|
+
}
|
|
1162
|
+
numericMemo.set(name, ok);
|
|
1163
|
+
return ok;
|
|
1164
|
+
};
|
|
1165
|
+
/**
|
|
1166
|
+
* Whether the code between offsets from and to, tokens [a, b), is
|
|
1167
|
+
* provably a number: number literals, number names, Math, Number(),
|
|
1168
|
+
* parseInt(), parseFloat(), Boolean(), Date.now(), a const table's
|
|
1169
|
+
* number and a plain value's length, joined by operators.
|
|
1170
|
+
*/
|
|
1171
|
+
const numericSpan = (a, b, from, to, self) => {
|
|
1172
|
+
let digits = false;
|
|
1173
|
+
let at = from;
|
|
1174
|
+
for (let j = a; j <= b; j++) {
|
|
1175
|
+
const gap = body.slice(at, j < b ? tokens[j].start : to);
|
|
1176
|
+
if (!isNumberText(gap))
|
|
1177
|
+
return false;
|
|
1178
|
+
if (/\d/.test(gap.replace(/\/\/[^\n]*|\/\*[\s\S]*?\*\//g, "")))
|
|
1179
|
+
digits = true;
|
|
1180
|
+
if (j < b)
|
|
1181
|
+
at = tokens[j].end;
|
|
1182
|
+
}
|
|
1183
|
+
if (a >= b)
|
|
1184
|
+
return digits;
|
|
1185
|
+
const trusted = (w) => nameVerdict(w).k === "global" && builtinChanged(w) === undefined;
|
|
1186
|
+
for (let j = a; j < b;) {
|
|
1187
|
+
const t = tokens[j];
|
|
1188
|
+
if (t.type === "punctuation") {
|
|
1189
|
+
if (!/^[-+*/%()~!<>=&|^?:]$/.test(t.value))
|
|
1190
|
+
return false;
|
|
1191
|
+
j++;
|
|
1192
|
+
continue;
|
|
1193
|
+
}
|
|
1194
|
+
if (t.type !== "identifier" || punct(j - 1, "."))
|
|
1195
|
+
return false;
|
|
1196
|
+
const w = t.value;
|
|
1197
|
+
if (LITERAL_WORDS.has(w)) {
|
|
1198
|
+
j++;
|
|
1199
|
+
continue;
|
|
1200
|
+
}
|
|
1201
|
+
if ((w === "Math" || w === "Number" || w === "Date") && trusted(w)) {
|
|
1202
|
+
let q = j + 1;
|
|
1203
|
+
if (punct(q, ".") && word(q + 1)) {
|
|
1204
|
+
if (w === "Date" && !/^(?:now|parse|UTC)$/.test(tokens[q + 1].value))
|
|
1205
|
+
return false;
|
|
1206
|
+
q += 2;
|
|
1207
|
+
}
|
|
1208
|
+
else if (w !== "Number")
|
|
1209
|
+
return false;
|
|
1210
|
+
if (punct(q, "(") && partner[q] !== -1 && partner[q] < b)
|
|
1211
|
+
q = partner[q] + 1;
|
|
1212
|
+
else if (w === "Date" || q === j + 1)
|
|
1213
|
+
return false;
|
|
1214
|
+
j = q;
|
|
1215
|
+
continue;
|
|
1216
|
+
}
|
|
1217
|
+
if ((w === "parseInt" || w === "parseFloat" || w === "Boolean") &&
|
|
1218
|
+
trusted(w) &&
|
|
1219
|
+
punct(j + 1, "(") &&
|
|
1220
|
+
partner[j + 1] !== -1 &&
|
|
1221
|
+
partner[j + 1] < b) {
|
|
1222
|
+
j = partner[j + 1] + 1;
|
|
1223
|
+
continue;
|
|
1224
|
+
}
|
|
1225
|
+
if (punct(j + 1, ".") && word(j + 2)) {
|
|
1226
|
+
if (punct(j + 3, "(") || punct(j + 3, ".") || punct(j + 3, "["))
|
|
1227
|
+
return false;
|
|
1228
|
+
const member = tokens[j + 2].value;
|
|
1229
|
+
const verdict = nameVerdict(w);
|
|
1230
|
+
// A table's number, or the length of a plain value.
|
|
1231
|
+
if (!(verdict.k === "table"
|
|
1232
|
+
? tableNumber(w, member)
|
|
1233
|
+
: member === "length" && evalRange(j, j + 1).ok))
|
|
1234
|
+
return false;
|
|
1235
|
+
j += 3;
|
|
1236
|
+
continue;
|
|
1237
|
+
}
|
|
1238
|
+
if (punct(j + 1, "(") || punct(j + 1, "["))
|
|
1239
|
+
return false;
|
|
1240
|
+
// The parameter being judged, passed on to its own position
|
|
1241
|
+
// (label(n) inside a function of n): a number if every other
|
|
1242
|
+
// call passes one.
|
|
1243
|
+
if (w !== self && !numericName(w))
|
|
1244
|
+
return false;
|
|
1245
|
+
j++;
|
|
1246
|
+
}
|
|
1247
|
+
return true;
|
|
1248
|
+
};
|
|
1249
|
+
/** For each local function: argument positions where some call passes what may not be a number (null: a spread). */
|
|
1250
|
+
const callNumbers = new Map();
|
|
1251
|
+
/** Each local function's parameter names, by position. */
|
|
1252
|
+
const parameterNames = new Map();
|
|
1253
|
+
for (const [k, p] of params)
|
|
1254
|
+
if (p.owner !== undefined) {
|
|
1255
|
+
const list = parameterNames.get(p.owner) ?? [];
|
|
1256
|
+
list[p.index] = tokens[k].value;
|
|
1257
|
+
parameterNames.set(p.owner, list);
|
|
1258
|
+
}
|
|
1259
|
+
const callsPassNumbers = (fn, index) => {
|
|
1260
|
+
if (!callNumbers.has(fn)) {
|
|
1261
|
+
callNumbers.set(fn, "pending");
|
|
1262
|
+
const names = parameterNames.get(fn) ?? [];
|
|
1263
|
+
let bad = [];
|
|
1264
|
+
for (const k of occurrences.get(fn.name) ?? []) {
|
|
1265
|
+
if (bad === null)
|
|
1266
|
+
break;
|
|
1267
|
+
if (tokens[k].start === fn.start || word(k - 1, "function"))
|
|
1268
|
+
continue;
|
|
1269
|
+
if (!punct(k + 1, "(") || partner[k + 1] === -1)
|
|
1270
|
+
continue;
|
|
1271
|
+
const close = partner[k + 1];
|
|
1272
|
+
let position = 0;
|
|
1273
|
+
const flags = bad;
|
|
1274
|
+
eachArgument(k + 2, close, (from, to) => {
|
|
1275
|
+
if (punct(from, ".") &&
|
|
1276
|
+
punct(from + 1, ".") &&
|
|
1277
|
+
punct(from + 2, ".")) {
|
|
1278
|
+
bad = null;
|
|
1279
|
+
return false;
|
|
1280
|
+
}
|
|
1281
|
+
const start = tokens[from - 1].end;
|
|
1282
|
+
const end = to < n ? tokens[to].start : body.length;
|
|
1283
|
+
if (!(from === to && body.slice(start, end).trim() === ""))
|
|
1284
|
+
if (!numericSpan(from, to, start, end, names[position]))
|
|
1285
|
+
flags[position] = true;
|
|
1286
|
+
position++;
|
|
1287
|
+
return true;
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
callNumbers.set(fn, bad);
|
|
1291
|
+
}
|
|
1292
|
+
const flags = callNumbers.get(fn);
|
|
1293
|
+
return Array.isArray(flags) && flags[index] !== true;
|
|
1294
|
+
};
|
|
1295
|
+
/** A helper's verdict: undefined when every return is plain. */
|
|
1296
|
+
const helperVerdict = (fn) => {
|
|
1297
|
+
const memo = helperMemo.get(fn);
|
|
1298
|
+
if (memo === "pending")
|
|
1299
|
+
return { what: `${fn.name}()`, why: "a function that calls itself" };
|
|
1300
|
+
if (memo !== undefined || helperMemo.has(fn))
|
|
1301
|
+
return memo;
|
|
1302
|
+
if (depth >= MAX_DEPTH)
|
|
1303
|
+
return {
|
|
1304
|
+
what: `${fn.name}()`,
|
|
1305
|
+
why: "a chain of calls too long to follow",
|
|
1306
|
+
};
|
|
1307
|
+
helperMemo.set(fn, "pending");
|
|
1308
|
+
depth++;
|
|
1309
|
+
let cause;
|
|
1310
|
+
if (fn.expression) {
|
|
1311
|
+
const a = tokenAt(fn.body);
|
|
1312
|
+
const b = tokenAt(fn.end);
|
|
1313
|
+
const r = evalRange(a, b);
|
|
1314
|
+
if (!r.ok)
|
|
1315
|
+
cause = r.cause;
|
|
1316
|
+
else if (r.kind.k === "html")
|
|
1317
|
+
helperHtml.add(fn);
|
|
1318
|
+
}
|
|
1319
|
+
else
|
|
1320
|
+
for (const k of returnsOf.get(fn) ?? []) {
|
|
1321
|
+
if (punct(k + 1, ";") || punct(k + 1, "}"))
|
|
1322
|
+
continue;
|
|
1323
|
+
const r = evalRange(k + 1, expressionEnd(k + 1, false));
|
|
1324
|
+
if (!r.ok) {
|
|
1325
|
+
cause = r.cause;
|
|
1326
|
+
break;
|
|
1327
|
+
}
|
|
1328
|
+
if (r.kind.k === "html")
|
|
1329
|
+
helperHtml.add(fn);
|
|
1330
|
+
}
|
|
1331
|
+
depth--;
|
|
1332
|
+
helperMemo.set(fn, cause);
|
|
1333
|
+
return cause;
|
|
1334
|
+
};
|
|
1335
|
+
/** What a name holds, judged from every declaration and use of it in the file. */
|
|
1336
|
+
const nameVerdict = (name) => {
|
|
1337
|
+
const memo = nameMemo.get(name);
|
|
1338
|
+
if (memo === "pending")
|
|
1339
|
+
return {
|
|
1340
|
+
k: "unknown",
|
|
1341
|
+
cause: { what: name, why: "a value defined in terms of itself" },
|
|
1342
|
+
};
|
|
1343
|
+
if (memo !== undefined)
|
|
1344
|
+
return memo;
|
|
1345
|
+
// A chain of names this long is not followed: the stack stays bounded.
|
|
1346
|
+
if (depth >= MAX_DEPTH)
|
|
1347
|
+
return {
|
|
1348
|
+
k: "unknown",
|
|
1349
|
+
cause: { what: name, why: "a chain of values too long to follow" },
|
|
1350
|
+
};
|
|
1351
|
+
nameMemo.set(name, "pending");
|
|
1352
|
+
depth++;
|
|
1353
|
+
const v = judgeName(name);
|
|
1354
|
+
depth--;
|
|
1355
|
+
nameMemo.set(name, v);
|
|
1356
|
+
return v;
|
|
1357
|
+
};
|
|
1358
|
+
const judgeName = (name) => {
|
|
1359
|
+
const occs = occurrences.get(name) ?? [];
|
|
1360
|
+
const kinds = [];
|
|
1361
|
+
const reads = [];
|
|
1362
|
+
const no = (why, what = name) => ({
|
|
1363
|
+
k: "unknown",
|
|
1364
|
+
cause: { what, why },
|
|
1365
|
+
});
|
|
1366
|
+
let fromParam = false;
|
|
1367
|
+
for (const k of occs) {
|
|
1368
|
+
const prev = k > 0 ? tokens[k - 1] : undefined;
|
|
1369
|
+
const prevWord = prev?.type === "identifier" ? prev.value : "";
|
|
1370
|
+
if (imported[k])
|
|
1371
|
+
return no("a value imported from another module");
|
|
1372
|
+
if (prevWord === "let" || prevWord === "var")
|
|
1373
|
+
return no("a let or var, which can be reassigned");
|
|
1374
|
+
if (TYPE_DECLARATIONS.has(prevWord) && prevWord !== "declare")
|
|
1375
|
+
return no("a class or one of its fields");
|
|
1376
|
+
if (prevWord === "declare")
|
|
1377
|
+
return no("a declared global");
|
|
1378
|
+
if (prevWord === "const") {
|
|
1379
|
+
const range = constValue(k);
|
|
1380
|
+
if (range === undefined)
|
|
1381
|
+
return no("a loop variable");
|
|
1382
|
+
const [a, b] = range;
|
|
1383
|
+
const fn = functionAt.get(tokens[k].start);
|
|
1384
|
+
if (fn !== undefined && (word(a) || punct(a, "("))) {
|
|
1385
|
+
if (exported[k])
|
|
1386
|
+
return no("an exported function", `${name}()`);
|
|
1387
|
+
kinds.push({ k: "helper" });
|
|
1388
|
+
continue;
|
|
1389
|
+
}
|
|
1390
|
+
if (isTable(a, b)) {
|
|
1391
|
+
if (exported[k])
|
|
1392
|
+
return no("an exported object, which other modules can change");
|
|
1393
|
+
kinds.push({ k: "table" });
|
|
1394
|
+
continue;
|
|
1395
|
+
}
|
|
1396
|
+
const r = evalRange(a, b);
|
|
1397
|
+
if (!r.ok)
|
|
1398
|
+
return { k: "unknown", cause: r.cause };
|
|
1399
|
+
kinds.push(r.kind);
|
|
1400
|
+
continue;
|
|
1401
|
+
}
|
|
1402
|
+
if (prevWord === "function") {
|
|
1403
|
+
const fn = functionAt.get(tokens[k].start);
|
|
1404
|
+
if (fn === undefined)
|
|
1405
|
+
return no("a function Bascik cannot follow", `${name}()`);
|
|
1406
|
+
if (exported[k])
|
|
1407
|
+
return no("an exported function", `${name}()`);
|
|
1408
|
+
kinds.push({ k: "helper" });
|
|
1409
|
+
continue;
|
|
1410
|
+
}
|
|
1411
|
+
const param = params.get(k);
|
|
1412
|
+
if (param !== undefined) {
|
|
1413
|
+
if (param.catch)
|
|
1414
|
+
return no("a catch binding");
|
|
1415
|
+
if (param.request) {
|
|
1416
|
+
kinds.push({ k: "request", name });
|
|
1417
|
+
continue;
|
|
1418
|
+
}
|
|
1419
|
+
if (!param.plain)
|
|
1420
|
+
return no("a parameter not typed number, bigint or boolean");
|
|
1421
|
+
if (param.owner === undefined ||
|
|
1422
|
+
param.owner.name === name ||
|
|
1423
|
+
nameVerdict(param.owner.name).k !== "helper")
|
|
1424
|
+
return no("a parameter of a function whose callers this file cannot all see (exported, a callback, a method, or passed on), and a JavaScript caller can pass markup whatever its type says");
|
|
1425
|
+
if (param.defaultRange !== undefined) {
|
|
1426
|
+
const [a, b] = param.defaultRange;
|
|
1427
|
+
if (!numericSpan(a, b, tokens[a - 1].end, tokens[b].start))
|
|
1428
|
+
return no("a number parameter whose default value Bascik cannot prove is a number");
|
|
1429
|
+
}
|
|
1430
|
+
if (!callsPassNumbers(param.owner, param.index))
|
|
1431
|
+
return no("a number parameter that a call fills with a value Bascik cannot prove is a number");
|
|
1432
|
+
fromParam = true;
|
|
1433
|
+
kinds.push(PRIM);
|
|
1434
|
+
continue;
|
|
1435
|
+
}
|
|
1436
|
+
// A destructuring pattern: a declaration or an assignment target.
|
|
1437
|
+
const open = parentOpen[k];
|
|
1438
|
+
if (open !== -1 &&
|
|
1439
|
+
(punct(open, "{") || punct(open, "[")) &&
|
|
1440
|
+
!isIndex(open)) {
|
|
1441
|
+
const root = patternRoot[open];
|
|
1442
|
+
const close = partner[root];
|
|
1443
|
+
const declared = word(root - 1, "const") ||
|
|
1444
|
+
word(root - 1, "let") ||
|
|
1445
|
+
word(root - 1, "var");
|
|
1446
|
+
if (!isIndex(root) &&
|
|
1447
|
+
(declared ||
|
|
1448
|
+
(close !== -1 &&
|
|
1449
|
+
punct(close + 1, "=") &&
|
|
1450
|
+
!punct(close + 2, "=") &&
|
|
1451
|
+
!punct(close + 2, ">")))) {
|
|
1452
|
+
if (word(root - 1, "const") && punct(root, "{") && close !== -1) {
|
|
1453
|
+
let simple = true;
|
|
1454
|
+
for (let j = root + 1; j < close; j++)
|
|
1455
|
+
if (!(word(j) || punct(j, ",")))
|
|
1456
|
+
simple = false;
|
|
1457
|
+
if (simple && punct(close + 1, "=")) {
|
|
1458
|
+
const r = evalRange(close + 2, expressionEnd(close + 2, true));
|
|
1459
|
+
if (r.ok && r.kind.k === "request") {
|
|
1460
|
+
kinds.push({ k: "request" });
|
|
1461
|
+
continue;
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
return no(declared
|
|
1466
|
+
? "a destructured name"
|
|
1467
|
+
: "a destructuring assignment target");
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
reads.push(k);
|
|
1471
|
+
}
|
|
1472
|
+
if (kinds.length === 0)
|
|
1473
|
+
return { k: "global" };
|
|
1474
|
+
if (TRUSTED_GLOBALS.has(name) || GLOBAL_OBJECTS.has(name))
|
|
1475
|
+
return no("a built-in name this file declares as its own");
|
|
1476
|
+
const first = kinds[0];
|
|
1477
|
+
if (!kinds.every((x) => x.k === first.k))
|
|
1478
|
+
return no("a name declared more than once in different ways");
|
|
1479
|
+
let escapes = false;
|
|
1480
|
+
for (const k of reads) {
|
|
1481
|
+
if (isAssignAt(k + 1) || isPrefixUpdate(k))
|
|
1482
|
+
return no("a value assigned elsewhere in the file");
|
|
1483
|
+
if (fromParam && word(k + 1, "as"))
|
|
1484
|
+
return no("a parameter cast with as");
|
|
1485
|
+
if (exported[k] && first.k !== "prim")
|
|
1486
|
+
return no("a value exported to other modules");
|
|
1487
|
+
switch (first.k) {
|
|
1488
|
+
case "helper":
|
|
1489
|
+
if (!punct(k + 1, "(") || word(k - 1, "new"))
|
|
1490
|
+
return no("a function passed on as a value, so its callers are out of sight", `${name}()`);
|
|
1491
|
+
break;
|
|
1492
|
+
case "table": {
|
|
1493
|
+
const literalKey = (punct(k + 1, ".") && word(k + 2) && !punct(k + 3, "(")) ||
|
|
1494
|
+
(punct(k + 1, "[") &&
|
|
1495
|
+
partner[k + 1] !== -1 &&
|
|
1496
|
+
(partner[k + 1] === k + 2 ||
|
|
1497
|
+
(partner[k + 1] === k + 3 &&
|
|
1498
|
+
tokens[k + 2].type === "string")) &&
|
|
1499
|
+
/^\[\s*(?:\d+|"[^"]*"|'[^']*')\s*\]$/.test(body.slice(tokens[k + 1].start, tokens[partner[k + 1]].end)));
|
|
1500
|
+
const after = punct(k + 1, ".") ? k + 3 : partner[k + 1] + 1;
|
|
1501
|
+
if (!literalKey || isAssignAt(after))
|
|
1502
|
+
return no("an object or array other code can change");
|
|
1503
|
+
break;
|
|
1504
|
+
}
|
|
1505
|
+
case "request":
|
|
1506
|
+
case "date": {
|
|
1507
|
+
const chain = chainAfter(k);
|
|
1508
|
+
if (chain.assigned && chain.members > 0)
|
|
1509
|
+
return no("an object whose fields are written elsewhere in the file");
|
|
1510
|
+
if (first.k === "request" &&
|
|
1511
|
+
first.name !== "request" &&
|
|
1512
|
+
chain.methods.some((m) => !REQUEST_READ_METHODS.has(m)))
|
|
1513
|
+
return no("an object changed elsewhere in the file");
|
|
1514
|
+
if (chain.members === 0 &&
|
|
1515
|
+
!(first.k === "request" && first.name === "request") &&
|
|
1516
|
+
handsOn(k))
|
|
1517
|
+
escapes = true;
|
|
1518
|
+
break;
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
if (first.k === "date" && escapes)
|
|
1523
|
+
return no("a Date handed to other code, which may change it");
|
|
1524
|
+
if (first.k === "request")
|
|
1525
|
+
return { k: "request", name: first.name, escapes };
|
|
1526
|
+
return first;
|
|
1527
|
+
};
|
|
1528
|
+
/** Evaluates the value at token k (a name, a call, `new ...`) and what follows it. */
|
|
1529
|
+
const evalChain = (k, limit) => {
|
|
1530
|
+
const t = tokens[k];
|
|
1531
|
+
const name = t.value;
|
|
1532
|
+
let j = k + 1;
|
|
1533
|
+
let kind;
|
|
1534
|
+
const tagged = (at) => body[tokens[at - 1]?.end ?? 0] === "`";
|
|
1535
|
+
// An html value: html`...`, raw(), safeUrl(), json(). What follows it
|
|
1536
|
+
// (a member, a call) is checked below.
|
|
1537
|
+
let htmlEnd = -1;
|
|
1538
|
+
if (opts.isHtmlValue(name) || opts.htmlTags.includes(name)) {
|
|
1539
|
+
const end = tagged(j) ? opts.templateEndAt(t.end) : undefined;
|
|
1540
|
+
if (end !== undefined) {
|
|
1541
|
+
kind = HTML;
|
|
1542
|
+
j = htmlEnd = tokenAt(end);
|
|
1543
|
+
}
|
|
1544
|
+
else if (punct(j, "(") && partner[j] !== -1) {
|
|
1545
|
+
kind = HTML;
|
|
1546
|
+
j = htmlEnd = partner[j] + 1;
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
if (kind !== undefined) {
|
|
1550
|
+
// An html value; nothing more to resolve.
|
|
1551
|
+
}
|
|
1552
|
+
else if (name === "new") {
|
|
1553
|
+
const ctor = tokens[k + 1];
|
|
1554
|
+
if (!word(k + 1) || !punct(k + 2, "(") || partner[k + 2] === -1)
|
|
1555
|
+
return unknown(`new ${ctor?.value ?? ""}`.trim(), "a constructed object");
|
|
1556
|
+
const close = partner[k + 2];
|
|
1557
|
+
const changed = builtinChanged(ctor.value);
|
|
1558
|
+
if (changed !== undefined && TRUSTED_GLOBALS.has(ctor.value))
|
|
1559
|
+
return { ok: false, cause: changed };
|
|
1560
|
+
if (ctor.value === "Date" && nameVerdict("Date").k === "global")
|
|
1561
|
+
kind = { k: "date" };
|
|
1562
|
+
else if ((ctor.value === "URL" || ctor.value === "URLSearchParams") &&
|
|
1563
|
+
nameVerdict(ctor.value).k === "global") {
|
|
1564
|
+
// A URL decodes %3C and %26 into < and &: it holds request data
|
|
1565
|
+
// only when built from request data (a base may be a literal with
|
|
1566
|
+
// no % in it).
|
|
1567
|
+
if (requestTaint !== undefined)
|
|
1568
|
+
return { ok: false, cause: requestTaint };
|
|
1569
|
+
let failed;
|
|
1570
|
+
let position = 0;
|
|
1571
|
+
eachArgument(k + 3, close, (from, to) => {
|
|
1572
|
+
const plainBase = position > 0 &&
|
|
1573
|
+
to === from + 1 &&
|
|
1574
|
+
tokens[from].type === "string" &&
|
|
1575
|
+
!tokens[from].value.includes("%");
|
|
1576
|
+
position++;
|
|
1577
|
+
if (plainBase)
|
|
1578
|
+
return true;
|
|
1579
|
+
const r = from < to ? evalRange(from, to) : undefined;
|
|
1580
|
+
if (r !== undefined && !r.ok)
|
|
1581
|
+
failed = r;
|
|
1582
|
+
else if (r === undefined || r.kind.k !== "request")
|
|
1583
|
+
failed = unknown(`new ${ctor.value}()`, "a URL built from something other than request data, which decodes %3C and %26 into markup characters");
|
|
1584
|
+
return failed === undefined;
|
|
1585
|
+
});
|
|
1586
|
+
if (failed !== undefined)
|
|
1587
|
+
return failed;
|
|
1588
|
+
kind = { k: "request" };
|
|
1589
|
+
}
|
|
1590
|
+
else
|
|
1591
|
+
return unknown(`new ${ctor.value}()`, "a constructed object");
|
|
1592
|
+
j = close + 1;
|
|
1593
|
+
}
|
|
1594
|
+
else {
|
|
1595
|
+
const v = nameVerdict(name);
|
|
1596
|
+
if (v.k === "unknown") {
|
|
1597
|
+
const cause = v.cause.what === name || v.cause.what === `${name}()`
|
|
1598
|
+
? v.cause
|
|
1599
|
+
: {
|
|
1600
|
+
what: `${v.cause.what.split(" (through ")[0]} (through ${name})`,
|
|
1601
|
+
why: v.cause.why,
|
|
1602
|
+
};
|
|
1603
|
+
return { ok: false, cause };
|
|
1604
|
+
}
|
|
1605
|
+
if (v.k === "global") {
|
|
1606
|
+
const changed = builtinChanged(name);
|
|
1607
|
+
if (changed !== undefined)
|
|
1608
|
+
return { ok: false, cause: changed };
|
|
1609
|
+
if (STRING_CALLS.has(name) && punct(j, "(") && partner[j] !== -1) {
|
|
1610
|
+
const r = evalArgs(j + 1, partner[j]);
|
|
1611
|
+
if (!r.ok)
|
|
1612
|
+
return r;
|
|
1613
|
+
kind = PRIM;
|
|
1614
|
+
j = partner[j] + 1;
|
|
1615
|
+
}
|
|
1616
|
+
else if (NUMBER_CALLS.has(name) &&
|
|
1617
|
+
punct(j, "(") &&
|
|
1618
|
+
partner[j] !== -1) {
|
|
1619
|
+
kind = PRIM;
|
|
1620
|
+
j = partner[j] + 1;
|
|
1621
|
+
}
|
|
1622
|
+
else if (name === "Number" &&
|
|
1623
|
+
punct(j, ".") &&
|
|
1624
|
+
/^(?:isFinite|isInteger|isSafeInteger|isNaN|parseInt|parseFloat|MAX_SAFE_INTEGER|MIN_SAFE_INTEGER|EPSILON)$/.test(tokens[j + 1]?.value ?? "")) {
|
|
1625
|
+
j += 2;
|
|
1626
|
+
if (punct(j, "(") && partner[j] !== -1)
|
|
1627
|
+
j = partner[j] + 1;
|
|
1628
|
+
kind = PRIM;
|
|
1629
|
+
}
|
|
1630
|
+
else if (name === "Math" && punct(j, ".") && word(j + 1)) {
|
|
1631
|
+
j += 2;
|
|
1632
|
+
if (punct(j, "(") && partner[j] !== -1)
|
|
1633
|
+
j = partner[j] + 1;
|
|
1634
|
+
kind = PRIM;
|
|
1635
|
+
}
|
|
1636
|
+
else if (name === "Date" &&
|
|
1637
|
+
punct(j, ".") &&
|
|
1638
|
+
/^(?:now|parse|UTC)$/.test(tokens[j + 1]?.value ?? "") &&
|
|
1639
|
+
punct(j + 2, "(") &&
|
|
1640
|
+
partner[j + 2] !== -1) {
|
|
1641
|
+
j = partner[j + 2] + 1;
|
|
1642
|
+
kind = PRIM;
|
|
1643
|
+
}
|
|
1644
|
+
else if (name === "process" &&
|
|
1645
|
+
punct(j, ".") &&
|
|
1646
|
+
word(j + 1, "env") &&
|
|
1647
|
+
((punct(j + 2, ".") && word(j + 3)) ||
|
|
1648
|
+
(punct(j + 2, "[") &&
|
|
1649
|
+
partner[j + 2] === j + 4 &&
|
|
1650
|
+
tokens[j + 3].type === "string"))) {
|
|
1651
|
+
j = punct(j + 2, ".") ? j + 4 : j + 5;
|
|
1652
|
+
kind = PRIM;
|
|
1653
|
+
}
|
|
1654
|
+
else
|
|
1655
|
+
return unknown(name, "a value from outside the file (a global or an undeclared name)");
|
|
1656
|
+
}
|
|
1657
|
+
else if (v.k === "helper") {
|
|
1658
|
+
if (!punct(j, "(") || partner[j] === -1)
|
|
1659
|
+
return unknown(`${name}()`, "a function passed on as a value");
|
|
1660
|
+
const fn = opts.functions.filter((f) => f.name === name);
|
|
1661
|
+
for (const f of fn) {
|
|
1662
|
+
const cause = helperVerdict(f);
|
|
1663
|
+
if (cause !== undefined)
|
|
1664
|
+
return {
|
|
1665
|
+
ok: false,
|
|
1666
|
+
cause: cause.what === `${name}()`
|
|
1667
|
+
? cause
|
|
1668
|
+
: {
|
|
1669
|
+
what: `${name}()`,
|
|
1670
|
+
why: `a function that returns ${cause.what}, ${cause.why}`,
|
|
1671
|
+
},
|
|
1672
|
+
};
|
|
1673
|
+
}
|
|
1674
|
+
if (fn.length === 0)
|
|
1675
|
+
return unknown(`${name}()`, "a function Bascik cannot follow");
|
|
1676
|
+
// Its arguments reach the result only through its parameters,
|
|
1677
|
+
// which are judged where they are read.
|
|
1678
|
+
kind = fn.some((f) => helperHtml.has(f)) ? HTML : PRIM;
|
|
1679
|
+
j = partner[j] + 1;
|
|
1680
|
+
}
|
|
1681
|
+
else if (v.k === "table") {
|
|
1682
|
+
if (punct(j, ".") && word(j + 1))
|
|
1683
|
+
j += 2;
|
|
1684
|
+
else if (punct(j, "[") && partner[j] !== -1)
|
|
1685
|
+
j = partner[j] + 1;
|
|
1686
|
+
else
|
|
1687
|
+
return unknown(name, "an object or array other code can change");
|
|
1688
|
+
kind = PRIM;
|
|
1689
|
+
}
|
|
1690
|
+
else if (v.k === "request") {
|
|
1691
|
+
if (v.name === "context") {
|
|
1692
|
+
if (!(punct(j, ".") &&
|
|
1693
|
+
/^(?:params|remoteIp)$/.test(tokens[j + 1]?.value ?? "")))
|
|
1694
|
+
return unknown(`${name}.${tokens[j + 1]?.value ?? ""}`, "context data other than params and remoteIp, which can hold the host's own values");
|
|
1695
|
+
}
|
|
1696
|
+
if (v.escapes && (punct(j, ".") || punct(j, "[")))
|
|
1697
|
+
return unknown(name, "an object handed to other code, which may change it");
|
|
1698
|
+
if (requestTaint !== undefined)
|
|
1699
|
+
return { ok: false, cause: requestTaint };
|
|
1700
|
+
kind = { k: "request" };
|
|
1701
|
+
}
|
|
1702
|
+
else
|
|
1703
|
+
kind = v;
|
|
1704
|
+
}
|
|
1705
|
+
// What follows: members, calls and indexes.
|
|
1706
|
+
for (;;) {
|
|
1707
|
+
if (j >= limit)
|
|
1708
|
+
break;
|
|
1709
|
+
if (punct(j, "!") && !punct(j + 1, "=")) {
|
|
1710
|
+
j++;
|
|
1711
|
+
continue;
|
|
1712
|
+
}
|
|
1713
|
+
if (punct(j, "?") && punct(j + 1, ".") && adjacent(j + 1)) {
|
|
1714
|
+
j++;
|
|
1715
|
+
if (punct(j + 1, "(") || punct(j + 1, "["))
|
|
1716
|
+
j++;
|
|
1717
|
+
}
|
|
1718
|
+
if (punct(j, ".") && word(j + 1)) {
|
|
1719
|
+
const member = tokens[j + 1].value;
|
|
1720
|
+
// The call's `(`: right after the member, or after `?.`.
|
|
1721
|
+
const callAt = punct(j + 2, "(") && partner[j + 2] !== -1
|
|
1722
|
+
? j + 2
|
|
1723
|
+
: punct(j + 2, "?") &&
|
|
1724
|
+
punct(j + 3, ".") &&
|
|
1725
|
+
adjacent(j + 3) &&
|
|
1726
|
+
punct(j + 4, "(") &&
|
|
1727
|
+
partner[j + 4] !== -1
|
|
1728
|
+
? j + 4
|
|
1729
|
+
: -1;
|
|
1730
|
+
const call = callAt !== -1;
|
|
1731
|
+
if (kind.k === "date") {
|
|
1732
|
+
// A Date's methods return text or numbers; its other members
|
|
1733
|
+
// (constructor and the like) are not followed.
|
|
1734
|
+
if (!call)
|
|
1735
|
+
return unknown(body.slice(t.start, tokens[j + 1].end), `a read of .${member} on a Date, which Bascik cannot follow`);
|
|
1736
|
+
const r = evalArgs(callAt + 1, partner[callAt]);
|
|
1737
|
+
if (!r.ok)
|
|
1738
|
+
return r;
|
|
1739
|
+
j = partner[callAt] + 1;
|
|
1740
|
+
kind = PRIM;
|
|
1741
|
+
continue;
|
|
1742
|
+
}
|
|
1743
|
+
if (kind.k === "request") {
|
|
1744
|
+
if (call && !REQUEST_READ_METHODS.has(member))
|
|
1745
|
+
return unknown(`.${member}()`, "a method Bascik does not know returns text");
|
|
1746
|
+
j += 2;
|
|
1747
|
+
if (call) {
|
|
1748
|
+
const r = evalArgs(callAt + 1, partner[callAt]);
|
|
1749
|
+
if (!r.ok)
|
|
1750
|
+
return r;
|
|
1751
|
+
j = partner[callAt] + 1;
|
|
1752
|
+
}
|
|
1753
|
+
continue;
|
|
1754
|
+
}
|
|
1755
|
+
if (member === "length" && !call) {
|
|
1756
|
+
kind = PRIM;
|
|
1757
|
+
j += 2;
|
|
1758
|
+
continue;
|
|
1759
|
+
}
|
|
1760
|
+
if (STRING_METHODS.has(member) && call && kind.k !== "html") {
|
|
1761
|
+
const r = evalArgs(callAt + 1, partner[callAt]);
|
|
1762
|
+
if (!r.ok)
|
|
1763
|
+
return r;
|
|
1764
|
+
j = partner[callAt] + 1;
|
|
1765
|
+
kind = PRIM;
|
|
1766
|
+
continue;
|
|
1767
|
+
}
|
|
1768
|
+
return unknown(body.slice(t.start, tokens[call ? partner[callAt] : j + 1].end), call
|
|
1769
|
+
? `a call of .${member}(), which Bascik does not know returns text`
|
|
1770
|
+
: `a read of .${member}, which Bascik cannot follow`);
|
|
1771
|
+
}
|
|
1772
|
+
if (punct(j, "[") && partner[j] !== -1) {
|
|
1773
|
+
if (kind.k === "html")
|
|
1774
|
+
return unknown(body.slice(t.start, tokens[partner[j]].end), "an index Bascik cannot follow");
|
|
1775
|
+
const r = evalRange(j + 1, partner[j]);
|
|
1776
|
+
if (!r.ok)
|
|
1777
|
+
return r;
|
|
1778
|
+
j = partner[j] + 1;
|
|
1779
|
+
if (kind.k !== "request")
|
|
1780
|
+
kind = PRIM;
|
|
1781
|
+
continue;
|
|
1782
|
+
}
|
|
1783
|
+
if (punct(j, "(") && partner[j] !== -1) {
|
|
1784
|
+
return unknown(body.slice(t.start, tokens[partner[j]].end), "a call Bascik cannot follow");
|
|
1785
|
+
}
|
|
1786
|
+
break;
|
|
1787
|
+
}
|
|
1788
|
+
if (j !== htmlEnd && tagged(j) && j <= limit)
|
|
1789
|
+
return unknown(body.slice(t.start, tokens[j - 1].end), "a tagged template");
|
|
1790
|
+
return { ok: true, kind, end: j };
|
|
1791
|
+
};
|
|
1792
|
+
/** Evaluates tokens [a, b): every value in them must be plain. */
|
|
1793
|
+
const evalRange = (a, b) => {
|
|
1794
|
+
if (a >= b)
|
|
1795
|
+
return { ok: true, kind: PRIM, end: b };
|
|
1796
|
+
// A template with holes leaves no token for its backtick or text, so
|
|
1797
|
+
// the gaps around the range count too (kept inside the hole itself).
|
|
1798
|
+
let lo = a > 0 ? tokens[a - 1].end : 0;
|
|
1799
|
+
let hi = b < n ? tokens[b].start : body.length;
|
|
1800
|
+
if (tokens[a].start >= holeLo && tokens[b - 1].end <= holeHi) {
|
|
1801
|
+
lo = Math.max(lo, holeLo);
|
|
1802
|
+
hi = Math.min(hi, holeHi);
|
|
1803
|
+
}
|
|
1804
|
+
lo = Math.min(lo, tokens[a].start);
|
|
1805
|
+
hi = Math.max(hi, tokens[b - 1].end);
|
|
1806
|
+
if (opts.markupWithin(lo, hi))
|
|
1807
|
+
return unknown(body.slice(tokens[a].start, tokens[b - 1].end).slice(0, 40), "markup built as a string");
|
|
1808
|
+
if (opts.markupCharacterTextWithin?.(lo, hi))
|
|
1809
|
+
return unknown(body.slice(tokens[a].start, tokens[b - 1].end).slice(0, 40), MARKUP_CHARACTER);
|
|
1810
|
+
let chains = 0;
|
|
1811
|
+
let other = false;
|
|
1812
|
+
let last = PRIM;
|
|
1813
|
+
// An html value here, and whether + or a template turns it into text.
|
|
1814
|
+
let html = false;
|
|
1815
|
+
let plus = false;
|
|
1816
|
+
let i = a;
|
|
1817
|
+
while (i < b) {
|
|
1818
|
+
const t = tokens[i];
|
|
1819
|
+
if (t.type === "punctuation") {
|
|
1820
|
+
const v = t.value;
|
|
1821
|
+
if (v === "(") {
|
|
1822
|
+
if (partner[i] === -1 || partner[i] >= b)
|
|
1823
|
+
return unknown(t.value, "an expression Bascik cannot follow");
|
|
1824
|
+
const r = evalRange(i + 1, partner[i]);
|
|
1825
|
+
if (!r.ok)
|
|
1826
|
+
return r;
|
|
1827
|
+
i = partner[i] + 1;
|
|
1828
|
+
other = true;
|
|
1829
|
+
if (r.kind.k === "html")
|
|
1830
|
+
html = true;
|
|
1831
|
+
// A member of a parenthesized value.
|
|
1832
|
+
while (punct(i, ".") && word(i + 1)) {
|
|
1833
|
+
const m = tokens[i + 1].value;
|
|
1834
|
+
if (r.kind.k === "html")
|
|
1835
|
+
return unknown(`.${m}`, HTML_AS_TEXT);
|
|
1836
|
+
if (m === "length" && !punct(i + 2, "(")) {
|
|
1837
|
+
i += 2;
|
|
1838
|
+
continue;
|
|
1839
|
+
}
|
|
1840
|
+
if (STRING_METHODS.has(m) &&
|
|
1841
|
+
punct(i + 2, "(") &&
|
|
1842
|
+
partner[i + 2] !== -1) {
|
|
1843
|
+
const ar = evalArgs(i + 3, partner[i + 2]);
|
|
1844
|
+
if (!ar.ok)
|
|
1845
|
+
return ar;
|
|
1846
|
+
i = partner[i + 2] + 1;
|
|
1847
|
+
continue;
|
|
1848
|
+
}
|
|
1849
|
+
return unknown(`.${m}`, "a member Bascik cannot follow");
|
|
1850
|
+
}
|
|
1851
|
+
continue;
|
|
1852
|
+
}
|
|
1853
|
+
if (v === "{")
|
|
1854
|
+
return unknown("{...}", "an object literal, which Bascik does not follow");
|
|
1855
|
+
if (v === "[")
|
|
1856
|
+
return unknown("[...]", "an array literal, which Bascik does not follow");
|
|
1857
|
+
if (v === ".") {
|
|
1858
|
+
if (isSpread(i + 3) || (punct(i + 1, ".") && punct(i + 2, ".")))
|
|
1859
|
+
return unknown("...", "a spread, which Bascik does not follow");
|
|
1860
|
+
// A member of a literal or a template.
|
|
1861
|
+
if (word(i + 1)) {
|
|
1862
|
+
const m = tokens[i + 1].value;
|
|
1863
|
+
if (m === "length" && !punct(i + 2, "(")) {
|
|
1864
|
+
i += 2;
|
|
1865
|
+
continue;
|
|
1866
|
+
}
|
|
1867
|
+
if (STRING_METHODS.has(m) &&
|
|
1868
|
+
punct(i + 2, "(") &&
|
|
1869
|
+
partner[i + 2] !== -1) {
|
|
1870
|
+
const ar = evalArgs(i + 3, partner[i + 2]);
|
|
1871
|
+
if (!ar.ok)
|
|
1872
|
+
return ar;
|
|
1873
|
+
i = partner[i + 2] + 1;
|
|
1874
|
+
continue;
|
|
1875
|
+
}
|
|
1876
|
+
return unknown(`.${m}`, "a member Bascik cannot follow");
|
|
1877
|
+
}
|
|
1878
|
+
i++;
|
|
1879
|
+
continue;
|
|
1880
|
+
}
|
|
1881
|
+
if (OP_CHARS.has(v)) {
|
|
1882
|
+
const op = opAt(i);
|
|
1883
|
+
if (op.includes("=>"))
|
|
1884
|
+
return unknown("=>", "a function, which Bascik does not follow");
|
|
1885
|
+
if (isAssignOp(op))
|
|
1886
|
+
return unknown(op, "an assignment, which Bascik does not follow");
|
|
1887
|
+
if (op.includes("+"))
|
|
1888
|
+
plus = true;
|
|
1889
|
+
i += Math.max(1, op.length);
|
|
1890
|
+
other = true;
|
|
1891
|
+
continue;
|
|
1892
|
+
}
|
|
1893
|
+
other = true;
|
|
1894
|
+
i++;
|
|
1895
|
+
continue;
|
|
1896
|
+
}
|
|
1897
|
+
if (t.type === "string" || t.type === "template") {
|
|
1898
|
+
if (holdsMarkupCharacter(t.value))
|
|
1899
|
+
return unknown((t.type === "string" ? `"${t.value}"` : `\`${t.value}\``).slice(0, 40), MARKUP_CHARACTER);
|
|
1900
|
+
// A template token is a template with no holes; one right after a
|
|
1901
|
+
// value is tagged, which evalChain has refused already.
|
|
1902
|
+
i++;
|
|
1903
|
+
other = true;
|
|
1904
|
+
continue;
|
|
1905
|
+
}
|
|
1906
|
+
const w = t.value;
|
|
1907
|
+
if (LITERAL_WORDS.has(w)) {
|
|
1908
|
+
i++;
|
|
1909
|
+
other = true;
|
|
1910
|
+
continue;
|
|
1911
|
+
}
|
|
1912
|
+
if (w === "typeof" || w === "void") {
|
|
1913
|
+
// typeof x is a type name and void x is undefined, whatever x is.
|
|
1914
|
+
i++;
|
|
1915
|
+
if (punct(i, "(") && partner[i] !== -1)
|
|
1916
|
+
i = partner[i] + 1;
|
|
1917
|
+
else if (word(i))
|
|
1918
|
+
i = chainAfter(i).end;
|
|
1919
|
+
other = true;
|
|
1920
|
+
continue;
|
|
1921
|
+
}
|
|
1922
|
+
if (w === "await" || w === "in" || w === "instanceof") {
|
|
1923
|
+
i++;
|
|
1924
|
+
if (w !== "await")
|
|
1925
|
+
other = true;
|
|
1926
|
+
continue;
|
|
1927
|
+
}
|
|
1928
|
+
if (w === "as" || w === "satisfies") {
|
|
1929
|
+
if (w === "as" && word(i + 1, "const")) {
|
|
1930
|
+
i += 2;
|
|
1931
|
+
continue;
|
|
1932
|
+
}
|
|
1933
|
+
return unknown("as", "a value cast with as");
|
|
1934
|
+
}
|
|
1935
|
+
if (UNKNOWN_WORDS.has(w))
|
|
1936
|
+
return unknown(w, w === "this" ? "a this field" : "a value Bascik cannot follow");
|
|
1937
|
+
const r = evalChain(i, b);
|
|
1938
|
+
if (!r.ok)
|
|
1939
|
+
return r;
|
|
1940
|
+
chains++;
|
|
1941
|
+
last = r.kind;
|
|
1942
|
+
if (r.kind.k === "html")
|
|
1943
|
+
html = true;
|
|
1944
|
+
i = r.end;
|
|
1945
|
+
}
|
|
1946
|
+
if (html && (plus || opts.plainTemplateWithin?.(lo, hi)))
|
|
1947
|
+
return unknown(body.slice(tokens[a].start, tokens[b - 1].end).slice(0, 40), HTML_AS_TEXT);
|
|
1948
|
+
return {
|
|
1949
|
+
ok: true,
|
|
1950
|
+
kind: html ? HTML : chains === 1 && !other ? last : PRIM,
|
|
1951
|
+
end: b,
|
|
1952
|
+
};
|
|
1953
|
+
};
|
|
1954
|
+
return (from, to) => {
|
|
1955
|
+
if (rewrites !== undefined)
|
|
1956
|
+
return rewrites;
|
|
1957
|
+
if (prototypeReach !== undefined)
|
|
1958
|
+
return prototypeReach;
|
|
1959
|
+
const a = tokenAt(from);
|
|
1960
|
+
let b = a;
|
|
1961
|
+
while (b < n && tokens[b].start < to)
|
|
1962
|
+
b++;
|
|
1963
|
+
holeLo = from;
|
|
1964
|
+
holeHi = to;
|
|
1965
|
+
const r = evalRange(a, b);
|
|
1966
|
+
holeLo = holeHi = -1;
|
|
1967
|
+
return r.ok ? undefined : r.cause;
|
|
1968
|
+
};
|
|
1969
|
+
}
|
|
1970
|
+
//# sourceMappingURL=plain-holes.js.map
|