2018-03-06 21:20:04 +00:00
|
|
|
/* eslint-env node */
|
|
|
|
|
|
|
|
const Path = require('path');
|
|
|
|
|
|
|
|
const compose = require('lodash/fp/compose');
|
2018-03-07 15:57:39 +00:00
|
|
|
const escapeRegExp = require('lodash/escapeRegExp');
|
|
|
|
const isRegExp = require('lodash/isRegExp');
|
|
|
|
const isString = require('lodash/isString');
|
2018-03-06 21:20:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
const PHONE_NUMBER_PATTERN = /\+\d{7,12}(\d{3})/g;
|
|
|
|
const GROUP_ID_PATTERN = /(group\()([^)]+)(\))/g;
|
|
|
|
|
|
|
|
const APP_ROOT_PATH = Path.join(__dirname, '..', '..', '..');
|
2018-03-07 15:57:39 +00:00
|
|
|
const APP_ROOT_PATH_PATTERN = (() => {
|
|
|
|
try {
|
|
|
|
// Safe `String::replaceAll`:
|
|
|
|
// https://github.com/lodash/lodash/issues/1084#issuecomment-86698786
|
|
|
|
return new RegExp(escapeRegExp(APP_ROOT_PATH), 'g');
|
|
|
|
} catch (error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})();
|
2018-03-06 21:20:04 +00:00
|
|
|
|
|
|
|
const REDACTION_PLACEHOLDER = '[REDACTED]';
|
|
|
|
|
|
|
|
// redactPhoneNumbers :: String -> String
|
|
|
|
exports.redactPhoneNumbers = (text) => {
|
|
|
|
if (!isString(text)) {
|
2018-03-23 22:24:50 +00:00
|
|
|
throw new TypeError('"text" must be a string');
|
2018-03-06 21:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return text.replace(PHONE_NUMBER_PATTERN, `+${REDACTION_PLACEHOLDER}$1`);
|
|
|
|
};
|
|
|
|
|
|
|
|
// redactGroupIds :: String -> String
|
|
|
|
exports.redactGroupIds = (text) => {
|
|
|
|
if (!isString(text)) {
|
2018-03-23 22:24:50 +00:00
|
|
|
throw new TypeError('"text" must be a string');
|
2018-03-06 21:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return text.replace(
|
|
|
|
GROUP_ID_PATTERN,
|
|
|
|
(match, before, id, after) =>
|
|
|
|
`${before}${REDACTION_PLACEHOLDER}${id.slice(-3)}${after}`
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// redactSensitivePaths :: String -> String
|
|
|
|
exports.redactSensitivePaths = (text) => {
|
|
|
|
if (!isString(text)) {
|
2018-03-23 22:24:50 +00:00
|
|
|
throw new TypeError('"text" must be a string');
|
2018-03-06 21:20:04 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 15:57:39 +00:00
|
|
|
if (!isRegExp(APP_ROOT_PATH_PATTERN)) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2018-03-06 21:20:04 +00:00
|
|
|
return text.replace(APP_ROOT_PATH_PATTERN, REDACTION_PLACEHOLDER);
|
|
|
|
};
|
|
|
|
|
|
|
|
// redactAll :: String -> String
|
|
|
|
exports.redactAll = compose(
|
|
|
|
exports.redactSensitivePaths,
|
|
|
|
exports.redactGroupIds,
|
|
|
|
exports.redactPhoneNumbers
|
|
|
|
);
|