Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RD-14191-SECRET_MASKING_EXACT_PATH-change-the-response-body-of-the-function #528

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/spans/awsSpan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,36 @@ describe('awsSpan', () => {
expect(endSpan.envs.length).toBeGreaterThan(startSpan.envs.length);
});

test('getEndFunctionSpan does not modify returnValue payload', () => {
const functionSpan = {
id: '6d26e3c8-60a6-4cee-8a70-f525f47a4caf_started',
};

const handlerReturnValue = {
err: null,
data: {
string: 'value',
object: {
string: 'value',
object: {
string: 'value',
},
},
},
};
process.env[LUMIGO_SECRET_MASKING_EXACT_PATH] =
'["string","object.string", "object.object.string"]';

const endFunctionSpan = awsSpan.getEndFunctionSpan(functionSpan, handlerReturnValue);
expect(endFunctionSpan.return_value).toEqual(
'{"string":"****","object":{"string":"****","object":{"string":"****"}}}'
);
// here we expect the original data to be untouched
expect(handlerReturnValue.data.string).toEqual('value');
expect(handlerReturnValue.data.object.string).toEqual('value');
expect(handlerReturnValue.data.object.object.string).toEqual('value');
});

test('Lambda invoked by S3 -> shouldnt scrub known S3 fields', () => {
const { context } = TracerGlobals.getHandlerInputs();
const event = {
Expand Down
49 changes: 27 additions & 22 deletions src/utils/payloadStringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const nativeTypes = ['string', 'bigint', 'number', 'undefined', 'boolean'];
const SCRUBBED_TEXT = '****';
const TRUNCATED_TEXT = '...[too long]';
const FAILED_SCRUBBING_BY_PATH = 'Failed to scrub payload by exact path';
const clone = require('rfdc')();

const isNativeType = (obj) => nativeTypes.includes(typeof obj);

Expand Down Expand Up @@ -176,6 +177,30 @@ function logSecretMaskingDebug(logger, message, additionalData) {
}
}

function scrubPayloadBasedOnExactPath(originalPayload) {
let payload = clone(originalPayload);
let secretPaths = getSecretPaths();
if (secretPaths.length > 0) {
const uniquePaths = getUniqPaths(secretPaths);
if (isString(payload)) {
payload = safeExecute(
scrubJsonStringBySecretPath,
FAILED_SCRUBBING_BY_PATH,
logger.LOG_LEVELS.DEBUG,
payload
)(payload, secretPaths, uniquePaths, '');
} else {
payload = safeExecute(
scrubJsonBySecretPath,
FAILED_SCRUBBING_BY_PATH,
logger.LOG_LEVELS.DEBUG,
payload
)(payload, secretPaths, uniquePaths, '');
}
}
return payload;
}

export const payloadStringify = (
payload,
maxPayloadSize = getEventEntitySize(),
Expand All @@ -190,29 +215,9 @@ export const payloadStringify = (

let isPruned = false;

if (getSecretMaskingExactPath()) {
let secretPaths = getSecretPaths();
if (secretPaths.length > 0) {
const uniquePaths = getUniqPaths(secretPaths);
if (isString(payload)) {
payload = safeExecute(
scrubJsonStringBySecretPath,
FAILED_SCRUBBING_BY_PATH,
logger.LOG_LEVELS.DEBUG,
payload
)(payload, secretPaths, uniquePaths, '');
} else {
payload = safeExecute(
scrubJsonBySecretPath,
FAILED_SCRUBBING_BY_PATH,
logger.LOG_LEVELS.DEBUG,
payload
)(payload, secretPaths, uniquePaths, '');
}
}
}
let result = getSecretMaskingExactPath() ? scrubPayloadBasedOnExactPath(payload) : payload;

let result = JSON.stringify(payload, function (key, value) {
result = JSON.stringify(result, function (key, value) {
const type = typeof value;
const isObj = type === 'object';
const isStr = type === 'string';
Expand Down