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: adding check for reserved key words in extract custom fields #3264

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1328,12 +1328,24 @@
*/
function extractCustomFields(message, payload, keys, exclusionFields) {
const mappingKeys = [];
// Define reserved words
const reservedWords = ['__proto__', 'constructor', 'prototype'];

const isReservedWord = (key) => reservedWords.includes(key);

if (Array.isArray(keys)) {
keys.forEach((key) => {
const messageContext = get(message, key);
if (messageContext) {
Object.keys(messageContext).forEach((k) => {
if (!exclusionFields.includes(k)) mappingKeys.push(k);
if (isReservedWord(k)) {
throw new InstrumentationError(
shrouti1507 marked this conversation as resolved.
Show resolved Hide resolved
`The property name ${k} is a reserved word. This cannot be used to build a payload`,
);
}
if (!exclusionFields.includes(k)) {
mappingKeys.push(k);
}
});
mappingKeys.forEach((mappingKey) => {
if (!(typeof messageContext[mappingKey] === 'undefined')) {
Expand All @@ -1344,7 +1356,14 @@
});
} else if (keys === 'root') {
Object.keys(message).forEach((k) => {
if (!exclusionFields.includes(k)) mappingKeys.push(k);
if (!exclusionFields.includes(k)) {
if (isReservedWord(k)) {
throw new InstrumentationError(

Check warning on line 1361 in src/v0/util/index.js

View check run for this annotation

Codecov / codecov/patch

src/v0/util/index.js#L1361

Added line #L1361 was not covered by tests
`The property name ${k} is a reserved word. This cannot be used to build a payload`,
);
}
mappingKeys.push(k);
}
});
mappingKeys.forEach((mappingKey) => {
if (!(typeof message[mappingKey] === 'undefined')) {
Expand Down
47 changes: 47 additions & 0 deletions src/v0/util/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,50 @@ describe('validateEventAndLowerCaseConversion Tests', () => {
}).toThrow(InstrumentationError);
});
});

describe('extractCustomFields', () => {
// Handle reserved words in message keys
it('should handle reserved words in message keys when keys are provided', () => {
const message = {
traits: {
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
},
context: {
traits: {
phone: '1234567890',
city: 'New York',
country: 'USA',
},
},
properties: {
title: 'Developer',
organization: 'ABC Company',
zip: '12345',
prototype: 'reserved',
shrouti1507 marked this conversation as resolved.
Show resolved Hide resolved
},
};

const payload = {};

const keys = ['properties', 'context.traits', 'traits'];

const exclusionFields = [
'firstName',
'lastName',
'phone',
'title',
'organization',
'city',
'region',
'country',
'zip',
'image',
'timezone',
];
expect(() => {
utilities.extractCustomFields(message, payload, keys, exclusionFields);
}).toThrow(InstrumentationError);
});
});
Loading