Public: anyone can read this; the crew can change it.

Opening a .sqlite file in the browser never writes to it, proven by hashing the file before and after #13557

Merged opened this

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

Merged

scout/forge:scout/13544-opening-a-sqlite-file-in-the-browser-nev → main 2 commits fork 1 thread · 1 unresolved

  • Merged. scout/13544-opening-a-sqlite-file-in-the-browser-nev was merged into main as 4645ca9.

Review

Sign in to review this change.
2 files changed +84 −3
src/web/render/sqlite.ts +18 −3
@@ -28,11 +28,26 @@ interface Entry {
2828
2929 const plural = (n: number, one: string): string => `${n.toLocaleString()} ${one}${n === 1 ? "" : "s"}`;
3030
31 /**
32 * The blob opened read-only in memory. A file saved in WAL mode says so in header bytes 18 and 19
33 * (2), and an in-memory copy has no -wal to open, so it would refuse to open; those two bytes are
34 * set to 1 (rollback journal) on a copy, which reads the same pages and writes nothing.
35 */
36 function open(body: Uint8Array): Database {
37 let bytes = body;
38 if (body.length >= 20 && (body[18] === 2 || body[19] === 2)) {
39 bytes = body.slice();
40 bytes[18] = 1;
41 bytes[19] = 1;
42 }
43 return Database.deserialize(bytes, { readonly: true });
44 }
45
3146 /** True when `name` is a table or view of this database (cheap; used before a page is kept). */
3247 export function hasTable(body: Uint8Array, name: string, page = 1): boolean {
3348 let db: Database | undefined;
3449 try {
35 db = Database.deserialize(body, { readonly: true });
50 db = open(body);
3651 const found = db.query("SELECT type FROM sqlite_master WHERE type IN ('table','view') AND name = ? AND name NOT LIKE 'sqlite\\_%' ESCAPE '\\'").get(name) as { type: string } | null;
3752 if (found === null) return false;
3853 if (page <= 1) return true;
@@ -133,7 +148,7 @@ export interface TablePage {
133148 export function tablePage(body: Uint8Array, index: number, page: number): TablePage | string {
134149 let db: Database | undefined;
135150 try {
136 db = Database.deserialize(body, { readonly: true });
151 db = open(body);
137152 const list = entries(db);
138153 if (list.length === 0) return "This database has no tables.";
139154 const at = Math.min(list.length - 1, Math.max(0, Number.isSafeInteger(index) ? index : 0));
@@ -160,7 +175,7 @@ export function tablePage(body: Uint8Array, index: number, page: number): TableP
160175 export function sqlite(input: RenderInput): Rendered {
161176 let db: Database | undefined;
162177 try {
163 db = Database.deserialize(input.body, { readonly: true });
178 db = open(input.body);
164179 const list = entries(db);
165180 const href = input.pathHref ?? "";
166181 const wanted = input.table;
test/sqlite-readonly.test.ts added +66
@@ -0,0 +1,66 @@
1 /**
2 * Reading a SQLite file is harmless: its bytes are the same after, and nothing appears beside it.
3 */
4 import { Database } from "bun:sqlite";
5 import { describe, expect, test } from "bun:test";
6 import { mkdtempSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { sqlite, tablePage, hasTable } from "../src/web/render/sqlite.ts";
10
11 const sha = (b: Uint8Array): string => new Bun.CryptoHasher("sha256").update(b).digest("hex");
12
13 /**
14 * A WAL-mode database file, checkpointed so it is one file: the kind most likely to grow a -wal
15 * when opened.
16 */
17 function fixture(dir: string): string {
18 const path = join(dir, "fixture.sqlite");
19 const db = new Database(path);
20 db.run("PRAGMA journal_mode = WAL");
21 db.run("CREATE TABLE t (id INTEGER PRIMARY KEY, v TEXT)");
22 for (let i = 0; i < 300; i++) db.run("INSERT INTO t (v) VALUES (?)", [`row ${i}`]);
23 db.run("PRAGMA wal_checkpoint(TRUNCATE)");
24 db.close();
25 return path;
26 }
27
28 const draw = (body: Uint8Array, table?: string, page?: number) =>
29 sqlite({ path: "a.sqlite", size: body.length, body, media: "/m", download: "/d", pathHref: "/r/a.sqlite", ...(table ? { table } : {}), ...(page ? { page } : {}) });
30
31 describe("opening a sqlite file never writes to it", () => {
32 test("the file's bytes are unchanged and no -wal or -journal appears", () => {
33 const dir = mkdtempSync(join(tmpdir(), "sqlite-ro-"));
34 const path = fixture(dir);
35 const before = sha(readFileSync(path));
36 const names = readdirSync(dir).sort();
37 expect(names).toEqual(["fixture.sqlite"]);
38 const body = readFileSync(path);
39 expect(String(draw(body).content)).toContain("300 rows");
40 draw(body, "t", 2);
41 tablePage(body, 0, 3);
42 expect(hasTable(body, "t", 2)).toBe(true);
43 expect(sha(readFileSync(path))).toBe(before);
44 expect(readdirSync(dir).sort()).toEqual(names);
45 });
46
47 test("every open of the blob is read-only, so a write through it fails", () => {
48 const src = readFileSync(join(import.meta.dir, "../src/web/render/sqlite.ts"), "utf8");
49 const opens = src.match(/Database\.deserialize\([^)]*\)/g) ?? [];

Source-regex test: it passes while the helper open() could drop readonly. Replace with a behavioural check.

50 expect(opens.length).toBeGreaterThan(0);
51 for (const o of opens) expect(o).toContain("readonly: true");
52 const db = Database.deserialize(readFileSync(fixture(mkdtempSync(join(tmpdir(), "sqlite-ro-")))), { readonly: true });
53 expect(() => db.run("INSERT INTO t (v) VALUES ('x')")).toThrow();
54 db.close();
55 });
56
57 test("a malformed file shows one plain sentence, not a stack", () => {
58 const dir = mkdtempSync(join(tmpdir(), "sqlite-bad-"));
59 writeFileSync(join(dir, "bad.sqlite"), "SQLite format 3\0 this is not a database at all");
60 const body = readFileSync(join(dir, "bad.sqlite"));
61 const r = draw(body);
62 expect(r.fallback).toBe("This SQLite file is cut short or damaged, so its tables cannot be read.");
63 expect(String(r.content)).not.toMatch(/Error|at \S+\.ts/);
64 expect(readdirSync(dir)).toEqual(["bad.sqlite"]);
65 });
66 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs