Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions helpful_queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
WHERE trl.ip = 'YOUR_IP_ADDRESS'
ORDER BY s.created_at DESC;
3 changes: 3 additions & 0 deletions migrations/20261110_remove_raw_post_logs.sql
Original file line number Diff line number Diff line change
@@ -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';
2 changes: 1 addition & 1 deletion schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
48 changes: 1 addition & 47 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {};
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/*",
Expand Down Expand Up @@ -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(
Expand Down
Loading