From 6040b60e8660fa50df709a1718211080aae7a589 Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 17 Nov 2026 23:10:18 +0100 Subject: [PATCH] fix(db): keep NUL only in the password fields TREK hashes, not every key naming a password nulFreeInput left a string as sent under any key containing "password". That covered the admin settings password_login and password_registration, which are flags: 'true\u0000' failed the lockout check as a disabled password login while the database stored 'true'. It also tested the raw key, so 'pass\u0000word' lost the NUL of the password it carried. The exemption is now HASHED_PASSWORD_KEYS, the fields TREK only ever hashes with bcrypt (password, current_password, new_password), matched exactly on the key without its NUL. A unit test reads every field of the shared schemas and the instance settings named like a password, passphrase or secret and fails until each is either in that list or decided against with its reason. --- server/CLAUDE.md | 2 +- server/src/db/nul-free.ts | 38 +++++++--- server/tests/unit/db/nul-free.test.ts | 101 +++++++++++++++++++++++++- 3 files changed, 125 insertions(+), 16 deletions(-) diff --git a/server/CLAUDE.md b/server/CLAUDE.md index b41a2a88a..9bd088b58 100644 --- a/server/CLAUDE.md +++ b/server/CLAUDE.md @@ -109,7 +109,7 @@ SQLite is the default engine and Postgres the second (`TREK_DB_URL`, see Engines - **The new id**: `.returning('id').executeTakeFirstOrThrow()`. Never `InsertResult.insertId` or `last_insert_rowid()`; both are SQLite's rowid and undefined on Postgres. - **Time**: `currentTimestamp`/`currentTimestampKysely`, `nowMinusDays`, `nowMinusHours`, `nowPlusSeconds`/`nowPlusSecondsKysely`, `nowDateOffset`, `dateOf`, `dateAdd`, `unixEpochToIsoKysely` from `src/db/dialect/sql-functions.ts` (the MikroORM `raw()` helpers) and `src/db/dialect/kysely-functions.ts` (their Kysely-expression twins). Never `datetime('now')`, `strftime` or `julianday`. - **Search that ignores case**: `ilike(platform, refs)` builds the `(a LIKE ? ESCAPE '\' OR …)` condition, `ILIKE` on Postgres, where `LIKE` is exact. A bare `LIKE` is for a prefix that is exact on both engines (a settings key, a storage path), and says so in a `// like-exact: ` comment (`lint:dialect`). -- **No NUL in text**: Postgres text cannot hold U+0000, so TREK stores it on neither engine. `src/db/nul-free.ts` drops it where every value leaves for the database (both MikroORM platforms, both Kysely dialects, the move from SQLite), filters included, so the same request leaves the same row on either engine. A client's input loses it before any logic runs (`src/nest/common/nul-free-input.ts`: the `nulFreeRequest` middleware for the path, query, body and cookies, the first global pipe for a multipart body, the websocket adapter for every frame, `trekMcpInputMapper` for MCP arguments), so a throttle key, a pre-check or a comparison sees the value the database stores; a string under a key naming a password is left as sent, since it is only ever hashed. New code never strips NUL itself. A plugin's own data is the plugin's: its SQLite file binds a NUL exactly, and its Postgres schema refuses one with an error (`plugin-data-checks.ts`), so its keys never change meaning. +- **No NUL in text**: Postgres text cannot hold U+0000, so TREK stores it on neither engine. `src/db/nul-free.ts` drops it where every value leaves for the database (both MikroORM platforms, both Kysely dialects, the move from SQLite), filters included, so the same request leaves the same row on either engine. A client's input loses it before any logic runs (`src/nest/common/nul-free-input.ts`: the `nulFreeRequest` middleware for the path, query, body and cookies, the first global pipe for a multipart body, the websocket adapter for every frame, `trekMcpInputMapper` for MCP arguments), so a throttle key, a pre-check or a comparison sees the value the database stores; a password TREK only ever hashes (the keys in `HASHED_PASSWORD_KEYS`, matched exactly; `password_login` is a flag and is cleaned) is left as sent, and `tests/unit/db/nul-free.test.ts` fails on a new field of the shared schemas or the settings named like a secret until it is decided either way. New code never strips NUL itself. A plugin's own data is the plugin's: its SQLite file binds a NUL exactly, and its Postgres schema refuses one with an error (`plugin-data-checks.ts`), so its keys never change meaning. - **Dates bound as dates**: SQLite rolls an impossible day (`2024-02-30`) over into the next month where Postgres refuses to cast it; a helper that binds a value either engine reads as a date (`dayDistance`) refuses anything `isCalendarDate` does not accept, on both engines, and its caller checks first. - **Strings and casts**: `concat`/`concatKysely` instead of `||`, `substring`/`substringKysely`, `lower`/`lowerTrim`, `collateNoCase`, `castInteger`/`castIntegerKysely`, and `startsWithIsoDate`/`startsWithIsoDateKysely` instead of `GLOB`. - **JSON columns** are read whole and parsed in the service; no `json_extract` in SQL. Each one is declared once in `src/db/json-columns.ts` (its Zod shape and the fallback a reader gets for an empty or broken row) and read with `decodeJson`/`decodeJsonResult`, written from a server-built value with `encodeJson` (`src/utils/json-column.ts`), never with a bare `JSON.parse` at the call site: one column, one answer to a broken row, logged. diff --git a/server/src/db/nul-free.ts b/server/src/db/nul-free.ts index f7442197f..25d898627 100644 --- a/server/src/db/nul-free.ts +++ b/server/src/db/nul-free.ts @@ -65,8 +65,23 @@ export function nulFreeJson(value: unknown): unknown { return value; } -/** A key whose string value is a password: hashed or handed on as sent, never stored or compared as text. */ -const PASSWORD_KEY = /password/i; +/** + * The keys whose string value is a password TREK only ever hashes with bcrypt + * or compares with a bcrypt hash: signup and login (`password`), the password + * change (`current_password`, `new_password`), the reset (`new_password`), + * an admin creating or editing an account (`password`), and the re-entry + * that guards MFA and passkey changes (`password`). Matched exactly, on the + * key without its NUL characters, at any depth, so the `password` of a + * document connection's `credentials` stays as sent too; that one is stored + * only encrypted, never as text. Every other key loses its NUL like any + * other input, however it is named: the settings + * `password_login` and `password_registration` are flags, a Synology + * password or an OIDC client secret is stored encrypted and handed to + * another server. `tests/unit/db/nul-free.test.ts` fails on a field of the + * shared schemas named like a secret that is neither listed here nor + * decided against in its list there. + */ +export const HASHED_PASSWORD_KEYS: ReadonlySet = new Set(['password', 'current_password', 'new_password']); /** * A request's input as the application handles it: every string in it, object @@ -77,12 +92,12 @@ const PASSWORD_KEY = /password/i; * stores and filters by. Without it `victim@x.test\u0000` was a new key for * the per-email reset throttle while the lookup found the victim's row. * - * A string under a key that names a password is left exactly as sent. A - * password never reaches the database as text, only as its bcrypt hash, so - * the stores above never meet it, and bcryptjs hashes a NUL like any other - * byte. Stripping it here would change the hash input and lock out an account - * whose password was set with a NUL before this rule existed; left alone, - * signup, login and a password change all hash the same string. + * A string under one of {@link HASHED_PASSWORD_KEYS} is left exactly as + * sent. Such a password never reaches the database as text, only as its + * bcrypt hash, so the stores above never meet it, and bcryptjs hashes a NUL + * like any other byte. Stripping it here would change the hash input and lock + * out an account whose password was set with a NUL before this rule existed; + * left alone, signup, login and a password change all hash the same string. * * Returns the same value, by identity, when it holds no NUL. Arrays and plain * objects (null-prototype ones too, the shape a query or multipart parser @@ -102,9 +117,10 @@ function nulFreeInputValue(value: unknown, password: boolean): unknown { const prototype: unknown = Object.getPrototypeOf(value); if (prototype !== Object.prototype && prototype !== null) return value; const entries = Object.entries(value as Record); - const cleaned = entries.map( - ([key, item]) => [stripNul(key), nulFreeInputValue(item, PASSWORD_KEY.test(key))] as const, - ); + const cleaned = entries.map(([key, item]) => { + const name = stripNul(key); + return [name, nulFreeInputValue(item, HASHED_PASSWORD_KEYS.has(name))] as const; + }); if (cleaned.every(([key, item], i) => key === entries[i][0] && item === entries[i][1])) return value; // Defined, not assigned, so a key named `__proto__` stays a plain property. const copy = Object.create(prototype as object | null) as Record; diff --git a/server/tests/unit/db/nul-free.test.ts b/server/tests/unit/db/nul-free.test.ts index ab61b2c56..74c57a779 100644 --- a/server/tests/unit/db/nul-free.test.ts +++ b/server/tests/unit/db/nul-free.test.ts @@ -5,7 +5,14 @@ * whole app on SQLite and on Postgres. */ import { TrekPostgreSqlPlatform } from '../../../src/db/engine/postgres/postgres-driver'; -import { nulFreeInput, nulFreeJson, nulFreeParameter, stripNul, withNulFreeParameters } from '../../../src/db/nul-free'; +import { + HASHED_PASSWORD_KEYS, + nulFreeInput, + nulFreeJson, + nulFreeParameter, + stripNul, + withNulFreeParameters, +} from '../../../src/db/nul-free'; import { type AbortableOperationOptions, @@ -18,6 +25,8 @@ import { SqliteIntrospector, SqliteQueryCompiler, } from 'kysely'; +import fs from 'node:fs'; +import path from 'node:path'; import { describe, expect, it } from 'vitest'; const RELEASE = Symbol('release'); @@ -162,23 +171,45 @@ describe('nulFreeInput: a request value as the application handles it', () => { expect(nulFreeInput(list)).toBe(list); }); - it('leaves a password exactly as sent, under any key that names one', () => { + it('leaves a password TREK only hashes exactly as sent, at any depth', () => { const body = { email: 'a@b.c\u0000', password: 'pa\u0000ss', new_password: 'n\u0000ew', current_password: 'c\u0000ur', - user: { synology_password: 's\u0000y' }, + credentials: { password: 'na\u0000s' }, }; expect(nulFreeInput(body)).toEqual({ email: 'a@b.c', password: 'pa\u0000ss', new_password: 'n\u0000ew', current_password: 'c\u0000ur', - user: { synology_password: 's\u0000y' }, + credentials: { password: 'na\u0000s' }, }); }); + it('cleans every other key that only mentions a password, so a check sees the value the database stores', () => { + expect( + nulFreeInput({ + password_login: 'true\u0000', + password_registration: 'false\u0000', + password_hint: 'h\u0000int', + synology_password: 's\u0000y', + Password: 'P\u0000w', + }), + ).toEqual({ + password_login: 'true', + password_registration: 'false', + password_hint: 'hint', + synology_password: 'sy', + Password: 'Pw', + }); + }); + + it('decides on the key as the application reads it, without its NUL', () => { + expect(nulFreeInput({ 'pass\u0000word': 'pa\u0000ss' })).toEqual({ password: 'pa\u0000ss' }); + }); + it('still walks an object under a password key, only its strings stay as sent', () => { expect(nulFreeInput({ passwordReset: { note: 'x\u0000' } })).toEqual({ passwordReset: { note: 'x' } }); }); @@ -209,3 +240,65 @@ describe('nulFreeInput: a request value as the application handles it', () => { expect(nulFreeInput(file)).toEqual({ originalname: 'photo.jpg', buffer }); }); }); + +/** + * Every field of the shared schemas and every instance setting whose name + * looks like a secret, with the reason it is not one of + * {@link HASHED_PASSWORD_KEYS}: it loses its NUL like any other input. + */ +const NOT_HASHED: Record = { + password_login: 'an instance setting holding the flag true or false', + password_registration: 'an instance setting holding the flag true or false', + synology_password: 'stored encrypted and sent to the Synology API, like every other provider credential', + passphrase: "a Synology shared album's passphrase, stored and sent to the Synology API", + client_secret: "an OAuth client's or the OIDC provider's secret, stored or compared as text", + oidc_client_secret: 'stored encrypted and sent to the OIDC provider', + mail_ingest_webhook_secret: 'compared with the secret a mail webhook presents', + secret: "a plugin setting's flag that marks a field as secret", +}; + +/** The object keys named like a secret in `files` and in the TypeScript under `dir` (specs and locales left out). */ +function secretLikeKeys(dir: string | null, files: string[] = []): Set { + const visit = (at: string) => { + for (const entry of fs.readdirSync(at, { withFileTypes: true })) { + const full = path.join(at, entry.name); + if (entry.isDirectory() && entry.name !== 'i18n') visit(full); + else if (entry.isFile() && entry.name.endsWith('.ts') && !entry.name.endsWith('.spec.ts')) files.push(full); + } + }; + if (dir !== null) visit(dir); + const keys = new Set(); + for (const file of files) { + for (const match of fs.readFileSync(file, 'utf8').matchAll(/^\s*['"]?([A-Za-z_]\w*)['"]?\??\s*:/gm)) { + if (/password|passphrase|secret/i.test(match[1])) keys.add(match[1]); + } + } + return keys; +} + +describe('which secrets keep a NUL: a decision for every field named like one', () => { + const server = path.join(__dirname, '..', '..', '..'); + const keys = new Set([ + ...secretLikeKeys(path.join(server, '..', 'shared', 'src')), + ...secretLikeKeys(null, [path.join(server, 'src', 'nest', 'common', 'app-settings.registry.ts')]), + ]); + + it('reads the fields of the shared schemas and the instance settings', () => { + expect(keys.has('new_password')).toBe(true); + expect(keys.has('password_login')).toBe(true); + }); + + it('has each one either hashed or decided against, so a new one joins neither rule unseen', () => { + const undecided = [...keys].filter((key) => !HASHED_PASSWORD_KEYS.has(key) && !(key in NOT_HASHED)); + expect( + undecided, + 'A field named like a secret has no decision. If TREK only ever hashes it with bcrypt, add it to ' + + 'HASHED_PASSWORD_KEYS in src/db/nul-free.ts; otherwise add it to NOT_HASHED here with the reason.', + ).toEqual([]); + }); + + it('keeps no decision for a field that is gone', () => { + const stale = [...HASHED_PASSWORD_KEYS, ...Object.keys(NOT_HASHED)].filter((key) => !keys.has(key)); + expect(stale).toEqual([]); + }); +});