Repository navigation
fix(dsn): raise BadDsn instead of a bare ValueError for invalid ports - #8416
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
Dsn.__init__ reads parts.port, which urlsplit parses lazily and which raises a bare ValueError for ports outside 0-65535 or not numeric. The exception escapes Dsn, so a typo in SENTRY_DSN breaks Client() at startup with a traceback in urllib/parse.py and except BadDsn cannot catch it. Every other field in __init__ raises BadDsn, and project_id already uses the same try/except idiom. Extend it to the port.
Contributor
PR risk: mediumFor commit Main concerns: reach, host stability, public contract. This label is a hint for reviewers, not an approval. |
qianeric-backup
marked this pull request as ready for review
November 27, 2026 06:36
This branch has not been deployed
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.
A DSN with a malformed port raises
urllib.parse'sValueErrorout ofDsn, notBadDsn:SplitResult.portparses lazily, so the exception surfaces on the line that reads it and carries none of the DSN context — the traceback points aturllib/parse.pyand the message doesn't say which DSN was wrong. Because the DSN is parsed while the client is constructed (Client.__init__→make_transport→Transport.__init__→Dsn), a typo inSENTRY_DSNtakes down initialisation, andexcept BadDsn:— the documented way to handle a bad DSN — does not catch it.Every other field in
Dsn.__init__is validated asBadDsn, andproject_idalready uses the exact idiom this needs:so the change just extends it to the one field that reaches into
urllibdirectly:The message reports
netlocwith the userinfo stripped, so a mistyped DSN in a log or crash report doesn't leak the secret key.Tests
Two cases added to the existing
test_dsn_validationstable intests/utils/test_general.py:https://foobar@example.com:foo/1and...:65536/1, both expecting the new message — the table already covers the other fourBadDsnpaths, so this is the same shape rather than a new test module.masterwith only the tests:2 failed, 9 passed11 passedtests, excludingtests/integrations):164 failed, 1437 passed; the164are identical on both trees and thecomm -3againstmasteris empty. They are environment noise here — 156 fromtest_shadowed_module.pywith half the optional integrations installed, 4 transports missingh2, 2 urllib3-length assertions, 1 without dist metadata, 1span_streaming.o1234.org prefix, copy-construction,org_id=override) dump identically before and after.ruff==0.17.6(theuv.lockpin):ruff checkandruff format --checkclean on both trees.I did not fix the same bare-
ValueErroron a malformed IPv6 host inurlsplit— it raises before any of the sibling checks, so folding it in would make this PR argue two things at once. Happy to follow up separately.AI disclosure
Written with an AI coding assistant (Claude): used to trace the construction path, add the guard and the table entries, and run the before/after comparison. Reviewed by hand against the unpatched tree.