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
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/15224-the-json-offset-scanner-compares-by-char → main 2 commits fork
2 files
changed +61
−30
src/http/json_body.ts +54 −30
| @@ -8,8 +8,6 @@ | ||
| 8 | 8 | * is still `{}` (the route's own validation says what it lacked). |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | const NUMBER = /-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/y; | |
| 12 | ||
| 13 | 11 | export class InvalidJson extends Error { |
| 14 | 12 | constructor(readonly offset: number, readonly token: string) { |
| 15 | 13 | super(`not JSON at byte ${offset}`); |
| @@ -30,62 +28,88 @@ export function firstBadOffset(text: string): number { | ||
| 30 | 28 | const literal = (word: string) => { |
| 31 | 29 | if (text.startsWith(word, i)) { i += word.length; return true; } |
| 32 | 30 | 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++; | |
| 34 | 32 | i += k; |
| 35 | 33 | return false; |
| 36 | 34 | }; |
| 35 | const hex = (c: number) => (c >= 48 && c <= 57) || (c >= 65 && c <= 70) || (c >= 97 && c <= 102); | |
| 37 | 36 | const string = (): boolean => { |
| 38 | 37 | i++; |
| 39 | 38 | 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; | |
| 47 | 46 | 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; | |
| 49 | 48 | else { i++; return false; } |
| 50 | 49 | } else i++; |
| 51 | 50 | } |
| 52 | 51 | return false; |
| 53 | 52 | }; |
| 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. | |
| 54 | 64 | 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; | |
| 60 | 84 | return true; |
| 61 | 85 | }; |
| 62 | 86 | const value = (depth: number): boolean => { |
| 63 | 87 | 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; | |
| 69 | 93 | i++; ws(); |
| 70 | if (text[i] === close) { i++; return true; } | |
| 94 | if (text.charCodeAt(i) === close) { i++; return true; } | |
| 71 | 95 | for (;;) { |
| 72 | 96 | ws(); |
| 73 | if (c === "{") { | |
| 74 | if (text[i] !== '"' || !string()) return false; | |
| 97 | if (c === 123) { | |
| 98 | if (text.charCodeAt(i) !== 34 || !string()) return false; | |
| 75 | 99 | ws(); |
| 76 | if (text[i] !== ":") return false; | |
| 100 | if (text.charCodeAt(i) !== 58) return false; | |
| 77 | 101 | i++; |
| 78 | 102 | } |
| 79 | 103 | if (!value(depth + 1)) return false; |
| 80 | 104 | 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; } | |
| 83 | 107 | return false; |
| 84 | 108 | } |
| 85 | 109 | } |
| 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"); | |
| 89 | 113 | return number(); |
| 90 | 114 | }; |
| 91 | 115 | 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", () => { | ||
| 91 | 91 | expect(firstBadOffset("[".repeat(500) + "]".repeat(500))).toBe(-1); |
| 92 | 92 | expect(firstBadOffset("[".repeat(600) + "]".repeat(600))).toBeGreaterThan(500); |
| 93 | 93 | }); |
| 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 | }); |
new above
offline