fix Incomplete URL substring sanitization Unvalidated Redirects and BXSS #9435
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.
aws-cli/awscli/customizations/codecommit.py
Line 99 in f38990e
fix the problem need to parse the URL and check the host value correctly. Instead of checking if "amazonaws.com" is a substring of the host, we should use the
urlparse
function to extract the hostname and then check if it ends with "amazonaws.com". This ensures that the check is accurate and not prone to bypasses.urlparse
function from theurllib.parse
module.Sanitizing untrusted URLs is a common 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
evil-redacted.com
domain.The first two examples show unsafe checks that are easily bypassed. In
unsafe1
the attacker can simply addevil.com
anywhere in the url, for the vulnerablehttp://<aws-host>/evil.com
. Insafe2
,urlparse
is used to parse the URL, then the hostname is checked to make sure it ends with<aws-host>.evil.com
.References
SSRF
XSS Unvalidated Redirects and Forwards Cheat Sheet
CWE-20