2019-01-17 14:44:52 +01:00
|
|
|
import xss from 'xss';
|
2018-10-05 13:00:45 +02:00
|
|
|
|
2019-01-23 11:27:02 +01:00
|
|
|
const XSSWL = Object.keys(xss.whiteList).reduce((acc, element) => {
|
2019-08-01 14:38:34 +02:00
|
|
|
// @ts-ignore
|
2019-01-23 11:27:02 +01:00
|
|
|
acc[element] = xss.whiteList[element].concat(['class', 'style']);
|
|
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
const sanitizeXSS = new xss.FilterXSS({
|
2019-02-13 11:14:53 +01:00
|
|
|
whiteList: XSSWL,
|
2019-01-23 11:27:02 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns string safe from XSS attacks.
|
|
|
|
|
*
|
|
|
|
|
* Even though we allow the style-attribute, there's still default filtering applied to it
|
|
|
|
|
* Info: https://github.com/leizongmin/js-xss#customize-css-filter
|
|
|
|
|
* Whitelist: https://github.com/leizongmin/js-css-filter/blob/master/lib/default.js
|
|
|
|
|
*/
|
2019-02-13 11:14:53 +01:00
|
|
|
export function sanitize(unsanitizedString: string): string {
|
2019-01-17 14:44:52 +01:00
|
|
|
try {
|
2019-01-23 11:27:02 +01:00
|
|
|
return sanitizeXSS.process(unsanitizedString);
|
2019-01-17 14:44:52 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log('String could not be sanitized', unsanitizedString);
|
|
|
|
|
return unsanitizedString;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-08 00:44:09 +08:00
|
|
|
|
|
|
|
|
export function hasAnsiCodes(input: string): boolean {
|
|
|
|
|
return /\u001b\[\d{1,2}m/.test(input);
|
|
|
|
|
}
|
2019-06-25 09:06:28 +02:00
|
|
|
|
|
|
|
|
export function escapeHtml(str: string): string {
|
|
|
|
|
return String(str)
|
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
|
.replace(/</g, '<')
|
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
|
.replace(/"/g, '"');
|
|
|
|
|
}
|