ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
36 lines • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSafeUrl = exports.getSafeUrl = void 0;
/**
* Return the URL if it can be used as a link href without executing code,
* or undefined otherwise (and log an error)
*
* Rejects javascript: URLs, including obfuscated ones (mixed case,
* whitespace, control characters). Relative URLs are safe.
*
* @example
* getSafeUrl('https://example.com'); // 'https://example.com'
* getSafeUrl('/posts/1'); // '/posts/1'
* getSafeUrl('javascript:alert(1)'); // undefined
*/
const getSafeUrl = (url) => {
if ((0, exports.isSafeUrl)(url)) {
return url;
}
console.error(`Blocked unsafe URL: ${JSON.stringify(url)}`);
return undefined;
};
exports.getSafeUrl = getSafeUrl;
/**
* Check that a URL can be used as a link href without executing code
*
* @see getSafeUrl
*/
const isSafeUrl = (url) => !isJavaScriptProtocol.test(url);
exports.isSafeUrl = isSafeUrl;
// A javascript: URL can contain leading C0 control characters or spaces,
// and tabs or newlines anywhere (https://url.spec.whatwg.org/#url-parsing)
const isJavaScriptProtocol =
// eslint-disable-next-line no-control-regex
/^[\u0000-\u001F ]*j[\r\n\t]*a[\r\n\t]*v[\r\n\t]*a[\r\n\t]*s[\r\n\t]*c[\r\n\t]*r[\r\n\t]*i[\r\n\t]*p[\r\n\t]*t[\r\n\t]*:/i;
//# sourceMappingURL=getSafeUrl.js.map