Files
Neha Rungta 227bd55457 docs(examples): compare vulnerable and repaired invoice ownership checks (#2612)
Exercise both ownership outcomes in the loopback example and verify the target invoice exists.
2026-11-17 00:30:22 -08:00

100 lines
3.2 KiB
TypeScript

// Exercise the fixture over real HTTP and save the observed evidence.
import assert from "node:assert/strict";
import { mkdir, writeFile } from "node:fs/promises";
import type { AddressInfo } from "node:net";
import { dirname } from "node:path";
import { parseArgs, promisify } from "node:util";
import { createServer } from "./app.mjs";
type HttpResult = {
status: number;
body: { id?: string; owner?: string; amount?: number; error?: string };
};
async function main(): Promise<number> {
let output: string;
let mode: "vulnerable" | "repaired";
try {
const { values } = parseArgs({
options: {
output: { type: "string" },
mode: { type: "string", default: "vulnerable" },
help: { type: "boolean", short: "h" },
},
});
if (values.help) {
console.log(
"Usage: node validate.mjs --output PATH [--mode vulnerable|repaired]",
);
return 0;
}
if (values.output === undefined) throw new Error("--output is required");
if (values.mode !== "vulnerable" && values.mode !== "repaired")
throw new Error("--mode must be vulnerable or repaired");
output = values.output;
mode = values.mode;
} catch (error) {
console.error((error as Error).message);
return 2;
}
const server = await createServer({ enforceOwnership: mode === "repaired" });
const baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
async function get(invoice: string, token?: string): Promise<HttpResult> {
const response = await fetch(`${baseUrl}/invoices/${invoice}`, {
headers: token === undefined ? {} : { Authorization: `Bearer ${token}` },
signal: AbortSignal.timeout(5_000),
});
return {
status: response.status,
body: (await response.json()) as HttpResult["body"],
};
}
let evidence: {
anonymous: HttpResult;
own_invoice: HttpResult;
target_owner_invoice: HttpResult;
other_invoice: HttpResult;
cross_account_read: boolean;
};
try {
const anonymous = await get("1002");
const own_invoice = await get("1001", "demo-alice");
const target_owner_invoice = await get("1002", "demo-bob");
const other_invoice = await get("1002", "demo-alice");
assert.equal(anonymous.status, 401, "Authentication control failed");
for (const [result, id, owner] of [
[own_invoice, "1001", "alice"],
[target_owner_invoice, "1002", "bob"],
] as const) {
assert.ok(
result.status === 200 &&
result.body.id === id &&
result.body.owner === owner,
`Invoice ${id} must be accessible to its owner`,
);
}
evidence = {
anonymous,
own_invoice,
target_owner_invoice,
other_invoice,
cross_account_read:
other_invoice.status === 200 &&
other_invoice.body.id === target_owner_invoice.body.id &&
other_invoice.body.owner === target_owner_invoice.body.owner,
};
} finally {
await promisify(server.close).call(server);
}
const proof = { mode, ...evidence, server_stopped: true };
await mkdir(dirname(output), { recursive: true });
await writeFile(output, `${JSON.stringify(proof, null, 2)}\n`, "utf8");
console.log(JSON.stringify(proof));
return 0;
}
process.exitCode = await main();