Public: anyone can read this; the crew can change it.
The data browser refuses another repository's source, and the video and binary views draw what their tests expect #15931
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15929-the-data-browser-refuses-another-reposit → main 0 commits fork
2 files
changed +185
−0
test/data-repo-scope.test.ts added +113
| @@ -0,0 +1,113 @@ | ||
| 1 | /** repo_scope.ts: the one guard of the repo-addressed data doors, and the picks behind them. */ | |
| 2 | import { afterAll, beforeAll, describe, 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, type Repo } from "../src/core/repos.ts"; | |
| 12 | import { createUser, type User } from "../src/core/users.ts"; | |
| 13 | import { isFailure, parsePage, pickSource, repoSources, rowPage, sourceList } from "../src/data/repo_scope.ts"; | |
| 14 | import type { Source } from "../src/data/sources.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | ||
| 19 | describe("repo_scope", () => { | |
| 20 | let dataDir: string; | |
| 21 | let app: App; | |
| 22 | const user: Record<string, User> = {}; | |
| 23 | let notes: Repo; | |
| 24 | let other: Repo; | |
| 25 | let appId = ""; | |
| 26 | ||
| 27 | beforeAll(async () => { | |
| 28 | dataDir = await mkdtemp(join(tmpdir(), "forge-repo-scope-")); | |
| 29 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 30 | for (const name of ["owner1", "alice", "bob", "carol"]) { | |
| 31 | const made = await createUser(app.db, { | |
| 32 | origin: "seed", trusted: true, username: name, | |
| 33 | email: `${name}@forge.local`, password: "a-good-passphrase", adminIfFirst: name === "owner1", | |
| 34 | }); | |
| 35 | if (!made.ok) throw new Error(made.error); | |
| 36 | user[name] = made.value; | |
| 37 | } | |
| 38 | const a = await createRepo(app.db, app.reposRoot, { ownerId: user["alice"]!.id, name: "notes", visibility: "public" }); | |
| 39 | const b = await createRepo(app.db, app.reposRoot, { ownerId: user["carol"]!.id, name: "secret", visibility: "private" }); | |
| 40 | if (!a.ok || !b.ok) throw new Error("repo"); | |
| 41 | notes = a.value; | |
| 42 | other = b.value; | |
| 43 | const rec = createAppRecord(app.db, { repoId: notes.id, slug: "notes", access: "public" }); | |
| 44 | const rec2 = createAppRecord(app.db, { repoId: other.id, slug: "secret", access: "private" }); | |
| 45 | if (!rec.ok || !rec2.ok) throw new Error("app"); | |
| 46 | appId = rec.value.id; | |
| 47 | for (const [id, table] of [[appId, "users"], [rec2.value.id, "vault"]] as const) { | |
| 48 | mkdirSync(envDataDir(dataDir, id, "production"), { recursive: true }); | |
| 49 | const d = new Database(join(envDataDir(dataDir, id, "production"), "app.db")); | |
| 50 | d.exec(`CREATE TABLE ${table} (id INTEGER PRIMARY KEY, name TEXT)`); | |
| 51 | d.exec(`INSERT INTO ${table} VALUES (1, 'ann')`); | |
| 52 | d.close(); | |
| 53 | } | |
| 54 | }, 60_000); | |
| 55 | ||
| 56 | afterAll(async () => { | |
| 57 | app.close(); | |
| 58 | await rm(dataDir, { recursive: true, force: true }); | |
| 59 | }); | |
| 60 | ||
| 61 | test("repoSources is undefined for a viewer who cannot write the repository", () => { | |
| 62 | expect(repoSources(app.db, dataDir, undefined, notes)).toBeUndefined(); | |
| 63 | expect(repoSources(app.db, dataDir, user["bob"], notes)).toBeUndefined(); | |
| 64 | expect(repoSources(app.db, dataDir, user["bob"], other)).toBeUndefined(); | |
| 65 | }); | |
| 66 | ||
| 67 | test("a writer sees only their repository's sources, never another's", () => { | |
| 68 | const mine = repoSources(app.db, dataDir, user["alice"], notes)!; | |
| 69 | expect(mine.map((s) => s.id)).toEqual([`app:${appId}:production:app.db`]); | |
| 70 | // Carol writes `secret`; asked about `notes` she is a stranger. | |
| 71 | expect(repoSources(app.db, dataDir, user["carol"], notes)).toBeUndefined(); | |
| 72 | const hers = repoSources(app.db, dataDir, user["carol"], other)!; | |
| 73 | expect(hers.map((s) => s.id)).not.toContain(`app:${appId}:production:app.db`); | |
| 74 | expect(hers).toHaveLength(1); | |
| 75 | // An admin has the forge's own database in listSources, but not through a repository door. | |
| 76 | const admin = repoSources(app.db, dataDir, user["owner1"], notes); | |
| 77 | expect((admin ?? []).every((s) => s.kind === "app" && s.id.startsWith(`app:${appId}:`))).toBe(true); | |
| 78 | }); | |
| 79 | ||
| 80 | test("pickSource answers not_found for an unknown source and invalid for a bad name", () => { | |
| 81 | const sources = repoSources(app.db, dataDir, user["alice"], notes)!; | |
| 82 | const ok = pickSource(sources, { env: "production", file: "app.db" }); | |
| 83 | expect(isFailure(ok)).toBe(false); | |
| 84 | expect((ok as Source).id).toBe(`app:${appId}:production:app.db`); | |
| 85 | expect(pickSource(sources, { env: "production", file: "nope.db" })).toMatchObject({ error: "not_found" }); | |
| 86 | expect(pickSource([], { env: "production", file: "app.db" })).toMatchObject({ error: "not_found" }); | |
| 87 | expect(pickSource(sources, { env: "../x", file: "app.db" })).toMatchObject({ error: "invalid" }); | |
| 88 | expect(pickSource(sources, { env: "production" })).toMatchObject({ error: "invalid" }); | |
| 89 | expect(pickSource(sources, { file: "app.db" })).toMatchObject({ error: "invalid" }); | |
| 90 | }); | |
| 91 | ||
| 92 | test("sourceList names env and file, not the id", () => { | |
| 93 | const sources = repoSources(app.db, dataDir, user["alice"], notes)!; | |
| 94 | expect(sourceList(sources)).toEqual([{ env: "production", file: "app.db", label: "notes · production · app.db" }]); | |
| 95 | }); | |
| 96 | ||
| 97 | test("rowPage clamps a bad page and refuses an unknown table", () => { | |
| 98 | const source = repoSources(app.db, dataDir, user["alice"], notes)![0]!; | |
| 99 | expect(rowPage(source, "users", undefined)?.page).toBe(0); | |
| 100 | expect(rowPage(source, "users", -5)?.page).toBe(0); | |
| 101 | expect(rowPage(source, "users", Number.NaN)?.page).toBe(0); | |
| 102 | expect(rowPage(source, "users", 999_999)?.page).toBe(1000); | |
| 103 | expect(rowPage(source, "users", 0)?.rows).toEqual([[1, "ann"]]); | |
| 104 | expect(rowPage(source, "vault", 0)).toBeUndefined(); | |
| 105 | }); | |
| 106 | ||
| 107 | test("parsePage takes a whole number from 0 to 1000 and nothing else", () => { | |
| 108 | expect(parsePage("0")).toBe(0); | |
| 109 | expect(parsePage("1000")).toBe(1000); | |
| 110 | expect(parsePage(7)).toBe(7); | |
| 111 | for (const bad of ["1001", "-1", "1.5", "", "abc", "00000", 1001, -1, 1.5, null, undefined, {}]) expect(parsePage(bad)).toBeUndefined(); | |
| 112 | }); | |
| 113 | }); |
test/render-binary-video.test.ts added +72
| @@ -0,0 +1,72 @@ | ||
| 1 | /** The binary-facts view and the video player, drawn from small heads. */ | |
| 2 | import { describe, expect, test } from "bun:test"; | |
| 3 | import { binaryView } from "../src/web/render/binary-view.ts"; | |
| 4 | import { video } from "../src/web/render/video.ts"; | |
| 5 | import type { RenderInput } from "../src/web/render/renderers.ts"; | |
| 6 | ||
| 7 | const ascii = (s: string) => new TextEncoder().encode(s); | |
| 8 | const input = (path: string, body: Uint8Array, size = body.length): RenderInput => ({ | |
| 9 | path, size, body, media: "/o/n/media/main/" + path, download: "/o/n/raw/main/" + path + "?download=1", | |
| 10 | }); | |
| 11 | ||
| 12 | describe("video", () => { | |
| 13 | const head = new Uint8Array([0, 0, 0, 0x18, ...ascii("ftypmp42"), 0, 0, 0, 0, ...ascii("mp42isom")]); | |
| 14 | ||
| 15 | test("a small mp4 draws a player with its container and size", () => { | |
| 16 | const out = video(input("clips/a.mp4", head, 2048)); | |
| 17 | const text = String(out.content); | |
| 18 | expect(text).toContain('<video class="media-player" controls playsinline preload="metadata">'); | |
| 19 | expect(text).toContain('type="video/mp4"'); | |
| 20 | expect(text).toContain('src="/o/n/media/main/clips/a.mp4"'); | |
| 21 | expect(text).toContain("?download=1"); | |
| 22 | expect(out.summary).toBe("2.0 KB · MPEG-4"); | |
| 23 | expect(out.lines).toBe(0); | |
| 24 | }); | |
| 25 | ||
| 26 | test("a QuickTime file offers both types", () => { | |
| 27 | const text = String(video(input("a.mov", head)).content); | |
| 28 | expect(text).toContain('type="video/quicktime"'); | |
| 29 | expect(text).toContain('type="video/mp4"'); | |
| 30 | }); | |
| 31 | ||
| 32 | test("a hostile name is escaped", () => { | |
| 33 | const name = `"><script>alert(1)</script>.mp4`; | |
| 34 | const out = video({ ...input(name, head), media: `/m/${name}` }); | |
| 35 | const text = String(out.content); | |
| 36 | expect(text).not.toContain("<script>"); | |
| 37 | expect(text).toContain("<script>"); | |
| 38 | }); | |
| 39 | }); | |
| 40 | ||
| 41 | describe("binaryView", () => { | |
| 42 | const dwg = ascii("AC1032" + "\0".repeat(20)); | |
| 43 | ||
| 44 | test("a known binary draws its facts and a download", () => { | |
| 45 | const out = binaryView(input("plans/house.dwg", dwg, 4096)); | |
| 46 | const text = String(out.content); | |
| 47 | expect(text).toContain("AutoCAD drawing"); | |
| 48 | expect(text).toContain("AutoCAD 2018 (AC1032)"); | |
| 49 | expect(text).toContain('href="/o/n/raw/main/plans/house.dwg?download=1"'); | |
| 50 | expect(out.summary).toContain("AutoCAD drawing AutoCAD 2018"); | |
| 51 | expect(out.fallback).toBeUndefined(); | |
| 52 | }); | |
| 53 | ||
| 54 | test("a binary whose magic is wrong falls back to plain binary", () => { | |
| 55 | const out = binaryView(input("fake.dwg", ascii("not a drawing at all"))); | |
| 56 | expect(out.fallback).toBe("not a readable dwg file"); | |
| 57 | expect(String(out.content)).toBe(""); | |
| 58 | }); | |
| 59 | ||
| 60 | test("a hostile file name is escaped", () => { | |
| 61 | const out = binaryView(input(`d/<img src=x onerror=alert(1)>.dwg`, dwg)); | |
| 62 | const text = String(out.content); | |
| 63 | expect(text).not.toContain("<img"); | |
| 64 | expect(text).toContain("<img src=x onerror=alert(1)>.dwg facts"); | |
| 65 | }); | |
| 66 | ||
| 67 | test("a hostile download address is escaped", () => { | |
| 68 | const out = binaryView({ ...input("a.blend", ascii("BLENDER-v404")), download: `/x"><script>1</script>` }); | |
| 69 | expect(String(out.content)).not.toContain("<script>"); | |
| 70 | expect(String(out.content)).toContain("Blender file"); | |
| 71 | }); | |
| 72 | }); |
new above
offline