Skip to content

Degrade gracefully on malformed URLs in _parse#141

Merged
adbar merged 1 commit into
adbar:masterfrom
santhreal:fix/parse-malformed-url-valueerror
Jul 18, 2026
Merged

Degrade gracefully on malformed URLs in _parse#141
adbar merged 1 commit into
adbar:masterfrom
santhreal:fix/parse-malformed-url-valueerror

Conversation

@santhreal

Copy link
Copy Markdown
Contributor

get_base_url()/get_hostinfo() and other helpers that route through the shared _parse() choke point leak a raw ValueError on malformed URLs like HTTP://[::1]& ("Invalid IPv6 URL" from urlsplit). These helpers already degrade to empty output for hostless input, so a caller iterating over a list of scraped URLs hits an uncaught crash on one bad entry instead of the same graceful skip.

Wrap the urlsplit call in _parse in a try/except ValueError that returns an empty SplitResult, so malformed URLs degrade like hostless ones. Added a regression test asserting the specific malformed inputs return empty host info rather than raising.

Repro:

from courlan import get_base_url
get_base_url('HTTP://[::1]&')   # ValueError: Invalid IPv6 URL

urlsplit raises ValueError on some malformed inputs (e.g. a bad IPv6
literal like HTTP://[::1]&). This leaked out of get_base_url and
get_hostinfo, which are contracted to return a value for any string.
Guard _parse so those inputs degrade to empty parts, matching how a
hostless URL already parses. get_host_and_path still raises its own
documented incomplete-URL error.
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (f73bb10) to head (43463e8).

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #141   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           12        12           
  Lines          857       860    +3     
  Branches       167       167           
=========================================
+ Hits           857       860    +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@adbar

adbar commented Jul 18, 2026

Copy link
Copy Markdown
Owner

Thanks!

@adbar
adbar merged commit 6f0f507 into adbar:master Jul 18, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants