libreoffice-online/browser/js/sanitize-url.js
Gökay Şatır 6ddabcb792 Update sanitizeurl function calls.
Signed-off-by: Gökay Şatır <gokaysatir@collabora.com>
Change-Id: I07c12c072240307ea67d4f8f9ade8c1a9f63d1b8
2023-04-01 17:49:56 +03:00

38 lines
1.3 KiB
JavaScript

var invalidProtocolRegex = /^([^\w]*)(javascript|data|vbscript)/im;
var htmlEntitiesRegex = /&#(\w+)(^\w|;)?/g;
var htmlCtrlEntityRegex = /&(newline|tab);/gi;
var ctrlCharactersRegex = /[\u0000-\u001F\u007F-\u009F\u2000-\u200D\uFEFF]/gim;
var urlSchemeRegex = /^.+(:|&colon;)/gim;
var relativeFirstCharacters = [".", "/"];
function isRelativeUrlWithoutProtocol(url) {
return relativeFirstCharacters.indexOf(url[0]) > -1;
}
// adapted from https://stackoverflow.com/a/29824550/2601552
function decodeHtmlCharacters(str) {
return str.replace(htmlEntitiesRegex, function (match, dec) {
return String.fromCharCode(dec);
});
}
function sanitizeUrl(url) {
var sanitizedUrl = decodeHtmlCharacters(url || "")
.replace(htmlCtrlEntityRegex, "")
.replace(ctrlCharactersRegex, "")
.trim();
if (!sanitizedUrl) {
return "about:blank";
}
if (isRelativeUrlWithoutProtocol(sanitizedUrl)) {
return sanitizedUrl;
}
var urlSchemeParseResults = sanitizedUrl.match(urlSchemeRegex);
if (!urlSchemeParseResults) {
return sanitizedUrl;
}
var urlScheme = urlSchemeParseResults[0];
if (invalidProtocolRegex.test(urlScheme)) {
return "about:blank";
}
return sanitizedUrl;
}
window.sanitizeUrl = sanitizeUrl;