diff --git a/helpful_queries.sql b/helpful_queries.sql index 759d457..26c2147 100644 --- a/helpful_queries.sql +++ b/helpful_queries.sql @@ -18,27 +18,7 @@ WHERE score_percentage = 0; DELETE FROM submissions WHERE score_percentage = 0; --- Find all submissions from a particular IP address --- This query joins submissions with raw_post_logs to find submissions by IP --- Replace 'YOUR_IP_ADDRESS' with the actual IP address you're looking for -SELECT - s.id, - s.model, - s.provider, - s.score_percentage, - s.timestamp, - s.created_at, - s.benchmark_version, - rpl.ip, - rpl.created_at as request_time -FROM submissions s -JOIN raw_post_logs rpl - ON rpl.path = '/api/submit' - AND datetime(rpl.created_at) BETWEEN datetime(s.created_at, '-5 seconds') AND datetime(s.created_at, '+5 seconds') -WHERE rpl.ip = 'YOUR_IP_ADDRESS' -ORDER BY s.created_at DESC; - --- Alternative: Find IP addresses associated with a token's registrations +-- Find IP addresses associated with a token's registrations -- This shows which IPs were used to register tokens SELECT s.id as submission_id, @@ -52,5 +32,5 @@ FROM submissions s JOIN tokens t ON s.token_id = t.id JOIN token_registration_limits trl ON datetime(trl.created_at) BETWEEN datetime(t.created_at, '-1 hour') AND datetime(t.created_at, '+1 hour') -WHERE trl.ip = '66.42.81.28' -ORDER BY s.created_at DESC; \ No newline at end of file +WHERE trl.ip = 'YOUR_IP_ADDRESS' +ORDER BY s.created_at DESC; diff --git a/migrations/20261110_remove_raw_post_logs.sql b/migrations/20261110_remove_raw_post_logs.sql new file mode 100644 index 0000000..3259fb2 --- /dev/null +++ b/migrations/20261110_remove_raw_post_logs.sql @@ -0,0 +1,3 @@ +-- Apply after deploying the Worker without raw POST request logging. +-- Preserve separate admin action audit entries (method = 'ADMIN'). +DELETE FROM raw_post_logs WHERE method = 'POST'; diff --git a/schema.sql b/schema.sql index 85618c6..0a0406f 100644 --- a/schema.sql +++ b/schema.sql @@ -67,7 +67,7 @@ CREATE INDEX IF NOT EXISTS idx_token_registration_limits_ip ON token_registratio CREATE INDEX IF NOT EXISTS idx_token_registration_limits_created_at ON token_registration_limits(created_at); CREATE INDEX IF NOT EXISTS idx_benchmark_versions_semver ON benchmark_versions(semver); --- Raw POST request logging for debugging +-- Admin action audit logs (legacy raw POST entries are removed by migration) CREATE TABLE IF NOT EXISTS raw_post_logs ( id INTEGER PRIMARY KEY AUTOINCREMENT, method TEXT NOT NULL, diff --git a/src/index.test.ts b/src/index.test.ts new file mode 100644 index 0000000..ef53886 --- /dev/null +++ b/src/index.test.ts @@ -0,0 +1,43 @@ +import { describe, expect, test, vi } from "vitest"; +import worker from "./index"; +import type { Bindings } from "./types"; + +describe("API request logging", () => { + test("does not persist credentials or request bodies from submissions", async () => { + const queries: string[] = []; + const boundValues: unknown[] = []; + const db = { + prepare(sql: string) { + queries.push(sql); + return { + bind(...values: unknown[]) { + boundValues.push(...values); + return { first: async () => null }; + }, + }; + }, + } as unknown as Bindings["prod_pinchbench"]; + const waitUntil = vi.fn(); + const token = "pb_live_test_credential"; + const officialKey = "test_official_credential"; + const response = await worker.fetch( + new Request("https://api.pinchbench.com/api/results", { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-PinchBench-Token": token, + "X-PinchBench-Official-Key": officialKey, + }, + body: JSON.stringify({ model: "test", password: "test_body_credential" }), + }), + { prod_pinchbench: db } as Bindings, + { waitUntil } as unknown as ExecutionContext, + ); + + expect(response.status).toBe(401); + expect(queries).toEqual(["SELECT id FROM tokens WHERE token_hash = ? LIMIT 1"]); + expect(boundValues).not.toContain(token); + expect(boundValues).not.toContain(officialKey); + expect(waitUntil).not.toHaveBeenCalled(); + }); +}); diff --git a/src/index.ts b/src/index.ts index 19e6f3a..4fed416 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,52 +23,6 @@ const app = new Hono<{ Bindings: Bindings }>(); // Mount admin routes (protected by Cloudflare Access) app.route("/admin", admin); -// Log every POST request body for debugging -app.use("/api/*", async (c, next) => { - if (c.req.method !== "POST") { - return next(); - } - - // Clone the request so the body can still be consumed by route handlers - const cloned = c.req.raw.clone(); - let body: string | null = null; - try { - body = await cloned.text(); - } catch { - body = null; - } - - const ip = - c.req.header("CF-Connecting-IP") || - c.req.header("X-Forwarded-For")?.split(",")[0]?.trim() || - "unknown"; - - const headers: Record = {}; - c.req.raw.headers.forEach((value, key) => { - headers[key] = value; - }); - - // Fire-and-forget: don't block the response on the log write - c.executionCtx.waitUntil( - c.env.prod_pinchbench - .prepare( - `INSERT INTO raw_post_logs (method, path, headers, body, ip, created_at) - VALUES (?, ?, ?, ?, ?, datetime('now'))`, - ) - .bind( - c.req.method, - new URL(c.req.url).pathname, - JSON.stringify(headers), - body, - ip, - ) - .run() - .catch(() => {}), // Silently ignore logging failures - ); - - return next(); -}); - // Enable CORS for frontend access app.use( "/api/*", @@ -251,7 +205,7 @@ export default { env: { prod_pinchbench: import("@cloudflare/workers-types").D1Database }, ctx: ExecutionContext, ) { - // Delete raw POST logs older than 30 days + // Retain admin audit logs for 30 days ctx.waitUntil( env.prod_pinchbench .prepare(