python / python/cpython

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

Aperta
#157,518 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib topic-XML type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da xml.etree.ElementTree.register_namespace() e confronta la relativa validazione documentata con i binding non validi di prefisso e namespace mostrati nel report. La modifica è completata quando questi casi sollevano ValueError, mentre il caso del prefisso vuoto rimane separato; gh-157521 è la PR collegata da esaminare per verificare il lavoro già in corso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.