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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/16138-a-board-query-that-would-read-more-than → main 0 commits fork 2 threads · 2 unresolved

  • Merged. scout/16138-a-board-query-that-would-read-more-than was merged into main as b9322ec.

Review

Sign in to review this change.
3 files changed +63 −0
src/boards/query.ts +14
@@ -4,6 +4,7 @@
44 * `sources.ts`; every value travels as a bound parameter.
55 */
66 import type { Database } from "bun:sqlite";
7 import { limitValue } from "../settings/limits.ts";
78 import { SOURCES, type ColumnType } from "./sources.ts";
89
910 /** `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 {
182183 }
183184 const grouped = [groupSql === "NULL" ? "" : "1", seriesSql === "" ? "" : "3"].filter(Boolean).join(", ");
184185
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

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.

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
185199 const sql =
186200 `SELECT ${groupSql} AS "group", ${valueSql} AS "value"${seriesSql} FROM ${from}` +
187201 whereSql(where) +
src/settings/limits.ts +9
@@ -73,6 +73,7 @@ export type LimitKey =
7373 | "retention.event_days"
7474 | "retention.deploy_pull_days"
7575 | "sampler.rows_per_source"
76 | "boards.query_rows_max"
7677 | "security.sweep_max_usd";
7778
7879 export interface LimitDef {
@@ -465,6 +466,14 @@ export const LIMITS: Record<LimitKey, LimitDef> = {
465466 max: 100_000,
466467 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.",
467468 },
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 },
468477 "security.sweep_max_usd": {
469478 label: "Weekly security sweep spend ceiling",
470479 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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs