python / python/cpython

`HTMLParser.unknown_decl` receives corrupted data (`'CDATA['`) when parsing an empty `CDATA` section

未关闭
#140,878 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.13 3.14 3.15 stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

When parsing the input <![CDATA[]]>, the unknown_decl hook is incorrectly called with the corrupted, partial string 'CDATA['.

A correct parser has only two possible-and-correct behaviors:

  • If CDATA is supported: Call handle_cdata('').
  • If the declaration is "unrecognized," the unknown_decl hook must receive the entire content inside <!...>, which would be '[CDATA[]]'.

The actual result ('CDATA[') matches neither of those. I use the private _set_support_cdata(True) method here since I think it was the only available trigger to activate this specific code path to expose the bug.

from html.parser import HTMLParser

class CdataBugParser(HTMLParser):
    def __init__(self):
        super().__init__()
        self.unknown_decls = []

    def unknown_decl(self, data):
        self.unknown_decls.append(data)

html_input = "<![CDATA[]]>"
parser = CdataBugParser()
parser._set_support_cdata(True)
parser.feed(html_input)
print(parser.unknown_decls)
['CDATA[']
CPython versions tested on:

3.12

Operating systems tested on:

Linux

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先使用 HTMLParser 运行 issue 的 Python 复现程序,并检查调用 unknown_decl 的声明处理路径。为一个空的 CDATA 节添加覆盖测试,并验证解析结果是 handle_cdata('') 或完整的未知声明 '[CDATA[]]',而不是 'CDATA['。

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

评估

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

把新 issue 发到你的邮箱

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