Public: anyone can read this; the crew can change it.
Sentinel: shared:01m23vygyffx5hg2f5vm8r74sr:uq7lwkbwgh is past its budget: trevato/forge: "a large malformed numeric array is scanned in linear time" failed on 3 pulls in the last hour — the lan… #15171
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/15153-sentinel-shared-01m23vygyffx5hg2f5vm8r74 → main 3 commits fork
2 files
changed +31
−4
src/http/json_body.ts +5 −1
| @@ -20,7 +20,11 @@ export class InvalidJson extends Error { | ||
| 20 | 20 | export function firstBadOffset(text: string): number { |
| 21 | 21 | let i = 0; |
| 22 | 22 | const ws = () => { |
| 23 | while (i < text.length && " \t\n\r".includes(text[i] as string)) i++; | |
| 23 | for (;;) { | |
| 24 | const c = text.charCodeAt(i); | |
| 25 | if (c === 32 || c === 10 || c === 13 || c === 9) i++; | |
| 26 | else return; | |
| 27 | } | |
| 24 | 28 | }; |
| 25 | 29 | // Each reader returns true when it read a value; on false, `i` is the bad spot. |
| 26 | 30 | const literal = (word: string) => { |
test/invalid-json-readers.test.ts +26 −3
| @@ -9,6 +9,7 @@ import { createSession } from "../src/core/sessions.ts"; | ||
| 9 | 9 | import { firstBadOffset } from "../src/http/json_body.ts"; |
| 10 | 10 | import { serve, type Servers } from "../src/http/server.ts"; |
| 11 | 11 | import { seedAdmin } from "./helpers/admin.ts"; |
| 12 | import { expectWork } from "./helpers/work.ts"; | |
| 12 | 13 | |
| 13 | 14 | // #14269: both body readers (body, boundedBody) throw InvalidJson, and the |
| 14 | 15 | // server's door answers it 400 invalid_json, signed in or not. |
| @@ -58,10 +59,32 @@ test("a boundedBody route answers invalid_json", async () => { | ||
| 58 | 59 | }); |
| 59 | 60 | |
| 60 | 61 | test("a large malformed numeric array is scanned in linear time", () => { |
| 62 | // Counted work, not a clock (#7824): the quadratic scan copied the rest of | |
| 63 | // the body at every number (`text.slice(i)`), so the bytes copied grew as | |
| 64 | // n². Linear means the scan copies at most a few bytes per escape. | |
| 61 | 65 | const text = `[${"1,".repeat(400_000)}x]`; |
| 62 | const t = performance.now(); | |
| 63 | expect(firstBadOffset(text)).toBe(800_001); | |
| 64 | expect(performance.now() - t).toBeLessThan(250); | |
| 66 | const slice = String.prototype.slice; | |
| 67 | const substring = String.prototype.substring; | |
| 68 | let copied = 0; | |
| 69 | String.prototype.slice = function (this: string, ...a: [number?, number?]) { | |
| 70 | const out = slice.apply(this, a); | |
| 71 | copied += out.length; | |
| 72 | return out; | |
| 73 | }; | |
| 74 | String.prototype.substring = function (this: string, ...a: [number, number?]) { | |
| 75 | const out = substring.apply(this, a); | |
| 76 | copied += out.length; | |
| 77 | return out; | |
| 78 | }; | |
| 79 | let at: number; | |
| 80 | try { | |
| 81 | at = firstBadOffset(text); | |
| 82 | } finally { | |
| 83 | String.prototype.slice = slice; | |
| 84 | String.prototype.substring = substring; | |
| 85 | } | |
| 86 | expect(at).toBe(800_001); | |
| 87 | expectWork(copied, { max: 0 }); | |
| 65 | 88 | }); |
| 66 | 89 | |
| 67 | 90 | test("nesting past 512 is refused, at the first bracket too deep", () => { |
new above
offline