2020-06-15 10:34:59 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
const invalidPatternsRegexes = [/<%[^=]([^<>%]*)%>/m, /\${([^{}]*)}/m];
|
2022-03-07 16:15:48 +01:00
|
|
|
const authorizedKeys = [
|
|
|
|
'URL',
|
|
|
|
'SERVER_URL',
|
|
|
|
'CODE',
|
|
|
|
'USER',
|
|
|
|
'USER.email',
|
|
|
|
'USER.username',
|
|
|
|
'TOKEN',
|
|
|
|
];
|
2020-06-15 10:34:59 +02:00
|
|
|
|
|
|
|
const matchAll = (pattern, src) => {
|
|
|
|
const matches = [];
|
|
|
|
let match;
|
|
|
|
|
|
|
|
const regexPatternWithGlobal = RegExp(pattern, 'g');
|
|
|
|
while ((match = regexPatternWithGlobal.exec(src))) {
|
|
|
|
const [, group] = match;
|
|
|
|
|
|
|
|
matches.push(_.trim(group));
|
|
|
|
}
|
|
|
|
return matches;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isValidEmailTemplate = template => {
|
|
|
|
for (let reg of invalidPatternsRegexes) {
|
|
|
|
if (reg.test(template)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const matches = matchAll(/<%=([^<>%=]*)%>/, template);
|
|
|
|
for (const match of matches) {
|
|
|
|
if (!authorizedKeys.includes(match)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
isValidEmailTemplate,
|
|
|
|
};
|