Skip to content

Backport hostname and IDNA validation fixes to 2.x - #1383

Merged
stevehu merged 3 commits into
2.xfrom
fix/1377-hostname-validation-2.x
Nov 7, 2026
Merged

stevehu merged 3 commits into
2.xfrom
fix/1377-hostname-validation-2.x

Conversation

@stevehu

@stevehu stevehu commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Resolve RFC5892 conflict by taking the final master implementation, including rejecting empty interior labels and multiple trailing separators.
  • Replace List.of in tests with a Java 8-compatible singletonList; make Adlam/Cherokee expectations follow the running JDK's Unicode data, and skip the Adlam joining-context test when the script is unknown.
  • Retain public UnicodeDatabase.isGreek/isHebrew/isKatakana with their existing behavior for 2.x API compatibility; the corrected validator uses UnicodeScript instead.
  • Keep Jackson 2 and existing build configuration unchanged.

Validation:

  • Refreshed format fixtures fail against unchanged origin/2.x production code before the fix (including invalid A-label, bidi and empty-label cases).
  • Full offline suite passes on all four installed JDKs with pinned Surefire 3.5.1 and no plugin override:
    • Java 8: 10,810 tests, 0 failures/errors, 61 skipped.
    • Java 11: 10,810 tests, 0 failures/errors, 60 skipped.
    • Java 17: 10,810 tests, 0 failures/errors, 60 skipped.
    • Java 21: 10,810 tests, 0 failures/errors, 59 skipped.
  • GitNexus indexed this 2.x worktree before changes. Per-symbol impact checks were LOW or MEDIUM; compare-to-origin/2.x change detection is MEDIUM, covering the expected format/Unicode paths (one indexed table-loading process). No HIGH/CRITICAL risk reported.

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.

#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)
@stevehu

stevehu commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Review of this backport (head e0c71a6). I found no defect in the validator code. HostnameFormat, Punycode and RFC5892 are byte-identical to master at 9cde0ad, and the six hostname and idn-hostname fixture files are identical to master. The only code change from master is that UnicodeDatabase keeps its public isGreek, isHebrew and isKatakana.

I ran RFC5892Test, HostnameFormatTest, PunycodeTest and FormatValidatorTest on JDK 11, 21, 25 and 26 (all pass), and JsonSchemaTestSuiteTest (7,512 tests) on JDK 11, 25 and 26 (all pass). I have no JDK 8 here, so I could not run Java 8; the new main and test files do compile with --release 8. Only minor points remain, ranked most-severe first.

1. The CHANGELOG omits the Java 8 consequence (CHANGELOG.md:27)
On Java 8 (Unicode 6.2), any code point newer than the JDK's data is treated as unassigned. So xn--9d6hc.example (Adlam) and U+13F5 are rejected, although hostname accepted every well-formed LDH label before. The tests already encode this, but the entry says Adlam small letters and new Cherokee capitals "are accepted" and gives only a Java 17 versus 21 example. For 2.x, where Java 8 is the baseline, add a known limit for Java 8 and 11.

2. The U+1C80 assertions are vacuous on Java 8 (RFC5892Test.java:225)
isValid('a', U+1C80) and isValidALabel("xn--a-wyl") are expected to be false. U+1C80 is Unicode 9, so on Java 8 it is rejected as unassigned and the case-folding branch never runs. The Cherokee and U+180F lines in the same method are guarded; these two need assumeTrue(Character.isDefined(0x1C80)) or their own test.

3. CHANGELOG formatting (CHANGELOG.md:33)
The "Backport ... from #1377" bullet is a process note, and the blank line after it splits the list. ### Fixed follows the last bullet with no blank line.

4. Breaking-changes heading (CHANGELOG.md:10)
The stricter validation (over-long names, invalid A-labels, a..) sits under ### Changed. The comparable date-time tightening is listed under ### Breaking changes in the same file.

5. Unused public helpers (UnicodeDatabase.java:54)
isGreek, isHebrew and isKatakana are kept for compatibility but nothing uses them, so they are dead weight allocated at class load. Marking them @Deprecated with a pointer to Character.UnicodeScript would tell callers that they test code blocks, not scripts.

6. One value is special-cased in a parameterized test (HostnameFormatTest.java:58)
accepts flips its expectation for xn--9d6hc.example on Java 8. Move that value to its own test using assertEquals(Character.isDefined(0x1E922), matches(value)), as RFC5892Test does.

@stevehu

stevehu commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Review of head 9d50768 (the commit since e0c71a6). All six earlier points are addressed. I ran RFC5892Test (29 tests), HostnameFormatTest (33), PunycodeTest (20) and FormatValidatorTest on JDK 11, 21 and 26 (all pass), and mvn javadoc:javadoc still succeeds with the new @deprecated tags. I have no JDK 8 here. Nothing else in the repo calls the three deprecated helpers. One minor point remains.

1. Two non-breaking entries now sit under ### Breaking changes (CHANGELOG.md:28)
The whole block moved there. "Preserve the public legacy UnicodeDatabase.isGreek, isHebrew and isKatakana helpers ... marked deprecated" and "Updated the vendored hostname.json and idn-hostname.json suite files" describe no behaviour change. Readers scanning that section for upgrade risks have to read past them. They would fit a separate ### Changed section, with the validation tightenings and the date-time entry staying under ### Breaking changes.

@stevehu

stevehu commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Review of head 29fc49a (the commit since 9d50768). The earlier point is addressed: the helper deprecation and the suite refresh now sit under ### Changed. The commit changes only CHANGELOG.md and moves those two bullets unchanged, so I did not rerun the tests. I found nothing new.

@stevehu
stevehu merged commit 713aaec into 2.x Nov 7, 2026
5 checks passed
@stevehu
stevehu deleted the fix/1377-hostname-validation-2.x branch November 7, 2026 15:52
stevehu pushed a commit that referenced this pull request Nov 9, 2026
* 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.
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