Skip to content

Limit uri-template literals to RFC 6570 characters - #1385

Merged
stevehu merged 4 commits into
networknt:masterfrom
ravismash:fix/uri-template
Nov 9, 2026
Merged

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

Conversation

@ravismash

@ravismash ravismash commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Limit uri-template literals to RFC 6570 characters

The uri-template literal class was [^\p{Cntrl}"%<>\^`{|}]. It accepted the space and the backslash, and because \p{Cntrl} covers only U+0000-001F and U+007F, it also accepted C1 controls, lone surrogates, noncharacters and other characters RFC 6570 section 2.1 excludes.

  • The literal class now lists section 2.1's ranges: the ASCII characters, ucschar and iprivate, with pct-encoded triplets matched as before. Newly rejected in a literal: the space, the backslash, C1 controls, lone surrogates, noncharacters (U+FDD0-FDEF and the last two code points of every plane), U+FFF0-FFFD and U+E0000-E0FFF. %20 and %5C stay valid. Ranges are listed explicitly rather than using \p{Cn}, because ucschar allows unassigned code points and \p{Cn} would make the result depend on the JDK's Unicode version.
  • The apostrophe is also outside the RFC's set, but the upstream suite marks a'b valid, so it stays accepted (uriTemplateAllowsApostropheInLiteral).
  • UriTemplateFormatTest checks the pattern against a predicate written from the RFC's ABNF for every code point U+0000-10FFFF as a literal, and lists the rejected and accepted examples directly. It calls the format, not a schema, so lone surrogates don't depend on the JSON parser.
  • Runs of literal characters and varchars are matched possessively, so long templates no longer throw StackOverflowError (the old pattern recursed once per character). The ASCII literals are a separate class listed character by character, which java.util.regex compiles to a bit set; ASCII templates match about twice as fast as on master.
  • UriTemplateFormatTest.longTemplatesDoNotOverflowTheStack covers inputs up to 1,000,000 characters, and rejects covers a stray brace or percent sign. The backslash case isn't in the upstream suite yet (Add uri-template cases for the remaining characters RFC 6570 excludes from literals json-schema-org/JSON-Schema-Test-Suite#1260 adds it).
  • Updates the vendored optional/format/uri-template.json files for draft 6, 7, 2019-09, 2020-12 and draft-next to the current upstream suite. The draft-next copy keeps this repository's $schema URI.

2.x port: #1387 (also carries #1275, which isn't on 2.x).

@stevehu

stevehu commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Review of this PR (head f4343fa). The pattern change works: a b and a\b are now rejected, and %20 and %5C stay valid. I ran the full suite with the PR applied on master: 11,098 tests, 0 failures, 57 skipped. The five uri-template.json files are identical across drafts apart from the $schema line, and the draft-next copy keeps the repository's existing $schema URI. Two points remain, ranked most-severe first.

1. Other characters RFC 6570 excludes from literals are still accepted (Formats.java:47)
In Java, \p{Cntrl} without UNICODE_CHARACTER_CLASS covers only U+0000-001F and U+007F. I ran the new pattern on these inputs, and all still match:

  • a<U+0085>b and a<U+0080>b (C1 controls)
  • a<U+FFFF>b, a<U+FFFE>b and a<U+FDD0>b (noncharacters)
  • a lone surrogate, and U+1FFFE

Tag characters in U+E0000-E0FFF are accepted the same way. Section 2.1 allows only the listed ASCII ranges, ucschar and iprivate, and ucschar excludes all of these. The PR and CHANGELOG frame the change as RFC 6570 conformance, but only the space and backslash were closed, and no test covers the rest. The class could also exclude \p{Cc}, \p{Cs}, \p{Cn} and the tag range, or the CHANGELOG entry could list these under known limits alongside the apostrophe.

2. 2.x needs its own port (CHANGELOG.md:11)
origin/2.x has the same bug. Its pattern also still excludes ', because #1275 was not backported there. A cherry-pick of this change will therefore conflict, and the two branches will differ on uri-template (apostrophe, space, backslash) until a backport lands.

@ravismash ravismash changed the title Reject spaces and backslashes in uri-template literals Limit uri-template literals to RFC 6570 characters Nov 8, 2026
@ravismash

Copy link
Copy Markdown
Contributor Author
  1. The literal class now lists RFC 6570 section 2.1's ranges explicitly: the ASCII characters, ucschar and iprivate. That rejects every case you listed, plus U+FFF0-FFFD and the U+E0000-E0FFF tag range. I used ranges rather than \p{Cn}, because ucschar allows unassigned code points and \p{Cn} would make the result depend on the JDK's Unicode version. The apostrophe is the one deliberate exception, as in the suite.
    The new UriTemplateFormatTest compares the pattern with a predicate written from the RFC's ABNF, for every code point from U+0000 to U+10FFFF used as a literal. It found no differences. It also lists your examples directly. It calls the format itself, so lone surrogates don't depend on the JSON parser.
    The CHANGELOG entry lists everything that is newly rejected. I retitled the PR "Limit uri-template literals to RFC 6570 characters" to match.
  2. 2.x: the port is Limit uri-template literals to RFC 6570 characters on 2.x #1387. It carries Allow apostrophe in uri-template literal #1275 as well, so 2.x accepts a'b and gets the same literal rules, and the two branches agree on uri-template.

Full suite with this commit passes, including the 24 UriTemplateFormatTest cases.

@stevehu

stevehu commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Review of head 282b4a0 (the commit since f4343fa). The literal ranges match RFC 6570 section 2.1, and the code-point sweep against an ABNF-derived predicate is a good check. The full suite passes on JDK 25: 11122 tests, 0 failures, 57 skipped. Both earlier points are addressed. Remaining points, most-severe first.

1. The pattern overflows the stack on long input (Formats.java:65)
I ran matches() on the master and PR patterns: a 5000-character literal throws StackOverflowError in both, and { + 1000 a + } throws in the new pattern. About 2000 characters of mixed literals and expressions also overflow. uri-template is checked against untrusted strings, and an Error is not caught as an Exception. This predates the PR, but the PR rewrites this line and adds no long-input test. Letting a possessive literal class consume runs ((?:LIT++|%XX|\{...\})*) keeps recursion per expression or escape rather than per character.

2. The new literal class is slower (Formats.java:46)
20000 iterations after warmup: a 312-character ASCII template with expressions takes 311 ms versus 89 ms on the old pattern, and a 220-character CJK/emoji template takes 211 ms versus 110 ms. A negated class of the excluded set, or the possessive class from point 1, keeps the per-character cost flat.

3. CHANGELOG heading (CHANGELOG.md:11)
Spaces, C1 controls, U+FFFD and the other newly rejected characters were valid in 3.0.9. This is a tightening, so it belongs under ### Breaking changes, as for #1384.

4. Redundant test (FormatValidatorTest.java:245)
uriTemplateRejectsSpaceAndBackslashInLiteral uses the same inputs as rejects/accepts in UriTemplateFormatTest, which also sweeps every code point.

5. Gaps in the sweep (UriTemplateFormatTest.java:59)
It skips %, { and }, and rejects has no a{, a} or a% case. A change to the expression or pct-encoded branches that accepted a stray brace or lone percent sign would not fail here.

ravismash added a commit to ravismash/json-schema-validator that referenced this pull request Nov 9, 2026
Formats.java and UriTemplateFormatTest are again identical to networknt#1385:
runs of literal characters and varchars are matched possessively, so
long templates no longer overflow the stack, and the tests add
long-input and stray brace or percent sign cases.

The two FormatValidatorTest cases are removed, as UriTemplateFormatTest
covers the same inputs. The CHANGELOG entry names 2.0.8 and links
networknt#1387, networknt#1385 and networknt#1275, and a Fixed entry covers the stack overflow.
@ravismash

Copy link
Copy Markdown
Contributor Author
  1. Stack overflow: fixed. Runs of literal characters and varchars are now matched possessively ([...]++ inside (?:...)*+), so a run costs no stack. longTemplatesDoNotOverflowTheStack covers a 1,000,000-character literal, { + 1,000,000 a + }, 100,000 escapes, 100,000 expressions and mixed runs. It passes with -Xss256k, and it throws StackOverflowError on the previous pattern. The accepted set is unchanged: the new pattern agrees with the previous one on 7.1 million generated templates and on every code point. There's also a Fixed entry in the CHANGELOG.
  2. Speed: the literal set is now three classes (ASCII, BMP, supplementary). The ASCII class lists its characters one by one, so java.util.regex compiles it to a bit set. 20,000 matches after warmup (JDK 21), master / previous PR / now:
    • 370-character ASCII template: 186 / 465 / 95 ms
    • 226-character CJK path: 87 / 309 / 73 ms
    • 23-character template: 11 / 33 / 8 ms
    • 224 characters alternating CJK, emoji and ASCII every few characters: 111 / 289 / 187 ms. Switching between the three classes costs one loop step per run, so this case is still slower than master.
  3. The entry is under Breaking changes. Reject a trailing label separator in idn-hostname #1384 adds the same heading, so whichever merges second needs a one-line CHANGELOG rebase; I'll do it.
  4. Removed uriTemplateRejectsSpaceAndBackslashInLiteral.
  5. rejects now has a{b, a}b, a%b, a%2, a%g0, {, }, {}, {a and {a b}.

Full suite with this commit: 11,132 tests, 0 failures, 57 skipped.

@stevehu

stevehu commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Review of head 5164970 (the commit since 282b4a0). All five earlier points are addressed and I found no correctness defect. The full suite passes on JDK 25: 11132 tests, 0 failures, 57 skipped.

What I checked:

  • Same accepted set. I compared the previous pattern and this one on about 10 million generated templates (every token sequence up to length 4, 6 million random ones, and expression-shaped cases) on JDK 17 and JDK 25. No differences.
  • Stack. Literals, expressions, pct-encoded runs and dotted names of 600k to 1.2M characters, including failing tails, run on 64k and 160k thread stacks on JDK 17 and JDK 25 without a StackOverflowError.
  • Speed (JDK 25, 20000 matches after warmup, master / previous head / now). A 346-character ASCII template with expressions: 210 / 388 / 96 ms. A CJK path: 78 / 175 / 64 ms. A 22-character template: 11 / 24 / 5 ms.

Two small points remain:

1. Test helper (UriTemplateFormatTest.java:73)
repeat(String, int) re-implements String.repeat. This module compiles tests at Java 17, and ExecutionLimitsStackProbe already uses String.repeat. (The 2.x port targets Java 8 and still needs the helper.)

2. Class-alternating input is still slower than master (Formats.java:48)
A 400-character template alternating a, é and U+1F600 every character takes 247 ms against 139 ms on master (the previous head took 325 ms). You already note this. Typical templates are faster than master, so I would not block on it.

RFC 6570 section 2.1 excludes the space and the backslash from template literals, but the uri-template pattern accepted both. Add them to the excluded characters, add a unit test, and update the vendored uri-template.json suite files (draft 6, 7, 2019-09, 2020-12 and draft-next) to the current JSON-Schema-Test-Suite.
Address review: the literal class now lists RFC 6570 section 2.1's
ranges (the ASCII characters, ucschar and iprivate) instead of
excluding a few characters. C1 controls, lone surrogates,
noncharacters, U+FFF0 to U+FFFD and U+E0000 to U+E0FFF are rejected
in a literal. The apostrophe stays valid, as in the
JSON-Schema-Test-Suite.

UriTemplateFormatTest checks the pattern against a predicate written
from the RFC's ABNF for every code point, and covers the reviewed
examples directly.
The uri-template pattern recursed once per character, so a
5,000-character literal or a 1,000-character variable name threw
StackOverflowError. Runs of literal characters and of varchars are
now matched possessively, which needs no stack per character. The
literal set is unchanged: the ASCII, BMP and supplementary characters
are three classes, and the ASCII class lists its characters one by one
so java.util.regex compiles it to a bit set.

UriTemplateFormatTest adds long-input cases, which overflow the
previous pattern, and rejects a stray brace or percent sign.
FormatValidatorTest.uriTemplateRejectsSpaceAndBackslashInLiteral is
removed, as UriTemplateFormatTest covers the same inputs.

The CHANGELOG entry moves to Breaking changes, and a Fixed entry
covers the stack overflow.
@ravismash ravismash reopened this Nov 9, 2026
@ravismash

Copy link
Copy Markdown
Contributor Author
  1. UriTemplateFormatTest now uses String.repeat, and the helper is gone. The 2.x port keeps it for Java 8.
  2. Agreed, I'll leave the alternating case as it is.

I've also rebased onto master after #1384, so the CHANGELOG conflict is resolved: both entries now sit under Breaking changes. UriTemplateFormatTest passes, including the long-input test with -Xss256k.

ravismash added a commit to ravismash/json-schema-validator that referenced this pull request Nov 9, 2026
Port of networknt#1385 to 2.x, together with networknt#1275, which was not backported.
The literal class lists RFC 6570 section 2.1's ranges (the ASCII
characters, ucschar and iprivate). A space, a backslash, C1 controls,
lone surrogates, noncharacters, U+FFF0 to U+FFFD and U+E0000 to
U+E0FFF are rejected in a literal. The apostrophe is accepted, as in
the JSON-Schema-Test-Suite and on master, so both branches agree on
uri-template. The vendored uri-template.json suite files are updated.
ravismash added a commit to ravismash/json-schema-validator that referenced this pull request Nov 9, 2026
Formats.java and UriTemplateFormatTest are again identical to networknt#1385:
runs of literal characters and varchars are matched possessively, so
long templates no longer overflow the stack, and the tests add
long-input and stray brace or percent sign cases.

The two FormatValidatorTest cases are removed, as UriTemplateFormatTest
covers the same inputs. The CHANGELOG entry names 2.0.8 and links
networknt#1387, networknt#1385 and networknt#1275, and a Fixed entry covers the stack overflow.
@stevehu

stevehu commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Review of head 673f889, rebased onto master after #1384. I found no defect and no remaining blocker.

  • Last commit. 673f889 switches UriTemplateFormatTest to String.repeat and removes the helper, which closes the one open code point.
  • Rebase. range-diff shows the earlier commits unchanged apart from CHANGELOG context. Formats.java is byte-identical to 5164970. The CHANGELOG diff against master is the uri-template entry under Breaking changes, next to the idn-hostname one, and the Fixed entry.
  • Full suite. JDK 25: 11133 tests, 0 failures, 45 skipped, including longTemplatesDoNotOverflowTheStack.

The class-alternating slowdown stays, as you said. It only affects templates that switch script almost every character, and typical templates are faster than master, so I do not object.

@stevehu
stevehu merged commit 74b7e58 into networknt:master Nov 9, 2026
3 checks passed
stevehu pushed a commit that referenced this pull request Nov 9, 2026
* Limit uri-template literals to RFC 6570 characters on 2.x

Port of #1385 to 2.x, together with #1275, which was not backported.
The literal class lists RFC 6570 section 2.1's ranges (the ASCII
characters, ucschar and iprivate). A space, a backslash, C1 controls,
lone surrogates, noncharacters, U+FFF0 to U+FFFD and U+E0000 to
U+E0FFF are rejected in a literal. The apostrophe is accepted, as in
the JSON-Schema-Test-Suite and on master, so both branches agree on
uri-template. The vendored uri-template.json suite files are updated.

* Address review: re-port the #1385 review fixes, CHANGELOG links

Formats.java and UriTemplateFormatTest are again identical to #1385:
runs of literal characters and varchars are matched possessively, so
long templates no longer overflow the stack, and the tests add
long-input and stray brace or percent sign cases.

The two FormatValidatorTest cases are removed, as UriTemplateFormatTest
covers the same inputs. The CHANGELOG entry names 2.0.8 and links
#1387, #1385 and #1275, and a Fixed entry covers the stack overflow.

* Address review: drop "as on master" from the CHANGELOG entry
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