Skip to content

Prevent secrets from sending in email #502

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

Merged
merged 2 commits into from
Oct 22, 2018
Merged
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
79 changes: 78 additions & 1 deletion packages/mail/src/classes/mail-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class MailService {
*/
constructor() {

//Set client and initialize substitution wrappers
//Set client, initialize substitution wrappers and secret rules
//filter
this.setClient(new Client());
this.setSubstitutionWrappers('{{', '}}');
this.secretRules = [];
}

/**
Expand Down Expand Up @@ -49,6 +51,78 @@ class MailService {
this.substitutionWrappers[1] = right;
}

/**
* Set secret rules for filtering the e-mail content
*/
setSecretRules(rules) {
if (!(rules instanceof Array)) {
rules = [rules];
}

const tmpRules = rules.map(function (rule) {
const ruleType = typeof rule;

if (ruleType === 'string') {
return {
pattern: new RegExp(rule)
};
} else if (ruleType === 'object') {
// normalize rule object
if (rule instanceof RegExp) {
rule = {
pattern: rule
}
} else if (rule.hasOwnProperty('pattern')
&& (typeof rule.pattern === 'string')
) {
rule.pattern = new RegExp(rule.pattern);
}

try {
// test if rule.pattern is a valid regex
rule.pattern.test('');
return rule
} catch (err) {
}
}
});

this.secretRules = tmpRules.filter(function (val) {
return val;
});
}

/**
* Check if the e-mail is safe to be sent
*/
filterSecrets(body) {
if ((typeof body === 'object') && !body.hasOwnProperty('content')) {
return;
}

const self = this;

body.content.forEach(function (data) {
self.secretRules.forEach(function (rule) {
if (rule.hasOwnProperty('pattern')
&& !rule.pattern.test(data.value)
) {
return;
}

let message = `The pattern '${rule.pattern}'`;

if (rule.name) {
message += `identified by '${rule.name}'`;
}

message += ` was found in the Mail content!`;

throw new Error(message);
});
});
}

/**
* Send email
*/
Expand Down Expand Up @@ -96,6 +170,9 @@ class MailService {
const mail = Mail.create(data);
const body = mail.toJSON();

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

//Create request
const request = {
method: 'POST',
Expand Down