Repository navigation
Backport hostname and IDNA validation fixes to 2.x - #1383
Conversation
#1377) * Reject over-long hostnames and invalid A-labels in the hostname format * Decode A-labels with Punycode instead of java.net.IDN java.net.IDN implements IDNA2003, so it rejects A-labels whose U-label has code points that IDNA2008 allows in context (U+00DF, U+03C2, U+200C, U+200D). Decode the Punycode directly, require it to round-trip, and leave the code points to the RFC 5892 rules. * Address review: keep hostname A-label checks lenient on newer code points - hostname: check A-labels with RFC5892.isValidALabel, which does not reject code points that the bundled Unicode 5.2 table lists as unassigned when the JDK assigns them (e.g. xn--ls8h.la, U+1F600, Adlam) - Lower-case A-labels before decoding, so XN--BCHER-KVA and xn--Bcher-kva pass - idn-hostname: measure the 63-character limit with Punycode.encode instead of IDN.toASCII, and limit names to 253 characters in A-label form - Check label length before the quadratic Punycode decode - Test IDNA rules once per code point instead of once per char - Make Punycode package-private and drop the unreachable grow branch - Add PunycodeTest, RFC5892Test and HostnameFormatTest - Refresh idn-hostname.json, and draft-next hostname.json and idn-hostname.json * Disable the refreshed idn-hostname cases the validator does not support yet The current suite adds idn-hostname cases for UTS 46 mapping, trailing label separators and the RFC 5893 digit rules. These are existing gaps, not regressions, so mark them disabled with a reason instead of widening this PR. * Apply the same unassigned-code-point policy to idn-hostname hostname accepted xn--ls8h.la while idn-hostname rejected it, because only hostname ignored the Unicode 5.2 table's UNASSIGNED entries. Use one rule set for both, so idn-hostname accepts every name hostname accepts. * Address second review: derive new code points, any virama, code point hyphens - Accept a code point the Unicode 5.2 table lists as unassigned only if the JDK classifies it as Ll, Lm, Lo, Mn, Mc or Nd and NFKC and lower-casing leave it unchanged, so emoji, symbols, upper case and bidi controls added since are rejected (an approximation of the RFC 5892 derivation) - Accept ZWJ and ZWNJ after any Canonical_Combining_Class Virama code point, not only U+094D, and read the preceding code point, not char - Count the RFC 5891 4.2.3.1 hyphen positions in code points - Apply RFC 5893 rules 1, 3 and 4 to any label with an R, AL or AN character - Reject Cherokee small letters, which case folding maps to the capitals - Publish the lazily loaded RFC 5892 and joining-type tables with a volatile flag instead of checking BitSet.isEmpty() outside the lock - Walk hostname labels without splitting, and check ASCII without a stream - Replace the incorrect "idn-hostname accepts everything hostname accepts" test with tests for the new rules * Judge CONTEXTO by script and step ZWNJ context by code point Found by comparing against Python idna 3.20 on every assigned code point: - KATAKANA MIDDLE DOT needs a Hiragana, Katakana or Han character anywhere in the label (RFC 5892 A.7), not only right after it. - Greek, Hebrew and Kana/Han checks use Character.UnicodeScript, not code blocks (Coptic in the Greek block, U+30FC is Common). - The ZWNJ joining-type scan steps over supplementary transparent marks by code point instead of by UTF-16 char. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01U6XXMPvkRmkr5kqQuxPXm4 * Follow RFC 6452 and case folding for code points outside the 5.2 table Found by comparing against Python idna 3.20 on every assigned code point: - U+0CF1 and U+0CF2 are PVALID and U+19DA is DISALLOWED since Unicode 6.0 (RFC 6452), unlike the bundled Unicode 5.2 table. - New letters that case folding maps to another letter, such as U+1C80..U+1C88, are rejected, using lower(upper(c)) as the fold. - U+180F, a default-ignorable mark, is rejected. - New Cherokee capitals such as U+13F5 are accepted. The derivation now matches the idna PVALID table for all 41,890 code points assigned after Unicode 5.2. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01U6XXMPvkRmkr5kqQuxPXm4 * Apply the Bidi rule to whole names and address the third review - RFC 5893: once a label has an R, AL or AN character, every label of the name must satisfy the Bidi Rule, in idn-hostname and hostname. Re-enables the three suite cases for rules 1 and 4. - Virama list updated to Unicode 16.0 (Tulu-Tigalari, Gurung Khema). - Hyphen-position check no longer allocates. - Removes the unused block-based isGreek/isHebrew/isKatakana. - Tests: concurrent first use of the tables in a fresh class loader, name-wide Bidi, Unicode 16 virama; U+180F is skipped on JDKs that do not know it. - CHANGELOG split per change, with the known limits. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01U6XXMPvkRmkr5kqQuxPXm4 * Keep the virama table sorted The Unicode 16 additions were appended out of order, so the binary search missed U+11D45 and U+11D97. Adds a test that the table is sorted. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01U6XXMPvkRmkr5kqQuxPXm4 * Keep hostname RFC 1123 and address the fourth review - hostname applies the RFC 5893 Bidi Rule within each A-label only, so 3com.xn--mgbaam7a8h and 1a.xn--4dbc are valid again, as on 3.0.9. - Remove the public satisfiesBidiDomainRule. The cross-label rule stays private to idn-hostname, which checks only the labels without right-to-left characters, since isValidULabel already checked the rest. - HostnameFormat walks the labels once, with no split or second decode. - The hyphen check is one startsWith at the third code point. - Test the virama table against the JDK's combining classes, derived from NFD reordering, on any JDK. - HostnameFormat tests for RTL A-labels; CHANGELOG states the stricter hostname behaviour and which format applies the cross-label rule. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01U6XXMPvkRmkr5kqQuxPXm4 * Address the fifth review - idn-hostname checks each label once: checkULabel reports whether a valid label is right-to-left, so isValid needs no second scan and no label array. Earlier labels are re-checked only when a name turns out to be a Bidi domain name. - The virama test scans every defined code point and requires an exact match with the JDK's combining classes, with no JDK version cutoff. - Pin RFC 5893 rules 4 and 5 for hostname A-labels. - CHANGELOG: use an A-label as the hostname Bidi example. - Share the reflective VIRAMAS read and tidy the test imports. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01U6XXMPvkRmkr5kqQuxPXm4 * Address the sixth review - The virama test always fails on a code point the table wrongly lists. A missing virama fails only when the JDK's Unicode data is not newer than the table's 16.0; on a newer JDK it is logged instead. - checkULabel returns a LabelStatus enum (INVALID, NO_RTL, HAS_RTL), declared with the other constants. - Keep the U-labels of A-labels, allocated only when a name has one, so an earlier A-label is not decoded again when the first right-to-left label appears. - Test the Punycode decoder's surrogate and out-of-range branches. - CHANGELOG: only Adlam small letters are accepted, not the capitals. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01U6XXMPvkRmkr5kqQuxPXm4 * Address the seventh review - Report missing post-16.0 viramas through JUnit (assumeTrue) instead of an slf4j warning, and detect a JDK newer than Unicode 16.0 from the scripts added in 17.0 (documented UnicodeScript.valueOf behaviour) rather than enum ordering, with a test for the detection. - Keep one source of truth for U-labels in isValid: each label is replaced by its U-label in place, so the Bidi back-check reads labels[m] with no fallback and no extra array. * Address the eighth review - idn-hostname accepts at most one trailing label separator (split with limit -1); a.. and xn--4dbc.. are now rejected, and the single root separator still does not count toward 253 characters. CHANGELOG updated. - Make the Unicode-version detection testable: the script lookup is a parameter, so both results are exercised on every JDK, and the missing-virama decision is extracted and tested for both branches. - isACE uses regionMatches instead of allocating a substring, and the LTR/RTL rule predicates are composed once instead of per label. - isValid Javadoc covers the cross-label Bidi rule and the trailing separator. --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Steve Hu <[email protected]> (cherry picked from commit 9cde0ad)
|
Review of this backport (head I ran 1. The CHANGELOG omits the Java 8 consequence ( 2. The U+1C80 assertions are vacuous on Java 8 ( 3. CHANGELOG formatting ( 4. Breaking-changes heading ( 5. Unused public helpers ( 6. One value is special-cased in a parameterized test ( |
|
Review of head 1. Two non-breaking entries now sit under |
|
Review of head |
* Reject a trailing label separator in idn-hostname on 2.x Port of #1384 to 2.x. The JSON-Schema-Test-Suite treats a trailing label separator (example., example。, example., example。) as an invalid idn-hostname, and hostname already rejects a trailing dot. #1383 still accepted one trailing separator on 2.x. Reject it, so 2.x and master agree, and re-enable the 16 upstream cases that were disabled for it. * Address review: 2.x CHANGELOG wording, list the rules, test the format class The entry names 2.0.8, the latest 2.x release, which skipped empty labels: .example, a..b and example. were all valid there. It now covers leading and doubled separators too, and links #1386 and #1384. Also ports the #1384 review changes: the isValid Javadoc lists the rules, and idnHostnameFormatRejectsTrailingLabelSeparator checks the IdnHostnameFormat contract directly.
Backports merged master PR #1377 (9cde0ad) to 2.x using cherry-pick -x. The existing hostname regex accepts overlong hostnames and invalid A-labels; the older IDNA code also mishandles contextual characters, supplementary code points, empty labels and bidi rules.
Ports HostnameFormat, the RFC 3492 Punycode codec, RFC5892 corrections, safe Unicode-table publication, regression tests and refreshed hostname/idn-hostname fixtures. The CHANGELOG documents stricter validation: previously accepted invalid hostnames may now fail.
Maintenance-branch adaptations:
Validation:
Known limits remain as on master: Unicode decisions depend on the JDK's Unicode version; virama data stops at Unicode 16; idn-hostname does not apply UTS 46 mapping and accepts a single trailing separator. Corresponding unsupported suite cases remain disabled with reasons.
Review follow-up (9d50768): document Java 8 / Unicode 6.2 rejection of newer valid labels, consolidate tightening notes under Breaking changes, remove redundant changelog metadata, deprecate retained UnicodeDatabase helpers without removing them, isolate the Adlam expectation in its own test, and guard the U+1C80 case-folding test on character availability (Cherokee coverage remains independent).
Focused offline verification after follow-up: RFC5892Test, HostnameFormatTest, PunycodeTest and FormatValidatorTest pass on actual installed JDK 8 and 21 (106 tests each; 4 skipped on Java 8 and 1 on Java 21). The case-folding guard skips on Java 8 and runs on Java 21. Full-suite figures above are from the original backport commit; the follow-up changed documentation, deprecation annotations and tests only. GitNexus checks for edited symbols are LOW risk; complete PR comparison remains MEDIUM in the previously documented format/Unicode paths.