Repository navigation
fix(api): charge wallet quotas only for fresh signed writes #153
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
Vasanthdev2004
wants to merge
1
commit into
main
Choose a base branch
from
codex/request-hardening
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // The integration tests use real signatures, replacing only the external RPC. | ||
| import type { Abi, Address, Hex } from "viem"; | ||
| import type { ChainKey } from "../src/lib/chainPublic.ts"; | ||
|
|
||
| type Client = { | ||
| readContract: (args: { address: Address; abi: Abi; functionName: string; args: readonly unknown[] }) => Promise<bigint>; | ||
| verifyMessage: (args: { address: Address; message: string; signature: Hex }) => Promise<boolean>; | ||
| getChainId: () => Promise<number>; | ||
| }; | ||
| let client: Client | null = null; | ||
| export function setPostsTestClient(value: Client) { client = value; } | ||
| export function publicClient(_chain: ChainKey) { | ||
| if (!client) throw new Error("posts integration-test client not configured"); | ||
| return client; | ||
| } |
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,8 @@ | ||
| // Explicit integration-test adapter. Never imported by the application build. | ||
| import type { Db } from "../src/lib/db.ts"; | ||
|
|
||
| let connection: Db | null = null; | ||
|
|
||
| export function setPostsTestDb(value: Db | null) { connection = value; } | ||
| export function maybeDb(): Db | null { return connection; } | ||
| export type { Db }; |
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,76 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { randomBytes } from "node:crypto"; | ||
| import test from "node:test"; | ||
| import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; | ||
| import { getMock, resetMock } from "../../../../lib/db-mock.ts"; | ||
| import { buildAdminSignInMessage } from "../../../../lib/admin/signIn.ts"; | ||
| import { POST } from "./route.ts"; | ||
|
|
||
| let sequence = 0; | ||
| function fixture() { | ||
| resetMock(); | ||
| const account = privateKeyToAccount(generatePrivateKey()); | ||
| const nonces = new Set<string>(); | ||
| let sessions = 0; | ||
| let onRead: (() => Promise<void>) | undefined; | ||
| getMock().query = async (sql, values) => { | ||
| const nonce = String(values[0]); | ||
| if (sql.includes("SELECT 1 FROM bb_admin_nonces")) { | ||
| const open = nonces.has(nonce); | ||
| await onRead?.(); | ||
| return open ? [{}] : []; | ||
| } | ||
| if (sql.includes("UPDATE bb_admin_nonces SET used_at")) { | ||
| if (!nonces.delete(nonce)) return []; | ||
| return [{ nonce }]; | ||
| } | ||
| if (sql.includes("INSERT INTO bb_admin_sessions")) sessions++; | ||
| return []; | ||
| }; | ||
| async function signed() { | ||
| const nonce = randomBytes(16).toString("hex"); | ||
| nonces.add(nonce); | ||
| const message = buildAdminSignInMessage({ origin: "https://openlaunch.lol", address: account.address, nonce, issuedAt: new Date() }); | ||
| return { message, signature: await account.signMessage({ message }), nonce }; | ||
| } | ||
| function submit(body: unknown) { | ||
| return POST(new Request("http://localhost/api/admin/session", { | ||
| method: "POST", headers: { "content-type": "application/json", origin: "https://openlaunch.lol", "x-forwarded-for": `admin-quota-${++sequence}` }, body: JSON.stringify(body), | ||
| })); | ||
| } | ||
| function barrier(count: number) { | ||
| let arrived = 0; | ||
| let release!: () => void; | ||
| const ready = new Promise<void>((resolve) => { release = resolve; }); | ||
| onRead = async () => { if (++arrived === count) { onRead = undefined; release(); } await ready; }; | ||
| } | ||
| return { account, nonces, sessions: () => sessions, signed, submit, barrier }; | ||
| } | ||
|
|
||
| test("concurrent admin sign-in replays cannot consume the wallet's sign-in allowance", { timeout: 10_000 }, async (t) => { | ||
| const f = fixture(); | ||
| const previous = process.env.ADMIN_WALLETS; | ||
| process.env.ADMIN_WALLETS = f.account.address; | ||
| t.after(() => { if (previous === undefined) delete process.env.ADMIN_WALLETS; else process.env.ADMIN_WALLETS = previous; }); | ||
| const original = await f.signed(); | ||
| f.barrier(12); // Every precheck sees the nonce open before any contender consumes it. | ||
| const replies = await Promise.all(Array.from({ length: 12 }, () => f.submit(original))); | ||
| assert.equal(replies.filter((r) => r.status === 200).length, 1); | ||
| assert.equal(replies.filter((r) => r.status === 401).length, 11); | ||
| assert.equal(f.sessions(), 1); | ||
| assert.equal((await f.submit(await f.signed())).status, 200, "fresh login retains its allowance"); | ||
| assert.equal(f.sessions(), 2); | ||
| }); | ||
|
|
||
| test("fresh signed admin logins retain the cap and over-quota nonces cannot be replayed", async (t) => { | ||
| const f = fixture(); | ||
| const previous = process.env.ADMIN_WALLETS; | ||
| process.env.ADMIN_WALLETS = f.account.address; | ||
| t.after(() => { if (previous === undefined) delete process.env.ADMIN_WALLETS; else process.env.ADMIN_WALLETS = previous; }); | ||
| for (let i = 0; i < 10; i++) assert.equal((await f.submit(await f.signed())).status, 200); | ||
| const excess = await f.signed(); | ||
| assert.equal((await f.submit(excess)).status, 429); | ||
| assert.equal(f.nonces.has(excess.nonce), false); | ||
| assert.equal((await f.submit(excess)).status, 401); | ||
| assert.equal(f.sessions(), 10); | ||
| }); |
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,173 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
| import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3"; | ||
| import sharp from "sharp"; | ||
| import { BANNER_MAX_BYTES, IMAGE_MAX_BYTES, IMAGE_UPLOADS_PER_HOUR } from "@/lib/launchpad/images"; | ||
| import { POST } from "./route.ts"; | ||
|
|
||
| const wallet = (n: number) => `0x${n.toString(16).padStart(40, "0")}`; | ||
| const garbage = new Uint8Array(32); | ||
| const ipLimit = IMAGE_UPLOADS_PER_HOUR * 2; | ||
|
|
||
| function request(ip: string, owner: string, bytes: Uint8Array, kind?: string, headers?: HeadersInit): Request { | ||
| const form = new FormData(); | ||
| form.set("wallet", owner); | ||
| // Intentionally misleading extension/MIME: the route must validate the actual bytes. | ||
| form.set("file", new Blob([new Uint8Array(bytes)], { type: "image/png" }), "upload.png"); | ||
| if (kind) form.set("kind", kind); | ||
| return new Request("http://localhost/api/launch/image", { | ||
| method: "POST", headers: { "x-forwarded-for": ip, ...headers }, body: form, | ||
| }); | ||
| } | ||
|
|
||
| test("image upload route validates images and limits anonymous callers by trusted IP", async (t) => { | ||
| const env = { | ||
| BUCKET_NAME: "image-route-test", | ||
| AWS_ENDPOINT_URL_S3: "https://image-store.invalid", | ||
| AWS_ACCESS_KEY_ID: "test-key", | ||
| AWS_SECRET_ACCESS_KEY: "test-secret", | ||
| IMAGE_PUBLIC_BASE: "https://images.example/api/launch/image", | ||
| }; | ||
| const previous = Object.fromEntries(Object.keys(env).map((key) => [key, process.env[key]])); | ||
| Object.assign(process.env, env); | ||
| t.after(() => { | ||
| for (const [key, value] of Object.entries(previous)) { | ||
| if (value === undefined) delete process.env[key]; | ||
| else process.env[key] = value; | ||
| } | ||
| }); | ||
| // Exercise the real multipart parser, limiter, decoder and storage command without doing I/O. | ||
| const stored: PutObjectCommand[] = []; | ||
| t.mock.method(S3Client.prototype, "send", async (command: PutObjectCommand) => { | ||
| assert.ok(command instanceof PutObjectCommand); | ||
| stored.push(command); | ||
| return {}; | ||
| }); | ||
| const png = await sharp({ create: { width: 600, height: 240, channels: 4, background: "#0052ff" } }) | ||
| .withMetadata({ exif: { IFD0: { Copyright: "private metadata" } } }).png().toBuffer(); | ||
|
|
||
| await t.test("invalid uploads naming another wallet cannot spend that wallet's allowance", async () => { | ||
| for (let i = 0; i < IMAGE_UPLOADS_PER_HOUR; i++) { | ||
| const denied = await POST(request("192.0.2.1", wallet(1), garbage)); | ||
| assert.equal(denied.status, 415); | ||
| } | ||
| const response = await POST(request("192.0.2.2", wallet(1), png)); | ||
| assert.equal(response.status, 200, "the named wallet can still upload from its own IP"); | ||
| assert.match((await response.json()).url, /\/t\/[0-9a-f]{48}\.webp$/); | ||
| }); | ||
|
|
||
| await t.test("valid uploads naming another wallet cannot spend that wallet's allowance", async () => { | ||
| for (let i = 0; i < IMAGE_UPLOADS_PER_HOUR; i++) { | ||
| const response = await POST(request("192.0.2.3", wallet(2), png)); | ||
| assert.equal(response.status, 200); | ||
| } | ||
| const response = await POST(request("192.0.2.4", wallet(2), png)); | ||
| assert.equal(response.status, 200, "valid bytes still do not prove ownership of the wallet field"); | ||
| }); | ||
|
|
||
| await t.test("rotating wallet fields cannot bypass the existing IP allowance", async () => { | ||
| const before = stored.length; | ||
| for (let i = 0; i < ipLimit; i++) { | ||
| const response = await POST(request("192.0.2.5", wallet(100 + i), i % 2 ? garbage : png)); | ||
| assert.equal(response.status, i % 2 ? 415 : 200); | ||
| } | ||
| const denied = await POST(request("192.0.2.5", wallet(999), png)); | ||
| assert.equal(denied.status, 429); | ||
| assert.deepEqual(await denied.json(), { error: "slow down" }); | ||
| assert.equal(stored.length - before, ipLimit / 2, "invalid and over-budget requests never reach storage"); | ||
| }); | ||
|
|
||
| await t.test("spoofing forwarded prefixes cannot buy new IP allowances", async () => { | ||
| for (let i = 0; i < ipLimit; i++) { | ||
| const response = await POST(request(`198.51.100.${i}, 192.0.2.6`, wallet(200 + i), garbage)); | ||
| assert.equal(response.status, 415); | ||
| } | ||
| const denied = await POST(request("198.51.100.254, 192.0.2.6", wallet(998), png)); | ||
| assert.equal(denied.status, 429); | ||
| assert.deepEqual(await denied.json(), { error: "slow down" }); | ||
| }); | ||
|
|
||
| await t.test("Fly's client IP takes precedence over forwarded values", async (t) => { | ||
| const previous = process.env.FLY_APP_NAME; | ||
| process.env.FLY_APP_NAME = "image-route-test"; | ||
| t.after(() => { if (previous === undefined) delete process.env.FLY_APP_NAME; else process.env.FLY_APP_NAME = previous; }); | ||
| for (let i = 0; i < ipLimit; i++) { | ||
| const response = await POST(request(`198.51.100.${i}`, wallet(300 + i), garbage, undefined, { "fly-client-ip": "192.0.2.7" })); | ||
| assert.equal(response.status, 415); | ||
| } | ||
| assert.equal((await POST(request("198.51.100.254", wallet(997), png, undefined, { "fly-client-ip": "192.0.2.7" }))).status, 429); | ||
| }); | ||
|
|
||
| await t.test("wallet shape, format, decoder and size failures retain their responses", async () => { | ||
| const before = stored.length; | ||
| const disguised = new Uint8Array(32); | ||
| disguised.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); | ||
| const tiny = await sharp({ create: { width: 8, height: 8, channels: 4, background: "#0052ff" } }).png().toBuffer(); | ||
| for (const [req, status, error] of [ | ||
| [request("192.0.2.8", "not-a-wallet", png), 400, "connect a wallet first"], | ||
| [request("192.0.2.8", wallet(3), garbage), 415, "PNG, JPEG, WebP or GIF only"], | ||
| [request("192.0.2.8", wallet(3), disguised), 415, "not a valid image"], | ||
| [request("192.0.2.8", wallet(3), tiny), 415, "image too small (min 16×16)"], | ||
| [request("192.0.2.8", wallet(3), new Uint8Array(IMAGE_MAX_BYTES + 1)), 413, "image must be ≤ 2 MB"], | ||
| [request("192.0.2.8", wallet(3), new Uint8Array(BANNER_MAX_BYTES + 1), "banner"), 413, "image must be ≤ 4 MB"], | ||
| [request("192.0.2.8", wallet(3), png, undefined, { "content-length": String(BANNER_MAX_BYTES + 16 * 1024 + 1) }), 413, "image must be ≤ 4 MB"], | ||
| ] as const) { | ||
| const response = await POST(req); | ||
| assert.equal(response.status, status); | ||
| assert.deepEqual(await response.json(), { error }); | ||
| } | ||
| assert.equal(stored.length, before, "rejected requests are never stored"); | ||
| }); | ||
|
|
||
| await t.test("logos and banners still store stripped WebP bytes with random keys", async () => { | ||
| const keys: string[] = []; | ||
| for (const [kind, width, height] of [[undefined, 512, 512], ["banner", 1500, 500]] as const) { | ||
| const response = await POST(request("192.0.2.9", wallet(4), png, kind)); | ||
| assert.equal(response.status, 200); | ||
| assert.equal(response.headers.get("cache-control"), "no-store"); | ||
| const body = await response.json(); | ||
| const command = stored.at(-1)!; | ||
| assert.match(command.input.Key!, /^t\/[0-9a-f]{48}\.webp$/); | ||
| keys.push(command.input.Key!); | ||
| assert.equal(body.url, `${env.IMAGE_PUBLIC_BASE}/${command.input.Key}`); | ||
| assert.equal(command.input.ContentType, "image/webp"); | ||
| const output = command.input.Body as Buffer; | ||
| assert.equal(body.bytes, output.length); | ||
| const metadata = await sharp(output).metadata(); | ||
| assert.equal(metadata.format, "webp"); | ||
| assert.equal(metadata.width, width); | ||
| assert.equal(metadata.height, height); | ||
| assert.equal(metadata.exif, undefined); | ||
| assert.equal(output.includes("private metadata"), false); | ||
| } | ||
| assert.notEqual(keys[0], keys[1]); | ||
| }); | ||
|
|
||
| for (const declared of [undefined, "10"]) { | ||
| await t.test(`stops reading oversized multipart streams with ${declared === undefined ? "no" : "an understated"} size header`, async () => { | ||
| const before = stored.length; | ||
| const chunk = new Uint8Array(64 * 1024); | ||
| const availableChunks = 80; | ||
| let chunksRead = 0; | ||
| let cancelled = false; | ||
| const body = new ReadableStream<Uint8Array>({ | ||
| pull(controller) { | ||
| if (chunksRead === availableChunks) controller.close(); | ||
| else { chunksRead++; controller.enqueue(chunk); } | ||
| }, | ||
| cancel() { cancelled = true; }, | ||
| }, { highWaterMark: 0 }); | ||
| const req = new Request("http://localhost/api/launch/image", { | ||
| method: "POST", body, duplex: "half", | ||
| headers: { "content-type": "multipart/form-data; boundary=test", "x-forwarded-for": "192.0.2.10", ...(declared === undefined ? {} : { "content-length": declared }) }, | ||
| } as RequestInit & { duplex: "half" }); | ||
| const response = await POST(req); | ||
| assert.equal(response.status, 413, `read ${chunksRead}/${availableChunks} chunks; cancelled=${cancelled}`); | ||
| assert.deepEqual(await response.json(), { error: "image must be ≤ 4 MB" }); | ||
| assert.equal(chunksRead, Math.floor((BANNER_MAX_BYTES + 16 * 1024) / chunk.length) + 1); | ||
| assert.ok(chunksRead < availableChunks, "the parser must not drain the rest of the request"); | ||
| assert.equal(cancelled, true); | ||
| assert.equal(stored.length, before); | ||
| }); | ||
| } | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pin
TRUSTED_PROXY_HOPSin the image-route test fixture.If the inherited environment sets
TRUSTED_PROXY_HOPSto 2 or higher,clientIpcannot read a single forwarded address and returns0.0.0.0. Every request in this test then shares one rate-limit bucket. The two subtests together exceed theIMAGE_UPLOADS_PER_HOUR * 2limit, and the second subtest receives 429 on its last upload instead of 200.Suggested fix
IMAGE_PUBLIC_BASE: "https://images.example/api/launch/image", + TRUSTED_PROXY_HOPS: "1", };The existing
t.aftercleanup restores the previous value ofTRUSTED_PROXY_HOPS.