fix: Reject non-http(s) URL schemes in HTTP clients#1862
Open
fix: Reject non-http(s) URL schemes in HTTP clients#1862
Conversation
URLs from untrusted sources (sitemap entries, robots.txt `Sitemap:` directives, scraped HTML passed to `context.send_request()`) reached the HTTP-client backends as raw strings, letting schemes like `gopher://`, `file://`, `dict://` or `ftp://` through to libcurl/httpx/impit. Validate the scheme at `send_request` and `stream` entry points so non-http(s) URLs are rejected before any request is issued.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1862 +/- ##
==========================================
+ Coverage 92.43% 92.47% +0.04%
==========================================
Files 158 158
Lines 11009 11020 +11
==========================================
+ Hits 10176 10191 +15
+ Misses 833 829 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes a gap where URLs from untrusted sources (sitemap entries,
robots.txtSitemap:directives, raw strings passed toBasicCrawlingContext.send_request()) reached the HTTP-client backends without scheme validation, lettinggopher://,file://,dict://,ftp://etc. through to libcurl / httpx / impit.What changed
validate_http_url(url)at the top ofsend_requestandstreaminImpitHttpClient,HttpxHttpClient,CurlImpersonateHttpClient, andPlaywrightHttpClient. Non-http(s) schemes now raisepydantic.ValidationErrorbefore any request is issued.crawl()was already covered, sinceRequest.urlis validated by Pydantic on construction.Tests
tests/unit/_utils/test_urls.py— parametrized coverage of other schemes (gopher://,file://,dict://,ftp://,javascript:, no-scheme) againstvalidate_http_url.tests/unit/http_clients/test_http_clients.py— one smoke test per(client × method)verifying each HTTP client wires up the validation, using the canonical PoC URLgopher://127.0.0.1:6379/_PING.