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 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
13 changes: 11 additions & 2 deletions src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1328,12 +1328,19 @@ const generateExclusionList = (mappingConfig) =>
*/
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 (!exclusionFields.includes(k) && !isReservedWord(k)) {
mappingKeys.push(k);
}
});
mappingKeys.forEach((mappingKey) => {
if (!(typeof messageContext[mappingKey] === 'undefined')) {
Expand All @@ -1344,7 +1351,9 @@ function extractCustomFields(message, payload, keys, exclusionFields) {
});
} else if (keys === 'root') {
Object.keys(message).forEach((k) => {
if (!exclusionFields.includes(k)) mappingKeys.push(k);
if (!exclusionFields.includes(k) && !isReservedWord(k)) {
mappingKeys.push(k);
}
});
mappingKeys.forEach((mappingKey) => {
if (!(typeof message[mappingKey] === 'undefined')) {
Expand Down
184 changes: 184 additions & 0 deletions src/v0/util/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,187 @@ describe('validateEventAndLowerCaseConversion Tests', () => {
}).toThrow(InstrumentationError);
});
});

describe('extractCustomFields', () => {
// Handle reserved words in message keys
it('should handle reserved word "prototype" in message keys when keys are provided', () => {
const message = {
traits: {
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
prototype: 'reserved',
},
context: {
traits: {
phone: '1234567890',
city: 'New York',
country: 'USA',
prototype: 'reserved',
},
},
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',
];

const result = utilities.extractCustomFields(message, payload, keys, exclusionFields);

expect(result).toEqual({
email: '[email protected]',
});
});

it('should handle reserved word "__proto__" in message keys when keys are provided', () => {
const message = {
traits: {
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
__proto__: 'reserved',
},
context: {
traits: {
phone: '1234567890',
city: 'New York',
country: 'USA',
__proto__: 'reserved',
},
},
properties: {
title: 'Developer',
organization: 'ABC Company',
zip: '12345',
__proto__: 'reserved',
},
};

const payload = {};

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

const exclusionFields = [
'firstName',
'lastName',
'phone',
'title',
'organization',
'city',
'region',
'country',
'zip',
'image',
'timezone',
];
const result = utilities.extractCustomFields(message, payload, keys, exclusionFields);
expect(result).toEqual({
email: '[email protected]',
});
});

it('should handle reserved word "constructor" in message keys when keys are provided', () => {
const message = {
traits: {
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
constructor: 'reserved',
},
context: {
traits: {
phone: '1234567890',
city: 'New York',
country: 'USA',
constructor: 'reserved',
},
},
properties: {
title: 'Developer',
organization: 'ABC Company',
zip: '12345',
constructor: 'reserved',
},
};

const payload = {};

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

const exclusionFields = [
'firstName',
'lastName',
'phone',
'title',
'organization',
'city',
'region',
'country',
'zip',
'image',
'timezone',
];
const result = utilities.extractCustomFields(message, payload, keys, exclusionFields);
expect(result).toEqual({
email: '[email protected]',
});
});

it('should handle reserved words in message keys when key is root', () => {
const message = {
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
prototype: 'reserved',
phone: '1234567890',
city: 'New York',
country: 'USA',
__proto__: 'reserved',
constructor: 'reserved',
};

const payload = {};

const keys = 'root';

const exclusionFields = [
'firstName',
'lastName',
'phone',
'title',
'organization',
'city',
'region',
'country',
'zip',
'image',
'timezone',
];

const result = utilities.extractCustomFields(message, payload, keys, exclusionFields);

expect(result).toEqual({
email: '[email protected]',
});
});
});
Loading