Extract Privacy module

Centralizes how we redact sensitive information.
This commit is contained in:
Daniel Gasienica 2018-03-06 16:20:04 -05:00
parent 0c317c5498
commit 49e0850fb2
4 changed files with 60 additions and 42 deletions

53
js/modules/privacy.js Normal file
View file

@ -0,0 +1,53 @@
/* eslint-env node */
const Path = require('path');
const isString = require('lodash/isString');
const compose = require('lodash/fp/compose');
const PHONE_NUMBER_PATTERN = /\+\d{7,12}(\d{3})/g;
const GROUP_ID_PATTERN = /(group\()([^)]+)(\))/g;
const APP_ROOT_PATH = Path.join(__dirname, '..', '..', '..');
const APP_ROOT_PATH_PATTERN = new RegExp(APP_ROOT_PATH, 'g');
const REDACTION_PLACEHOLDER = '[REDACTED]';
// redactPhoneNumbers :: String -> String
exports.redactPhoneNumbers = (text) => {
if (!isString(text)) {
throw new TypeError('`text` must be a string');
}
return text.replace(PHONE_NUMBER_PATTERN, `+${REDACTION_PLACEHOLDER}$1`);
};
// redactGroupIds :: String -> String
exports.redactGroupIds = (text) => {
if (!isString(text)) {
throw new TypeError('`text` must be a string');
}
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)) {
throw new TypeError('`text` must be a string');
}
return text.replace(APP_ROOT_PATH_PATTERN, REDACTION_PLACEHOLDER);
};
// redactAll :: String -> String
exports.redactAll = compose(
exports.redactSensitivePaths,
exports.redactGroupIds,
exports.redactPhoneNumbers
);