python / python/cpython

xml.etree.ElementTree.register_namespace() does not validate the prefix

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

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

stdlib topic-XML 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

The documentation of register_namespace() says that ValueError is raised "if prefix is reserved or is invalid", but only the reserved form ns followed by digits is checked. Any other string is accepted, and the serializer then produces XML which is not well-formed or not namespace-well-formed:

>>> import xml.etree.ElementTree as ET
>>> def show(prefix, uri):
...     ET.register_namespace(prefix, uri)
...     print(ET.tostring(ET.Element(f'{{{uri}}}a', {f'{{{uri}}}k': 'v'}), encoding='unicode'))
...
>>> show('xml', 'u')
<xml:a xml:k="v" />
>>> show('foo', 'http://www.w3.org/XML/1998/namespace')
<foo:a xmlns:foo="http://www.w3.org/XML/1998/namespace" foo:k="v" />
>>> show('xmlns', 'u')
<xmlns:a xmlns:xmlns="u" xmlns:k="v" />
>>> show('a:b', 'u')
<a:b:a xmlns:a:b="u" a:b:k="v" />
>>> show('a b', 'u')
<a b:a xmlns:a b="u" a b:k="v" />

The xml prefix can only be bound to http://www.w3.org/XML/1998/namespace and no other prefix can be bound to it, xmlns cannot be used as a prefix, and a prefix must be an NCName (Namespaces in XML 1.0, sections 3 and 4). register_namespace() should raise ValueError in these cases, like it does for the reserved prefixes.

The empty prefix, which registers a global default namespace, is a separate case, see gh-118416.

Linked PRs
  • gh-157521

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 từ xml.etree.ElementTree.register_namespace() và so sánh việc validation được tài liệu hóa của nó với các binding prefix và namespace không hợp lệ được nêu trong báo cáo. Thay đổi được hoàn tất khi các trường hợp này raise ValueError, trong khi trường hợp prefix rỗng vẫn được giữ riêng; gh-157521 là PR liên kết cần kiểm tra để xem công việc đã được tiến hành hay chưa.

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
backend
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
35/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.