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

Open Beginner friendly
#1,588 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 6 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
devtools

Research direction

The affected checkers are in jsonschema/_format.py, and the regression tests belong in tests/test_format.py. Start by reproducing the leak with the FormatChecker snippet in the issue, then check the official-suite optional/format/ cases for the trailing-newline instances. The reporter already has a branch, so check the comment thread and ask a maintainer before writing a competing patch. Done means every listed format rejects a single trailing \n and the full test suite still passes.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
5k
Forks
741
Avg merge
4d 17h
Merged PRs (30d)
6

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-jsonschema/jsonschema

All issues in python-jsonschema/jsonschema

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.