Skip to content

Allow '#' inside json-pointer and relative-json-pointer tokens - #1394

Open
kayodeo1 wants to merge 4 commits into
networknt:masterfrom
kayodeo1:fix-json-pointer-hash-in-token
Open

kayodeo1 wants to merge 4 commits into
networknt:masterfrom
kayodeo1:fix-json-pointer-hash-in-token

Conversation

@kayodeo1

Copy link
Copy Markdown

The json-pointer and relative-json-pointer format patterns exclude # from reference tokens ([^/#~]), so format assertions reject valid JSON Pointers such as /a#b.

RFC 6901 section 3 defines a reference token as *( unescaped / escaped ) with unescaped = %x00-2E / %x30-7D / %x7F-10FFFF. That excludes only / (0x2F) and ~ (0x7E), so # (0x23) is an ordinary character in a token. Only the URI fragment identifier representation (section 6) begins with #, and that form is not a JSON Pointer string. The JSON-Schema-Test-Suite cases #, #/ and #a are still rejected, because a JSON Pointer must start with /.

format value before after
json-pointer /a#b invalid valid
json-pointer /# invalid valid
json-pointer /foo#bar/baz invalid valid
relative-json-pointer 0/a#b invalid valid
relative-json-pointer 2/# invalid valid
json-pointer #/a, a#b, /a~2# invalid invalid
relative-json-pointer /a#b, 01/a, 0## invalid invalid

The fix removes # from the excluded characters in both patterns ([^/#~] → [^/~]), and nothing else changes. The 0# form of relative-json-pointer is unaffected.

JsonPointerFormatTest checks both formats through a schema with format assertions enabled. Its 7 cases with # inside a token fail without the change, and the reject cases pass before and after.

The full build (mvn --batch-mode verify, as in CI) passes:

  • JDK 17: 11303 tests, 0 failures, 0 errors, 47 skipped (the same skips as on master), plus the packaged-boundary test.
  • JDK 21: 11303 tests, 0 failures, 0 errors, 46 skipped.
  • Without the change the same build fails only on the 7 new cases.

The 2.x branch has the same two patterns (Formats.java lines 75–76) if a backport is wanted.

The change and the tests were prepared with an AI assistant (Claude Code with Claude Opus 5.5).

I used an AI assistant to help prepare this change.

RFC 6901 section 3 lets a reference token contain any character except
'/' and '~' (escaped as ~0 and ~1), so "/a#b" is a valid JSON Pointer.
The json-pointer and relative-json-pointer patterns also excluded '#',
so format assertions rejected such pointers. Only the URI fragment
representation (section 6) starts with '#', and that form is still
rejected because a JSON Pointer must start with '/'.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 09:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@stevehu

stevehu commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Review of #1394

Reviewed at head 727d2de. The change looks right to me: RFC 6901 section 3 excludes only / and ~ from a reference token, so # belongs there. All 11,303 tests pass on JDK 25 here, with 45 skipped.

Since this PR rewrites both patterns, two older problems in them are worth fixing here too, plus a CHANGELOG line.

Older problems in the same patterns

1. A long reference token throws StackOverflowError (Formats.java:75 and :76)

With format assertions on, validating "/" + "a".repeat(5000) as json-pointer makes schema.validate throw java.lang.StackOverflowError. "0/" + "a".repeat(5000) does the same for relative-json-pointer. The JDK regex engine recurses once per character on the ([^/~]|[~](?=[01]))* group. master overflows at the same length.

Suggested fix: use possessive groups, as uri-template in the same file already does:

^(?:/(?:[^/~]|~[01])*+)*+$

Use the same token part inside relative-json-pointer. Each token can be split only one way, so the possessive form accepts the same values. In a probe it matched a 100,000-character token in about 4 ms, and it gave the same result as this PR's pattern on 18 accept and reject cases.

2. relative-json-pointer rejects index manipulation (Formats.java:76)

The 2020-12 validation spec references draft-bhutton-relative-json-pointer-00. Its grammar is origin-specification = non-negative-integer [index-manipulation] with index-manipulation = ("+" / "-") positive-integer. Under it, 0+1/foo and 1-1# are valid, but this pattern rejects them. The vendored suite has no such case.

Allowing ([+-][1-9]\d*)? after the integer would cover it. The earlier drafts reference draft-handrews, which has no index manipulation, and the format list is shared by every dialect, so this may need to apply to 2020-12 only. +1/foo/bar and -1/foo/bar must stay invalid, as the suite expects.

Minor

  1. CHANGELOG. /a#b, /# and 0/a#b are now valid, where 3.0.9 rejected them. The recent format changes (Reject over-long hostnames and invalid A-labels in the hostname format #1377, Reject a trailing label separator in idn-hostname #1384 to Tighten the fallback regex for uri, uri-reference, iri and iri-reference #1391) each documented accepted and rejected values under [Unreleased], so a line under ### Changed would match.

With format assertions on, a long reference token, such as "/" followed
by 5,000 characters as json-pointer or "0/" followed by them as
relative-json-pointer, made validation throw StackOverflowError: the
regex engine recursed once per character on the token group.

Both patterns now share one JSON Pointer sub-pattern with possessive
groups, as uri-template already uses. A token can be split into
characters and ~0/~1 escapes only one way, so the possessive form
accepts the same values; a 100,000-character token now validates.
Draft 2020-12 references draft-bhutton-relative-json-pointer-00, whose
origin-specification may end with an index manipulation,
("+" / "-") positive-integer, so 0+1/foo and 1-1# are valid there. The
earlier drafts reference draft-handrews-relative-json-pointer, which has
no index manipulation.

The format list is shared by all dialects, so relative-json-pointer is
now a RelativeJsonPointerFormat that reads the specification version from
the schema context, like BaseFormatValidator does, and allows the index
manipulation only for 2020-12. +1/foo/bar, -1/foo/bar, 0+0 and 0-01 stay
invalid, and nothing changes for draft 4 to 2019-09.
@stevehu

stevehu commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Re-review of #1394

Reviewed at head b32d58a. Thanks, all three points are addressed. All 11,330 tests pass on JDK 25 here, with 45 skipped, including the 52 in JsonPointerFormatTest.

  • Long tokens: JSON_POINTER is possessive, and RelativeJsonPointerFormat reuses it, so the 5,000- and 100,000-character cases no longer overflow.
  • Index manipulation: accepted only when the dialect is 2020-12 or later, which includes OpenAPI 3.1. Draft 7, 2019-09 and OpenAPI 3.0 are unchanged. A custom meta-schema built on 2020-12 takes its specification version from its base dialect, so it gets the new behaviour too. +1/foo/bar, -1/foo/bar and 0+0 stay invalid, as the suite expects.
  • CHANGELOG: both entries are there.

Minor

The StackOverflowError fix is the last sentence of a ### Changed bullet about #. A separate bullet under ### Fixed would be easier to find for someone who hit it in 3.0.9, for example: "json-pointer and relative-json-pointer no longer throw StackOverflowError with format assertions on when a reference token is long; 3.0.9 threw it at about 5,000 characters."

This branch has not been deployed

No deployments
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.

3 participants