Fix bugs found in the audit
- People tiles resolve a bare photo filename to /uploads, via personPhoto - api.ts keeps the Accept header when a request sets its own headers - Feedback hashes IPs with auth.js's salt and client IP helper - Drop the better-sqlite3 fallback (not installed); require Node 22.13+ Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9d8de0176e
commit
693dd84b6e
6 changed files with 15 additions and 56 deletions
|
|
@ -4,7 +4,7 @@
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=22"
|
"node": ">=22.13"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "node --watch src/index.js",
|
"dev": "node --watch src/index.js",
|
||||||
|
|
|
||||||
|
|
@ -304,7 +304,7 @@ export function createRow(db, entity, payload) {
|
||||||
)
|
)
|
||||||
.run(...Object.values(values))
|
.run(...Object.values(values))
|
||||||
: db.prepare(`INSERT INTO ${entity.table} DEFAULT VALUES`).run();
|
: db.prepare(`INSERT INTO ${entity.table} DEFAULT VALUES`).run();
|
||||||
// better-sqlite3 and node:sqlite disagree about BigInt here.
|
// node:sqlite may hand back a BigInt here.
|
||||||
newId = Number(result.lastInsertRowid);
|
newId = Number(result.lastInsertRowid);
|
||||||
} else {
|
} else {
|
||||||
const names = [entity.idColumn, ...Object.keys(values)];
|
const names = [entity.idColumn, ...Object.keys(values)];
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,12 @@
|
||||||
DATABASE
|
DATABASE
|
||||||
|
|
||||||
One SQLite file, opened once at boot and held for the life of
|
One SQLite file, opened once at boot and held for the life of
|
||||||
the process. Node's own sqlite module is used when it's there
|
the process, through Node's own node:sqlite (Node 22.13+).
|
||||||
(Node 24+), better-sqlite3 otherwise. Their APIs overlap enough
|
House rules:
|
||||||
that everything below works against either, as long as you:
|
|
||||||
|
|
||||||
• use positional ? parameters, never named ones
|
• use positional ? parameters, never named ones
|
||||||
• pass 0/1 for booleans, never true/false
|
• pass 0/1 for booleans, never true/false
|
||||||
• use tx() rather than db.transaction()
|
• use tx(): node:sqlite has no db.transaction()
|
||||||
|
|
||||||
Those three rules are the whole compatibility story.
|
|
||||||
═══════════════════════════════════════════════════════════════ */
|
═══════════════════════════════════════════════════════════════ */
|
||||||
|
|
||||||
import { readdirSync, readFileSync } from "node:fs";
|
import { readdirSync, readFileSync } from "node:fs";
|
||||||
|
|
@ -20,18 +17,6 @@ import { fileURLToPath } from "node:url";
|
||||||
const HERE = dirname(fileURLToPath(import.meta.url));
|
const HERE = dirname(fileURLToPath(import.meta.url));
|
||||||
const MIGRATIONS_DIR = join(HERE, "migrations");
|
const MIGRATIONS_DIR = join(HERE, "migrations");
|
||||||
|
|
||||||
/* ── Driver selection ──────────────────────────────────────── */
|
|
||||||
|
|
||||||
async function loadDriver() {
|
|
||||||
try {
|
|
||||||
const { DatabaseSync } = await import("node:sqlite");
|
|
||||||
return { Driver: DatabaseSync, name: "node:sqlite" };
|
|
||||||
} catch {
|
|
||||||
const { default: BetterSqlite3 } = await import("better-sqlite3");
|
|
||||||
return { Driver: BetterSqlite3, name: "better-sqlite3" };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ── Open ──────────────────────────────────────────────────────
|
/* ── Open ──────────────────────────────────────────────────────
|
||||||
WAL readers never block the writer, which matters the
|
WAL readers never block the writer, which matters the
|
||||||
moment a feedback POST lands mid page-load
|
moment a feedback POST lands mid page-load
|
||||||
|
|
@ -42,15 +27,15 @@ async function loadDriver() {
|
||||||
───────────────────────────────────────────────────────────── */
|
───────────────────────────────────────────────────────────── */
|
||||||
|
|
||||||
export async function openDatabase(path) {
|
export async function openDatabase(path) {
|
||||||
const { Driver, name } = await loadDriver();
|
const { DatabaseSync } = await import("node:sqlite");
|
||||||
const db = new Driver(path);
|
const db = new DatabaseSync(path);
|
||||||
|
|
||||||
db.exec("PRAGMA journal_mode = WAL");
|
db.exec("PRAGMA journal_mode = WAL");
|
||||||
db.exec("PRAGMA foreign_keys = ON");
|
db.exec("PRAGMA foreign_keys = ON");
|
||||||
db.exec("PRAGMA synchronous = NORMAL");
|
db.exec("PRAGMA synchronous = NORMAL");
|
||||||
db.exec("PRAGMA busy_timeout = 5000");
|
db.exec("PRAGMA busy_timeout = 5000");
|
||||||
|
|
||||||
db.driverName = name;
|
db.driverName = "node:sqlite";
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,12 @@
|
||||||
length caps rejected before anything touches the database
|
length caps rejected before anything touches the database
|
||||||
no HTML stored verbatim, escaped at render time
|
no HTML stored verbatim, escaped at render time
|
||||||
|
|
||||||
The write is a single INSERT with positional parameters, so it
|
The write is a single INSERT. No transaction: one statement is
|
||||||
works on node:sqlite and better-sqlite3 alike. No transaction:
|
already atomic.
|
||||||
one statement is already atomic.
|
|
||||||
═══════════════════════════════════════════════════════════════ */
|
═══════════════════════════════════════════════════════════════ */
|
||||||
|
|
||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { createHash, randomBytes } from "node:crypto";
|
import { clientIp, hashIp } from "../auth.js";
|
||||||
|
|
||||||
const feedback = new Hono();
|
const feedback = new Hono();
|
||||||
|
|
||||||
|
|
@ -35,25 +34,6 @@ const MIN_MESSAGE = 10;
|
||||||
// a renamed option shouldn't lose someone's submission.
|
// a renamed option shouldn't lose someone's submission.
|
||||||
const TYPES = ["broken", "confusing", "outdated", "request", "praise", "other"];
|
const TYPES = ["broken", "confusing", "outdated", "request", "praise", "other"];
|
||||||
|
|
||||||
/* ── IP hashing ────────────────────────────────────────────────
|
|
||||||
Stored so repeat abuse from one source is visible during
|
|
||||||
triage, hashed so the table never holds an address. Without
|
|
||||||
IP_SALT the salt is regenerated each boot, which makes hashes
|
|
||||||
incomparable across restarts — fine for dev, set it in
|
|
||||||
/etc/ngu/api.env for production.
|
|
||||||
───────────────────────────────────────────────────────────── */
|
|
||||||
|
|
||||||
const IP_SALT = process.env.IP_SALT ?? randomBytes(16).toString("hex");
|
|
||||||
|
|
||||||
if (!process.env.IP_SALT) {
|
|
||||||
console.warn("IP_SALT unset — feedback ip_hash values reset on restart");
|
|
||||||
}
|
|
||||||
|
|
||||||
function hashIp(ip) {
|
|
||||||
if (!ip) return null;
|
|
||||||
return createHash("sha256").update(`${IP_SALT}:${ip}`).digest("hex").slice(0, 32);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ── Input cleaning ──────────────────────────────────────────── */
|
/* ── Input cleaning ──────────────────────────────────────────── */
|
||||||
|
|
||||||
function clean(value, max) {
|
function clean(value, max) {
|
||||||
|
|
@ -118,7 +98,7 @@ feedback.post("/", async (c) => {
|
||||||
pagePath || null,
|
pagePath || null,
|
||||||
sectionId || null,
|
sectionId || null,
|
||||||
clean(c.req.header("user-agent"), 500) || null,
|
clean(c.req.header("user-agent"), 500) || null,
|
||||||
hashIp(c.req.header("x-forwarded-for")?.split(",")[0].trim()),
|
hashIp(clientIp(c)),
|
||||||
);
|
);
|
||||||
|
|
||||||
// created_at and status come from column defaults.
|
// created_at and status come from column defaults.
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import { Link } from "react-router-dom";
|
||||||
|
|
||||||
import { get } from "../lib/api.ts";
|
import { get } from "../lib/api.ts";
|
||||||
import { isBadId, personHref } from "../lib/hrefs.ts";
|
import { isBadId, personHref } from "../lib/hrefs.ts";
|
||||||
|
import { personPhoto } from "../lib/media.ts";
|
||||||
import "./PeopleTiles.css";
|
import "./PeopleTiles.css";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -547,7 +548,7 @@ function Tile({
|
||||||
const content = (
|
const content = (
|
||||||
<span className="pl__frame">
|
<span className="pl__frame">
|
||||||
<span className="pl__photo">
|
<span className="pl__photo">
|
||||||
<Photo src={photoSrc(person.photo)} name={person.name} />
|
<Photo src={personPhoto(person.photo)} name={person.name} />
|
||||||
</span>
|
</span>
|
||||||
<span className="pl__caption">
|
<span className="pl__caption">
|
||||||
<span className="pl__name">{person.name}</span>
|
<span className="pl__name">{person.name}</span>
|
||||||
|
|
@ -760,13 +761,6 @@ function hasDetails(person: Person): boolean {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Database rows carry a bare filename; a hand-written entry may
|
|
||||||
give a path or a full URL. Both should work. */
|
|
||||||
function photoSrc(photo?: string | null): string | null {
|
|
||||||
if (!photo) return null;
|
|
||||||
if (/^(https?:|\/|data:)/.test(photo)) return photo;
|
|
||||||
return `/people/${photo}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function initials(name = ""): string {
|
function initials(name = ""): string {
|
||||||
return name
|
return name
|
||||||
|
|
|
||||||
|
|
@ -38,8 +38,8 @@ export class ApiError extends Error {
|
||||||
|
|
||||||
async function request(path, options: RequestInit = {}) {
|
async function request(path, options: RequestInit = {}) {
|
||||||
const response = await fetch(`${BASE}${path}`, {
|
const response = await fetch(`${BASE}${path}`, {
|
||||||
headers: { Accept: "application/json", ...options.headers },
|
|
||||||
...options,
|
...options,
|
||||||
|
headers: { Accept: "application/json", ...options.headers },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.status === 204) return null;
|
if (response.status === 204) return null;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue