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

chore: copy data argument to avoid mutate origin. #1389

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions packages/mail/src/classes/mail-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,21 @@ class MailService {
//Send mail
try {

// copy object to avoid mutating original
const args = { ...data };
//Append multiple flag to data if not set
if (typeof data.isMultiple === 'undefined') {
data.isMultiple = isMultiple;
args.isMultiple = isMultiple;
}

//Append global substitution wrappers if not set in data
if (typeof data.substitutionWrappers === 'undefined') {
data.substitutionWrappers = this.substitutionWrappers;
args.substitutionWrappers = this.substitutionWrappers;
}

//Create Mail instance from data and get JSON body for request
const mail = Mail.create(data);
const mail = Mail.create(args);
const body = mail.toJSON();

//Filters the Mail body to avoid sensitive content leakage
this.filterSecrets(body);

Expand Down
33 changes: 33 additions & 0 deletions packages/mail/src/classes/mail-service.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { assert } = require('chai');

const MailService = require('./mail-service');
describe('MailService send', () => {
it('should not mutate original data variable', () => {
const mailService = new MailService();
mailService.setClient({
request: (req, cb) => {
return new Promise((resolve) => {
resolve();
});
},
});
const data = {
to: '[email protected]',
from: '[email protected]', // Use the email address or domain you verified above
subject: 'Sending with Twilio SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
return mailService.send(data).then(() => {
assert.deepStrictEqual(data,
{
to: '[email protected]',
from: '[email protected]', // Use the email address or domain you verified above
subject: 'Sending with Twilio SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
});

});
});
});
Loading