diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d9def139..3556189dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,10 +36,13 @@ This project does not adhere to [Semantic Versioning](https://semver.org/) and m - `idn-hostname` does not apply UTS 46 mapping. - Removed the unused `UnicodeDatabase.isGreek`, `isHebrew` and `isKatakana`, which tested code blocks rather than scripts. - Updated the vendored `hostname.json` and `idn-hostname.json` suite files to the current JSON-Schema-Test-Suite, including `draft-next`. Cases that need UTS 46 mapping are disabled with a reason. +- `json-pointer` and `relative-json-pointer` accept `#` inside a reference token, as RFC 6901 section 3 allows: `/a#b`, `/#` and `0/a#b` are now valid, where 3.0.9 rejected them. A value that starts with `#` (the URI fragment form), such as `#/a`, remains invalid as `json-pointer`. +- `relative-json-pointer` accepts an index manipulation after the integer in the 2020-12 dialects (Draft 2020-12 and OpenAPI 3.1), as draft-bhutton-relative-json-pointer-00 allows: `0+1/foo` and `1-1#` are now valid there. `+1/foo/bar`, `-1/foo/bar`, `0+0` and `0-01` remain invalid, and the earlier dialects, whose drafts have no index manipulation, are unchanged. `relative-json-pointer` is now implemented by `RelativeJsonPointerFormat` instead of a `PatternFormat`. ### Fixed - `uri`, `uri-reference`, `iri` and `iri-reference` check the value as it is when `java.net.URI` cannot parse it. Since JDK 25, the input of the `URISyntaxException` is trimmed, or empty, depending on the `jdk.includeInExceptions` security property, so `http://` with a leading space was accepted and `about:` could be rejected. - `uri-template` no longer throws `StackOverflowError` on long templates, such as a 5,000-character literal or an expression with a 1,000-character variable name. Runs of literal and variable-name characters are matched without recursion. +- `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. Reference tokens are matched possessively, so a token of 100,000 characters is accepted too. - Prevent descendant discriminator state from leaking between `oneOf` and `anyOf` branches in OpenAPI 3.0 and 3.1. [#1378](https://github.com/networknt/json-schema-validator/issues/1378) ## 3.0.9- 2026-11-03 diff --git a/src/main/java/com/networknt/schema/format/Formats.java b/src/main/java/com/networknt/schema/format/Formats.java index a3c646c2a..2795e02f3 100644 --- a/src/main/java/com/networknt/schema/format/Formats.java +++ b/src/main/java/com/networknt/schema/format/Formats.java @@ -64,6 +64,16 @@ static PatternFormat pattern(String name, String regex) { private static final String URI_TEMPLATE_VARSPEC = "(?:\\w++|%\\p{XDigit}{2})++" + "(?:\\.(?:\\w++|%\\p{XDigit}{2})++)*+(?::[1-9]\\d{0,3}|\\*)?"; + /** + * An RFC 6901 JSON Pointer: reference tokens, each preceded by {@code /}, whose characters are anything but + * {@code /} and {@code ~}, or the escapes {@code ~0} and {@code ~1}. + *
+ * A token can be split into characters and escapes only one way, so the groups are possessive, as in
+ * {@link #URI_TEMPLATE_LITERALS}: the regex engine does not recurse once per character, and a long token cannot
+ * overflow the stack.
+ */
+ static final String JSON_POINTER = "(?:/(?:[^/~]|~[01])*+)*+";
+
public static final List
+ * A non-negative integer, followed by {@code #} or a JSON Pointer. Draft 2020-12 references
+ * draft-bhutton-relative-json-pointer-00, which also allows an index manipulation after the integer,
+ * {@code ("+" / "-") positive-integer}, as in {@code 0+1/foo} or {@code 1-1#}. The earlier drafts reference
+ * draft-handrews-relative-json-pointer, which has no index manipulation, so it is accepted only when the dialect's
+ * specification version is 2020-12 or later.
+ */
+public class RelativeJsonPointerFormat implements Format {
+ private static final String NON_NEGATIVE_INTEGER = "(?:0|[1-9]\\d*+)";
+ private static final String INDEX_MANIPULATION = "(?:[+-][1-9]\\d*+)?";
+ private static final String END = "(?:#|" + Formats.JSON_POINTER + ")";
+
+ private static final Pattern PATTERN = Pattern.compile("^" + NON_NEGATIVE_INTEGER + END + "$");
+ private static final Pattern PATTERN_INDEX_MANIPULATION = Pattern
+ .compile("^" + NON_NEGATIVE_INTEGER + INDEX_MANIPULATION + END + "$");
+
+ @Override
+ public boolean matches(ExecutionContext executionContext, String value) {
+ return PATTERN.matcher(value).matches();
+ }
+
+ @Override
+ public boolean matches(ExecutionContext executionContext, SchemaContext schemaContext, String value) {
+ SpecificationVersion specification = schemaContext != null && schemaContext.getDialect() != null
+ ? schemaContext.getDialect().getSpecificationVersion()
+ : null;
+ if (specification != null && specification.getOrder() >= SpecificationVersion.DRAFT_2020_12.getOrder()) {
+ return PATTERN_INDEX_MANIPULATION.matcher(value).matches();
+ }
+ return matches(executionContext, value);
+ }
+
+ @Override
+ public String getName() {
+ return "relative-json-pointer";
+ }
+
+ @Override
+ public String getMessageKey() {
+ return "format.relative-json-pointer";
+ }
+}
diff --git a/src/test/java/com/networknt/schema/format/JsonPointerFormatTest.java b/src/test/java/com/networknt/schema/format/JsonPointerFormatTest.java
new file mode 100644
index 000000000..c41c743cf
--- /dev/null
+++ b/src/test/java/com/networknt/schema/format/JsonPointerFormatTest.java
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2026 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.networknt.schema.format;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import com.networknt.schema.Error;
+import com.networknt.schema.InputFormat;
+import com.networknt.schema.Schema;
+import com.networknt.schema.SchemaRegistry;
+import com.networknt.schema.SpecificationVersion;
+
+/**
+ * Tests for the json-pointer and relative-json-pointer formats.
+ *
+ * RFC 6901 section 3: a reference token may contain any character except '/' and '~', which is escaped as '~0' or
+ * '~1'. A '#' is an ordinary character inside a token; only the URI fragment representation (section 6) starts with
+ * '#', and that is not a JSON Pointer string.
+ */
+class JsonPointerFormatTest {
+
+ private static boolean valid(String format, String value) {
+ return valid(SpecificationVersion.DRAFT_2020_12, format, value);
+ }
+
+ private static boolean valid(SpecificationVersion version, String format, String value) {
+ Schema schema = SchemaRegistry.withDefaultDialect(version)
+ .getSchema("{\"type\": \"string\", \"format\": \"" + format + "\"}");
+ List