Skip to content

Commit

Permalink
fix: adding check for reserved key words in extract custom fields (#3264
Browse files Browse the repository at this point in the history
)

* fix: adding check for reserved key words

* fix: review comments addressed
  • Loading branch information
shrouti1507 authored Apr 12, 2024
1 parent 007009f commit 3399c47
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 2 deletions.
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',
},
};

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]',
});
});
});

0 comments on commit 3399c47

Please sign in to comment.