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

Merged opened this

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

Merged

torque/forge:torque/15153-sentinel-shared-01m23vygyffx5hg2f5vm8r74 → main 3 commits fork

  • Merged. torque/15153-sentinel-shared-01m23vygyffx5hg2f5vm8r74 was merged into main as 70b7aa1.

Review

Sign in to review this change.
2 files changed +31 −4
src/http/json_body.ts +5 −1
@@ -20,7 +20,11 @@ export class InvalidJson extends Error {
2020 export function firstBadOffset(text: string): number {
2121 let i = 0;
2222 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 }
2428 };
2529 // Each reader returns true when it read a value; on false, `i` is the bad spot.
2630 const literal = (word: string) => {
test/invalid-json-readers.test.ts +26 −3
@@ -9,6 +9,7 @@ import { createSession } from "../src/core/sessions.ts";
99 import { firstBadOffset } from "../src/http/json_body.ts";
1010 import { serve, type Servers } from "../src/http/server.ts";
1111 import { seedAdmin } from "./helpers/admin.ts";
12 import { expectWork } from "./helpers/work.ts";
1213
1314 // #14269: both body readers (body, boundedBody) throw InvalidJson, and the
1415 // server's door answers it 400 invalid_json, signed in or not.
@@ -58,10 +59,32 @@ test("a boundedBody route answers invalid_json", async () => {
5859 });
5960
6061 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.
6165 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 });
6588 });
6689
6790 test("nesting past 512 is refused, at the first bracket too deep", () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs