Skip to content

Commit cd63ac1

Browse files
committed
changed secretRules type and refactored filterSecrets
- The method setSecretRules can now receive an object, a string and an array of string and/or objects, then it will try to standardize the rules to the following structure: [ { name: 'rule_name', // optional pattern: /pattern/ // required } ] - Refactored filterSecrets to use the new secretRules types, changed the exception from string to Error type and the error message is a template string now;
1 parent 776fbb5 commit cd63ac1

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

packages/mail/src/mail.js

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class MailService {
2020
constructor() {
2121
this.client = client;
2222
this.substitutionWrappers = ['{{', '}}'];
23-
this.secretRules = {};
23+
this.secretRules = [];
2424
}
2525

2626
/**
@@ -45,15 +45,41 @@ class MailService {
4545
* Set secret rules for filtering the e-mail content
4646
*/
4747
setSecretRules(rules) {
48-
if (typeof rules === 'string') {
49-
rules = {
50-
0: new RegExp(rules)
51-
}
52-
} else if (typeof rules !== 'object') {
53-
return;
48+
if (!(rules instanceof Array)) {
49+
rules = [rules];
5450
}
5551

56-
this.secretRules = rules
52+
var tmpRules = rules.map(function (rule) {
53+
var ruleType = typeof rule;
54+
55+
if (ruleType === 'string') {
56+
return {
57+
pattern: new RegExp(rule)
58+
};
59+
} else if (ruleType === 'object') {
60+
// normalize rule object
61+
if (rule instanceof RegExp) {
62+
rule = {
63+
pattern: rule
64+
}
65+
} else if (rule.hasOwnProperty('pattern')
66+
&& (typeof rule.pattern === 'string')
67+
) {
68+
rule.pattern = new RegExp(rule.pattern);
69+
}
70+
71+
try {
72+
// test if rule.pattern is a valid regex
73+
rule.pattern.test('');
74+
return rule
75+
} catch (err) {
76+
}
77+
}
78+
});
79+
80+
this.secretRules = tmpRules.filter(function (val) {
81+
return val;
82+
});
5783
}
5884

5985
/**
@@ -65,20 +91,24 @@ class MailService {
6591
}
6692

6793
var self = this;
68-
var secretRulesKeys = Object.keys(this.secretRules);
6994

7095
body.content.forEach(function (data) {
71-
secretRulesKeys.forEach(function (id) {
72-
if (self.secretRules.hasOwnProperty(id)
73-
&& !self.secretRules[id].test(data.value)
96+
self.secretRules.forEach(function (rule) {
97+
if (rule.hasOwnProperty('pattern')
98+
&& !rule.pattern.test(data.value)
7499
) {
75100
return;
76101
}
77102

78-
var errorMsg = 'The pattern \'' + self.secretRules[id] + '\'';
79-
errorMsg += ' identified by \'' + id + '\' was found in the message!';
103+
var message = `The pattern '${rule.pattern}'`;
104+
105+
if (rule.name) {
106+
message += `identified by '${rule.name}'`;
107+
}
108+
109+
message += ` was found in the Mail content!`;
80110

81-
throw errorMsg;
111+
throw new Error(message);
82112
});
83113
});
84114
}

0 commit comments

Comments
 (0)