mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* Sanitize html in panel links * Add sanitize-url package * Enable config mocking * Sanitize datalinks urls * Update public/app/core/config.ts * Minor test update * Remove sanitize-url dependency * Remove typings * Review update * Revert "Remove sanitize-url dependency" This reverts commitc4f38e6de6
. * Revert "Remove typings" This reverts commit676d47e8c2
. * Sanitaze, don't escape html when sanitizing URL
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import xss from 'xss';
|
|
import { sanitizeUrl as braintreeSanitizeUrl } from '@braintree/sanitize-url';
|
|
|
|
const XSSWL = Object.keys(xss.whiteList).reduce((acc, element) => {
|
|
// @ts-ignore
|
|
acc[element] = xss.whiteList[element].concat(['class', 'style']);
|
|
return acc;
|
|
}, {});
|
|
|
|
const sanitizeXSS = new xss.FilterXSS({
|
|
whiteList: XSSWL,
|
|
});
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
export function sanitize(unsanitizedString: string): string {
|
|
try {
|
|
return sanitizeXSS.process(unsanitizedString);
|
|
} catch (error) {
|
|
console.log('String could not be sanitized', unsanitizedString);
|
|
return unsanitizedString;
|
|
}
|
|
}
|
|
|
|
export function sanitizeUrl(url: string): string {
|
|
return braintreeSanitizeUrl(url);
|
|
}
|
|
|
|
export function hasAnsiCodes(input: string): boolean {
|
|
return /\u001b\[\d{1,2}m/.test(input);
|
|
}
|
|
|
|
export function escapeHtml(str: string): string {
|
|
return String(str)
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"');
|
|
}
|