fix Incomplete URL substring sanitization on revision()
#8653
+6
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
treeherder/ui/helpers/revision.js
Line 16 in 07236d2
fix the issue the code should parse the URL and explicitly check the host component to ensure it matches the expected value (
hg.mozilla.org
). This can be achieved using theURL
class, which provides a reliable way to parse and extract the host from a URL string. The fix involves replacing the substring check with a host comparison using theURL
class.Sanitizing untrusted URLs is an important technique for preventing attacks such as request forgeries and malicious redirections. Usually, this is done by checking that the host of a URL is in a set of allowed hosts. However, treating the URL as a string and checking if one of the allowed hosts is a substring of the URL is very prone to errors. Malicious URLs can bypass such security checks by embedding one of the allowed hosts in an unexpected location. Even if the substring check is not used in a security-critical context, the incomplete check may still cause undesirable behaviors when the check succeeds accidentally.
POC
The following code checks that a URL redirection will reach the
hg.mozilla.org
domain, or one of its subdomains, and not some malicious site.The substring check is, however, easy to bypass. by embedding redacted.com in the path component:
http://evil-hg.mozilla.net/hg.mozilla.org
, or in the query string component:http://evil-hg.mozilla.net/?x=hg.mozilla.org
. Address these shortcomings by checking the host of the parsed URL instead:This is still not a sufficient check as the following URLs bypass it:
http://evil-hg.mozilla.com http://hg.mozilla.org.evil-redacted.net
. Instead, use an explicit whitelist of allowed hosts to make the redirect secure:References
SSRF
XSS Unvalidated Redirects and Forwards Cheat Sheet
CWE-20