Skip to content

Commit 9e39280

Browse files
DF-22512: Redacts multiline sensitive fields (#637)
* DF-22512: Redacts multiline sensitive fields * apply linter * apply linter
1 parent 74b914a commit 9e39280

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

src/config/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -664,12 +664,13 @@ export class AdapterConfig<T extends SettingsDefinitionMap = SettingsDefinitionM
664664
)
665665
.map(([name]) => ({
666666
key: name,
667-
// Escaping potential special characters in values before creating regex
668667
value: new RegExp(
669-
((this.settings as Record<string, ValidSettingValue>)[name]! as string).replace(
670-
/[-[\]{}()*+?.,\\^$|#\s]/g,
671-
'\\$&',
672-
),
668+
((this.settings as Record<string, ValidSettingValue>)[name]! as string)
669+
// Escaping potential special characters in values before creating regex
670+
.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
671+
// Escaping special case for new line characters. This is needed to properly match and censor private keys,
672+
// ssh keys, and other multi-line string values.
673+
.replace(/\n/g, '\\n'),
673674
'gi',
674675
),
675676
}))

test/config.test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
SettingsDefinitionMap,
88
} from '../src/config'
99
import { validator } from '../src/validation/utils'
10+
import { Adapter } from '../src/adapter'
11+
import { buildSettingsList } from '../src/util/settings'
1012

1113
test.afterEach(async () => {
1214
process.env = {}
@@ -168,7 +170,7 @@ test.serial('Test validate function (scientific notation)', async (t) => {
168170
}
169171
})
170172

171-
test('sensitive configuration constants are properly flagged', (t) => {
173+
test.serial('sensitive configuration constants are properly flagged', (t) => {
172174
// Extract all settings that are marked as sensitive
173175
const actualSensitiveSettings = Object.entries(BaseSettingsDefinition)
174176
.filter(([_, setting]) => (setting as { sensitive?: boolean }).sensitive === true)
@@ -187,3 +189,41 @@ test('sensitive configuration constants are properly flagged', (t) => {
187189
// Deep equal comparison
188190
t.deepEqual(actualSensitiveSettings, expectedSensitiveSettings)
189191
})
192+
193+
test.serial('multiline sensitive configuration constants are properly redacted', async (t) => {
194+
// GIVEN
195+
process.env['PRIVATE_KEY'] =
196+
'-----BEGIN PRIVATE KEY-----\nthis\nis a fake\nprivate key\nused for testing only==\n-----END PRIVATE KEY-----'
197+
const customSettings: SettingsDefinitionMap = {
198+
PRIVATE_KEY: {
199+
description: 'Test custom env var',
200+
type: 'string',
201+
sensitive: true,
202+
validate: {
203+
meta: {
204+
details: 'placeholder validation',
205+
},
206+
fn: (_?: string) => {
207+
return ''
208+
},
209+
},
210+
},
211+
}
212+
const config = new AdapterConfig(customSettings)
213+
config.initialize()
214+
config.validate()
215+
config.buildCensorList()
216+
217+
const adapter = new Adapter({
218+
name: 'TEST_ADAPTER',
219+
endpoints: [],
220+
config: config,
221+
})
222+
223+
const settingsList = buildSettingsList(adapter)
224+
225+
const settingEntries = settingsList.filter((entry) => entry.name === 'PRIVATE_KEY')
226+
t.assert(settingEntries.length === 1)
227+
const settingEntry = settingEntries[0]
228+
t.assert(settingEntry.value === '[PRIVATE_KEY REDACTED]')
229+
})

0 commit comments

Comments
 (0)