Opened 6 days ago

Closed 5 days ago

#36007 closed Cleanup/optimization (fixed)

Dead code in URLValidator

Reported by: Mike Edmunds Owned by: Mike Edmunds
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

There's some dead code in django.core.validators.URLValidator which could be removed:

  • The entire Trivial case failed. Try for possible IDN domain section is no longer useful. This code attempts to re-validate a failed URL after encoding an international domain name using IDNA 2003 ("punycode"). But the URLValidator regular expressions have allowed all IDNs since Commit 2e65d56 (for #20003, in 2015), so the super call will never fail with a validation error that switching to IDNA 2003 would let pass.

For the first case, one way to verify the code is no longer in use is to run URLValidator on https://މިހާރު.com, which is a domain allowed by IDNA 2008 but prohibited by IDNA 2003. If the punycode() branch were coming into play, that URL would be rejected:

from django.core.validators import URLValidator
URLValidator()("https://މިހާރު.com")
# (No error)

from django.utils.encoding import punycode
punycode("މިހާރު.com")
# UnicodeError: Violation of BIDI requirement 3
# encoding with 'idna' codec failed

Change History (5)

comment:1 by Mike Edmunds, 6 days ago

Has patch: set

comment:2 by Claude Paroz, 5 days ago

Component: UncategorizedCore (Other)
Triage Stage: UnreviewedReady for checkin
Version: 5.1dev

comment:3 by JaeHyuckSa, 5 days ago

Owner: set to Mike Edmunds
Status: newassigned

comment:4 by Sarah Boyce <42296566+sarahboyce@…>, 5 days ago

In 9a891c38:

Refs #36007 -- Added IDNA 2008 test case for URLValidator.

Test a domain that is valid under IDNA 2008 but not IDNA 2003. This
helps verify that the branch in URLValidator which calls punycode() is
not actually being used for IDNs. punycode() implements IDNA 2003, so
the domain would fail to validate if that branch were active for IDNs.

comment:5 by Sarah Boyce <42296566+sarahboyce@…>, 5 days ago

Resolution: fixed
Status: assignedclosed

In 5405912:

Fixed #36007 -- Removed dead code from URLValidator.

The "Trivial case failed. Try for possible IDN domain" handling was
obsoleted by ticket-20003, which adjusted the regular expressions to
allow all international domain names (Refs #20003).

Uses of ul were moved to DomainNameValidator in ticket-18119
(Refs #18119).

Note: See TracTickets for help on using tickets.
Back to Top