Skip to content

Check the authority of uri, uri-reference, iri and iri-reference - #1390

Merged
stevehu merged 5 commits into
networknt:masterfrom
ravismash:fix/uri-authority
Nov 20, 2026
Merged

stevehu merged 5 commits into
networknt:masterfrom
ravismash:fix/uri-authority

Conversation

@ravismash

Copy link
Copy Markdown
Contributor

java.net.URI falls back to a registry-based authority when it cannot parse the authority as a server, and then checks neither the port, the userinfo nor the host. So http://example.com:abc/path, //a@b@example.com/, http://[::ffff:01.2.3.4] and an IPv6 address without brackets were accepted.

AbstractRFC3986Format now checks a parsed authority against RFC 3986 section 3.2 for all four formats: the port is digits only, the userinfo holds no other "@", the host is a reg-name or an IPv6 literal without leading zeros in an embedded IPv4 address (reusing the ipv6 pattern), and the IRI formats also allow RFC 3987 ucschar in the userinfo and the host. URI#parseServerAuthority() is not used because it rejects valid reg-names such as a_b.example. http:// and about: (#1071) stay valid.

Add the 12 failing JSON-Schema-Test-Suite cases to the vendored uri, uri-reference, iri and iri-reference suite files (draft 6, 7, 2019-09, 2020-12 and draft-next) and a unit test for the edge cases.

Closes #1389

java.net.URI falls back to a registry-based authority when it cannot
parse the authority as a server, and then checks neither the port, the
userinfo nor the host. So http://example.com:abc/path, //a@b@example.com/,
http://[::ffff:01.2.3.4] and an IPv6 address without brackets were
accepted.

AbstractRFC3986Format now checks a parsed authority against RFC 3986
section 3.2 for all four formats: the port is digits only, the userinfo
holds no other "@", the host is a reg-name or an IPv6 literal without
leading zeros in an embedded IPv4 address (reusing the ipv6 pattern), and
the IRI formats also allow RFC 3987 ucschar in the userinfo and the host.
URI#parseServerAuthority() is not used because it rejects valid
reg-names such as a_b.example. http:// and about: (networknt#1071) stay valid.

Add the 12 failing JSON-Schema-Test-Suite cases to the vendored uri,
uri-reference, iri and iri-reference suite files (draft 6, 7, 2019-09,
2020-12 and draft-next) and a unit test for the edge cases.

Closes networknt#1389
@stevehu

stevehu commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Thanks for this. With format assertions on, the PR passes all the vendored suite cases and every assertion case in the upstream format-uri/uri-reference/iri/iri-reference files at e2a68a1. I probed some edge cases against the built branch and found the following.

Should fix

  1. Leftover IPv6 check in IriFormat / IriReferenceFormat rejects valid hosts. Both validate() methods still run IPv6Format.PATTERN on the decoded getAuthority(). http://1%3A%3A2/ is valid as uri but invalid as iri, and //1%3A%3A2/ is invalid as iri-reference, because 1::2 matches the IPv6 pattern. 1%3A%3A2 is a valid ireg-name. The new port check already rejects every unbracketed IPv6 address, since any IPv6 address has at least two colons, so both blocks can be deleted. That also stops the large regex from running on every IRI.
  2. IPv6 zone IDs are newly rejected, and the CHANGELOG doesn't say so. isValidIpLiteral rejects any %, so http://[fe80::1%25en1]/ is now invalid in all four formats; 3.0.9 accepted it. Please add this to the breaking-change entry. The entry also says unbracketed IPv6 was accepted before for all four formats, but iri/iri-reference already rejected it.
  3. Bidi formatting characters are accepted in the IRI authority. isUcschar allows U+200E, U+200F and U+202A–U+202E, which RFC 3987 §4.1 forbids in IRIs. http://ex‮ample/ is valid as iri, which allows display spoofing.
  4. Third-party subclasses. AbstractRFC3986Format is public and matches() is final. Any downstream subclass now gets the authority check, and an IRI-like subclass rejects non-ASCII hosts unless it overrides the new isInternationalized() hook. Please mention this in the CHANGELOG.

Tests / cleanup

  1. Unreachable bracket branches. java.net.URI only returns a [-prefixed authority after a successful server parse, which guarantees [ IPv6 ] [: digits]. So end == -1, rest.charAt(0) != ':' and the port check after ] can never fire. The rows http://[::1]:x/p and http://[::1]x/p in UriAuthorityFormatTest fail in new URI(...) and never reach the new code. The only live checks in that branch are the % rejection and the IPv6 pattern.
  2. Duplicated IPv6 logic. isValidIpLiteral re-implements IPv6Format.matches() (whitespace check, % check, then the pattern). A static IPv6Format.isValid(String) that both call would keep them in sync. The whitespace loop is dead anyway.
  3. draft4 and draft3 suite files. Both drafts also have optional/format/uri.json, but the new uri cases were only added to draft6 and later.

Pre-existing, related to the question in #1389

  1. The fallback VALID regex is too loose. In [A-Za-z0-9+-\.], +-\. is a range that includes ,. The regex also allows a leading digit, and the scheme is optional before //. So 1http://, bar,baz:// and ,: pass as uri, 1http: passes as uri-reference, and // passes as both uri and iri. My view on the issue's question: // should be invalid for uri/iri, since RFC 3986 §3 requires a scheme, and valid for uri-reference, as the upstream suite expects. Something like [A-Za-z][A-Za-z0-9+.-]*: with the scheme required for the absolute formats would fix both. This could be a follow-up.
  2. Relying on java.net.URI. Valid IPvFuture literals such as http://[v1.fe80::a+en1]/ are still rejected because java.net.URI throws first. Longer term, a single RFC 3986/3987 reference validator, which isValid/isUnreserved/isSubDelim already partly implement, could replace java.net.URI, the VALID fallback and the per-format query checks. Not a blocker for this PR.

- IriFormat and IriReferenceFormat no longer run the IPv6 pattern on the
  decoded authority, which rejected a valid reg-name such as 1%3A%3A2.
  The port check already rejects an IPv6 address without brackets.
- The IRI formats reject the bidirectional formatting characters U+200E,
  U+200F and U+202A to U+202E in the userinfo and the host (RFC 3987
  section 4.1).
- The bracketed host is checked with a new IPv6Format.isValid, shared
  with the ipv6 format, instead of a copy of its checks. The branches for
  a missing "]" and for the port after "]" are removed: java.net.URI only
  returns a bracketed host after a successful server parse.
- UriAuthorityFormatTest drops two rows that never reached the new code
  and covers the cases above.
- The uri cases are added to the draft 3 and draft 4 suite files too.
- CHANGELOG: zone ids are now rejected, iri and iri-reference already
  rejected an IPv6 address without brackets, and third-party subclasses
  of AbstractRFC3986Format get the check and the isInternationalized()
  hook.
@ravismash

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review. Fixed in f1741ab:

  1. Removed the leftover IPv6 check from IriFormat and IriReferenceFormat, so http://1%3A%3A2/ (iri) and //1%3A%3A2/ (iri-reference) are valid now.
  2. The CHANGELOG now lists the zone-id change, and says only uri/uri-reference accepted an IPv6 address without brackets before.
  3. The IRI formats reject U+200E, U+200F and U+202A–U+202E in the userinfo and host (RFC 3987 §4.1). Tests added.
  4. The CHANGELOG mentions third-party subclasses and the isInternationalized() hook.
  5. Removed the unreachable ]/port branches and the two test rows that never reached them.
  6. Added IPv6Format.isValid(String), used by both the ipv6 format and the authority check.
  7. Added the uri cases to the draft3 and draft4 suite files.

On 8, agreed: // should be invalid for uri/iri and valid for uri-reference, and the scheme in the fallback regex should start with a letter. Since it changes the fallback for all four formats, I'd send it as a follow-up PR right after this one, unless you'd rather have it here. 9 makes sense as a longer-term direction.

RFC 3987 section 4.1 forbids the bidirectional formatting characters
U+200E, U+200F and U+202A to U+202E anywhere in an IRI, not only in the
authority. iri and iri-reference now reject them in the path, the query
and the fragment too; the check moves from ucschar to the whole value.

The CHANGELOG example for a second "@" now uses an absolute URI, and an
unrelated whitespace change is reverted.
@ravismash

Copy link
Copy Markdown
Contributor Author

Also rejects bidi formatting characters in the path, query and fragment of iri and iri-reference (RFC 3987 section 4.1).

@stevehu

stevehu commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Thanks, f1741ab and cb7d826 address everything we agreed on. With format assertions on, all vendored suite cases pass (7,709) and so do the 44 unit cases. I re-checked the fixed cases on the built branch: 1%3A%3A2 is valid as iri/iri-reference, zone IDs are rejected, and bidi characters are rejected in every component. A follow-up PR for the fallback regex (item 8) is fine with me.

A few smaller points on the new code:

  1. Unguarded indexOf(']') (AbstractRFC3986Format.java:86). hostAndPort.substring(1, hostAndPort.indexOf(']')) relies on java.net.URI always returning the ]. That holds today, but if it ever doesn't, this throws StringIndexOutOfBoundsException out of matches() instead of returning false. int end = hostAndPort.indexOf(']'); return end > 0 && IPv6Format.isValid(hostAndPort.substring(1, end)); keeps the simplification and fails safely.
  2. Bidi isolates. isBidiFormatting covers exactly the seven characters RFC 3987 §4.1 lists. The isolates added in Unicode 6.3, U+2066–U+2069 (LRI, RLI, FSI, PDI), and ARABIC LETTER MARK U+061C still pass, including in the host: http://ex⁧ample/ is valid as iri. That conforms to the RFC, which predates them, but it leaves the same display-spoofing risk (Trojan Source, CVE-2021-42574). Consider adding them, or say in the CHANGELOG that only the RFC 3987 list is rejected.
  3. isInternationalized() now controls two rules. Its javadoc still says it only allows ucschar in the userinfo and host, but it now also turns on whole-value bidi rejection. The CHANGELOG bullet for subclasses mentions only the non-ASCII part. Please update both. Another option is to move the bidi check into IriFormat/IriReferenceFormat.validate() using uri.toString(), the way UriFormat does its ASCII check.
  4. draft3 cases never run. JsonSchemaTestSuiteTest has no draft3 runner; the suite total went up by 3, which is only the draft4 additions. I shouldn't have asked for draft3 in my earlier comment, so the draft3 hunk can be reverted.
  5. Two test gaps: bidi in the userinfo (iri | http://a‮b@example.com/ | false), and a percent-encoded bidi character in an IRI (iri | http://example.com/a%E2%80%AEb | true). Both behave correctly now; this is just regression coverage.
  6. Minor: all seven bidi characters are in the BMP, so a plain for loop over value.charAt(i) avoids the codePoints() stream on every IRI validation.

… check

- The bracketed host check returns false instead of throwing if no "]"
  follows the "[".
- The IRI formats also reject U+061C (ALM) and the isolates U+2066 to
  U+2069, which can reorder the displayed text like the RFC 3987 list.
- The bidi check moves into IriFormat and IriReferenceFormat, so
  isInternationalized() controls only ucschar in the userinfo and host.
  It scans with a charAt loop.
- The draft 3 suite change is reverted: draft 3 has no runner.
- Tests cover bidi in the userinfo, a percent-encoded bidi character,
  and the added characters. The CHANGELOG is updated.
@ravismash

Copy link
Copy Markdown
Contributor Author

Thanks, all six are in 99a4891:

  1. The bracketed host check is now end > 0 && IPv6Format.isValid(...), so it returns false instead of throwing.
  2. Added U+061C and the isolates U+2066–U+2069. The CHANGELOG now lists both groups: the RFC 3987 §4.1 characters, and the ones Unicode added later that reorder text the same way.
  3. Moved the bidi check into IriFormat/IriReferenceFormat.validate() on uri.toString(), as UriFormat does its ASCII check. isInternationalized() again controls only ucschar in the userinfo and host, and the CHANGELOG subclass note says so.
  4. Reverted the draft3 suite change.
  5. Added the two rows (bidi in the userinfo is invalid, percent-encoded bidi is valid), plus rows for the new characters.
  6. containsBidiFormatting uses a charAt loop.

mvn test: 11227 tests, 0 failures. All 112 upstream uri/iri string cases still pass. I'll send the fallback regex (item 8) as a follow-up once this is in.

@stevehu

stevehu commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Thanks, 99a4891 covers all six. I re-ran the format tests on the built branch (49 unit cases and 7,709 suite cases pass) and all upstream uri/iri assertion cases still pass. On that build, the new bidi characters (U+061C, U+2066–U+2069) are rejected in the host, userinfo and fragment, the percent-encoded form stays valid, and the uri formats are unaffected. I found no new bugs.

Two small, non-blocking points:

  1. Third-party IRI subclasses don't get the bidi check. containsBidiFormatting is package-private, and the check now runs only in IriFormat/IriReferenceFormat. An IRI-like subclass of AbstractRFC3986Format in another package that follows the CHANGELOG and overrides isInternationalized() still accepts http://ex‮ample/, and it can't call the helper. Either make it protected static, or add a sentence to the CHANGELOG saying such subclasses should extend IriFormat/IriReferenceFormat or do their own bidi check.
  2. Duplicated IRI logic (optional). Both IRI classes now repeat the same isInternationalized() override and the same bidi guard, while the helper sits in the URI base class. A small AbstractRFC3987Format between AbstractRFC3986Format and the two IRI formats could hold both, and future RFC 3987 rules would then go in one place.

Either can land here or later. Looks good to me otherwise, and the follow-up for the fallback regex works for me.

Address review: a subclass of AbstractRFC3986Format in another package
for an IRI-like format can now call containsBidiFormatting from its
validate(), and the CHANGELOG says it should.
@ravismash

Copy link
Copy Markdown
Contributor Author

Thanks. Point 1 is in fa9de11: containsBidiFormatting is now protected static, and the CHANGELOG says IRI-like subclasses should extend IriFormat/IriReferenceFormat or call it themselves. I'll do point 2 (AbstractRFC3987Format) in the follow-up with the fallback regex, since that touches the same classes. mvn test: 11227 tests, 0 failures.

@stevehu

stevehu commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Thanks, fa9de11 looks good: it builds and the format tests pass. Doing AbstractRFC3987Format in the follow-up is fine.

One thing to keep in mind for that follow-up: containsBidiFormatting is now public API, since it's protected static and the CHANGELOG tells subclasses to call it. If it moves into AbstractRFC3987Format, an outside subclass of AbstractRFC3986Format that already calls it would stop compiling. Either keep it on AbstractRFC3986Format (the new class can still use it from there), or land the follow-up before the next release so the API only changes once.

Approving from my side.

@ravismash

Copy link
Copy Markdown
Contributor Author

Hi Steve, just a gentle reminder on this one when you have a moment. Once it’s in, I’ll open the follow-up for the fallback regex and AbstractRFC3987Format

@stevehu
stevehu merged commit 1e32eb8 into networknt:master Nov 20, 2026
3 checks passed
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.

uri, uri-reference, iri and iri-reference accept an invalid authority (port, userinfo, IP literal)

2 participants