Address Cody review round 3: CODY-003 and CODY-006

CODY-003 Request identity is durable before submission. Storage results are
         explicit at the DevicePort boundary (storageSet resolves true only
         on the host's own timely confirmation; storageGet distinguishes
         unknown from empty). A request is not sent until the phone has
         confirmed storing its id; a timeout or refusal holds it back,
         unsent and retryable under the same id. Unreadable storage at
         start is not treated as unpaired; an unsaveable pairing is revoked.
CODY-006 The state directory is set to 0700 on every open, including when
         it already existed, and the bridge refuses to start with an
         operator-facing error if it cannot be. Deployment guide creates
         it with install -d -m 0700.

Checks: typecheck clean; npm test 194 passed; client build ok;
test:real-stt 11 passed; test:real-hermes 9 passed; test:simulator 6
passed; git diff --check clean.

Not reviewed. No physical-device, droplet or production evidence.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-18 20:29:05 -08:00
co-authored by Claude Fable 5.2
parent e1c02d5266
commit 1507cea077
19 changed files with 759 additions and 45 deletions
+35 -7
View File
@@ -1,5 +1,5 @@
import Database from 'better-sqlite3';
import { chmodSync, closeSync, existsSync, mkdirSync, openSync } from 'node:fs';
import { chmodSync, closeSync, existsSync, mkdirSync, openSync, statSync } from 'node:fs';
import { dirname } from 'node:path';
export type Db = Database.Database;
@@ -87,14 +87,42 @@ export function secureDbFiles(path: string): void {
for (const file of sidecars(path)) if (existsSync(file)) chmodSync(file, FILE_MODE);
}
/** The state directory could not be made private. The message is written for the operator. */
export class StateDirectoryError extends Error {
override name = 'StateDirectoryError';
}
/**
* Make the directory that holds the database owner-only (0700), whether it was just created
* or already existed, and confirm it. A directory that cannot be restricted (not ours, or on
* a filesystem without permissions) is refused: the bridge does not keep credentials where
* other users can list or replace files.
*/
export function secureStateDir(dir: string): void {
if (!existsSync(dir)) mkdirSync(dir, { recursive: true, mode: DIR_MODE });
const explain = (why: string) =>
new StateDirectoryError(
`The state directory ${dir} ${why}. The bridge keeps device credentials there and needs a ` +
`directory of its own, owned by the user it runs as, with mode 0700. ` +
`Create one (for example: install -d -m 0700 ${dir}) or point HERMEVEN_DB_PATH at one.`,
);
let info = statSync(dir);
if (!info.isDirectory()) throw explain('is not a directory');
if ((info.mode & 0o777) !== DIR_MODE) {
try {
chmodSync(dir, DIR_MODE);
} catch {
throw explain('is accessible to other users and could not be restricted');
}
info = statSync(dir);
if ((info.mode & 0o777) !== DIR_MODE) throw explain('did not keep mode 0700 after it was set');
}
}
export function openDb(path: string): Db {
if (path !== ':memory:') {
const dir = dirname(path);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true, mode: DIR_MODE });
// mkdir's mode is masked by the umask; state the result explicitly.
chmodSync(dir, DIR_MODE);
}
// Before anything is created inside it.
secureStateDir(dirname(path));
// Create the file ourselves, owner-only, BEFORE SQLite opens it. SQLite gives the WAL and
// SHM files the same permissions as the database file, so they are never world-readable,
// not even for a moment.
+4 -1
View File
@@ -1,5 +1,6 @@
import { LOG_REDACT, createBridge } from './app.ts';
import { ConfigError, loadConfig } from './config.ts';
import { StateDirectoryError } from './db.ts';
import { loadEnvFile } from './env.ts';
import { BuiltinWhisperStt } from './stt/builtinWhisper.ts';
@@ -49,6 +50,8 @@ async function main(): Promise<void> {
main().catch((err: unknown) => {
// Configuration problems are operator-facing; never print the environment.
console.error(err instanceof ConfigError ? `Configuration error: ${err.message}` : err);
if (err instanceof ConfigError) console.error(`Configuration error: ${err.message}`);
else if (err instanceof StateDirectoryError) console.error(`Cannot start: ${err.message}`);
else console.error(err);
process.exit(1);
});