xml.sax: the content of an external entity is not checked to be well-formed
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
The parsing of an external entity is never finalized, so errors which are only detected at the end of the input are not reported. An external entity whose content is not well-formed is silently accepted.
import io, xml.sax
from xml.sax.handler import feature_external_ges
from xml.sax.xmlreader import InputSource
class Resolver:
def resolveEntity(self, pubid, sysid):
source = InputSource()
source.setByteStream(io.BytesIO(b'<entity>')) # no end tag
return source
parser = xml.sax.make_parser()
parser.setFeature(feature_external_ges, True)
parser.setEntityResolver(Resolver())
parser.feed('<!DOCTYPE d [<!ENTITY e SYSTEM "x">]><d>&e;</d>')
parser.close() # no error
ExpatParser.close() returns early when _entity_stack is not empty, so feed(b"", isFinal=True) is never called for the parser created for the entity. The check is needed to not end the document while the entity is being parsed, but it also skips finalizing the entity itself.
If the parser of the entity is finalized, the example above fails with "error in processing external entity reference", and test_sax still passes.
Errors which Expat detects while feeding data, like a mismatched tag, are reported even now. Only errors detected at the end of the input, like an unclosed element, are lost.
Linked PRs
- gh-156828
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 ExpatParser.close() 和报告中描述的外部实体解析器路径开始;使用提供的复现程序确认缺少最终化处理。运行 test_sax,并验证格式错误的外部实体会引发报告中的错误,同时现有测试仍然通过。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100