Skip to content

Commit

Permalink
fix: TRAC-33-crash-if-LUMIGO_SECRET_MASKING_REGEX-has-invalid-regex-e…
Browse files Browse the repository at this point in the history
…xp (#517)

* fix: keyToRegexes()
  • Loading branch information
eugene-lumigo authored Oct 7, 2024
1 parent 58e1104 commit f83458f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/utils/payloadStringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,26 @@ const keyToRegexes = (
backwardCompRegexEnvVarName = LUMIGO_SECRET_MASKING_REGEX_BACKWARD_COMP,
regexesEnvVarName = LUMIGO_SECRET_MASKING_REGEX
) => {
if (process.env[backwardCompRegexEnvVarName]) {
const parseResponse = parseJsonFromEnvVar(backwardCompRegexEnvVarName, true);
if (parseResponse) {
regexesList = parseResponse;
}
} else if (process.env[regexesEnvVarName]) {
const parseResponse = parseJsonFromEnvVar(regexesEnvVarName, true);
if (parseResponse) {
regexesList = parseResponse;
const fallbackRegexesList = regexesList;

const tryParseEnvVar = (envVarName) => {
if (process.env[envVarName]) {
return parseJsonFromEnvVar(envVarName, true);
}
return null;
};

// Try parsing backward compatibility or main environment variables
const regexes =
tryParseEnvVar(backwardCompRegexEnvVarName) || tryParseEnvVar(regexesEnvVarName) || regexesList;

try {
return regexes.map((x) => new RegExp(x, 'i'));
} catch (e) {
invalidMaskingRegexWarning(e);
logger.warn('Fallback to default regexes list', { fallbackRegexesList });
return fallbackRegexesList.map((x) => new RegExp(x, 'i'));
}
return regexesList.map((x) => new RegExp(x, 'i'));
};

export const keyToOmitRegexes = () => {
Expand Down
15 changes: 15 additions & 0 deletions src/utils/payloadStringify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,21 @@ describe('payloadStringify', () => {
]);
});

test('shallowMask -> requestBody -> LUMIGO_SECRET_MASKING_REGEX -> bad regex', () => {
utils.setDebug();
process.env[LUMIGO_SECRET_MASKING_REGEX] = '["a(a"]';
expect(shallowMask('requestBody', { a: 'b', aa: 'bla' })).toEqual({ a: 'b', aa: 'bla' });

expect(ConsoleWritesForTesting.getLogs()).toEqual([
expect.objectContaining({
msg: '#LUMIGO# - WARNING - "Failed to parse the given masking regex"',
}),
expect.objectContaining({
msg: '#LUMIGO# - WARNING - "Fallback to default regexes list"',
}),
]);
});

test.each`
envVarValue | event | expectedResults
${['["object.foo"]']} | ${[{ secret: { key: 'value' } }, { object: { foo: 'value' } }]} | ${JSON.stringify([{ secret: '****' }, { object: { foo: '****' } }])}
Expand Down

0 comments on commit f83458f

Please sign in to comment.