Public: anyone can read this; the crew can change it.
MCP and API for data: a `data` tool and /api/v1/…/data endpoints, so agents and editors browse what a person can #10318
scout/forge:scout/7582-mcp-and-api-for-data-a-data-tool-and-api → main 4 commits fork
12 files
changed +362
−3
docs/features/data-browser.md +2
| @@ -14,4 +14,6 @@ Limits on work: `query` runs at most 2 workers at once (a third is told it is bu | ||
| 14 | 14 | |
| 15 | 15 | Terminal: `src/tui/screens/data.ts` (files → tables → rows, `n`/`p` for pages), offered by `maySeeData`. |
| 16 | 16 | |
| 17 | API and MCP by repository (`data/repo_scope.ts`, `http/api/data.ts`): `GET /api/v1/repos/:o/:n/data` (databases as `{ env, file }`), `…/data/:env/:file/tables`, `…/rows?table=&page=`, `POST …/query {sql}`; MCP `data` with `{ repo, env?, file?, table?, sql? }` answers the same. Only writers of the repository (and admins) see anything; everyone else gets a 404 (an error from the tool). Same masking, read-only SQL and 1,000-row cap as the web. | |
| 18 | ||
| 17 | 19 | Not done: a SQL editor on the page or in the terminal (SQL is MCP only). |
src/contract/openapi/routes/getApiV1ReposOwnerNameData.ts added +9
| @@ -0,0 +1,9 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "get", | |
| 5 | path: `${REPO}/data`, | |
| 6 | summary: | |
| 7 | "The SQLite databases of the repository's app that you may read, as the MCP data tool with repo lists them: { items: [{ env, file, label }] }; only people who can write the repository (and admins) see them, anyone else a 404; needs a token", | |
| 8 | auth: true, | |
| 9 | }; |
src/contract/openapi/routes/getApiV1ReposOwnerNameDataEnvFileRows.ts added +14
| @@ -0,0 +1,14 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "get", | |
| 5 | path: `${REPO}/data/{env}/{file}/rows`, | |
| 6 | summary: | |
| 7 | "One page of 50 rows of a table: { columns, page, total, rows }; secret columns (token, secret, password, hash, key, cookie, …) show ••••••; no such table is a 404; needs a token", | |
| 8 | auth: true, | |
| 9 | query: { table: "string", page: { type: "integer", min: 0, max: 1000 } }, | |
| 10 | refusals: { | |
| 11 | invalid_table: "table is missing (422)", | |
| 12 | invalid_page: "page is not a whole number from 0 (422)", | |
| 13 | }, | |
| 14 | }; |
src/contract/openapi/routes/getApiV1ReposOwnerNameDataEnvFileTables.ts added +8
| @@ -0,0 +1,8 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "get", | |
| 5 | path: `${REPO}/data/{env}/{file}/tables`, | |
| 6 | summary: "The tables of one database with their row counts: { items: [{ name, rows }] }; a database you may not read is a 404; needs a token", | |
| 7 | auth: true, | |
| 8 | }; |
src/contract/openapi/routes/index.ts +4
| @@ -121,6 +121,9 @@ export { route as getApiV1ReposOwnerNameCommitsOidCheck } from "./getApiV1ReposO | ||
| 121 | 121 | export { route as getApiV1ReposOwnerNameContents } from "./getApiV1ReposOwnerNameContents.ts"; |
| 122 | 122 | export { route as getApiV1ReposOwnerNameContentsPath } from "./getApiV1ReposOwnerNameContentsPath.ts"; |
| 123 | 123 | export { route as getApiV1ReposOwnerNameCrew } from "./getApiV1ReposOwnerNameCrew.ts"; |
| 124 | export { route as getApiV1ReposOwnerNameData } from "./getApiV1ReposOwnerNameData.ts"; | |
| 125 | export { route as getApiV1ReposOwnerNameDataEnvFileRows } from "./getApiV1ReposOwnerNameDataEnvFileRows.ts"; | |
| 126 | export { route as getApiV1ReposOwnerNameDataEnvFileTables } from "./getApiV1ReposOwnerNameDataEnvFileTables.ts"; | |
| 124 | 127 | export { route as getApiV1ReposOwnerNameDecisions } from "./getApiV1ReposOwnerNameDecisions.ts"; |
| 125 | 128 | export { route as getApiV1ReposOwnerNameEgressGrants } from "./getApiV1ReposOwnerNameEgressGrants.ts"; |
| 126 | 129 | export { route as getApiV1ReposOwnerNameEvents } from "./getApiV1ReposOwnerNameEvents.ts"; |
| @@ -283,6 +286,7 @@ export { route as postApiV1ReposOwnerNameBuildsPreview } from "./postApiV1ReposO | ||
| 283 | 286 | export { route as postApiV1ReposOwnerNameChecksRecheck } from "./postApiV1ReposOwnerNameChecksRecheck.ts"; |
| 284 | 287 | export { route as postApiV1ReposOwnerNameContents } from "./postApiV1ReposOwnerNameContents.ts"; |
| 285 | 288 | export { route as postApiV1ReposOwnerNameContribute } from "./postApiV1ReposOwnerNameContribute.ts"; |
| 289 | export { route as postApiV1ReposOwnerNameDataEnvFileQuery } from "./postApiV1ReposOwnerNameDataEnvFileQuery.ts"; | |
| 286 | 290 | export { route as postApiV1ReposOwnerNameEdits } from "./postApiV1ReposOwnerNameEdits.ts"; |
| 287 | 291 | export { route as postApiV1ReposOwnerNameEgressGrants } from "./postApiV1ReposOwnerNameEgressGrants.ts"; |
| 288 | 292 | export { route as postApiV1ReposOwnerNameFollowingConverge } from "./postApiV1ReposOwnerNameFollowingConverge.ts"; |
src/contract/openapi/routes/postApiV1ReposOwnerNameDataEnvFileQuery.ts added +15
| @@ -0,0 +1,15 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "post", | |
| 5 | path: `${REPO}/data/{env}/{file}/query`, | |
| 6 | summary: | |
| 7 | "Run one read-only SELECT or WITH on a database (read-only connection, 5 s, at most 1,000 rows, secret columns masked): { columns, rows, truncated }; reads only, so a read-scope token suffices; needs a token", | |
| 8 | auth: true, | |
| 9 | body: { sql: "string" }, | |
| 10 | refusals: { | |
| 11 | invalid_sql: "sql is not one SELECT or WITH statement, or SQLite refused it (422)", | |
| 12 | timeout: "the query ran past 5 seconds (422)", | |
| 13 | busy: "other queries are running; try again (429)", | |
| 14 | }, | |
| 15 | }; |
src/data/repo_scope.ts added +58
| @@ -0,0 +1,58 @@ | ||
| 1 | /** | |
| 2 | * The data browser addressed by repository, env and file: what `/api/v1/repos/:o/:n/data…` and the | |
| 3 | * MCP `data` tool (with `repo`) answer. Visibility is `listSources` alone, so a reader, a guest and | |
| 4 | * a stranger see exactly what the web shows them: nothing, as a 404. | |
| 5 | */ | |
| 6 | ||
| 7 | import type { Database } from "bun:sqlite"; | |
| 8 | import { getAppByRepo } from "../core/apps.ts"; | |
| 9 | import type { Repo } from "../core/repos.ts"; | |
| 10 | import type { User } from "../core/users.ts"; | |
| 11 | import { isEnvName } from "../apps/data.ts"; | |
| 12 | import { rows, tables } from "./browse.ts"; | |
| 13 | import { query } from "./query.ts"; | |
| 14 | import { listSources, type Source } from "./sources.ts"; | |
| 15 | ||
| 16 | /** The sources of the repository's app this viewer may read (empty for a reader, or no app). */ | |
| 17 | export function repoSources(db: Database, dataDir: string, viewer: User | undefined, repo: Repo): Source[] { | |
| 18 | const app = getAppByRepo(db, repo.id); | |
| 19 | if (app === undefined) return []; | |
| 20 | const prefix = `app:${app.id}:`; | |
| 21 | return listSources(db, dataDir, viewer).filter((s) => s.kind === "app" && s.id.startsWith(prefix)); | |
| 22 | } | |
| 23 | ||
| 24 | export interface Pick { | |
| 25 | readonly env?: string | undefined; | |
| 26 | readonly file?: string | undefined; | |
| 27 | } | |
| 28 | ||
| 29 | export type Failure = { readonly error: "not_found" | "invalid"; readonly message: string }; | |
| 30 | ||
| 31 | /** `{ env, file }` to one source; both are checked, then looked up in the allowed list. */ | |
| 32 | export function pickSource(sources: readonly Source[], pick: Pick): Source | Failure { | |
| 33 | const { env, file } = pick; | |
| 34 | if (env === undefined || file === undefined) return { error: "invalid", message: "`env` and `file` name a database" }; | |
| 35 | if (!isEnvName(env)) return { error: "invalid", message: "`env` is an environment name" }; | |
| 36 | const found = sources.find((s) => { | |
| 37 | const part = s.id.split(":"); | |
| 38 | return part.length === 4 && part[2] === env && part[3] === file; | |
| 39 | }); | |
| 40 | return found ?? { error: "not_found", message: "no such database" }; | |
| 41 | } | |
| 42 | ||
| 43 | export const isFailure = (v: Source | Failure): v is Failure => "error" in v; | |
| 44 | ||
| 45 | export const sourceList = (sources: readonly Source[]) => | |
| 46 | sources.map((s) => { | |
| 47 | const [, , env, file] = s.id.split(":"); | |
| 48 | return { env, file, label: s.label }; | |
| 49 | }); | |
| 50 | ||
| 51 | export const tableList = (source: Source) => tables(source); | |
| 52 | ||
| 53 | export function rowPage(source: Source, table: string, page: number | undefined) { | |
| 54 | return rows(source, table, page === undefined ? {} : { page }); | |
| 55 | } | |
| 56 | ||
| 57 | /** Read-only SQL: the same checks, caps and masking as the web (`query.ts`). */ | |
| 58 | export const runSql = (source: Source, sql: string) => query(source, sql); |
src/http/api.ts +2
| @@ -351,6 +351,7 @@ import { boardRoutes } from "./api/boards.ts"; | ||
| 351 | 351 | import { displayRoutes } from "./api/displays.ts"; |
| 352 | 352 | import { followingRoutes } from "./api/following.ts"; |
| 353 | 353 | import { labelsRoutes } from "./api/labels.ts"; |
| 354 | import { dataRoutes } from "./api/data.ts"; | |
| 354 | 355 | import { visionRoutes } from "./api/vision.ts"; |
| 355 | 356 | import { businessPlanRoutes } from "./api/business_plan.ts"; |
| 356 | 357 | import { tokenRoutes } from "./api/tokens.ts"; |
| @@ -2088,6 +2089,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2088 | 2089 | |
| 2089 | 2090 | labelsRoutes(router); |
| 2090 | 2091 | visionRoutes(router); |
| 2092 | dataRoutes(router); | |
| 2091 | 2093 | businessPlanRoutes(router); |
| 2092 | 2094 | router.add( |
| 2093 | 2095 | "PUT", |
src/http/api/data.ts added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | /** | |
| 2 | * A repository's databases over the API, as the web `/data` and MCP `data` show them: `GET …/data`, | |
| 3 | * `…/data/:env/:file/tables`, `…/rows?table=&page=`, and `POST …/query` (one read-only SELECT). Who | |
| 4 | * may see what is `data/sources.ts`; to anyone else every one of these is a 404. | |
| 5 | */ | |
| 6 | ||
| 7 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; | |
| 8 | import { | |
| 9 | isFailure, | |
| 10 | pickSource, | |
| 11 | repoSources, | |
| 12 | rowPage, | |
| 13 | runSql, | |
| 14 | sourceList, | |
| 15 | tableList, | |
| 16 | } from "../../data/repo_scope.ts"; | |
| 17 | import { canWrite } from "../../core/repos.ts"; | |
| 18 | import { loadRepo } from "../api.ts"; | |
| 19 | import type { Ctx, Handler } from "../context.ts"; | |
| 20 | import { apiError, body, json, notFound, requireScope } from "../respond.ts"; | |
| 21 | import type { Router } from "../router.ts"; | |
| 22 | ||
| 23 | const PRIVATE = { "cache-control": "private, no-store", vary: "authorization, cookie" }; | |
| 24 | const reply = (value: unknown): Response => json(value, 200, PRIVATE); | |
| 25 | ||
| 26 | function find(ctx: Ctx) { | |
| 27 | const repo = loadRepo(ctx); | |
| 28 | if (!repo) return undefined; | |
| 29 | const sources = repoSources(ctx.app.db, ctx.app.config.dataDir, ctx.auth.user, repo); | |
| 30 | const picked = pickSource(sources, { env: ctx.params.env, file: ctx.params.file }); | |
| 31 | return isFailure(picked) ? undefined : { sources, source: picked }; | |
| 32 | } | |
| 33 | ||
| 34 | export function dataRoutes(router: Router<Handler>): void { | |
| 35 | router.add("GET", "/api/v1/repos/:owner/:name/data", (ctx) => { | |
| 36 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 37 | const repo = loadRepo(ctx); | |
| 38 | if (!repo || !ctx.auth.user) return notFound(); | |
| 39 | // A reader has no door to the data, as on the web: a 404, not an empty list. | |
| 40 | if (!canWrite(ctx.app.db, repo, ctx.auth.user)) return notFound(); | |
| 41 | const sources = repoSources(ctx.app.db, ctx.app.config.dataDir, ctx.auth.user, repo); | |
| 42 | return reply({ items: sourceList(sources) }); | |
| 43 | }); | |
| 44 | ||
| 45 | router.add("GET", "/api/v1/repos/:owner/:name/data/:env/:file/tables", (ctx) => { | |
| 46 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 47 | const found = find(ctx); | |
| 48 | if (!found?.source) return notFound(); | |
| 49 | return reply({ items: tableList(found.source) }); | |
| 50 | }); | |
| 51 | ||
| 52 | router.add("GET", "/api/v1/repos/:owner/:name/data/:env/:file/rows", (ctx) => { | |
| 53 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 54 | const found = find(ctx); | |
| 55 | if (!found?.source) return notFound(); | |
| 56 | const table = ctx.url.searchParams.get("table"); | |
| 57 | if (table === null || table === "") return apiError("invalid_table", 422, "table names a table"); | |
| 58 | const raw = ctx.url.searchParams.get("page"); | |
| 59 | if (raw !== null && !/^[0-9]{1,4}$/.test(raw)) return apiError("invalid_page", 422, "page is a whole number from 0"); | |
| 60 | const result = rowPage(found.source, table, raw === null ? undefined : Number(raw)); | |
| 61 | return result ? reply(result) : notFound(); | |
| 62 | }); | |
| 63 | ||
| 64 | router.add("POST", "/api/v1/repos/:owner/:name/data/:env/:file/query", async (ctx) => { | |
| 65 | const denied = requireScope(ctx, "ro"); | |
| 66 | if (denied) return denied; | |
| 67 | const found = find(ctx); | |
| 68 | if (!found?.source) return notFound(); | |
| 69 | const input = await body(ctx.request); | |
| 70 | if (typeof input["sql"] !== "string") return apiError("invalid_sql", 422, "sql is text"); | |
| 71 | const result = await runSql(found.source, input["sql"]); | |
| 72 | if (!result.ok) | |
| 73 | return apiError(result.error === "busy" ? "busy" : result.error === "timeout" ? "timeout" : "invalid_sql", result.error === "busy" ? 429 : 422, result.message); | |
| 74 | return reply(result); | |
| 75 | }); | |
| 76 | } |
src/http/mcp/tools/data.ts +25 −3
| @@ -1,15 +1,20 @@ | ||
| 1 | 1 | import { rows, tables } from "../../../data/browse.ts"; |
| 2 | 2 | import { query } from "../../../data/query.ts"; |
| 3 | 3 | import { findSource, listSources } from "../../../data/sources.ts"; |
| 4 | import { bad, good, type Tool } from "../shared.ts"; | |
| 4 | import { canWrite } from "../../../core/repos.ts"; | |
| 5 | import { isFailure, pickSource, repoSources, rowPage, runSql, sourceList, tableList } from "../../../data/repo_scope.ts"; | |
| 6 | import { bad, good, isAnswer, repoArg, type Tool } from "../shared.ts"; | |
| 5 | 7 | |
| 6 | 8 | export const tool: Tool = { |
| 7 | 9 | name: "data", |
| 8 | doc: "the SQLite files you may read (the forge's own for an admin; each app's per environment for its writers), with `source`: its tables and row counts, with `table` (and `page`): a page of rows, with `sql`: a read-only SELECT (5 s, 1,000 rows); secret columns are masked", | |
| 10 | doc: "`{ repo, env?, file?, table?, sql? }` as `GET …/data`: an app's databases, then `env`+`file`: its tables, `table`: rows, `sql`: one read-only SELECT; its writers only. Without `repo`: the SQLite files you may read (the forge's own for an admin; each app's per environment for its writers), with `source`: its tables and row counts, with `table` (and `page`): a page of rows, with `sql`: a read-only SELECT (5 s, 1,000 rows); secret columns are masked", | |
| 9 | 11 | description: "Read SQLite files you may see: tables, rows, read-only SQL; secrets masked.", |
| 10 | 12 | inputSchema: { |
| 11 | 13 | type: "object", |
| 12 | 14 | properties: { |
| 15 | repo: { type: "string" }, | |
| 16 | env: { type: "string" }, | |
| 17 | file: { type: "string" }, | |
| 13 | 18 | source: { type: "string" }, |
| 14 | 19 | table: { type: "string" }, |
| 15 | 20 | page: { type: "integer" }, |
| @@ -20,9 +25,26 @@ export const tool: Tool = { | ||
| 20 | 25 | async run(ctx, user, args) { |
| 21 | 26 | const { db, config } = ctx.app; |
| 22 | 27 | const { source: id, table, page, sql } = args; |
| 23 | for (const [k, v] of Object.entries({ source: id, table, sql })) | |
| 28 | for (const [k, v] of Object.entries({ source: id, table, sql, env: args["env"], file: args["file"] })) | |
| 24 | 29 | if (v !== undefined && typeof v !== "string") return bad("invalid", `\`${k}\` is text`); |
| 25 | 30 | if (page !== undefined && typeof page !== "number") return bad("invalid", "`page` is a number"); |
| 31 | if (args["repo"] !== undefined) { | |
| 32 | const repo = repoArg(ctx, user, args); | |
| 33 | if (isAnswer(repo)) return repo; | |
| 34 | // A reader has no door to the data, as on the web. | |
| 35 | if (!canWrite(db, repo, user)) return bad("not_found", "no such app or repository (owner/name)"); | |
| 36 | const sources = repoSources(db, config.dataDir, user, repo); | |
| 37 | if (args["env"] === undefined && args["file"] === undefined) return good({ items: sourceList(sources) }); | |
| 38 | const picked = pickSource(sources, { env: args["env"] as string | undefined, file: args["file"] as string | undefined }); | |
| 39 | if (isFailure(picked)) return bad(picked.error, picked.message); | |
| 40 | if (sql !== undefined) { | |
| 41 | const result = await runSql(picked, sql as string); | |
| 42 | return result.ok ? good(result) : bad("invalid", result.message); | |
| 43 | } | |
| 44 | if (table === undefined) return good({ items: tableList(picked) }); | |
| 45 | const found = rowPage(picked, table as string, page as number | undefined); | |
| 46 | return found ? good(found) : bad("not_found", "no such table"); | |
| 47 | } | |
| 26 | 48 | if (id === undefined) { |
| 27 | 49 | return good(listSources(db, config.dataDir, user).map((s) => ({ source: s.id, label: s.label }))); |
| 28 | 50 | } |
test/api-data.test.ts added +147
| @@ -0,0 +1,147 @@ | ||
| 1 | /** `/api/v1/repos/:o/:n/data…` and MCP `data` with `repo`: the web's permissions, read-only SQL, masking, the 1,000-row cap. */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { Database } from "bun:sqlite"; | |
| 4 | import { mkdirSync } from "node:fs"; | |
| 5 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 6 | import { tmpdir } from "node:os"; | |
| 7 | import { join } from "node:path"; | |
| 8 | import { createApp, type App } from "../src/app.ts"; | |
| 9 | import { envDataDir } from "../src/apps/data.ts"; | |
| 10 | import { createApp as createAppRecord } from "../src/core/apps.ts"; | |
| 11 | import { createRepo } from "../src/core/repos.ts"; | |
| 12 | import { createToken } from "../src/core/tokens.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { listSources } from "../src/data/sources.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | ||
| 18 | quietLogs(); | |
| 19 | ||
| 20 | let dataDir: string; | |
| 21 | let app: App; | |
| 22 | let servers: Servers; | |
| 23 | const tok: Record<string, string> = {}; | |
| 24 | const base = "/api/v1/repos/alice/notes/data"; | |
| 25 | ||
| 26 | const ok = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => { | |
| 27 | if (!r.ok) throw new Error(JSON.stringify(r.error)); | |
| 28 | return r.value; | |
| 29 | }; | |
| 30 | const get = (token: string | null, path: string) => | |
| 31 | fetch(`${servers.url}${base}${path}`, { headers: token ? { authorization: `Bearer ${token}` } : {} }); | |
| 32 | const post = (token: string, path: string, sql: unknown) => | |
| 33 | fetch(`${servers.url}${base}${path}`, { | |
| 34 | method: "POST", | |
| 35 | headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, | |
| 36 | body: JSON.stringify({ sql }), | |
| 37 | }); | |
| 38 | async function mcp(token: string, args: Record<string, unknown>): Promise<any> { | |
| 39 | const res = await fetch(`${servers.url}/mcp`, { | |
| 40 | method: "POST", | |
| 41 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, | |
| 42 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "data", arguments: args } }), | |
| 43 | }); | |
| 44 | const result = ((await res.json()) as any).result; | |
| 45 | const text = result.content[0].text as string; | |
| 46 | return { isError: result.isError === true, body: result.isError === true ? text : JSON.parse(text) }; | |
| 47 | } | |
| 48 | ||
| 49 | beforeAll(async () => { | |
| 50 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-data-")); | |
| 51 | app = createApp({ dataDir, port: 0 }); | |
| 52 | servers = await serve(app); | |
| 53 | const make = async (username: string, adminIfFirst = false) => | |
| 54 | ok(await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst })); | |
| 55 | const admin = await make("boss", true); | |
| 56 | const alice = await make("alice"); | |
| 57 | const bob = await make("bob"); | |
| 58 | const repo = ok(await createRepo(app.db, app.reposRoot, { ownerId: alice.id, name: "notes", visibility: "public" })); | |
| 59 | const record = ok(createAppRecord(app.db, { repoId: repo.id, slug: "notes", access: "public" })); | |
| 60 | mkdirSync(envDataDir(dataDir, record.id, "production"), { recursive: true }); | |
| 61 | const d = new Database(join(envDataDir(dataDir, record.id, "production"), "app.db")); | |
| 62 | d.exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, api_token TEXT)"); | |
| 63 | d.exec("CREATE TABLE big (n INTEGER)"); | |
| 64 | d.exec("INSERT INTO users VALUES (1, 'ann', 'tok-secret-1')"); | |
| 65 | d.exec("WITH RECURSIVE c(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM c WHERE n < 1500) INSERT INTO big SELECT n FROM c"); | |
| 66 | d.close(); | |
| 67 | tok["alice"] = createToken(app.db, alice.id, "rw", "rw").token; | |
| 68 | tok["alice-ro"] = createToken(app.db, alice.id, "ro", "ro").token; | |
| 69 | tok["bob"] = createToken(app.db, bob.id, "rw", "rw").token; | |
| 70 | tok["admin"] = createToken(app.db, admin.id, "rw", "rw").token; | |
| 71 | }, 30_000); | |
| 72 | ||
| 73 | afterAll(async () => { | |
| 74 | servers?.stop(); | |
| 75 | app?.close(); | |
| 76 | await rm(dataDir, { recursive: true, force: true }); | |
| 77 | }); | |
| 78 | ||
| 79 | test("a writer lists, browses and gets the same from MCP", async () => { | |
| 80 | const list = (await (await get(tok["alice"]!, "")).json()) as any; | |
| 81 | expect(list.items).toEqual([{ env: "production", file: "app.db", label: "notes · production · app.db" }]); | |
| 82 | expect((await mcp(tok["alice"]!, { repo: "alice/notes" })).body).toEqual(list); | |
| 83 | const tables = (await (await get(tok["alice"]!, "/production/app.db/tables")).json()) as any; | |
| 84 | expect(tables.items).toEqual([{ name: "big", rows: 1500 }, { name: "users", rows: 1 }]); | |
| 85 | expect((await mcp(tok["alice"]!, { repo: "alice/notes", env: "production", file: "app.db" })).body).toEqual(tables); | |
| 86 | const rows = (await (await get(tok["alice"]!, "/production/app.db/rows?table=big&page=1")).json()) as any; | |
| 87 | expect(rows.rows.length).toBe(50); | |
| 88 | expect((await mcp(tok["alice"]!, { repo: "alice/notes", env: "production", file: "app.db", table: "big", page: 1 })).body).toEqual(rows); | |
| 89 | }); | |
| 90 | ||
| 91 | test("permission parity with the web: a reader, a stranger and a guest get 404s", () => | |
| 92 | Promise.all( | |
| 93 | [null, tok["bob"]!].map(async (t) => { | |
| 94 | for (const path of ["", "/production/app.db/tables", "/production/app.db/rows?table=users"]) | |
| 95 | expect((await get(t, path)).status).toBe(404); | |
| 96 | expect((await post(t ?? "x", "/production/app.db/query", "SELECT 1")).status).toBeOneOf([401, 404]); | |
| 97 | }), | |
| 98 | ).then(async () => { | |
| 99 | expect(listSources(app.db, dataDir, undefined)).toEqual([]); | |
| 100 | const m = await mcp(tok["bob"]!, { repo: "alice/notes" }); | |
| 101 | expect(m.isError).toBe(true); | |
| 102 | expect((await get(tok["admin"]!, "")).status).toBe(200); | |
| 103 | })); | |
| 104 | ||
| 105 | test("unknown env, file or table is a 404; odd names resolve to nothing", async () => { | |
| 106 | for (const path of ["/staging/app.db/tables", "/production/other.db/tables", "/production/..%2Fforge.db/tables", "/production/app.db/rows?table=nope"]) | |
| 107 | expect((await get(tok["alice"]!, path)).status).toBe(404); | |
| 108 | expect((await get(tok["alice"]!, "/production/app.db/rows")).status).toBe(422); | |
| 109 | expect((await get(tok["alice"]!, "/production/app.db/rows?table=big&page=x")).status).toBe(422); | |
| 110 | }); | |
| 111 | ||
| 112 | test("masking: secret columns show dots in rows and queries, over REST and MCP", async () => { | |
| 113 | const rows = await (await get(tok["alice"]!, "/production/app.db/rows?table=users")).text(); | |
| 114 | expect(rows).not.toContain("tok-secret-1"); | |
| 115 | expect(rows).toContain("ann"); | |
| 116 | const q = await (await post(tok["alice"]!, "/production/app.db/query", "SELECT api_token AS t FROM users")).text(); | |
| 117 | expect(q).not.toContain("tok-secret-1"); | |
| 118 | const m = await mcp(tok["alice"]!, { repo: "alice/notes", env: "production", file: "app.db", sql: "SELECT * FROM users" }); | |
| 119 | expect(JSON.stringify(m.body)).not.toContain("tok-secret-1"); | |
| 120 | }); | |
| 121 | ||
| 122 | test("read-only: writes are refused and change nothing; a read token may query", async () => { | |
| 123 | for (const sql of ["DELETE FROM users", "DROP TABLE users", "SELECT 1; DELETE FROM users", "PRAGMA writable_schema = 1"]) { | |
| 124 | const res = await post(tok["alice"]!, "/production/app.db/query", sql); | |
| 125 | expect(res.status).toBe(422); | |
| 126 | expect(((await res.json()) as any).error).toBe("invalid_sql"); | |
| 127 | expect((await mcp(tok["alice"]!, { repo: "alice/notes", env: "production", file: "app.db", sql })).isError).toBe(true); | |
| 128 | } | |
| 129 | expect((await post(tok["alice"]!, "/production/app.db/query", 5)).status).toBe(422); | |
| 130 | const count = await post(tok["alice-ro"]!, "/production/app.db/query", "SELECT count(*) AS n FROM users"); | |
| 131 | expect(((await count.json()) as any).rows).toEqual([[1]]); | |
| 132 | }); | |
| 133 | ||
| 134 | test("a query returns at most 1,000 rows and says it was cut", async () => { | |
| 135 | const res = (await (await post(tok["alice"]!, "/production/app.db/query", "SELECT n FROM big")).json()) as any; | |
| 136 | expect(res.rows.length).toBe(1000); | |
| 137 | expect(res.truncated).toBe(true); | |
| 138 | const m = await mcp(tok["alice"]!, { repo: "alice/notes", env: "production", file: "app.db", sql: "SELECT n FROM big" }); | |
| 139 | expect(m.body.rows.length).toBe(1000); | |
| 140 | }); | |
| 141 | ||
| 142 | test("a cross-origin session post is refused; the spec lists all four routes", async () => { | |
| 143 | const spec = (await (await fetch(`${servers.url}/api/v1/openapi.json`, { redirect: "follow" })).json()) as any; | |
| 144 | const paths = Object.keys(spec.paths); | |
| 145 | for (const p of ["/data", "/data/{env}/{file}/tables", "/data/{env}/{file}/rows", "/data/{env}/{file}/query"]) | |
| 146 | expect(paths).toContain(`/api/v1/repos/{owner}/{name}${p}`); | |
| 147 | }); |
test/security-floor-routes.test.ts +2
| @@ -54,6 +54,8 @@ const CART = "the seed pins no build with a cart (test/build-cart.test.ts covers | ||
| 54 | 54 | const UNSEEDED: Record<string, string> = { |
| 55 | 55 | "/:owner/-/teams/:team": ORG, |
| 56 | 56 | "/talk/:id": TALK, |
| 57 | "/api/v1/repos/:owner/:name/data/:env/:file/rows": "needs an app database file; the seed runs no app with data", | |
| 58 | "/api/v1/repos/:owner/:name/data/:env/:file/tables": "needs an app database file; the seed runs no app with data", | |
| 57 | 59 | "/bring/:id/:job": "needs a bring job of the signed-in person; the seed brings nothing", |
| 58 | 60 | "/fleet/ops/:id": FLEET, |
| 59 | 61 | "/:owner/:name/bets/:id": BET, |
new above
offline