Repository navigation
lab-ops-hub: percent-encode non-ASCII outside the host in the container validator (#615) #616
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,21 @@ test("validateCrawlUrl accepts public http(s) targets", () => { | |
| assert.equal(validateCrawlUrl("https://8.8.8.8/").ok, true); | ||
| }); | ||
|
|
||
| test("validateCrawlUrl percent-encodes non-ASCII outside the host (#615)", () => { | ||
| // The Worker's returned form is the reference the container validator | ||
| // mirrors: WHATWG UTF-8 percent-encoding in userinfo/path/query. | ||
| const cases: Array<[string, string]> = [ | ||
| ["http://example.com/café", "http://example.com/caf%C3%A9"], | ||
| ["http://example.com/?q=café", "http://example.com/?q=caf%C3%A9"], | ||
| ["http://user:café@example.com/", "http://user:caf%C3%[email protected]/"], | ||
| ]; | ||
| for (const [raw, expected] of cases) { | ||
| const r = validateCrawlUrl(raw); | ||
| assert.equal(r.ok, true, raw); | ||
| if (r.ok) assert.equal(r.url, expected, raw); | ||
| } | ||
| }); | ||
|
|
||
| test("validateCrawlUrl refuses non-http schemes and junk", () => { | ||
| assert.equal(validateCrawlUrl("ftp://example.com/").ok, false); | ||
| assert.equal(validateCrawlUrl("file:///etc/passwd").ok, false); | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -884,6 +884,33 @@ def test_accepts_public_targets(self): | |
| ): | ||
| self.assertAccepted(url) | ||
|
|
||
| def test_percent_encodes_non_ascii_outside_the_host(self): | ||
| # WHATWG percent-encodes non-ASCII characters in the userinfo, | ||
| # path, query and fragment as UTF-8; urlparse hands them through | ||
| # verbatim, and http.client then raises UnicodeEncodeError on | ||
| # the returned URL before any request is sent — a target both | ||
| # validators accepted 502s in the container (#615). The returned | ||
| # URL carries the encoded form; the host keeps its spelling | ||
| # (judged in mapped form, fetched via IDNA) and existing %XX | ||
| # sequences are not double-encoded. | ||
| cases = { | ||
| "http://example.com/café": "http://example.com/caf%C3%A9", | ||
| "http://example.com/日本語": ( | ||
| "http://example.com/%E6%97%A5%E6%9C%AC%E8%AA%9E" | ||
| ), | ||
| "http://example.com/?q=café": "http://example.com/?q=caf%C3%A9", | ||
| "http://example.com/?q=\U0001f600": ("http://example.com/?q=%F0%9F%98%80"), | ||
| "http://example.com/x#café": "http://example.com/x#caf%C3%A9", | ||
| "http://user:café@example.com/": ("http://user:caf%C3%[email protected]/"), | ||
| "http://café@example.com/": "http://caf%C3%[email protected]/", | ||
| "http://münchen.de/café": "http://münchen.de/caf%C3%A9", | ||
| } | ||
| for raw, expected in cases.items(): | ||
| self.assertEqual(validate_crawl_url(raw), expected, raw) | ||
| # Already-encoded spellings pass through byte-identical. | ||
| self.assertAccepted("http://example.com/caf%C3%A9") | ||
| self.assertAccepted("http://example.com/?q=caf%C3%A9") | ||
|
|
||
|
|
||
| class TestFetchCrawl(unittest.TestCase): | ||
| # _fetch_crawl fetches an already-validated URL (validation is | ||
|
|
@@ -989,6 +1016,18 @@ def test_redirect_failures_raise_not_a_302_result(self): | |
| self.assertEqual(status, 200) | ||
| self.assertIn(b"Missing Thing", raw) | ||
|
|
||
| def test_fetches_percent_encoded_non_ascii_targets(self): | ||
| # #615: the form validate_crawl_url returns for a non-ASCII | ||
| # path/query is fetchable end to end (the raw spelling raises | ||
| # UnicodeEncodeError inside http.client before any request). | ||
| url = app._url_with_encoded_non_ascii( | ||
| f"http://127.0.0.1:{self.port}/café?q=café" | ||
| ) | ||
| self.assertEqual(url, f"http://127.0.0.1:{self.port}/caf%C3%A9?q=caf%C3%A9") | ||
| status, raw = app._fetch_crawl(url) | ||
| self.assertEqual(status, 200) | ||
| self.assertIn(b"Missing Thing", raw) | ||
|
|
||
| def test_transport_failure_still_raises(self): | ||
| import urllib.error | ||
|
|
||
|
|
||
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.
Re-serializing every accepted URL with
ParseResult.geturl()drops delimiters for empty components, so the container now changes otherwise-ASCII request targets:http://example.com/path?becomeshttp://example.com/path, andhttp://example.com/a;becomeshttp://example.com/a. The Worker’snew URL(raw).toString()andurllib.request.Request(raw).selectorpreserve these delimiters, so routes, caches, or signature checks that distinguish the original target will crawl the wrong resource; preserve empty?/;delimiters while encoding non-ASCII text.Useful? React with 👍 / 👎.