Skip to content

Commit e1512de

Browse files
DF-22512: Redacts multiline sensitive fields
1 parent b2094ab commit e1512de

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

src/config/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -664,12 +664,19 @@ 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(
671+
/[-[\]{}()*+?.,\\^$|#\s]/g,
672+
'\\$&',
673+
)
674+
// Escaping special case for new line characters. This is needed to properly match and censor private keys,
675+
// ssh keys, and other multi-line string values.
676+
.replace(
677+
/\n/g,
678+
'\\n',
679+
),
673680
'gi',
674681
),
675682
}))

test/config.test.ts

Lines changed: 40 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,40 @@ 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'] = '-----BEGIN PRIVATE KEY-----\nthis\nis a fake\nprivate key\nused for testing only==\n-----END PRIVATE KEY-----'
196+
const customSettings: SettingsDefinitionMap = {
197+
PRIVATE_KEY: {
198+
description: 'Test custom env var',
199+
type: 'string',
200+
sensitive: true,
201+
validate: {
202+
meta: {
203+
details: 'placeholder validation',
204+
},
205+
fn: (value?: string) => {
206+
return ''
207+
}
208+
}
209+
},
210+
}
211+
const config = new AdapterConfig(customSettings)
212+
config.initialize()
213+
config.validate()
214+
config.buildCensorList()
215+
216+
const adapter = new Adapter({
217+
name: "TEST_ADAPTER",
218+
endpoints: [],
219+
config: config,
220+
})
221+
222+
const settingsList = buildSettingsList(adapter)
223+
224+
const settingEntries = settingsList.filter((entry) => entry.name === 'PRIVATE_KEY')
225+
t.assert(settingEntries.length === 1)
226+
const settingEntry = settingEntries[0]
227+
t.assert(settingEntry.value === "[PRIVATE_KEY REDACTED]")
228+
})

0 commit comments

Comments
 (0)