Files
openchamber/packages/web/bin/lib/cli-settings-accessors.js
T
quiz152 7a165fd0bb fix(cli): atomic settings writes and gate relay key regeneration in connect-url
The CLI's settings accessors wrote settings.json directly with writeFile and
read it leniently, with no strict-reader gate on relay identity. Running
'openchamber connect-url' while the desktop app is up could:
  - tear the file for a concurrent reader in the app, tripping the relay
    service's read and mapping it to {} (first-run);
  - then regenerate the relay signing/encryption keys, changing serverId and
    orphaning every paired device and push binding.

Move the accessors into a dedicated module that mirrors the settings
runtime's guarantees: atomic tmp+rename writes (with the Windows fallback)
so no reader can observe a partial file, and a strict reader that throws on
corrupt/unreadable payloads so identity regeneration is gated exactly like
the server runtime. Wire the strict reader into the CLI relay identity path.

Adds unit tests covering atomic writes under concurrent readers, strict-read
behavior, and that a corrupt settings file makes getRelayIdentity fail
instead of minting a replacement keypair.
2026-08-09 11:16:30 +08:00

102 lines
3.8 KiB
JavaScript

// Minimal settings.json access for CLI contexts (connect-url, pairing
// candidate building) that must not load the full web settings runtime.
//
// The running app already treats settings.json as a shared store — the relay
// identity, tunnels, notifications, the Electron main, and ssh-manager all
// read-modify-write it. This accessor must therefore mirror the settings
// runtime's guarantees or it will corrupt or regenerate shared state:
//
// - ATOMIC writes (write tmp, rename into place). A plain writeFile can
// interleave with a concurrent reader in the running app; the reader sees
// a half-written file, its lenient read maps it to `{}`, and relay
// identity logic then mints a NEW serverId — orphaning every paired
// device. The tmp+rename below means no reader can ever observe a partial
// file.
//
// - A STRICT read that THROWS on corrupt/unreadable payloads, gating relay
// identity regeneration. Only a genuinely missing file means "no
// settings"; any other failure (corrupt JSON, EACCES, transient I/O,
// non-object payload) must propagate so callers never confuse a broken
// read with first run and mint a replacement signing/encryption keypair.
export const createSettingsAccessors = ({ fsPromises, path, dataDir, settingsFileName }) => {
const settingsPath = path.join(dataDir, settingsFileName);
const readSettingsFromDiskMigrated = async () => {
try {
return JSON.parse(await fsPromises.readFile(settingsPath, 'utf8'));
} catch {
return {};
}
};
const readSettingsStrict = async () => {
let raw;
try {
raw = await fsPromises.readFile(settingsPath, 'utf8');
} catch (error) {
if (error && typeof error === 'object' && error.code === 'ENOENT') {
return {};
}
throw error;
}
const parsed = JSON.parse(raw);
if (!parsed || typeof parsed !== 'object') {
throw new Error('Settings file is malformed (non-object payload)');
}
return parsed;
};
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const isTransientWindowsReplaceError = (error) => {
if (process.platform !== 'win32' || !error || typeof error !== 'object') {
return false;
}
return error.code === 'EPERM' || error.code === 'EACCES' || error.code === 'EBUSY';
};
const replaceFile = async (tmp, target) => {
const maxAttempts = process.platform === 'win32' ? 6 : 1;
let lastError = null;
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
try {
await fsPromises.rename(tmp, target);
return;
} catch (error) {
lastError = error;
if (!isTransientWindowsReplaceError(error) || attempt === maxAttempts) {
break;
}
await sleep(25 * attempt);
}
}
if (!isTransientWindowsReplaceError(lastError)) {
throw lastError;
}
// Windows can transiently reject the atomic replace while another process
// briefly holds the target open. Copy the COMPLETE tmp file into place so
// persistence never wedges; a reader can still never see partial content.
await fsPromises.copyFile(tmp, target);
await fsPromises.rm(tmp, { force: true });
};
const writeSettingsToDisk = async (settings) => {
await fsPromises.mkdir(path.dirname(settingsPath), { recursive: true });
const tmp = `${settingsPath}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
await fsPromises.writeFile(tmp, JSON.stringify(settings, null, 2), { encoding: 'utf8', mode: 0o600 });
if (process.platform !== 'win32') {
await fsPromises.chmod(tmp, 0o600);
}
await replaceFile(tmp, settingsPath);
if (process.platform !== 'win32') {
await fsPromises.chmod(settingsPath, 0o600);
}
};
return { readSettingsFromDiskMigrated, readSettingsStrict, writeSettingsToDisk };
};