Skip to content

Commit 8ca5769

Browse files
Allow underscore and tilde in URI hostnames as per RFC 3986 (#853)
The implementation of [RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986) in the URI validator is more strict than the specification allows. While the RFC says > URI producers should use names that conform to the DNS syntax, even when use of DNS is not immediately apparent the host component grammar actually allows more characters than are allowed in DNS names. This is a problem for the Drupal project which uses this package in conjunction with custom stream wrappers with module names in the host component. Module names conform to PHP function name standards, not DNS standards - that is, they allow underscores but not dashes. The relevant RFC grammar is ``` host = IP-literal / IPv4address / reg-name reg-name = *( unreserved / pct-encoded / sub-delims ) unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" ``` This PR widens the `host` validation to allow all characters in the `unreserved` set. While `pct-encoded` and `sub-delims` are also technically allowed, this is the smallest change that will help us out. --------- Co-authored-by: Danny van der Sluijs <danny.vandersluijs@icloud.com>
1 parent fd8e5c6 commit 8ca5769

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Fixed
10+
- Allow underscore and tilde in URI hostnames as per RFC 3986 ([#853](https://github.com/jsonrainbow/json-schema/pull/853))
911

1012
## [6.6.1] - 2025-11-07
1113
### Changed

src/JsonSchema/Tool/Validator/UriValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static function isValid(string $uri): bool
1212
$hierarchicalPattern = '/^
1313
([a-z][a-z0-9+\-.]*):\/\/ # Scheme (http, https, ftp, etc.)
1414
(?:([^:@\/?#]+)(?::([^@\/?#]*))?@)? # Optional userinfo (user:pass@)
15-
([a-z0-9.-]+|\[[a-f0-9:.]+\]) # Hostname or IPv6 in brackets
15+
([a-z0-9._~-]+|\[[a-f0-9:.]+\]) # Hostname or IPv6 in brackets
1616
(?::(\d{1,5}))? # Optional port
1717
(\/[a-zA-Z0-9._~!$&\'()*+,;=:@\/%-]*)* # Path (valid characters only)
1818
(\?([^#]*))? # Optional query

tests/Tool/Validator/UriValidatorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public function validUriDataProvider(): \Generator
3232
yield 'Data URI' => ['uri' => 'data:text/plain;charset=utf-8,Hello%20World!'];
3333
yield 'ISBN URN URI' => ['uri' => 'urn:isbn:0451450523'];
3434
yield 'OASIS URN URI' => ['uri' => 'urn:oasis:names:specification:docbook:dtd:xml:4.1.2'];
35+
yield 'Custom URI with underscore' => ['uri' => 'custom://reg_name/path/file.json'];
36+
yield 'Custom URI with tilde' => ['uri' => 'custom://reg~name/path/file.json'];
3537
}
3638

3739
public function invalidUriDataProvider(): \Generator

0 commit comments

Comments
 (0)