mirror of
https://github.com/openai/codex-security.git
synced 2026-11-23 01:54:36 +08:00
Exercise both ownership outcomes in the loopback example and verify the target invoice exists.
72 lines
2.4 KiB
TypeScript
72 lines
2.4 KiB
TypeScript
// Deliberately vulnerable local fixture. Do not deploy this application.
|
|
import { once } from "node:events";
|
|
import { realpathSync } from "node:fs";
|
|
import { createServer as createHttpServer, type Server } from "node:http";
|
|
import type { AddressInfo } from "node:net";
|
|
|
|
// These identities, tokens, and records are synthetic demo data.
|
|
const tokens = new Map([
|
|
["demo-alice", "alice"],
|
|
["demo-bob", "bob"],
|
|
]);
|
|
type Invoice = { id: string; owner: string; amount: number };
|
|
const invoices = new Map<string, Invoice>([
|
|
["1001", { id: "1001", owner: "alice", amount: 25 }],
|
|
["1002", { id: "1002", owner: "bob", amount: 80 }],
|
|
]);
|
|
|
|
export async function createServer({
|
|
enforceOwnership = false,
|
|
}: { enforceOwnership?: boolean } = {}): Promise<Server> {
|
|
const server = createHttpServer((request, response) => {
|
|
function reply(status: number, body: Invoice | { error: string }): void {
|
|
const encoded = JSON.stringify(body);
|
|
response.writeHead(status, {
|
|
"Content-Type": "application/json",
|
|
"Content-Length": Buffer.byteLength(encoded),
|
|
});
|
|
response.end(encoded);
|
|
}
|
|
|
|
if (request.method !== "GET") {
|
|
response.writeHead(501).end();
|
|
return;
|
|
}
|
|
const authorization = request.headers.authorization ?? "";
|
|
const token = authorization.startsWith("Bearer ")
|
|
? authorization.slice("Bearer ".length)
|
|
: authorization;
|
|
const user = tokens.get(token);
|
|
if (user === undefined) return reply(401, { error: "unauthorized" });
|
|
const path = request.url ?? "";
|
|
if (!path.startsWith("/invoices/"))
|
|
return reply(404, { error: "not found" });
|
|
const invoice = invoices.get(path.slice("/invoices/".length));
|
|
if (invoice === undefined) return reply(404, { error: "not found" });
|
|
// The default demo path is vulnerable; the repaired control is an explicit test mode.
|
|
if (enforceOwnership && invoice.owner !== user)
|
|
return reply(403, { error: "forbidden" });
|
|
reply(200, invoice);
|
|
});
|
|
server.listen(0, "127.0.0.1");
|
|
await once(server, "listening");
|
|
return server;
|
|
}
|
|
|
|
// Keep the copied demo fixture independent of repository helpers.
|
|
function isMain(): boolean {
|
|
try {
|
|
return (
|
|
process.argv[1] !== undefined &&
|
|
realpathSync(process.argv[1]) === realpathSync(new URL(import.meta.url))
|
|
);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (isMain()) {
|
|
const server = await createServer();
|
|
console.log(`http://127.0.0.1:${(server.address() as AddressInfo).port}`);
|
|
}
|