2018-03-06 21:20:04 +00:00
|
|
|
/* eslint-env node */
|
|
|
|
|
2018-04-06 16:43:34 +00:00
|
|
|
const is = require('@sindresorhus/is');
|
2018-04-06 16:40:26 +00:00
|
|
|
const path = require('path');
|
2018-03-06 21:20:04 +00:00
|
|
|
|
2018-04-02 19:08:53 +00:00
|
|
|
const { compose } = require('lodash/fp');
|
2018-04-06 16:43:34 +00:00
|
|
|
const { escapeRegExp } = require('lodash');
|
2018-03-06 21:20:04 +00:00
|
|
|
|
|
|
|
|
2018-04-06 16:54:29 +00:00
|
|
|
const APP_ROOT_PATH = path.join(__dirname, '..', '..', '..');
|
2018-03-06 21:20:04 +00:00
|
|
|
const PHONE_NUMBER_PATTERN = /\+\d{7,12}(\d{3})/g;
|
|
|
|
const GROUP_ID_PATTERN = /(group\()([^)]+)(\))/g;
|
2018-04-06 16:54:29 +00:00
|
|
|
const REDACTION_PLACEHOLDER = '[REDACTED]';
|
2018-03-06 21:20:04 +00:00
|
|
|
|
2018-04-06 16:54:29 +00:00
|
|
|
|
|
|
|
// _redactPath :: Path -> String -> String
|
|
|
|
exports._redactPath = (filePath) => {
|
|
|
|
if (!is.string(filePath)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'filePath' must be a string");
|
2018-04-06 16:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const filePathPattern = exports._pathToRegExp(filePath);
|
|
|
|
|
|
|
|
return (text) => {
|
|
|
|
if (!is.string(text)) {
|
2018-04-11 19:44:52 +00:00
|
|
|
throw new TypeError("'text' must be a string");
|
2018-04-06 16:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is.regExp(filePathPattern)) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.replace(filePathPattern, REDACTION_PLACEHOLDER);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// _pathToRegExp :: Path -> Maybe RegExp
|
|
|
|
exports._pathToRegExp = (filePath) => {
|
2018-03-07 15:57:39 +00:00
|
|
|
try {
|
2018-04-06 17:19:41 +00:00
|
|
|
const pathWithNormalizedSlashes = filePath.replace(/\//g, '\\');
|
2018-04-06 18:25:55 +00:00
|
|
|
const pathWithEscapedSlashes = filePath.replace(/\\/g, '\\\\');
|
2018-04-06 17:19:41 +00:00
|
|
|
const urlEncodedPath = encodeURI(filePath);
|
2018-03-07 15:57:39 +00:00
|
|
|
// Safe `String::replaceAll`:
|
|
|
|
// https://github.com/lodash/lodash/issues/1084#issuecomment-86698786
|
2018-04-06 17:19:41 +00:00
|
|
|
const patternString = [
|
|
|
|
filePath,
|
|
|
|
pathWithNormalizedSlashes,
|
2018-04-06 18:25:55 +00:00
|
|
|
pathWithEscapedSlashes,
|
2018-04-06 17:19:41 +00:00
|
|
|
urlEncodedPath,
|
|
|
|
].map(escapeRegExp).join('|');
|
|
|
|
return new RegExp(patternString, 'g');
|
2018-03-07 15:57:39 +00:00
|
|
|
} catch (error) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-04-06 16:54:29 +00:00
|
|
|
};
|
2018-03-06 21:20:04 +00:00
|
|
|
|
2018-04-06 16:54:29 +00:00
|
|
|
// Public API
|
2018-03-06 21:20:04 +00:00
|
|
|
// redactPhoneNumbers :: String -> String
|
|
|
|
exports.redactPhoneNumbers = (text) => {
|
2018-04-06 16:43:34 +00:00
|
|
|
if (!is.string(text)) {
|
2018-04-11 19:44:52 +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) => {
|
2018-04-06 16:43:34 +00:00
|
|
|
if (!is.string(text)) {
|
2018-04-11 19:44:52 +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
|
2018-04-06 16:54:29 +00:00
|
|
|
exports.redactSensitivePaths = exports._redactPath(APP_ROOT_PATH);
|
2018-03-06 21:20:04 +00:00
|
|
|
|
|
|
|
// redactAll :: String -> String
|
|
|
|
exports.redactAll = compose(
|
|
|
|
exports.redactSensitivePaths,
|
|
|
|
exports.redactGroupIds,
|
|
|
|
exports.redactPhoneNumbers
|
|
|
|
);
|