Repository navigation
lab-ops-hub: decode-and-judge percent-encoded crawl hosts (#529) #533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,7 +255,10 @@ def test_refuses_percent_encoded_hosts_and_zone_ids(self): | |
| # (%6cocalhost is localhost) and rejects the other % spellings | ||
| # outright; urlparse passes them through verbatim and the old | ||
| # code mistook them for public DNS names, while ipaddress | ||
| # accepted the IPv6 zone id (#505). | ||
| # accepted the IPv6 zone id (#505). Every case here still | ||
| # refuses under decode-and-judge (#529): the first four decode | ||
| # to blocked or forbidden-character hosts, the rest are invalid | ||
| # escapes or a zone id. | ||
| for url in ( | ||
| "http://%6cocalhost/", | ||
| "http://localhost%00/", | ||
|
|
@@ -269,6 +272,60 @@ def test_refuses_percent_encoded_hosts_and_zone_ids(self): | |
| ): | ||
| self.assertRefused(url) | ||
|
|
||
| def test_accepts_percent_encoded_spellings_of_public_hosts(self): | ||
| # #505's blanket refusal also refused spellings that decode to | ||
| # public hosts, which the Worker validator accepts — the two | ||
| # validators diverged, including on redirect targets, which | ||
| # are validated raw (#529). The decoded form is judged, and | ||
| # the validated URL carries the decoded host so the fetcher | ||
| # connects to the host that was judged (urllib does not | ||
| # percent-decode hosts). | ||
| for url, expected in ( | ||
| ("http://%65xample.com/", "http://example.com/"), | ||
| ("http://example%2ecom/", "http://example.com/"), | ||
| ("http://8%2e8%2e8%2e8/", "http://8.8.8.8/"), | ||
| ("http://%38%2e8.8.8/", "http://8.8.8.8/"), | ||
| # Port, path and userinfo survive the host substitution. | ||
| ("http://%65xample.com:8080/page", "http://example.com:8080/page"), | ||
| ( | ||
| "http://user@%65xample.com/page", | ||
| "http://[email protected]/page", | ||
| ), | ||
| ): | ||
| self.assertEqual(validate_crawl_url(url), expected, url) | ||
|
|
||
| def test_refuses_percent_decoded_blocked_and_invalid_hosts(self): | ||
| # Decode-and-judge (#529): spellings that decode to blocked | ||
| # targets refuse via the ordinary checks, and spellings WHATWG | ||
| # rejects outright (decoded delimiters, a double-encoded %, | ||
| # undecodable bytes) refuse at the decode step. | ||
| for url in ( | ||
| "http://127%2e0%2e0%2e1/", # -> 127.0.0.1 | ||
| "http://foo%2elocalhost/", # -> foo.localhost | ||
| "http://exam%2fple.com/", # -> exam/ple.com (delimiter) | ||
| "http://exam%3aple.com/", # -> exam:ple.com (delimiter) | ||
| "http://example%252ecom/", # -> example%2ecom (still has %) | ||
| "http://%ff.com/", # decoded bytes are not valid UTF-8 | ||
| "http://%c3%28.com/", # truncated UTF-8 sequence | ||
| ): | ||
| self.assertRefused(url) | ||
|
|
||
| def test_redirect_handler_follows_the_decoded_target(self): | ||
| # Redirect targets are validated raw, with no Worker | ||
| # normalisation in between (#529): the handler must fetch the | ||
| # decoded form of a percent-encoded public target, and refuse | ||
| # one that decodes to a blocked target. | ||
| import urllib.request | ||
|
|
||
| handler = app._ValidatingRedirectHandler() | ||
| req = urllib.request.Request("http://example.com/") | ||
| followed = handler.redirect_request( | ||
| req, None, 302, "Found", {}, "http://%65xample.org/final" | ||
| ) | ||
| self.assertEqual(followed.full_url, "http://example.org/final") | ||
| with self.assertRaises(ValueError): | ||
| handler.redirect_request(req, None, 302, "Found", {}, "http://%6cocalhost/") | ||
|
|
||
| def test_refuses_space_hosts_and_ipvfuture_literals(self): | ||
| # A space in the host is not a DNS name: WHATWG rejects it and | ||
| # http.client only raises for it deep inside the fetcher, so the | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a percent escape decodes to a Unicode compatibility delimiter,
_percent_decode_hostaccepts it because the character is not yet an ASCII delimiter, but the subsequent NFKC normalization converts it into one. For example,http://127.0.0.1%EF%BC%8Fx/decodes to a host containing a fullwidth slash, passes validation as127.0.0.1/x, and is rebuilt ashttp://127.0.0.1/x/;urllibthen connects to loopback. This bypasses the crawler's SSRF policy, so the delimiter check must also run after host normalization.Useful? React with 👍 / 👎.