-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ui): check Op code for literal credentials
- Loading branch information
1 parent
abbe2e0
commit 4b5e18d
Showing
3 changed files
with
50 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {sanitizeString} from './sanitizeSecrets'; | ||
|
||
describe('sanitizeString', () => { | ||
test('does not change clean string', () => { | ||
expect(sanitizeString('foo')).toEqual('foo'); | ||
}); | ||
test('does not change non-literal value', () => { | ||
expect(sanitizeString('"api_key": api_key')).toEqual('"api_key": api_key'); | ||
}); | ||
test('does sanitize literal value - double quotes', () => { | ||
expect(sanitizeString('"api_key": "abc"')).toEqual( | ||
'<Redacted: string contains api_key pattern>' | ||
); | ||
}); | ||
test('does sanitize literal value - single quotes', () => { | ||
expect(sanitizeString('"api_key": \'abc\'')).toEqual( | ||
'<Redacted: string contains api_key pattern>' | ||
); | ||
}); | ||
test('does sanitize known bad keys', () => { | ||
expect(sanitizeString('"auth_headers": \'abc\'')).toEqual( | ||
'<Redacted: string contains auth_headers pattern>' | ||
); | ||
expect(sanitizeString('"Authorization": \'abc\'')).toEqual( | ||
'<Redacted: string contains Authorization pattern>' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const BAD_KEYS = ['api_key', 'auth_headers', 'Authorization']; | ||
|
||
// Check for literal API key values in a string. | ||
// Not comprehensive, but meant as a stopgap. | ||
export const sanitizeString = (str: string): string => { | ||
for (const badKey of BAD_KEYS) { | ||
// Don't fire on non-literal key value, e.g. "api_key": api_key is OK | ||
const regex = new RegExp(`['"]${badKey}['"]:\\s+['"]`, 'i'); | ||
if (regex.test(str)) { | ||
// Note: This is replacing the entire string which seemed like | ||
// the more cautious approach, we could also consider only | ||
// redacting the literal value. | ||
return `<Redacted: string contains ${badKey} pattern>`; | ||
} | ||
} | ||
return str; | ||
}; |