Public: anyone can read this; the crew can change it.
A board query that would read more than 5,000 rows is refused naming the limit and the source #16156
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16138-a-board-query-that-would-read-more-than → main 0 commits fork 2 threads · 2 unresolved
3 files
changed +63
−0
src/boards/query.ts +14
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | * `sources.ts`; every value travels as a bound parameter. |
| 5 | 5 | */ |
| 6 | 6 | import type { Database } from "bun:sqlite"; |
| 7 | import { limitValue } from "../settings/limits.ts"; | |
| 7 | 8 | import { SOURCES, type ColumnType } from "./sources.ts"; |
| 8 | 9 | |
| 9 | 10 | /** `starts` is a prefix match on a text or time column (`target` starts `cpu:`); the rest compare. */ |
| @@ -182,6 +183,19 @@ export function runQuery(db: Database, q: unknown, scope: Scope): Answer { | ||
| 182 | 183 | } |
| 183 | 184 | const grouped = [groupSql === "NULL" ? "" : "1", seriesSql === "" ? "" : "3"].filter(Boolean).join(", "); |
| 184 | 185 | |
| 186 | // Refuse before grouping when the rows it would read pass the limit in | |
| 187 | // settings. A time-bucketed query is bounded by MAX_POINTS instead (its | |
| 188 | // refusal asks for a coarser step), since a day of 15 s samples alone is | |
| 189 | // more rows than the limit. | |
| 190 | const max = limitValue(db, "boards.query_rows_max"); | |
| 191 | const read = | |
| 192 | step === undefined | |
| 193 | ? (db.query(`SELECT COUNT(*) AS n FROM (SELECT 1 FROM ${from}${whereSql(where)} LIMIT ${max + 1})`).get(...params) as { n: number }) | |
| 194 | : { n: 0 }; | |
| 195 | if (read.n > max) { | |
| 196 | return refuse(`this query would read more than ${max.toLocaleString("en-US")} rows from ${query.source}: narrow the range or add a filter`); | |
| 197 | } | |
| 198 | ||
| 185 | 199 | const sql = |
| 186 | 200 | `SELECT ${groupSql} AS "group", ${valueSql} AS "value"${seriesSql} FROM ${from}` + |
| 187 | 201 | whereSql(where) + |
src/settings/limits.ts +9
| @@ -73,6 +73,7 @@ export type LimitKey = | ||
| 73 | 73 | | "retention.event_days" |
| 74 | 74 | | "retention.deploy_pull_days" |
| 75 | 75 | | "sampler.rows_per_source" |
| 76 | | "boards.query_rows_max" | |
| 76 | 77 | | "security.sweep_max_usd"; |
| 77 | 78 | |
| 78 | 79 | export interface LimitDef { |
| @@ -465,6 +466,14 @@ export const LIMITS: Record<LimitKey, LimitDef> = { | ||
| 465 | 466 | max: 100_000, |
| 466 | 467 | reason: "A backstop only: the age rule already keeps a day, 5,760 rows a source, so under defaults this never bites; it bounds the table when points come faster than every 15 s or the age rule stops, and the board says how many went.", |
| 467 | 468 | }, |
| 469 | "boards.query_rows_max": { | |
| 470 | label: "Rows one board query may read", | |
| 471 | unit: "rows", | |
| 472 | default: 5_000, | |
| 473 | min: 100, | |
| 474 | max: 1_000_000, | |
| 475 | reason: "A board query scans its source before it groups, so a wide window on a big table would hold the one process; past this many rows it is refused by name and the caller narrows the range or a filter.", | |
| 476 | }, | |
| 468 | 477 | "security.sweep_max_usd": { |
| 469 | 478 | label: "Weekly security sweep spend ceiling", |
| 470 | 479 | unit: "dollars", |
test/board-limit.test.ts added +40
| @@ -0,0 +1,40 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { migrations } from "../src/core/schema.ts"; | |
| 3 | import { openDb } from "../src/kernel/db.ts"; | |
| 4 | import { runQuery } from "../src/boards/query.ts"; | |
| 5 | import { LIMITS, setLimit } from "../src/settings/limits.ts"; | |
| 6 | ||
| 7 | const q = { source: "runs", aggregate: "count" }; | |
| 8 | ||
| 9 | function seed(n: number) { | |
| 10 | const db = openDb(":memory:", migrations); | |
| 11 | db.run("INSERT INTO user (id, username, email, password_hash) VALUES ('u1','u','u@x','x')"); | |
| 12 | db.run("INSERT INTO repo (id, owner_id, name) VALUES ('r1','u1','n')"); | |
| 13 | const ins = db.prepare("INSERT INTO run (id, repo_id, kind, state, created_at) VALUES (?, 'r1', 'agent', 'ok', '2026-10-01T10:00:00.000Z')"); | |
| 14 | db.transaction(() => { for (let i = 0; i < n; i++) ins.run(`r${i}`); })(); | |
| 15 | return db; | |
| 16 | } | |
| 17 | ||
| 18 | describe("board query row limit", () => { | |
| 19 | test("the default is 5,000, a setting", () => { | |
| 20 | expect(LIMITS["boards.query_rows_max"].default).toBe(5000); | |
| 21 | }); | |
| 22 | test("over the limit is refused naming limit and source", () => { | |
Next: add a case through the /api/v1/boards/query handler asserting status 400 and the message, since box 1 names the status. |
||
| 23 | const db = seed(5001); | |
| 24 | const a = runQuery(db, q, { repoIds: "all" }); | |
| 25 | expect(a.ok).toBe(false); | |
| 26 | if (!a.ok) { | |
| 27 | expect(a.error).toContain("5,000"); | |
| 28 | expect(a.error).toContain("runs"); | |
| 29 | } | |
| 30 | }); | |
| 31 | test("at the limit is unchanged", () => { | |
| 32 | const a = runQuery(seed(5000), q, { repoIds: "all" }); | |
| 33 | expect(a).toEqual({ ok: true, rows: [{ group: null, value: 5000 }] }); | |
| 34 | }); | |
| 35 | test("the limit is read from settings", () => { | |
| 36 | const db = seed(150); | |
| 37 | expect(setLimit(db, "boards.query_rows_max", 100, "u1").ok).toBe(true); | |
| 38 | expect(runQuery(db, q, { repoIds: "all" }).ok).toBe(false); | |
| 39 | }); | |
| 40 | }); |
new above
offline
Next: bucketed queries skip the row guard entirely (step !== undefined gives n: 0). Give them a separate, higher limit so a wide window cannot hold the process.