Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/settings-notification-redact-nested-urls.md
Original file line number Diff line number Diff line change
@@ -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.
60 changes: 35 additions & 25 deletions src/utils/settings-redaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ const isPlainObject = (value: unknown): value is Record<string, unknown> => {
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+\]$/, '')
Expand Down Expand Up @@ -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 = <T>(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<string, unknown> = {}

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<string, unknown> = {}

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 = <T>(settings: T): T => redactWalk(settings) as T
52 changes: 49 additions & 3 deletions test/unit/controllers/admin/patch-settings-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }])

Expand Down Expand Up @@ -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: {} },
Expand Down
38 changes: 37 additions & 1 deletion test/unit/utils/settings-redaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading