Skip to content

Tighten the fallback regex for uri, uri-reference, iri and iri-reference - #1391

Merged
stevehu merged 4 commits into
networknt:masterfrom
ravismash:fix/uri-scheme-regex
Nov 25, 2026
Merged

stevehu merged 4 commits into
networknt:masterfrom
ravismash:fix/uri-scheme-regex

Conversation

@ravismash

@ravismash ravismash commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #1390 (item 8). The fallback regex now requires a scheme that starts with a letter, and drops the comma range. // is invalid for uri/iri and stays valid for uri-reference/iri-reference.

After review: the fallback checks the original value (JDK 25), runs validate(URI) so custom formats get the // rule, accepts scheme:#fragment, and [ ] are rejected outside an IP-literal.

A scheme must start with a letter, and the comma is no longer allowed.
// is valid only for uri-reference and iri-reference.
@stevehu

stevehu commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Review of #1391

Reviewed at head 3c3de43. Nothing here blocks the merge. The regex change does what it says. All format tests and the full JSON-Schema-Test-Suite pass at this head, and I checked the edge cases below against a build of it. There are three existing bugs this PR touches but leaves in place, and three minor items.

Existing bugs this PR leaves in place

1. The fallback matches e.getInput(), which JDK 25 no longer returns unchanged (AbstractRFC3986Format.java:54)

JDK 25 builds the URISyntaxException input with formatMsg(filterNonSocketInfo(input)). That trims leading and trailing spaces and collapses repeated ones. It returns "" if the jdk.includeInExceptions security property does not include hostInfoExclSocket. On Corretto 25.0.5, with format assertions on:

  • With the default java.security, " http://" and "about: " are valid in all four formats. " // " is valid as uri-reference and iri-reference.
  • With jdk.includeInExceptions= (empty), HTTP://, a: and about: fail as uri, and // fails as uri-reference. UriFallbackFormatTest then fails.

The new startsWith("//") overrides in UriFormat.java:32 and IriFormat.java:36 read the same value.

Suggested fix: match the original value, for example by passing it from matches into a handleException(String value, URISyntaxException e) overload. Also add a padded value to UriFallbackFormatTest.

2. scheme:#fragment is rejected although RFC 3986 allows it (AbstractRFC3986Format.java:22)

All four formats reject about:#frag and about:#. In RFC 3986, URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ], and hier-part may be path-empty, so both are valid. java.net.URI throws "Expected scheme-specific part" for them, and VALID accepts only a bare scheme:. The new Javadoc on VALID and handleException reads as if the fallback covered every such value. Either accept scheme ":" "#" fragment and check the fragment, or say in the Javadoc that this case is not supported.

3. A custom absolute format still accepts // (UriFormat.java:30, IriFormat.java:34)

The fallback never calls validate(URI), and the rule against // is copied into UriFormat and IriFormat instead of living in the base class. Take a subclass of AbstractRFC3986Format whose validate requires uri.isAbsolute() && "https".equals(uri.getScheme()). It still accepts //, http:// and ftp:.

One option: in the base handleException, call validate on a parseable stand-in, such as the value with a placeholder authority or path, and drop the two overrides. Otherwise, document that such subclasses must override handleException.

Minor

  1. The CHANGELOG does not mention :// (CHANGELOG.md:13). The old pattern ([A-Za-z0-9+-\.]*:)?// allowed an empty scheme, so 3.0.9 accepted :// in all four formats. It is now invalid in each, including uri-reference and iri-reference. Please add it, and say that the scheme rule also applies to iri and iri-reference.

  2. Gaps in UriFallbackFormatTest (UriFallbackFormatTest.java:55). The scheme rule is tested only for uri, uri-reference and one iri-reference value. Suggested additions:

    • iri | 1http:// | false
    • iri | about: | true
    • uri-reference | :// | false
    • iri-reference | :// | false
    • uri | ' http://' | false, quoted because @CsvSource trims unquoted values.
  3. The description mentions AbstractRFC3987Format, but this PR does not add it (IriFormat.java:29). The only commit, 3c3de43, has no such class. IriFormat and IriReferenceFormat still each override isInternationalized() and call containsBidiFormatting themselves. Either push that commit or move the paragraph to the follow-up PR.

Pass the value as it is to handleException, since JDK 25 can trim or
empty the exception input. Check a fallback value with validate(URI),
using / for its empty path, so the // rule lives in the base class and
custom formats get it. Accept scheme ":" "#" fragment.
java.net.URI accepts them in the fragment and in an opaque part, such
as urn:a[b. Check them once in AbstractRFC3986Format, instead of the
query only in each format.
@ravismash

Copy link
Copy Markdown
Contributor Author

Thanks Steve. All six are addressed in 1f0516d, plus one gap I found in 50768df.

  1. matches now passes the original value to handleException, in a new URISyntaxException, so JDK 25's trimming or emptying no longer matters. handleException keeps its signature, so no new API. Padded values are in UriFallbackFormatTest.
  2. scheme ":" "#" fragment is accepted. The fragment is checked by java.net.URI, and for IRIs by the bidi check too.
  3. The fallback now calls validate(URI) on a stand-in with / as the empty path, for example http:/// for http://. The two // overrides are gone, and your https-only subclass rejects //, http:// and ftp: (there's a test for it).
  4. The CHANGELOG now covers ://, the scheme rule in all four formats, about:#frag, and the JDK 25 fix.
  5. Added your five rows, plus fragment cases.
  6. Moved that paragraph to the follow-up PR.

Extra, 50768df: java.net.URI accepts [ and ] in the fragment and in an opaque part, so http://x/#a[b and urn:a[b passed in every format. AbstractRFC3986Format now rejects them anywhere outside an IP-literal, and the four per-format query checks are gone.

mvn test: 11,277 tests pass on JDK 24, including 7,709 suite cases.

@stevehu

stevehu commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Re-review of #1391

Reviewed at head 50768df. Thanks, all six points are addressed. I checked each one against a build of this head, including padded values on JDK 25, about:#frag, and the https-only subclass. All 11,277 tests pass on JDK 25 here, and CI is green on 17, 21 and 25. The bracket check looks right: http://[::1]/p?q#f passes, and brackets in the fragment or an opaque part fail.

Nothing here blocks the merge. One existing bug and two small items remain.

Existing bug

1. iri and iri-reference accept private-use characters and noncharacters outside the query (IriFormat.java:11)

RFC 3987 allows iprivate only in iquery, and ucschar excludes U+FDD0 to U+FDEF and U+xFFFE/U+xFFFF. java.net.URI accepts any character that is not a control or a space, and validate adds only the bidi check. With format assertions on:

  • iri accepts http://x/, http://x/� and, through the new fallback, about:#.
  • iri-reference accepts #�.

The base commit already accepted the path cases. A check next to containsMisplacedBracket could reject any code point that is neither ASCII nor ucschar, while allowing iprivate in the query.

Minor

  1. The bracket check has no opt-out (AbstractRFC3986Format.java:33). It moved from the overridable validate into the final matches, using the private containsMisplacedBracket. A custom AbstractRFC3986Format subclass that accepted PHP-style queries such as https://x/search?tags[]=a now rejects them, and no method can be overridden to change that. If that is intended, a line in the CHANGELOG would help. Otherwise, make the check protected.

  2. Duplicate test row (UriFallbackFormatTest.java:68). "uri | :// | false" appears at lines 61 and 68. The second copy could become iri | :// | false, which is not covered yet.

@ravismash

Copy link
Copy Markdown
Contributor Author

Thanks Steve. Points 2 and 3 are fixed in de6258f: containsMisplacedBracket is now protected, so a subclass can accept ?tags[]=a (the CHANGELOG says so), and the duplicate row is now iri | :// | false.

Point 1 goes in the AbstractRFC3987Format follow-up, which already rejects non-ucschar characters, and iprivate outside the query, anywhere in an IRI.

@stevehu

stevehu commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Re-review of #1391

Thanks. de6258f isn't part of this PR yet. It is the tip of fix/uri-scheme-regex on your fork, but the PR head here is still 50768df. So the PR diff still has containsMisplacedBracket as private static and still has the duplicate uri | :// row. Pushing the branch again usually makes GitHub pick it up.

I read de6258f directly, and it looks good:

  • containsMisplacedBracket is protected, with a test and a CHANGELOG line.
  • The duplicate row is now iri | :// | false.

I found nothing new in it.

Moving the private-use and noncharacter check to the AbstractRFC3987Format follow-up is fine with me.

Once the PR head moves to de6258f, this looks good to merge from my side.

containsMisplacedBracket is protected, so a subclass can accept
?tags[]=a. Replace the duplicate :// row with the iri case.
@ravismash
ravismash force-pushed the fix/uri-scheme-regex branch from de6258f to f7a085c Compare November 25, 2026 19:13
@ravismash

Copy link
Copy Markdown
Contributor Author

Thanks Steve. Pushed again, so the PR head is now f7a085c, the same change as de6258f. The private-use check goes in the follow-up.

@stevehu

stevehu commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Re-reviewed at head f7a085c. Both remaining items are fixed. containsMisplacedBracket is now protected, with a test and a CHANGELOG line, and the duplicate row is now iri | :// | false. CI passes on 17, 21 and 25, and the format tests and the 7,709 suite cases pass locally on JDK 25. The private-use and noncharacter check for IRIs will go in the AbstractRFC3987Format follow-up, as agreed. Looks good to merge.

@stevehu
stevehu merged commit 9a2da0e into networknt:master Nov 25, 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.

2 participants