140 lines
4.3 KiB
TypeScript
140 lines
4.3 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import type { InstalledHttpContract } from "../../../src/contracts/external-contract-runtime.ts";
|
|
import {
|
|
checkJsonStructure,
|
|
JsonBudgetExceededError,
|
|
parseBoundedJson,
|
|
validateJsonStructure,
|
|
} from "../../../src/platform/validation/bounded-json.ts";
|
|
import { TEST_LIST_HTTP_CONTRACT } from "../../helpers/external-contract-fixture.ts";
|
|
import {
|
|
scopeSnapshot,
|
|
testExecutor,
|
|
type Transport,
|
|
} from "../../helpers/traffic-test-kit.ts";
|
|
|
|
const BUDGET = {
|
|
maxBytes: 100,
|
|
maxItems: 10,
|
|
maxDepth: 10,
|
|
maxStringLength: 100,
|
|
};
|
|
|
|
describe("parseBoundedJson", () => {
|
|
it("rejects oversized input before JSON.parse is invoked", () => {
|
|
const parse = vi.spyOn(JSON, "parse");
|
|
const payload = "x".repeat(101);
|
|
|
|
expect(() =>
|
|
parseBoundedJson(payload, {
|
|
maxBytes: 100,
|
|
maxItems: 10,
|
|
maxDepth: 10,
|
|
maxStringLength: 100,
|
|
}),
|
|
).toThrow(/too-large/i);
|
|
|
|
expect(parse).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("counts multi-byte characters against the byte ceiling", () => {
|
|
const payload = `"${"한".repeat(40)}"`; // 42 UTF-16 units, 122 UTF-8 bytes
|
|
expect(() => parseBoundedJson(payload, BUDGET)).toThrow(/too-large/i);
|
|
});
|
|
|
|
it("parses a payload inside every limit", () => {
|
|
expect(parseBoundedJson('{"a":[1,2,3]}', BUDGET)).toEqual({ a: [1, 2, 3] });
|
|
});
|
|
|
|
it("reports malformed JSON as a budget error, not a raw SyntaxError", () => {
|
|
expect(() => parseBoundedJson("{", BUDGET)).toThrow(JsonBudgetExceededError);
|
|
});
|
|
});
|
|
|
|
describe("validateJsonStructure", () => {
|
|
it("rejects arrays above maxItems", () => {
|
|
expect(() => validateJsonStructure([1, 2, 3], { ...BUDGET, maxItems: 2 }))
|
|
.toThrow(/items/i);
|
|
});
|
|
|
|
it("rejects nesting above maxDepth", () => {
|
|
let nested: unknown = 1;
|
|
for (let depth = 0; depth < 5; depth += 1) nested = [nested];
|
|
expect(() =>
|
|
validateJsonStructure(nested, { ...BUDGET, maxDepth: 3 }),
|
|
).toThrow(/deep/i);
|
|
});
|
|
|
|
it("rejects strings above maxStringLength", () => {
|
|
expect(() =>
|
|
validateJsonStructure({ name: "x".repeat(11) }, { ...BUDGET, maxStringLength: 10 }),
|
|
).toThrow(/string/i);
|
|
});
|
|
|
|
it("rejects objects wider than maxItems and counts total nodes", () => {
|
|
const wide = Object.fromEntries(
|
|
Array.from({ length: 11 }, (_, index) => [`k${index}`, index]),
|
|
);
|
|
expect(checkJsonStructure(wide, BUDGET)).toMatchObject({ ok: false });
|
|
expect(checkJsonStructure([[1, 2], [3, 4]], { ...BUDGET, maxItems: 6 })).toMatchObject({
|
|
ok: false,
|
|
violation: "too-many-items",
|
|
});
|
|
});
|
|
|
|
it("walks adversarial nesting iteratively without overflowing the stack", () => {
|
|
let nested: unknown = 0;
|
|
for (let depth = 0; depth < 200_000; depth += 1) nested = [nested];
|
|
expect(() =>
|
|
validateJsonStructure(nested, {
|
|
maxBytes: 1_000_000,
|
|
maxItems: 1_000_000,
|
|
maxDepth: 1_000_000,
|
|
maxStringLength: 10,
|
|
}),
|
|
).not.toThrow();
|
|
expect(checkJsonStructure(nested, { ...BUDGET, maxItems: 1_000_000 })).toMatchObject({
|
|
ok: false,
|
|
violation: "too-deep",
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("response structure budget in the V3 executor", () => {
|
|
const TEST_QUERY = TEST_LIST_HTTP_CONTRACT as InstalledHttpContract<
|
|
unknown,
|
|
unknown,
|
|
unknown
|
|
>;
|
|
|
|
it("rejects a deeply nested response before schema validation runs", async () => {
|
|
let nested = "0";
|
|
for (let depth = 0; depth < 100; depth += 1) nested = `[${nested}]`;
|
|
const safeParse = vi.fn(() => ({ success: true as const, data: [] }));
|
|
const operation = {
|
|
...TEST_QUERY,
|
|
contract: {
|
|
...TEST_QUERY.contract,
|
|
outputValidator: { schemaId: "SpyPayload", safeParse },
|
|
},
|
|
} as InstalledHttpContract<unknown, unknown, unknown>;
|
|
const transport = vi.fn<Transport>(
|
|
async () =>
|
|
new Response(nested, { headers: { "content-type": "application/json" } }),
|
|
);
|
|
|
|
const outcome = await testExecutor(transport).execute(operation, { limit: 1 }, {
|
|
routeId: "TEST_ROUTE",
|
|
scope: scopeSnapshot(),
|
|
});
|
|
|
|
expect(outcome).toMatchObject({
|
|
kind: "CONTRACT_VIOLATION",
|
|
violation: { kind: "RESPONSE_TOO_LARGE" },
|
|
});
|
|
expect(safeParse).not.toHaveBeenCalled();
|
|
expect(transport).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|