Public: anyone can read this; the crew can change it.

The JSON offset scanner compares by char code, so a 400k-element malformed array stays inside its time budget #15227

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/15224-the-json-offset-scanner-compares-by-char → main 2 commits fork

  • Merged. torque/15224-the-json-offset-scanner-compares-by-char was merged into main as f2ce1b7.

Review

Sign in to review this change.
2 files changed +61 −30
src/http/json_body.ts +54 −30
@@ -8,8 +8,6 @@
88 * is still `{}` (the route's own validation says what it lacked).
99 */
1010
11 const NUMBER = /-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/y;
12
1311 export class InvalidJson extends Error {
1412 constructor(readonly offset: number, readonly token: string) {
1513 super(`not JSON at byte ${offset}`);
@@ -30,62 +28,88 @@ export function firstBadOffset(text: string): number {
3028 const literal = (word: string) => {
3129 if (text.startsWith(word, i)) { i += word.length; return true; }
3230 let k = 0;
33 while (i + k < text.length && text[i + k] === word[k]) k++;
31 while (i + k < text.length && text.charCodeAt(i + k) === word.charCodeAt(k)) k++;
3432 i += k;
3533 return false;
3634 };
35 const hex = (c: number) => (c >= 48 && c <= 57) || (c >= 65 && c <= 70) || (c >= 97 && c <= 102);
3736 const string = (): boolean => {
3837 i++;
3938 while (i < text.length) {
40 const c = text[i] as string;
41 if (c === '"') { i++; return true; }
42 if (c < " ") return false;
43 if (c === "\\") {
44 const e = text[i + 1];
45 if (e === "u") {
46 if (!/^[0-9a-fA-F]{4}$/.test(text.slice(i + 2, i + 6))) return false;
39 const c = text.charCodeAt(i);
40 if (c === 34) { i++; return true; }
41 if (c < 32) return false;
42 if (c === 92) {
43 const e = text.charCodeAt(i + 1); // NaN past the end, which matches nothing
44 if (e === 117) {
45 for (let k = 2; k < 6; k++) if (!hex(text.charCodeAt(i + k))) return false;
4746 i += 6;
48 } else if (e !== undefined && '"\\/bfnrt'.includes(e)) i += 2;
47 } else if (e === 34 || e === 92 || e === 47 || e === 98 || e === 102 || e === 110 || e === 114 || e === 116) i += 2;
4948 else { i++; return false; }
5049 } else i++;
5150 }
5251 return false;
5352 };
53 const digits = (from: number): number => {
54 let j = from;
55 while (j < text.length) {
56 const c = text.charCodeAt(j);
57 if (c < 48 || c > 57) break;
58 j++;
59 }
60 return j;
61 };
62 // -?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)? by hand: an incomplete fraction or exponent is left
63 // unread.
5464 const number = (): boolean => {
55 // Sticky, so the scan never copies the rest of the body (it ran quadratic on `[1,1,1,…`).
56 NUMBER.lastIndex = i;
57 const m = NUMBER.exec(text);
58 if (m === null) return false;
59 i += m[0].length;
65 let j = i;
66 if (text.charCodeAt(j) === 45) j++;
67 const first = text.charCodeAt(j);
68 if (first === 48) j++;
69 else if (first >= 49 && first <= 57) j = digits(j + 1);
70 else return false;
71 if (text.charCodeAt(j) === 46) {
72 const e = digits(j + 1);
73 if (e > j + 1) j = e;
74 }
75 const x = text.charCodeAt(j);
76 if (x === 101 || x === 69) {
77 let k = j + 1;
78 const sg = text.charCodeAt(k);
79 if (sg === 43 || sg === 45) k++;
80 const e = digits(k);
81 if (e > k) j = e;
82 }
83 i = j;
6084 return true;
6185 };
6286 const value = (depth: number): boolean => {
6387 ws();
64 const c = text[i];
65 if (c === undefined || depth > 512) return false;
66 if (c === '"') return string();
67 if (c === "{" || c === "[") {
68 const close = c === "{" ? "}" : "]";
88 const c = text.charCodeAt(i);
89 if (Number.isNaN(c) || depth > 512) return false;
90 if (c === 34) return string();
91 if (c === 123 || c === 91) {
92 const close = c === 123 ? 125 : 93;
6993 i++; ws();
70 if (text[i] === close) { i++; return true; }
94 if (text.charCodeAt(i) === close) { i++; return true; }
7195 for (;;) {
7296 ws();
73 if (c === "{") {
74 if (text[i] !== '"' || !string()) return false;
97 if (c === 123) {
98 if (text.charCodeAt(i) !== 34 || !string()) return false;
7599 ws();
76 if (text[i] !== ":") return false;
100 if (text.charCodeAt(i) !== 58) return false;
77101 i++;
78102 }
79103 if (!value(depth + 1)) return false;
80104 ws();
81 if (text[i] === ",") { i++; continue; }
82 if (text[i] === close) { i++; return true; }
105 if (text.charCodeAt(i) === 44) { i++; continue; }
106 if (text.charCodeAt(i) === close) { i++; return true; }
83107 return false;
84108 }
85109 }
86 if (c === "t") return literal("true");
87 if (c === "f") return literal("false");
88 if (c === "n") return literal("null");
110 if (c === 116) return literal("true");
111 if (c === 102) return literal("false");
112 if (c === 110) return literal("null");
89113 return number();
90114 };
91115 if (!value(0)) return i;
test/invalid-json-readers.test.ts +7
@@ -91,3 +91,10 @@ test("nesting past 512 is refused, at the first bracket too deep", () => {
9191 expect(firstBadOffset("[".repeat(500) + "]".repeat(500))).toBe(-1);
9292 expect(firstBadOffset("[".repeat(600) + "]".repeat(600))).toBeGreaterThan(500);
9393 });
94
95 test("numbers scan as the grammar says: an incomplete fraction or exponent stops before it", () => {
96 for (const [text, at] of [
97 ["[1.]", 2], ["[1e]", 2], ["[1e+]", 2], ["[-]", 1], ["[01]", 2], ["[-0.5e-3]", -1],
98 ["[1E+10,2.50]", -1], ["[.5]", 1], ["[1.5.2]", 4],
99 ] as const) expect([text, firstBadOffset(text)]).toEqual([text, at]);
100 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs