jsonschema icon indicating copy to clipboard operation
jsonschema copied to clipboard

Several format checkers accept a single trailing newline (U+000A)

Open Master-Norna opened this issue 1 month ago • 2 comments

The date-time, time, uri, uri-reference, iri, iri-reference, and hostname/host-name checkers each accept a single trailing newline (U+000A), although none of the corresponding grammars allow one.

from jsonschema import FormatChecker

c = FormatChecker()
c.check("1985-04-12T23:20:50Z\n", "date-time")   # accepted
c.check("http://foo.bar/\n", "uri")              # accepted
c.check("example.com\n", "hostname")             # accepted

The official test suite now asserts these are invalid. The matching "trailing newline is invalid" cases appear under each draft's optional/format/ for all of the formats above (e.g. date-time.json, time.json, uri.json, uri-reference.json, iri.json, iri-reference.json, hostname.json). The instance without the trailing newline remains valid in every case, so the leak is the bare trailing \n and nothing else.

Why it happens

Each of these checkers delegates to an external validator package (rfc3339-validator, rfc3987 / rfc3986_validator / rfc3987_syntax, fqdn). Those packages each accept exactly one trailing \n — other stray whitespace is already rejected — so the behavior is asymmetric:

"1985-04-12T23:20:50Z"     # valid
"1985-04-12T23:20:50Z\n"   # accepted  (leak)
"1985-04-12T23:20:50Z\r\n" # rejected
"1985-04-12T23:20:50Z "    # rejected
" 1985-04-12T23:20:50Z"    # rejected

The vendored copy of the official suite shipped in json/ predates these cases, so CI does not currently catch it. I verified there is no format case in the pinned suite whose data contains a newline, so a guard here does not change any currently-pinned expectation.

Fix

A small shape guard in jsonschema/_format.py that rejects a trailing newline before delegating, shared by every affected checker and applied to both the rfc3987 and its rfc3986/rfc3987_syntax fallback branches, plus regression tests in tests/test_format.py that mirror the official-suite instances. Keeping the guard in jsonschema (rather than relying on each upstream library) covers all the delegated implementations in one place.

I've prepared it as a branch for cherry-pick: https://github.com/Master-Norna/jsonschema/compare/master...fix/formats-trailing-newline

The diff is small (~30 lines in _format.py, new test class in test_format.py) and I have it passing the full suite plus a re-run of the official suite's new format cases.

Master-Norna avatar Oct 14 '26 00:10 Master-Norna

I can take this one. Plan: reject a single trailing \n for the affected format checkers before delegating to the external validators, with regression tests mirroring the official suite's optional/format cases (date-time, time, uri, uri-reference, iri, iri-reference, hostname).

Madin-H23 avatar Oct 14 '26 02:10 Madin-H23

Thanks for offering to help! I already have an implementation and regression tests ready on this branch.

Based on the reports in #1558 that external contributors couldn't create PRs, I held off opening one. If the maintainers are happy to accept a PR, I can submit the prepared changes directly.

Master-Norna avatar Oct 14 '26 04:10 Master-Norna