Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/networknt/schema/format/Formats.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
* <p>
* 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<Format> DEFAULT;

static {
Expand All @@ -72,8 +82,8 @@ static PatternFormat pattern(String name, String regex) {
formats.add(new HostnameFormat());
formats.add(pattern("ipv4", "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$", "format.ipv4"));
formats.add(new IPv6Format());
formats.add(pattern("json-pointer", "^(/([^/#~]|[~](?=[01]))*)*$", "format.json-pointer"));
formats.add(pattern("relative-json-pointer", "^(0|([1-9]\\d*))(#|(/([^/#~]|[~](?=[01]))*)*)$", "format.relative-json-pointer"));
formats.add(pattern("json-pointer", "^" + JSON_POINTER + "$", "format.json-pointer"));
formats.add(new RelativeJsonPointerFormat());
formats.add(pattern("uri-template", "^(?:" + URI_TEMPLATE_LITERALS + "|%\\p{XDigit}{2}|\\{[+#./;?&=,!@|]?" + URI_TEMPLATE_VARSPEC + "(?:," + URI_TEMPLATE_VARSPEC + ")*+\\})*+$", "format.uri-template"));
formats.add(pattern("uuid", "^\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12}$", "format.uuid"));
formats.add(new DateFormat());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 java.util.regex.Pattern;

import com.networknt.schema.ExecutionContext;
import com.networknt.schema.SchemaContext;
import com.networknt.schema.SpecificationVersion;

/**
* Format for relative-json-pointer.
* <p>
* 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";
}
}
141 changes: 141 additions & 0 deletions src/test/java/com/networknt/schema/format/JsonPointerFormatTest.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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<Error> errors = schema.validate("\"" + value + "\"", InputFormat.JSON,
executionContext -> executionContext.executionConfig(config -> config.formatAssertionsEnabled(true)));
return errors.isEmpty();
}

@ParameterizedTest
@ValueSource(strings = { "", "/", "/foo", "/a#b", "/#", "/foo#bar/baz", "/~0#~1", "/c%d/e^f/g|h" })
void jsonPointerAccepts(String value) {
assertEquals(true, valid("json-pointer", value), value);
}

@ParameterizedTest
@ValueSource(strings = { "#", "#/", "#/a", "a#b", "/a~2#", "/~#" })
void jsonPointerRejects(String value) {
assertEquals(false, valid("json-pointer", value), value);
}

@ParameterizedTest
@ValueSource(strings = { "0", "0#", "1/foo", "0/a#b", "2/#", "10/foo#bar" })
void relativeJsonPointerAccepts(String value) {
assertEquals(true, valid("relative-json-pointer", value), value);
}

@ParameterizedTest
@ValueSource(strings = { "#/a", "/a#b", "01/a", "0/a~2#", "0##" })
void relativeJsonPointerRejects(String value) {
assertEquals(false, valid("relative-json-pointer", value), value);
}

/**
* 2020-12 references draft-bhutton-relative-json-pointer-00, whose origin-specification may end with an index
* manipulation: {@code ("+" / "-") positive-integer}.
*/
@ParameterizedTest
@ValueSource(strings = { "0+1/foo", "1-1#", "0+1", "2-1/a#b", "10+20#", "0-1/" })
void relativeJsonPointerIndexManipulationAcceptedIn202012(String value) {
assertEquals(true, valid(SpecificationVersion.DRAFT_2020_12, "relative-json-pointer", value), value);
}

@ParameterizedTest
@ValueSource(strings = { "+1/foo/bar", "-1/foo/bar", "0+0", "0-01", "0+", "0-", "0+1+1", "0++1", "01+1", "0+1##" })
void relativeJsonPointerInvalidIndexManipulationRejectedIn202012(String value) {
assertEquals(false, valid(SpecificationVersion.DRAFT_2020_12, "relative-json-pointer", value), value);
}

/**
* Drafts 7 and 2019-09 reference draft-handrews-relative-json-pointer, which has no index manipulation.
*/
@ParameterizedTest
@ValueSource(strings = { "0+1/foo", "1-1#", "0+1" })
void relativeJsonPointerIndexManipulationRejectedBefore202012(String value) {
assertEquals(false, valid(SpecificationVersion.DRAFT_2019_09, "relative-json-pointer", value), value);
assertEquals(false, valid(SpecificationVersion.DRAFT_7, "relative-json-pointer", value), value);
}

@ParameterizedTest
@ValueSource(strings = { "0", "0#", "1/foo", "0/a#b", "10/foo#bar" })
void relativeJsonPointerWithoutIndexManipulationAcceptedBefore202012(String value) {
assertEquals(true, valid(SpecificationVersion.DRAFT_2019_09, "relative-json-pointer", value), value);
assertEquals(true, valid(SpecificationVersion.DRAFT_7, "relative-json-pointer", value), value);
}

private static String repeat(String s, int count) {
StringBuilder sb = new StringBuilder(s.length() * count);
for (int i = 0; i < count; i++) {
sb.append(s);
}
return sb.toString();
}

/**
* The token and pointer groups are possessive, so the regex engine does not recurse once per character and a long
* token cannot overflow the stack.
*/
@ParameterizedTest
@ValueSource(ints = { 5_000, 100_000 })
void longTokensDoNotOverflow(int length) {
String token = repeat("a", length);
assertEquals(true, valid("json-pointer", "/" + token));
assertEquals(true, valid("json-pointer", "/" + repeat("a~0#", length / 4)));
assertEquals(false, valid("json-pointer", "/" + token + "~2"));
assertEquals(true, valid("json-pointer", repeat("/a", length / 2)));
assertEquals(true, valid("relative-json-pointer", "0/" + token));
assertEquals(true, valid("relative-json-pointer", "1" + repeat("/a~1", length / 4)));
assertEquals(false, valid("relative-json-pointer", "0/" + token + "~"));
assertEquals(true, valid("relative-json-pointer", "0" + repeat("/a", length / 2)));
}

@Test
void longTokenRejectedWithoutOverflow() {
assertEquals(false, valid("json-pointer", repeat("a", 100_000)));
assertEquals(false, valid("relative-json-pointer", "01/" + repeat("a", 100_000)));
}
}