Skip to content

Reject unparseable IRI authorities before encoding - #169

Open
anishpentyala wants to merge 2 commits into
python-hyper:mainfrom
anishpentyala:fix/iri-invalid-authority
Open

anishpentyala wants to merge 2 commits into
python-hyper:mainfrom
anishpentyala:fix/iri-invalid-authority

Conversation

@anishpentyala

Copy link
Copy Markdown

IRIReference.encode() currently rebuilds an authority through the host, userinfo, and port convenience properties. Those properties suppress InvalidAuthority and return None when the existing authority parser cannot parse the authority. Encoding then silently drops the authority:

>>> from rfc3986 import iri_reference
>>> iri_reference("https://example.com:bad/path?query#fragment").encode().unsplit()
'https:/path?query#fragment'

This calls authority_info() before rebuilding so an unparseable, nonempty authority raises the existing InvalidAuthority exception instead. Existing property dispatch, custom IDNA encoder behavior, missing-dependency precedence, and successful encodings are preserved.

Tests add 16 regression cases covering malformed ports, whitespace, malformed IPv6, invalid IPv4, and an invalid host character, each with the default and a custom encoder. Thirteen control cases cover valid credentials, IPv6/zone IDs, absent hosts/authorities, and the existing empty-port normalization. Against the unchanged base, all 16 regressions fail and all 13 controls pass.

Validation on CPython 3.10.12 and 3.13.17:

  • Baseline: 2,914 tests pass.
  • Fixed: 2,943 tests pass, 100% coverage.
  • Black, isort, and flake8 pass. The project's typing gate passes at 76.95% (75% required).
  • A fresh clone of the published branch also passes all 2,943 tests with 100% coverage on 3.13.17.

Scope and tradeoffs: this follows the existing authority parser, not a new claim of full RFC validity. For example, the parser currently rejects empty userinfo (@example.com), even though that form is allowed by the RFC; this change raises instead of silently discarding it. Existing empty-port stripping and numeric port-range behavior are unchanged. The extra parse measured about 1-1.5 microseconds per credential-bearing encode locally (roughly 4-8% overhead), so this is not throughput-neutral. No unrelated normalization or validation behavior is changed.

Preserve malformed nonempty authorities as InvalidAuthority rather than silently erasing them during encode. Source independently reviewed at bc90e68a6a8acf1f483359455c0ddf12224976da.
Preserve malformed nonempty authorities as InvalidAuthority rather than silently erasing them during encode. Source independently reviewed at bc90e68a6a8acf1f483359455c0ddf12224976da.
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.

1 participant