mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-25 06:36:49 +09:00
#4057: with encryption at rest on, `memory purge`'s sql.js fallback rewrote a plaintext native SQLite store as an RFE1 blob and still printed [OK]; the native bridge and every SQLite reader then rejected the file. Every whole-file read-modify-write (purge, delete, get access-count, store, schema migration, decay) now writes the image back in the format it read (plaintext -> plaintext, RFE1 -> RFE1) via writeDbImagePreservingFormat. applyTemporalDecay also read the raw bytes, so it failed on encrypted stores, and wrote plaintext: both fixed. #4056: `hooks post-command` stored every Bash command in full with no off switch. Add CLAUDE_FLOW_HOOKS_RECORD_COMMANDS=0 / CLAUDE_FLOW_HOOKS_ENABLED=false / hooks.recordCommands=false (nothing is stored when off), redact common secret shapes and cap stored text at 2000 chars (bounded patterns, linear on hostile input). `memory cleanup --expired-only` never saw expired rows because listEntries hides them (TTL is a read-time rule): listEntries gains includeExpired + expiresAt, and memory_cleanup uses it. ruflo-core 0.2.8: the Bash shim stands down when recording is off. Co-Authored-By: RuFlo <ruv@ruv.net>
253 lines
8.4 KiB
TypeScript
253 lines
8.4 KiB
TypeScript
/**
|
|
* Restricted-permission file helpers.
|
|
*
|
|
* audit_1776853149979: session/memory/terminal stores were written with the
|
|
* process umask, which on most macOS/Linux setups leaves them world-readable
|
|
* (mode 0644). They contain conversation snapshots, agent prompts, and
|
|
* terminal command history — anyone else on the host can read them.
|
|
*
|
|
* These helpers write atomically and force mode 0600 (files) / 0700 (dirs).
|
|
* chmod fails silently on Windows, where POSIX modes don't apply — that's
|
|
* fine, the OS-level ACL surface there is different.
|
|
*
|
|
* ADR-096 Phase 2: optional opt-in encryption-at-rest. When the caller
|
|
* passes `encrypt: true` AND the env-gated vault is enabled, payloads are
|
|
* AES-256-GCM-encrypted before hitting disk. Reads use the magic-byte
|
|
* sniff so legacy plaintext files keep working unchanged during the
|
|
* incremental migration.
|
|
*/
|
|
|
|
import {
|
|
chmodSync,
|
|
closeSync,
|
|
fsyncSync,
|
|
mkdirSync,
|
|
openSync,
|
|
readFileSync,
|
|
readSync,
|
|
renameSync,
|
|
unlinkSync,
|
|
writeSync,
|
|
} from 'node:fs';
|
|
import { basename, dirname, join } from 'node:path';
|
|
import {
|
|
decryptBuffer,
|
|
encryptBuffer,
|
|
getKey,
|
|
isEncryptedBlob,
|
|
isEncryptionEnabled,
|
|
MAGIC,
|
|
} from './encryption/vault.js';
|
|
|
|
/**
|
|
* Crash-safe atomic file write (issue #2584).
|
|
*
|
|
* A plain `writeFileSync(dbPath, db.export())` of a large sql.js image is NOT
|
|
* crash-safe: a kill/OOM mid-write — or a second process rewriting the same
|
|
* path concurrently — leaves a half-written image, and reopening it yields
|
|
* `database disk image is malformed`. The corruption window scales with file
|
|
* size, so a 185 MB monolithic flush is especially exposed.
|
|
*
|
|
* This writes to a unique temp sibling, fsyncs the bytes to disk, then
|
|
* `rename()`s over the target. rename() is atomic on POSIX within one
|
|
* filesystem, so a reader/reopen sees either the old complete image or the new
|
|
* complete image — never a torn one. The directory entry is best-effort fsynced
|
|
* so the rename itself survives a crash. On any failure the temp file is
|
|
* removed and the original is left untouched.
|
|
*/
|
|
export function writeFileAtomic(
|
|
path: string,
|
|
data: Buffer,
|
|
opts: { mode?: number } = {},
|
|
): void {
|
|
const mode = opts.mode ?? 0o600;
|
|
const dir = dirname(path);
|
|
const tmp = join(
|
|
dir,
|
|
`.${basename(path)}.tmp-${process.pid}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`,
|
|
);
|
|
let fd: number | undefined;
|
|
try {
|
|
fd = openSync(tmp, 'wx', mode);
|
|
if (data.length > 0) writeSync(fd, data, 0, data.length, 0);
|
|
fsyncSync(fd); // durability: force bytes to disk BEFORE the rename
|
|
closeSync(fd);
|
|
fd = undefined;
|
|
renameSync(tmp, path); // atomic swap — readers never see a torn image
|
|
try {
|
|
chmodSync(path, mode);
|
|
} catch {
|
|
// Windows / FS without POSIX modes — silently skip.
|
|
}
|
|
// Best-effort: fsync the directory so the rename survives a power loss.
|
|
try {
|
|
const dfd = openSync(dir, 'r');
|
|
try {
|
|
fsyncSync(dfd);
|
|
} finally {
|
|
closeSync(dfd);
|
|
}
|
|
} catch {
|
|
// Directory fsync unsupported (e.g. Windows) — the rename is still atomic.
|
|
}
|
|
} catch (e) {
|
|
if (fd !== undefined) {
|
|
try {
|
|
closeSync(fd);
|
|
} catch {
|
|
/* already closed */
|
|
}
|
|
}
|
|
try {
|
|
unlinkSync(tmp);
|
|
} catch {
|
|
/* temp never created / already gone */
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a directory tree with mode 0700 (owner-only). No-op if exists.
|
|
* Uses recursive: true so missing parents are created with the same mode.
|
|
*/
|
|
export function mkdirRestricted(path: string): void {
|
|
mkdirSync(path, { recursive: true, mode: 0o700 });
|
|
}
|
|
|
|
/**
|
|
* Options for writeFileRestricted. Object form so we can grow the API
|
|
* without churning every call site.
|
|
*/
|
|
export interface WriteOptions {
|
|
/** Buffer encoding when `data` is a string. Ignored for Buffer payloads. */
|
|
encoding?: BufferEncoding;
|
|
/**
|
|
* If true AND encryption is globally enabled (CLAUDE_FLOW_ENCRYPT_AT_REST),
|
|
* encrypt the payload with AES-256-GCM before writing. If encryption is
|
|
* NOT enabled, this flag is silently ignored — the legacy plaintext path
|
|
* runs unchanged. Default: false.
|
|
*/
|
|
encrypt?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Write a file and tighten its permissions to mode 0600 (owner read/write).
|
|
*
|
|
* Two call signatures, both supported (the legacy positional one keeps
|
|
* existing call sites working without churn):
|
|
*
|
|
* writeFileRestricted(path, data) // plaintext, utf-8
|
|
* writeFileRestricted(path, data, 'utf-8') // legacy: encoding only
|
|
* writeFileRestricted(path, data, { encrypt: true }) // opt-in encryption
|
|
*/
|
|
export function writeFileRestricted(
|
|
path: string,
|
|
data: string | Buffer,
|
|
optsOrEncoding: BufferEncoding | WriteOptions = 'utf-8',
|
|
): void {
|
|
const opts: WriteOptions =
|
|
typeof optsOrEncoding === 'string'
|
|
? { encoding: optsOrEncoding }
|
|
: optsOrEncoding;
|
|
const encoding = opts.encoding ?? 'utf-8';
|
|
|
|
let payload: string | Buffer = data;
|
|
if (opts.encrypt && isEncryptionEnabled()) {
|
|
const plaintext = Buffer.isBuffer(data) ? data : Buffer.from(data, encoding);
|
|
payload = encryptBuffer(plaintext, getKey());
|
|
}
|
|
|
|
// Atomic write (temp → fsync → rename) so a torn/concurrent flush can never
|
|
// leave a half-written file — the failure mode behind issue #2584. Buffers go
|
|
// straight through; strings are encoded first (default utf-8).
|
|
const buf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload, encoding);
|
|
writeFileAtomic(path, buf, { mode: 0o600 });
|
|
}
|
|
|
|
/**
|
|
* The at-rest format of an existing database file: `true` = RFE1-encrypted,
|
|
* `false` = plaintext, `null` = missing or unreadable (nothing to preserve).
|
|
* Reads four bytes only.
|
|
*/
|
|
export function existingFileIsEncrypted(path: string): boolean | null {
|
|
let fd: number | undefined;
|
|
try {
|
|
fd = openSync(path, 'r');
|
|
const head = Buffer.alloc(4);
|
|
const n = readSync(fd, head, 0, 4, 0);
|
|
if (n < 4) return null;
|
|
return head.equals(MAGIC);
|
|
} catch {
|
|
return null;
|
|
} finally {
|
|
if (fd !== undefined) {
|
|
try { closeSync(fd); } catch { /* best effort */ }
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Write a whole-file database image back in the format it was read in (#4057).
|
|
*
|
|
* A read-modify-write of a database image (sql.js loads the file, mutates it,
|
|
* exports it) must not change what the file IS. With encryption at rest on, a
|
|
* plain `{ encrypt: true }` write turned a plaintext native SQLite store into
|
|
* an RFE1 blob that the native bridge and every SQLite reader then rejected,
|
|
* while the command reported success. And the reverse, a plain
|
|
* `writeFileAtomic` of a decrypted image, silently downgraded an encrypted
|
|
* store to plaintext.
|
|
*
|
|
* existing file RFE1 -> RFE1 (encrypted)
|
|
* existing file plaintext -> plaintext
|
|
* no existing file -> `encrypt` (default true: the env gate decides)
|
|
* `encrypt: false` -> plaintext, always (explicit caller choice)
|
|
*/
|
|
export function writeDbImagePreservingFormat(
|
|
path: string,
|
|
data: Uint8Array | Buffer,
|
|
opts: { encrypt?: boolean } = {},
|
|
): void {
|
|
const requested = opts.encrypt ?? true;
|
|
const existing = existingFileIsEncrypted(path);
|
|
const encrypt = requested === false ? false : (existing ?? requested);
|
|
writeFileRestricted(path, Buffer.isBuffer(data) ? data : Buffer.from(data), { encrypt });
|
|
}
|
|
|
|
/**
|
|
* Read a file and transparently decrypt if it carries the RFE1 magic.
|
|
*
|
|
* Returns a string when the caller asks for one (default utf-8). Returns
|
|
* a Buffer when `encoding` is null. This matches Node's readFileSync
|
|
* shape so the function is a near-drop-in replacement.
|
|
*
|
|
* Migration semantics:
|
|
* - If the file IS encrypted, decrypt and return.
|
|
* - If the file is NOT encrypted, return its raw bytes (string-decoded
|
|
* under `encoding` if requested).
|
|
*
|
|
* That means a reader can be migrated *first*, before its writer flips
|
|
* `encrypt: true`, without breaking on the legacy plaintext path.
|
|
*/
|
|
export function readFileMaybeEncrypted(
|
|
path: string,
|
|
encoding?: BufferEncoding,
|
|
): string;
|
|
export function readFileMaybeEncrypted(
|
|
path: string,
|
|
encoding: null,
|
|
): Buffer;
|
|
export function readFileMaybeEncrypted(
|
|
path: string,
|
|
encoding: BufferEncoding | null = 'utf-8',
|
|
): string | Buffer {
|
|
const raw = readFileSync(path);
|
|
let plain: Buffer;
|
|
if (isEncryptedBlob(raw)) {
|
|
plain = decryptBuffer(raw, getKey());
|
|
} else {
|
|
plain = raw;
|
|
}
|
|
return encoding === null ? plain : plain.toString(encoding);
|
|
}
|