Skip to content

fix: Validate the uri, uri-reference and email formats as defined by RFC 3986 and RFC 5321 (email no longer accepts non-ASCII local parts) - #1005

Open
Roman3349 wants to merge 3 commits into
jsonrainbow:mainfrom
Roman3349:fix/uri-email-formats
Open

Roman3349 wants to merge 3 commits into
jsonrainbow:mainfrom
Roman3349:fix/uri-email-formats

Conversation

@Roman3349

Copy link
Copy Markdown
Contributor

Description

The uri, uri-reference and email formats accepted values that are not valid, and rejected some that are.

UriValidator and RelativeReferenceValidator checked URIs with patterns that only roughly follow RFC 3986:

  • A trailing newline passed, since the patterns ended with $ instead of \z.
  • A lone or incomplete percent-encoding (http://example.com/%, http://example.com/%6G) passed in a URI.
  • The userinfo and the path of a URI reference accepted characters RFC 3986 does not allow, such as [, " or non-ASCII characters, more than one @, and a non-numeric port.
  • An IPv6 literal was not validated, so a leading zero in its IPv4 part (http://[::ffff:01.2.3.4]) passed.
  • A colon in the first segment of a relative-path reference (1:b) passed, while the network-path reference // with an empty authority was rejected.

The email format used filter_var() with FILTER_FLAG_EMAIL_UNICODE, which accepts non-ASCII characters in the local part (aé@iana.org), but rejects quoted local parts with a space ("joe bloggs"@example.com, " "@iana.org), single-label domains (test@io) and domains that look like an IPv4 address (test@255.255.255.255).

Changes:

  • Tool\Validator\Rfc3986: new, matches the URI and URI-reference grammar of RFC 3986, section 3 and 4.1. An IP literal is checked with filter_var().
  • UriValidator, RelativeReferenceValidator: check the RFC 3986 grammar first, and then their existing rules, so the stricter rules they already had (for example for mailto: URIs, or rejecting ///) are kept. RelativeReferenceValidator no longer rejects //.
  • Tool\Validator\EmailValidator: new, validates an address as the Mailbox of RFC 5321, section 4.1.2: a dot-string or quoted-string local part of at most 64 octets, and a domain or an IPv4/IPv6 address literal. It is ASCII only, internationalized addresses are covered by idn-email.
  • FormatConstraint (non-strict mode) and the Draft-06, Draft-07 and Draft 2019-09 FormatConstraints: use EmailValidator for email.
  • JsonSchemaTestSuiteTest: removes the 68 skipped optional/format/uri.json, uri-reference.json and email.json cases, which all pass now, together with their comments and the comment of the optional anchor section, which is already empty on main.

Related Issue

None

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Other (please describe):

Checklist

  • I have read the CONTRIBUTING guidelines
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • All new and existing tests pass
  • I have updated the documentation accordingly
  • My changes generate no new warnings

Additional Notes

info@somewhere was an invalid email in FormatConstraintTest. It is valid now, as the test suite expects a single-label domain to be valid (test@io).

#1002 (Draft-04 in strict mode) and #971 (Draft 2020-12) include this commit and apply it to their FormatConstraints as well.

@DannyvdSluijs DannyvdSluijs left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved but I believe a small cleanup can be done to avoid duplicate checks as flagged by an agent review.

@Roman3349

Copy link
Copy Markdown
Contributor Author

Another AI agent review of this PR found some more possible issues. I'll fix them before this gets merged, so please hold off on merging for now.

Important

  1. uri still rejects URIs that RFC 3986 allows. The old patterns in UriValidator still run after the Rfc3986::isUri() check and reject values the grammar accepts. This was already the case before this PR. Examples that should be valid:

    • foo:bar, about:blank, tag:a,2020:x: only a fixed list of schemes is allowed
    • http://a!b.com/, http://%65xample.com/, http://[v1.fe]/: the host character set is restricted
    • http://:p@h/: an empty user is rejected
    • http:///x, http://: a host is required
    • mailto:?to=a@b.co, mailto:a@b.co,c@d.co: mailto must contain exactly one plain address
    • http://a..b/: a double dot in the host is rejected
    • http://h:123456/: the port is capped at 5 digits
  2. uri and uri-reference disagree. uri-reference is checked as UriValidator || RelativeReferenceValidator. The ^(http|https):// check in RelativeReferenceValidator is case-sensitive and only blocks lowercase http(s). So foo:bar, ftp://[v1.fe]/ and HTTP://a!b/ are invalid as uri but valid as uri-reference.

    I plan to fix both by leaving the decision to Rfc3986::isUri() and Rfc3986::isUriReference() alone.

Minor

  1. ///x is rejected. I was wrong in my reply above about keeping this check. ///x is a valid relative reference (an empty authority followed by the path /x), and ///, ////x and file:///x are already accepted. The check and the "Three slashes" test will go.
  2. The total e-mail length is not limited. The local part (64) and the domain (255) are checked separately. RFC 5321 §4.5.3.1.3 caps the whole path, which leaves 254 octets for the address, but a 320-character address is accepted today.
  3. E-mail address literals are checked with filter_var rather than the RFC 5321 grammar.
    • a@[127.000.000.001] is rejected, but it is valid.
    • a@[IPv6:1:2:3:4:5:6:7::] is accepted, but :: must stand for at least two groups.
  4. Test gaps.
    • There is no direct unit test for Rfc3986, e.g. for IPvFuture or for [1.2.3.4] being rejected.
    • EmailValidatorTest has no cases for a domain over 255 characters, a label over 63 characters, an unterminated escape, an invalid IPv6 literal or ""@a.com.

One more point that is a behaviour change rather than a bug: email now rejects non-ASCII local parts in every draft, including Draft-03/04. That matches the test suite, but I'll mention it in the CHANGELOG.

@Roman3349
Roman3349 force-pushed the fix/uri-email-formats branch from d60d16d to b37bb28 Compare December 4, 2026 11:07
@Roman3349 Roman3349 changed the title fix: Validate the uri, uri-reference and email formats as defined by RFC 3986 and RFC 5321 fix: Validate the uri, uri-reference and email formats as defined by RFC 3986 and RFC 5321 (email no longer accepts non-ASCII local parts) Dec 4, 2026
@Roman3349

Copy link
Copy Markdown
Contributor Author

All six findings from my previous comment are fixed in b37bb28:

  1. UriValidator now leaves the decision to Rfc3986::isUri(), without the old patterns that rejected URIs RFC 3986 allows.
  2. RelativeReferenceValidator uses the new Rfc3986::isRelativeReference(). uri || relative-ref is now exactly URI-reference, so uri and uri-reference agree.
  3. ///x is accepted.
  4. An e-mail address is limited to 254 octets in total.
  5. E-mail address literals follow the RFC 5321 grammar instead of filter_var.
  6. A new Rfc3986Test and more cases in the e-mail, URI and relative reference tests.

I checked Rfc3986 against a reference implementation written straight from the ABNF in RFC 3986, Appendix A. Over 400k generated strings and about 5.7k systematic IPv6 literal cases gave no differences.

Some existing test cases expected stricter behaviour than RFC 3986 allows, so they are now valid: https://example..com, port 65536, mailto: and htt:/bluebox.org.

About the CHANGELOG: it is generated from the PR title, so I put the behaviour change in the title instead of editing the CHANGELOG by hand. Feel free to add the breaking label if you consider it a BC break.

I also re-signed the commits after the rebase.

@DannyvdSluijs

Copy link
Copy Markdown
Collaborator

@Roman3349 #1010 was just merged. This fixes the running of the test suite as upstream was refactored into a new file and json structure. Could you rebase this and any other PR's to ensure there is no regression with the PR's comparing against the official test suite?

…RFC 3986 and RFC 5321

Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
… address literals

Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
@Roman3349
Roman3349 force-pushed the fix/uri-email-formats branch from b37bb28 to b5a2c2a Compare December 4, 2026 14:17
@Roman3349

Copy link
Copy Markdown
Contributor Author

Thanks! I rebased this PR and my other open PRs onto main with #1010: #1002, #971, #984, #987, #992, #1007 and #1008. All of them pass the full test suite locally against the pinned test suite, and CI is running now.

The rebase surfaced a few things:

I did not rebase #993, because it targets the branch of #926 rather than main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants