Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭 适合新手
#1,588 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
65/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
devtools

调研方向

受影响的检查器位于 jsonschema/_format.py,回归测试应添加到 tests/test_format.py。先用 issue 中的 FormatChecker 代码片段复现这个泄漏,然后检查官方测试套件中 optional/format/ 的用例,查看末尾带换行符的实例。报告者已经有一个分支,因此请先查看评论区,并在编写竞争性补丁之前征求维护者的意见。当所有列出的格式都会拒绝末尾单个 \n,且完整测试套件仍然通过时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Python
星标
5k
派生
694
平均合并
1 天 20 小时
30 天内合并 PR
6

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python-jsonschema/jsonschema 的其他 Issue

查看 python-jsonschema/jsonschema 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。