2022-10-11 01:57:18 +05:30
|
|
|
import DOMPurify from 'dompurify';
|
2021-10-29 20:43:33 +05:30
|
|
|
import { getNormalizedURL, getBaseURL, relativePathToAbsolute } from '~/lib/utils/url_utility';
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
const { sanitize: dompurifySanitize, addHook, isValidAttribute } = DOMPurify;
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
export const defaultConfig = {
|
2021-10-27 15:23:28 +05:30
|
|
|
// Safely allow SVG <use> tags
|
2022-01-26 12:08:38 +05:30
|
|
|
ADD_TAGS: ['use', 'gl-emoji', 'copy-code'],
|
2021-10-27 15:23:28 +05:30
|
|
|
// Prevent possible XSS attacks with data-* attributes used by @rails/ujs
|
|
|
|
// See https://gitlab.com/gitlab-org/gitlab-ui/-/issues/1421
|
2022-11-25 23:54:43 +05:30
|
|
|
FORBID_ATTR: [
|
|
|
|
'data-remote',
|
|
|
|
'data-url',
|
|
|
|
'data-type',
|
|
|
|
'data-method',
|
|
|
|
'data-disable-with',
|
|
|
|
'data-disabled',
|
|
|
|
'data-disable',
|
|
|
|
'data-turbo',
|
|
|
|
],
|
2022-07-16 23:28:13 +05:30
|
|
|
FORBID_TAGS: ['style', 'mstyle'],
|
2022-08-13 15:12:31 +05:30
|
|
|
ALLOW_UNKNOWN_PROTOCOLS: true,
|
2021-01-03 14:25:43 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
// Only icons urls from `gon` are allowed
|
|
|
|
const getAllowedIconUrls = (gon = window.gon) =>
|
2021-10-29 20:43:33 +05:30
|
|
|
[gon.sprite_file_icons, gon.sprite_icons]
|
|
|
|
.filter(Boolean)
|
|
|
|
.map((path) => relativePathToAbsolute(path, getBaseURL()));
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-10-29 20:43:33 +05:30
|
|
|
const isUrlAllowed = (url) =>
|
|
|
|
getAllowedIconUrls().some((allowedUrl) => getNormalizedURL(url).startsWith(allowedUrl));
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-10-29 20:43:33 +05:30
|
|
|
const isHrefSafe = (url) => url.match(/^#/) || isUrlAllowed(url);
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
const removeUnsafeHref = (node, attr) => {
|
|
|
|
if (!node.hasAttribute(attr)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isHrefSafe(node.getAttribute(attr))) {
|
|
|
|
node.removeAttribute(attr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
/**
|
|
|
|
* Appends 'noopener' & 'noreferrer' to rel
|
|
|
|
* attr values to prevent reverse tabnabbing.
|
|
|
|
*
|
|
|
|
* @param {String} rel
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
const appendSecureRelValue = (rel) => {
|
|
|
|
const attributes = new Set(rel ? rel.toLowerCase().split(' ') : []);
|
|
|
|
|
|
|
|
attributes.add('noopener');
|
|
|
|
attributes.add('noreferrer');
|
|
|
|
|
|
|
|
return Array.from(attributes).join(' ');
|
|
|
|
};
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
/**
|
|
|
|
* Sanitize icons' <use> tag attributes, to safely include
|
|
|
|
* svgs such as in:
|
|
|
|
*
|
|
|
|
* <svg viewBox="0 0 100 100">
|
|
|
|
* <use href="/assets/icons-xxx.svg#icon_name"></use>
|
|
|
|
* </svg>
|
|
|
|
*
|
2021-10-29 20:43:33 +05:30
|
|
|
* It validates both href & xlink:href attributes.
|
|
|
|
* Note that `xlink:href` is deprecated, but still in use
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href
|
|
|
|
*
|
2021-01-03 14:25:43 +05:30
|
|
|
* @param {Object} node - Node to sanitize
|
|
|
|
*/
|
2021-03-08 18:12:59 +05:30
|
|
|
const sanitizeSvgIcon = (node) => {
|
2021-01-03 14:25:43 +05:30
|
|
|
removeUnsafeHref(node, 'href');
|
|
|
|
removeUnsafeHref(node, 'xlink:href');
|
|
|
|
};
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
addHook('afterSanitizeAttributes', (node) => {
|
2021-01-03 14:25:43 +05:30
|
|
|
if (node.tagName.toLowerCase() === 'use') {
|
|
|
|
sanitizeSvgIcon(node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
const TEMPORARY_ATTRIBUTE = 'data-temp-href-target';
|
|
|
|
|
|
|
|
addHook('beforeSanitizeAttributes', (node) => {
|
|
|
|
if (node.tagName === 'A' && node.hasAttribute('target')) {
|
|
|
|
node.setAttribute(TEMPORARY_ATTRIBUTE, node.getAttribute('target'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
addHook('afterSanitizeAttributes', (node) => {
|
|
|
|
if (node.tagName === 'A' && node.hasAttribute(TEMPORARY_ATTRIBUTE)) {
|
|
|
|
node.setAttribute('target', node.getAttribute(TEMPORARY_ATTRIBUTE));
|
|
|
|
node.removeAttribute(TEMPORARY_ATTRIBUTE);
|
|
|
|
if (node.getAttribute('target') === '_blank') {
|
|
|
|
const rel = node.getAttribute('rel');
|
|
|
|
node.setAttribute('rel', appendSecureRelValue(rel));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
export const sanitize = (val, config) => dompurifySanitize(val, { ...defaultConfig, ...config });
|
2022-08-27 11:52:29 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
export { isValidAttribute };
|