diff --git a/.changeset/settings-notification-redact-nested-urls.md b/.changeset/settings-notification-redact-nested-urls.md new file mode 100644 index 000000000..2df8fb691 --- /dev/null +++ b/.changeset/settings-notification-redact-nested-urls.md @@ -0,0 +1,7 @@ +--- +'nostream': patch +--- + +fix(admin): hide a webhook url in the settings-change notification when a parent path is replaced + +The notification sent to the operator's recipients after a settings change redacts secrets by the settings path of each value. A change to a path above a webhook target, such as replacing `admin.notifications` or `admin`, carried the whole object as its value, and the redaction ran on it without that path, so the `url` of each notification target (a Discord or Slack webhook with a token in it) went out in the notification. The value of each change is now redacted with the path it was written at, so the url is replaced by `***` in the notification and in the response of the settings request. diff --git a/src/utils/settings-redaction.ts b/src/utils/settings-redaction.ts index 4981f99b1..668a5a56f 100644 --- a/src/utils/settings-redaction.ts +++ b/src/utils/settings-redaction.ts @@ -4,7 +4,17 @@ const isPlainObject = (value: unknown): value is Record => { return typeof value === 'object' && value !== null && !Array.isArray(value) } -export const isSensitiveSettingsPath = (path: string): boolean => { +// The settings reader trims the path and each of its segments (`" admin. notifications "` names `admin.notifications`), +// so a path is compared in that form here too. +const normalizeSettingsPath = (path: string): string => + path + .trim() + .split('.') + .map((segment) => segment.trim()) + .join('.') + +export const isSensitiveSettingsPath = (rawPath: string): boolean => { + const path = normalizeSettingsPath(rawPath) const segments = path.split('.') const lastSegment = segments[segments.length - 1] ?? '' const key = lastSegment.replace(/\[\d+\]$/, '') @@ -63,38 +73,38 @@ export const changedWriteProtectedSettingsPaths = (before: unknown, after: unkno return [...paths].filter((path) => protectedBefore[path] !== protectedAfter[path]).sort() } -export const redactSettingsValue = (path: string, value: unknown): unknown => { - if (isSensitiveSettingsPath(path) && typeof value === 'string' && value.length > 0) { - return '***' +const redactWalk = (value: unknown, path = ''): unknown => { + if (Array.isArray(value)) { + return value.map((entry, index) => redactWalk(entry, `${path}[${index}]`)) } - return value -} + if (!isPlainObject(value)) { + return value + } -export const redactSettingsSecrets = (settings: T): T => { - const redactWalk = (value: unknown, path = ''): unknown => { - if (Array.isArray(value)) { - return value.map((entry, index) => redactWalk(entry, `${path}[${index}]`)) - } + const result: Record = {} - if (!isPlainObject(value)) { - return value + for (const [key, entry] of Object.entries(value)) { + const childPath = path ? `${path}.${key}` : key + if (isSensitiveSettingsPath(childPath) && typeof entry === 'string' && entry.length > 0) { + result[key] = '***' + continue } - const result: Record = {} - - for (const [key, entry] of Object.entries(value)) { - const childPath = path ? `${path}.${key}` : key - if (isSensitiveSettingsPath(childPath) && typeof entry === 'string' && entry.length > 0) { - result[key] = '***' - continue - } + result[key] = redactWalk(entry, childPath) + } - result[key] = redactWalk(entry, childPath) - } + return result +} - return result +// A value written at a path above a sensitive one (replacing `admin.notifications` writes the webhook urls in its +// targets) is redacted with the path it was written at, so the nested paths are the ones the rules know. +export const redactSettingsValue = (path: string, value: unknown): unknown => { + if (isSensitiveSettingsPath(path) && typeof value === 'string' && value.length > 0) { + return '***' } - return redactWalk(settings) as T + return redactWalk(value, normalizeSettingsPath(path)) } + +export const redactSettingsSecrets = (settings: T): T => redactWalk(settings) as T diff --git a/test/unit/controllers/admin/patch-settings-controller.spec.ts b/test/unit/controllers/admin/patch-settings-controller.spec.ts index b87f1f28d..162e4b4dd 100644 --- a/test/unit/controllers/admin/patch-settings-controller.spec.ts +++ b/test/unit/controllers/admin/patch-settings-controller.spec.ts @@ -133,6 +133,43 @@ describe('PatchAdminSettingsController settings write lock', () => { expect(readsWhileLocked).to.deep.equal([true, true]) }) + it('redacts a webhook url in the notification and the response when a parent path is replaced', async () => { + const url = 'https://hooks.slack.com/services/T00/B00/xxxxx' + request = { + body: { + path: 'admin.notifications', + value: { targets: [{ id: 'slack-ops', type: 'slack', enabled: true, url }] }, + }, + ip: '127.0.0.1', + } + + const controller = new PatchAdminSettingsController(outboxRepository as any) + await controller.handleRequest(request as any, response as any) + + const [, payload] = outboxRepository.enqueue.firstCall.args + expect(JSON.stringify(payload)).not.to.include(url) + expect(payload.changes[0].value.targets[0].url).to.equal('***') + expect(JSON.stringify(response.send.firstCall.args[0])).not.to.include(url) + }) + + it('redacts the webhook url when the replaced parent path has whitespace around it', async () => { + const url = 'https://hooks.slack.com/services/T00/B00/xxxxx' + request = { + body: { + path: ' admin.notifications', + value: { targets: [{ id: 'slack-ops', type: 'slack', enabled: true, url }] }, + }, + ip: '127.0.0.1', + } + + const controller = new PatchAdminSettingsController(outboxRepository as any) + await controller.handleRequest(request as any, response as any) + + const [, payload] = outboxRepository.enqueue.firstCall.args + expect(JSON.stringify(payload)).not.to.include(url) + expect(JSON.stringify(response.send.firstCall.args[0])).not.to.include(url) + }) + it('does not save when merged validation fails', async () => { ;(settingsConfig.validateSettings as Sinon.SinonStub).returns([{ path: 'payments', message: 'bad' }]) @@ -226,9 +263,18 @@ describe('PatchAdminSettingsController settings write lock', () => { it('does not bring back a stored override an earlier change in the batch removed', async () => { const stored = { blacklist: ['a.example', 'b.example'] } - ;(settingsConfig.loadUserSettings as Sinon.SinonStub).returns({ ...baseSettings, limits: { event: { pubkey: stored } } }) - ;(settingsConfig.loadMergedSettings as Sinon.SinonStub).returns({ ...baseSettings, limits: { event: { pubkey: stored } } }) - ;(settingsConfig.loadDefaults as Sinon.SinonStub).returns({ ...baseSettings, limits: { event: { pubkey: { blacklist: [] } } } }) + ;(settingsConfig.loadUserSettings as Sinon.SinonStub).returns({ + ...baseSettings, + limits: { event: { pubkey: stored } }, + }) + ;(settingsConfig.loadMergedSettings as Sinon.SinonStub).returns({ + ...baseSettings, + limits: { event: { pubkey: stored } }, + }) + ;(settingsConfig.loadDefaults as Sinon.SinonStub).returns({ + ...baseSettings, + limits: { event: { pubkey: { blacklist: [] } } }, + }) request.body = { changes: [ { path: 'limits.event.pubkey', value: {} }, diff --git a/test/unit/utils/settings-redaction.spec.ts b/test/unit/utils/settings-redaction.spec.ts index 21ae9e763..c9ebbdcb8 100644 --- a/test/unit/utils/settings-redaction.spec.ts +++ b/test/unit/utils/settings-redaction.spec.ts @@ -58,9 +58,45 @@ describe('settings-redaction', () => { it('redacts single values by path', () => { expect(redactSettingsValue('mirroring.static[0].secret', 'top-secret')).to.equal('***') expect(redactSettingsValue('payments.enabled', true)).to.equal(true) - expect(redactSettingsValue('admin.notifications.targets[0].url', 'https://discord.com/api/webhooks/secret')).to.equal( + expect( + redactSettingsValue('admin.notifications.targets[0].url', 'https://discord.com/api/webhooks/secret'), + ).to.equal('***') + }) + + it('redacts a webhook url inside a value written at a parent path', () => { + const targets = [ + { id: 'slack-ops', type: 'slack', enabled: true, url: 'https://hooks.slack.com/services/T00/B00/xxxxx' }, + ] + + const redacted = redactSettingsValue('admin.notifications', { targets, recipients: ['ops@example.com'] }) as any + + expect(redacted.targets[0].url).to.equal('***') + expect(redacted.targets[0].id).to.equal('slack-ops') + expect(redacted.recipients).to.deep.equal(['ops@example.com']) + expect(redactSettingsValue('admin.notifications.targets', targets)).to.deep.equal([{ ...targets[0], url: '***' }]) + expect(redactSettingsValue('admin', { notifications: { targets } }) as any).to.have.nested.property( + 'notifications.targets[0].url', + '***', + ) + }) + + it('redacts a webhook url when the path has whitespace the settings reader ignores', () => { + const targets = [ + { id: 'slack-ops', type: 'slack', enabled: true, url: 'https://hooks.slack.com/services/T00/B00/xxxxx' }, + ] + + expect(redactSettingsValue(' admin.notifications ', { targets }) as any).to.have.nested.property( + 'targets[0].url', + '***', + ) + expect(redactSettingsValue('admin. notifications.targets', targets) as any).to.have.nested.property( + '[0].url', + '***', + ) + expect(redactSettingsValue('admin.notifications.targets[0]. url', 'https://hooks.slack.com/services/T00')).to.equal( '***', ) + expect(isSensitiveSettingsPath(' admin.notifications.targets[0].url')).to.equal(true) }) it('redacts notification webhook urls in nested settings', () => {