-
Notifications
You must be signed in to change notification settings - Fork 753
refactor(examples): port custom validation to TypeScript #792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kmbroai
wants to merge
2
commits into
dev/kyleb/remove-python-ci-utilities
from
dev/kyleb/remove-python-validation-example
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Deliberately vulnerable local fixture. Do not deploy this application. | ||
| import { once } from "node:events"; | ||
| import { createServer as createHttpServer, type Server } from "node:http"; | ||
| import type { AddressInfo } from "node:net"; | ||
| import { pathToFileURL } from "node:url"; | ||
|
|
||
| // 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(): 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" }); | ||
| // BUG: authentication does not establish ownership of this invoice. | ||
| reply(200, invoice); | ||
| }); | ||
| server.listen(0, "127.0.0.1"); | ||
| await once(server, "listening"); | ||
| return server; | ||
| } | ||
|
|
||
| if ( | ||
| process.argv[1] && | ||
| import.meta.url === pathToFileURL(process.argv[1]).href | ||
| ) { | ||
| const server = await createServer(); | ||
| console.log(`http://127.0.0.1:${(server.address() as AddressInfo).port}`); | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| Review invoice ownership checks in `app.py`. An authenticated account must not | ||
| Review invoice ownership checks in `app.mts`. An authenticated account must not | ||
| read another account's invoice. The fixed tokens and records are synthetic test | ||
| data, not production credentials. `validate.py` is a test harness, not an | ||
| data, not production credentials. `validate.mts` is a test harness, not an | ||
| application endpoint. Keep discovery source-only. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // 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 } 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; | ||
| try { | ||
| const { values } = parseArgs({ | ||
| options: { | ||
| output: { type: "string" }, | ||
| help: { type: "boolean", short: "h" }, | ||
| }, | ||
| }); | ||
| if (values.help) { | ||
| console.log("Usage: node validate.mjs --output PATH"); | ||
| return 0; | ||
| } | ||
| if (values.output === undefined) throw new Error("--output is required"); | ||
| output = values.output; | ||
| } catch (error) { | ||
| console.error((error as Error).message); | ||
| return 2; | ||
| } | ||
|
|
||
| const server = await createServer(); | ||
| 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; | ||
| other_invoice: HttpResult; | ||
| cross_account_read: boolean; | ||
| }; | ||
| try { | ||
| const anonymous = await get("1002"); | ||
| const own_invoice = await get("1001", "demo-alice"); | ||
| const other_invoice = await get("1002", "demo-alice"); | ||
| assert.equal(anonymous.status, 401, "Authentication control failed"); | ||
| assert.equal(own_invoice.status, 200, "Own-account control failed"); | ||
| evidence = { | ||
| anonymous, | ||
| own_invoice, | ||
| other_invoice, | ||
| cross_account_read: | ||
| other_invoice.status === 200 && other_invoice.body.owner === "bob", | ||
| }; | ||
| } finally { | ||
| await new Promise<void>((resolve, reject) => { | ||
| server.close((error) => (error ? reject(error) : resolve())); | ||
| }); | ||
| } | ||
|
|
||
| const proof = { ...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(); |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.