Files
jason.woltje 930182b54a fix(organization): accounts config directory checks every ancestor (#1873)
assertAccountsOwner refused a symlink or non-directory on the config path,
but checked owner and mode only on the leaf. An ancestor owned by another
user, or writable by others without the sticky bit, let that user rename the
directory away and put their own accounts.json in its place. Since M3 that
file decides root authority on a deployed Console.

One shared helper, config-dir-trust.mjs, now holds A2's rule: every ancestor
belongs to root or this uid and is group or other writable only with the
sticky bit; configDir itself is this uid's and not group or other writable.
The accounts store (load, init, lock, locked write, mosaic accounts) and the
root agent file's writer both call it, each with its own refusal code. A2's
messages are unchanged.

Accounts refusals no longer quote a path: the config directory and accounts
file uid messages name the rule and the uid only (redaction rule, #1916).
A missing config directory now refuses accounts-unsafe instead of a raw
ENOENT.
2026-11-10 22:21:06 -06:00

34 lines
2.1 KiB
JavaScript

// #1873: the config directory check shared by the accounts store (accounts.mjs) and the root agent
// file's writer (agent-levels-write.mjs). Both open, lock, write and rename files by path under
// configDir, so the path must stay this uid's from the check to the last write.
//
// Every ancestor is a real directory that only root or this uid can change: owned by one of them, and
// group or other writable only with the sticky bit (as /tmp is). Otherwise another user could rename
// configDir out from under the check and put their own file in its place. configDir itself belongs to
// this uid and is not group or other writable.
//
// Each caller keeps its own refusal code. No message quotes a path: a refusal names the rule and the
// uid only (the redaction rule, PR #1916).
import { lstatSync } from 'node:fs';
import { join, parse, resolve } from 'node:path';
import { OrgError } from './org-registry.mjs';
export function assertTrustedConfigDir(configDir, { uid, code }) {
const fail = message => { throw new OrgError(code, message); };
if (typeof configDir !== 'string' || resolve(configDir) !== configDir) fail('config directory must be an absolute normalized path');
const ancestor = `a config directory ancestor must belong to root or uid ${uid} and not be writable by others without the sticky bit`;
const trusted = st => (st.uid === 0 || st.uid === uid) && (!(st.mode & 0o022) || (st.mode & 0o1000));
const stat = path => { try { return lstatSync(path); } catch { return fail('config directory is missing'); } };
let current = parse(configDir).root;
if (!trusted(stat(current))) fail(ancestor);
for (const part of configDir.slice(current.length).split('/').filter(Boolean)) {
current = join(current, part);
const st = stat(current);
if (!st.isDirectory() || st.isSymbolicLink()) fail('config path contains a symbolic link or non-directory');
if (current !== configDir && !trusted(st)) fail(ancestor);
}
const st = stat(configDir);
if (st.uid !== uid) fail(`config directory must belong to uid ${uid}`);
if (st.mode & 0o022) fail('config directory must not be writable by group or other');
}