-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): make data redacters settings case insensitive (#48)
* chore(deps): update dependencies * feat(utils): make data redacter settings case-insensitive * chore(tests): add tests for utils --------- Co-authored-by: DataLens Team <[email protected]>
- Loading branch information
Showing
10 changed files
with
1,863 additions
and
1,158 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,13 @@ | ||
import {isTrueEnvValue} from '../../lib/utils/is-true-env'; | ||
|
||
it('successfully checks value for truthfulness', () => { | ||
expect(isTrueEnvValue('true')).toEqual(true); | ||
expect(isTrueEnvValue('1')).toEqual(true); | ||
}); | ||
|
||
it('successfully checks value for untruthfulness', () => { | ||
expect(isTrueEnvValue('false')).toEqual(false); | ||
expect(isTrueEnvValue('0')).toEqual(false); | ||
expect(isTrueEnvValue('')).toEqual(false); | ||
expect(isTrueEnvValue(undefined)).toEqual(false); | ||
}); |
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,43 @@ | ||
import {REDACTED_STRING} from '../../lib/consts'; | ||
import prepareSensitiveHeadersRedacter from '../../lib/utils/redact-sensitive-headers'; | ||
import prepareSensitiveQueryParamsRedacter from '../../lib/utils/redact-sensitive-query-params'; | ||
import {NodeKit} from '../../nodekit'; | ||
|
||
it('correctly removes sensitive data from headers', () => { | ||
const inputHeaders = { | ||
Cookie: 'some-cookie-value', | ||
SomeHeader: 'non-secret-header', | ||
Referer: 'https://example.com/?someSecretParameter=secretValue', | ||
}; | ||
|
||
const queryParamsRedacter = prepareSensitiveQueryParamsRedacter(['someSecretParameter']); | ||
const headersRedacter = prepareSensitiveHeadersRedacter( | ||
['cookie'], | ||
['referer'], | ||
queryParamsRedacter, | ||
); | ||
|
||
const redactedHeaders = headersRedacter(inputHeaders); | ||
|
||
expect(redactedHeaders['Cookie']).toEqual(REDACTED_STRING); | ||
|
||
const redactedRefererParams = new URL(redactedHeaders['Referer'] as string).searchParams; | ||
expect(redactedRefererParams.get('someSecretParameter')).toEqual(REDACTED_STRING); | ||
}); | ||
|
||
it('correctly removes sensitive data from headers using default config', () => { | ||
const inputHeaders = { | ||
Cookie: 'some-cookie-value', | ||
SomeHeader: 'non-secret-header', | ||
Referer: 'https://example.com/?token=secretValue', | ||
}; | ||
|
||
const nk = new NodeKit({config: {appSensitiveQueryParams: ['token']}}); | ||
|
||
const redactedHeaders = nk.utils.redactSensitiveHeaders(inputHeaders); | ||
|
||
expect(redactedHeaders['Cookie']).toEqual(REDACTED_STRING); | ||
|
||
const redactedRefererParams = new URL(redactedHeaders['Referer'] as string).searchParams; | ||
expect(redactedRefererParams.get('token')).toEqual(REDACTED_STRING); | ||
}); |
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,97 @@ | ||
import {REDACTED_STRING} from '../../lib/consts'; | ||
import {prepareSensitiveKeysRedacter} from '../../lib/utils/redact-sensitive-keys'; | ||
import {NodeKit} from '../../nodekit'; | ||
import {Dict} from '../../types'; | ||
|
||
function getTestData() { | ||
return { | ||
someString: 'lorem', | ||
anotherString: 'ipsum', | ||
someNumber: 100, | ||
someObject: { | ||
someValueInObject: 'hello', | ||
}, | ||
verySensitiveValue: 42, | ||
verySensitiveObject: { | ||
someDataInSensitiveObject: 200, | ||
}, | ||
someNonSensitiveObject: { | ||
verySensitiveValue: 300, | ||
}, | ||
VERYSENSITIVEVALUE: 42, | ||
verysensitivevalue: 42, | ||
}; | ||
} | ||
|
||
function getTestConfiguration() { | ||
return ['verysensitivevalue', 'verysensitiveobject']; | ||
} | ||
|
||
it('removes sensitive data from the input object', () => { | ||
const redactSensitiveKeys = prepareSensitiveKeysRedacter(getTestConfiguration()); | ||
const redactedData = redactSensitiveKeys(getTestData()); | ||
|
||
expect(redactedData.verySensitiveValue).toEqual(REDACTED_STRING); | ||
expect(redactedData.verySensitiveObject).toEqual(REDACTED_STRING); | ||
}); | ||
|
||
it('removes sensitive keys regardless of their case', () => { | ||
const redactSensitiveKeys = prepareSensitiveKeysRedacter(getTestConfiguration()); | ||
const redactedData = redactSensitiveKeys(getTestData()); | ||
|
||
expect(redactedData.VERYSENSITIVEVALUE).toEqual(REDACTED_STRING); | ||
expect(redactedData.verysensitivevalue).toEqual(REDACTED_STRING); | ||
}); | ||
|
||
it('removes sensitive keys regardless of case in configuration', () => { | ||
const redactSensitiveKeys = prepareSensitiveKeysRedacter( | ||
getTestConfiguration().map((s) => s.toUpperCase()), | ||
); | ||
const redactedData = redactSensitiveKeys(getTestData()); | ||
|
||
expect(redactedData.VERYSENSITIVEVALUE).toEqual(REDACTED_STRING); | ||
expect(redactedData.verysensitivevalue).toEqual(REDACTED_STRING); | ||
}); | ||
|
||
it('does not affect data inside objects', () => { | ||
const redactSensitiveKeys = prepareSensitiveKeysRedacter(getTestConfiguration()); | ||
const redactedData = redactSensitiveKeys(getTestData()); | ||
|
||
expect((redactedData.someNonSensitiveObject as Dict).verySensitiveValue).toEqual(300); | ||
}); | ||
|
||
it('contains default sensitive values', () => { | ||
const nk = new NodeKit(); | ||
|
||
const inputData = { | ||
nonSensitiveData: 42, | ||
authorization: 'some-auth-token', | ||
cookie: 'some-cookie', | ||
}; | ||
const redactedData = nk.utils.redactSensitiveKeys(inputData); | ||
|
||
expect(redactedData.nonSensitiveData).toEqual(42); | ||
expect(redactedData.authorization).toEqual(REDACTED_STRING); | ||
expect(redactedData.cookie).toEqual(REDACTED_STRING); | ||
}); | ||
|
||
it('conbines default sensitive values with additional from configuration', () => { | ||
const nk = new NodeKit({ | ||
config: { | ||
appSensitiveKeys: ['appLevelSensitiveKey'], | ||
}, | ||
}); | ||
|
||
const inputData = { | ||
nonSensitiveData: 42, | ||
authorization: 'some-auth-token', | ||
cookie: 'some-cookie', | ||
appLevelSensitiveKey: 'some-data', | ||
}; | ||
const redactedData = nk.utils.redactSensitiveKeys(inputData); | ||
|
||
expect(redactedData.nonSensitiveData).toEqual(42); | ||
expect(redactedData.authorization).toEqual(REDACTED_STRING); | ||
expect(redactedData.cookie).toEqual(REDACTED_STRING); | ||
expect(redactedData.appLevelSensitiveKey).toEqual(REDACTED_STRING); | ||
}); |
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,16 @@ | ||
import {REDACTED_STRING} from '../../lib/consts'; | ||
import prepareSensitiveQueryParamsRedacter from '../../lib/utils/redact-sensitive-query-params'; | ||
|
||
it('removes value of sensitive query parameters', () => { | ||
const redactSensitiveQueryParams = prepareSensitiveQueryParamsRedacter(['someSensitiveKey']); | ||
const inputUrl = | ||
'https://example.com/some/path?foo=42&someSensitiveKey=sensitiveData&someOtherData=hello'; | ||
|
||
const redactedUrl = redactSensitiveQueryParams(inputUrl); | ||
expect(redactedUrl.includes('sensitiveData')).toBe(false); | ||
|
||
const redactedParams = new URL(redactedUrl).searchParams; | ||
expect(redactedParams.get('foo')).toEqual('42'); | ||
expect(redactedParams.get('someOtherData')).toEqual('hello'); | ||
expect(redactedParams.get('someSensitiveKey')).toEqual(REDACTED_STRING); | ||
}); |