fix(config): protect operator credentials
Keep first-run access details confined to the active process console and remove development credentials from managed application logs. Treat notification delivery settings as sensitive during backup and restore, preserve private file access across creation and updates, and align the WebUI guidance with the new boundary.
This commit is contained in:
9 files changed
+123
-43
No files matched your search
@@ -160,7 +160,11 @@ describe('loadNotificationsConfig / saveNotificationsConfig (fs-backed)', () =>
|
||||
const mod = await import('../src/notifications/config');
|
||||
const cfg = mod.loadNotificationsConfig();
|
||||
expect(cfg).toEqual(mod.defaultNotificationsConfig());
|
||||
expect(fs.existsSync(path.join('config', 'notifications.json'))).toBe(true);
|
||||
const file = path.join('config', 'notifications.json');
|
||||
expect(fs.existsSync(file)).toBe(true);
|
||||
if (process.platform !== 'win32') {
|
||||
expect(fs.statSync(file).mode & 0o777).toBe(0o600);
|
||||
}
|
||||
});
|
||||
|
||||
it('round-trips a saved config across a fresh module load', async () => {
|
||||
@@ -177,6 +181,42 @@ describe('loadNotificationsConfig / saveNotificationsConfig (fs-backed)', () =>
|
||||
expect(mod2.loadNotificationsConfig()).toEqual(saved);
|
||||
});
|
||||
|
||||
it('repairs permissive permissions on an existing sensitive config', async () => {
|
||||
const file = path.join('config', 'notifications.json');
|
||||
fs.mkdirSync('config', { recursive: true });
|
||||
fs.writeFileSync(file, JSON.stringify({
|
||||
version: 1,
|
||||
debounceSeconds: 30,
|
||||
channels: [{ id: 'ops', url: 'https://example.com/private-hook' }],
|
||||
}), { encoding: 'utf8', mode: 0o644 });
|
||||
if (process.platform !== 'win32') fs.chmodSync(file, 0o644);
|
||||
|
||||
const mod = await import('../src/notifications/config');
|
||||
expect(mod.loadNotificationsConfig().channels).toHaveLength(1);
|
||||
if (process.platform !== 'win32') {
|
||||
expect(fs.statSync(file).mode & 0o777).toBe(0o600);
|
||||
}
|
||||
});
|
||||
|
||||
it('keeps restrictive permissions after replacing an existing config', async () => {
|
||||
const file = path.join('config', 'notifications.json');
|
||||
fs.mkdirSync('config', { recursive: true });
|
||||
fs.writeFileSync(file, JSON.stringify({ version: 1, debounceSeconds: 30, channels: [] }), {
|
||||
encoding: 'utf8',
|
||||
mode: 0o644,
|
||||
});
|
||||
if (process.platform !== 'win32') fs.chmodSync(file, 0o644);
|
||||
|
||||
const mod = await import('../src/notifications/config');
|
||||
mod.saveNotificationsConfig({
|
||||
channels: [{ id: 'ops', url: 'https://example.com/private-hook' }],
|
||||
});
|
||||
|
||||
if (process.platform !== 'win32') {
|
||||
expect(fs.statSync(file).mode & 0o777).toBe(0o600);
|
||||
}
|
||||
});
|
||||
|
||||
it('section-merges partial saves (debounce-only keeps channels)', async () => {
|
||||
const mod = await import('../src/notifications/config');
|
||||
mod.saveNotificationsConfig({ channels: [{ id: 'c1', url: 'https://a.com' }] });
|
||||
|
||||
@@ -27,9 +27,10 @@ describe('buildBackup', () => {
|
||||
expect(b.files['runtime.json']).toEqual({ encoding: 'utf8', data: '{"webuiPort":5099}' });
|
||||
});
|
||||
|
||||
it('treats all OneBot config (global + per-uin), webui.json, key.pem as credentials', () => {
|
||||
it('treats notification endpoints and all authentication material as credentials', () => {
|
||||
const map = {
|
||||
'runtime.json': Buffer.from('{}'),
|
||||
'notifications.json': Buffer.from('{"channels":[]}'),
|
||||
'webui.json': Buffer.from('{"hash":"x"}'),
|
||||
'key.pem': Buffer.from('KEY'),
|
||||
'cert.pem': Buffer.from('CERT'),
|
||||
@@ -42,7 +43,7 @@ describe('buildBackup', () => {
|
||||
expect(Object.keys(without.files).sort()).toEqual(['cert.pem', 'runtime.json']);
|
||||
const withCreds = buildBackup(reader(map), perUin, { includeCredentials: true }, TS);
|
||||
expect(Object.keys(withCreds.files).sort()).toEqual(
|
||||
['cert.pem', 'key.pem', 'onebot.json', 'onebot_12345.json', 'runtime.json', 'webui.json'],
|
||||
['cert.pem', 'key.pem', 'notifications.json', 'onebot.json', 'onebot_12345.json', 'runtime.json', 'webui.json'],
|
||||
);
|
||||
});
|
||||
|
||||
@@ -61,6 +62,7 @@ describe('buildBackup', () => {
|
||||
describe('specFor', () => {
|
||||
it('resolves static names and per-uin onebot pattern; rejects others', () => {
|
||||
expect(specFor('runtime.json')?.credential).toBe(false);
|
||||
expect(specFor('notifications.json')?.credential).toBe(true);
|
||||
expect(specFor('onebot.json')?.credential).toBe(true);
|
||||
expect(specFor('onebot_98765.json')).toEqual({ name: 'onebot_98765.json', binary: false, credential: true });
|
||||
expect(specFor('onebot_.json')).toBeNull();
|
||||
@@ -71,9 +73,9 @@ describe('specFor', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('BACKUP_FILES marks webui.json / key.pem / onebot.json as credentials, cert.pem public', () => {
|
||||
it('BACKUP_FILES marks notification endpoints and authentication material as credentials', () => {
|
||||
const creds = BACKUP_FILES.filter((f) => f.credential).map((f) => f.name).sort();
|
||||
expect(creds).toEqual(['key.pem', 'onebot.json', 'webui.json']);
|
||||
expect(creds).toEqual(['key.pem', 'notifications.json', 'onebot.json', 'webui.json']);
|
||||
expect(specFor('cert.pem')?.credential).toBe(false);
|
||||
});
|
||||
|
||||
@@ -156,10 +158,23 @@ describe('prepareRestorePlan — strict semantic preflight', () => {
|
||||
}),
|
||||
},
|
||||
}),
|
||||
{ restoreCredentials: false, readCurrent: () => null },
|
||||
{ restoreCredentials: true, readCurrent: () => null },
|
||||
)).toThrow(/notifications\.json.*channels/i);
|
||||
});
|
||||
|
||||
it('skips notification endpoints when credential restore is disabled', () => {
|
||||
const prepared = prepareRestorePlan(
|
||||
backupWith({
|
||||
'notifications.json': { encoding: 'utf8', data: '{private malformed value' },
|
||||
'runtime.json': { encoding: 'utf8', data: '{}' },
|
||||
}),
|
||||
{ restoreCredentials: false, readCurrent: () => null },
|
||||
);
|
||||
|
||||
expect(prepared.skipped).toEqual(['notifications.json']);
|
||||
expect(prepared.restore.map((entry) => entry.name)).toEqual(['runtime.json']);
|
||||
});
|
||||
|
||||
it('does not validate credential files that the operator chose to skip', () => {
|
||||
const prepared = prepareRestorePlan(
|
||||
backupWith({
|
||||
@@ -278,13 +293,18 @@ describe('prepareRestorePlan — strict semantic preflight', () => {
|
||||
const prepared = prepareRestorePlan(
|
||||
backupWith({
|
||||
'webui.json': { encoding: 'utf8', data: JSON.stringify(validAuth) },
|
||||
'notifications.json': {
|
||||
encoding: 'utf8',
|
||||
data: JSON.stringify({ version: 1, debounceSeconds: 30, channels: [] }),
|
||||
},
|
||||
}),
|
||||
{ restoreCredentials: true, readCurrent: () => null, listCurrentOneBotNames: () => [] },
|
||||
);
|
||||
|
||||
expect(prepared.restore).toEqual([
|
||||
expect(prepared.restore).toEqual(expect.arrayContaining([
|
||||
expect.objectContaining({ name: 'webui.json', mode: 0o600 }),
|
||||
]);
|
||||
expect.objectContaining({ name: 'notifications.json', mode: 0o600 }),
|
||||
]));
|
||||
});
|
||||
|
||||
it('rejects an encoding that does not match the allowlisted file kind', () => {
|
||||
|
||||
Reference in new issue
Block a user