mirror of
https://github.com/zulip/zulip.git
synced 2025-10-23 04:52:12 +00:00
frontend: Defensively filter unsafe links that may come from bugdown.
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
committed by
Tim Abbott
parent
64856d858e
commit
76ce370181
@@ -311,10 +311,12 @@ run_test("clean_user_content_links", () => {
|
|||||||
util.clean_user_content_links(
|
util.clean_user_content_links(
|
||||||
'<a href="http://example.com">good</a> ' +
|
'<a href="http://example.com">good</a> ' +
|
||||||
'<a href="http://localhost:NNNN">invalid</a> ' +
|
'<a href="http://localhost:NNNN">invalid</a> ' +
|
||||||
|
'<a href="javascript:alert(1)">unsafe</a> ' +
|
||||||
'<a href="/#fragment" target="_blank">fragment</a>'
|
'<a href="/#fragment" target="_blank">fragment</a>'
|
||||||
),
|
),
|
||||||
'<a href="http://example.com" target="_blank" rel="noopener noreferrer">good</a> ' +
|
'<a href="http://example.com" target="_blank" rel="noopener noreferrer">good</a> ' +
|
||||||
'<a>invalid</a> ' +
|
'<a>invalid</a> ' +
|
||||||
|
'<a>unsafe</a> ' +
|
||||||
'<a href="/#fragment">fragment</a>'
|
'<a href="/#fragment">fragment</a>'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@@ -367,9 +367,17 @@ exports.clean_user_content_links = function (html) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We detect URLs that are just fragments by comparing the URL
|
if (
|
||||||
// against a new URL generated using only the hash.
|
// eslint-disable-next-line no-script-url
|
||||||
if (url.hash === "" || url.href !== new URL(url.hash, window.location.href).href) {
|
["data:", "javascript:", "vbscript:"].indexOf(url.protocol) !== -1
|
||||||
|
) {
|
||||||
|
// Remove unsafe links completely.
|
||||||
|
elt.removeAttribute("href");
|
||||||
|
} else if (
|
||||||
|
// We detect URLs that are just fragments by comparing the URL
|
||||||
|
// against a new URL generated using only the hash.
|
||||||
|
url.hash === "" || url.href !== new URL(url.hash, window.location.href).href
|
||||||
|
) {
|
||||||
elt.setAttribute("target", "_blank");
|
elt.setAttribute("target", "_blank");
|
||||||
elt.setAttribute("rel", "noopener noreferrer");
|
elt.setAttribute("rel", "noopener noreferrer");
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user