python / python/cpython

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

Đang mở
#140,878 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

3.13 3.14 3.15 stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy trình tái hiện Python của issue với HTMLParser và kiểm tra đường xử lý khai báo gọi unknown_decl. Bổ sung coverage cho một phần CDATA rỗng và xác minh rằng việc phân tích cú pháp tạo ra либо handle_cdata('') hoặc khai báo không xác định đầy đủ '[CDATA[]]', thay vì 'CDATA['.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
web-dev
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
65/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.