-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
gh-149468: Add option to validate ElementTree during serialization #149469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e41d61c
a134c0b
c81fe70
ea414fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1387,6 +1387,194 @@ def test_attlist_default(self): | |||||||||||||||||||||||
| {'{http://www.w3.org/XML/1998/namespace}lang': 'eng'}) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| class XMLValidationTest(unittest.TestCase): | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def check(self, elem, expected=None): | ||||||||||||||||||||||||
| self.assertRaises(ValueError, | ||||||||||||||||||||||||
| ET.tostring, elem, validate=True) | ||||||||||||||||||||||||
| ET.tostring(elem) # no exception | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_comment(self): | ||||||||||||||||||||||||
| self.check(ET.Comment('a--b')) | ||||||||||||||||||||||||
| self.check(ET.Comment(' B+, B, or B-')) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_processing_instruction(self): | ||||||||||||||||||||||||
| self.check(ET.PI('')) | ||||||||||||||||||||||||
| self.check(ET.PI('0')) | ||||||||||||||||||||||||
| self.check(ET.PI('a/b')) | ||||||||||||||||||||||||
| self.check(ET.PI('foo\xa0bar')) | ||||||||||||||||||||||||
| self.check(ET.PI('xml')) | ||||||||||||||||||||||||
| self.check(ET.PI('xml', 'encoding="UTF-8"')) | ||||||||||||||||||||||||
| self.check(ET.PI('foo', 'a?>b')) | ||||||||||||||||||||||||
| self.check(ET.PI('foo', '\x00')) | ||||||||||||||||||||||||
| self.check(ET.PI('foo', '\ud8ff')) | ||||||||||||||||||||||||
| self.check(ET.PI('foo', '\ufffe')) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_tag(self): | ||||||||||||||||||||||||
| self.check(ET.Element('')) | ||||||||||||||||||||||||
| self.check(ET.Element('0')) | ||||||||||||||||||||||||
| self.check(ET.Element('a/b')) | ||||||||||||||||||||||||
| self.check(ET.Element(ET.QName(''))) | ||||||||||||||||||||||||
| self.check(ET.Element(ET.QName('0'))) | ||||||||||||||||||||||||
| self.check(ET.Element(ET.QName('a/b'))) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_attr_name(self): | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'': 'value'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'0': 'value'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'a/b': 'value'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={ET.QName(''): 'value'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={ET.QName('0'): 'value'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={ET.QName('a/b'): 'value'})) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_attr_value(self): | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'key': '\x00'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'key': '\ud8ff'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'key': '\ufffe'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'key': ET.QName('\x00')})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'key': ET.QName('\ud8ff')})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'key': ET.QName('\ufffe')})) | ||||||||||||||||||||||||
|
Comment on lines
+1429
to
+1435
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and several other methods could use
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll think about it. The drawback is that this can make tests less flexible. It will be difficult to add different assertions in the same method. We also cannot use |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_text(self): | ||||||||||||||||||||||||
| elem = ET.Element('tag') | ||||||||||||||||||||||||
| elem.text = '\x00' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.text = '\ud8ff' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.text = '\ufffe' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_tail(self): | ||||||||||||||||||||||||
| elem = ET.Element('tag') | ||||||||||||||||||||||||
| elem.tail = '\x00' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.tail = '\ud8ff' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.tail = '\ufffe' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_text_without_tag(self): | ||||||||||||||||||||||||
| elem = ET.Element(None) | ||||||||||||||||||||||||
| elem.text = '\x00' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.text = '\ud8ff' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.text = '\ufffe' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_subelements(self): | ||||||||||||||||||||||||
| elem = ET.Element('tag') | ||||||||||||||||||||||||
| subelem = ET.SubElement(elem, 'subtag') | ||||||||||||||||||||||||
| ET.SubElement(subelem, '\x00') | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.tag = None | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_namespace_uri(self): | ||||||||||||||||||||||||
| self.check(ET.Element('{\x00}tag')) | ||||||||||||||||||||||||
| self.check(ET.Element('{\ud8ff}tag')) | ||||||||||||||||||||||||
| self.check(ET.Element('{\ufffe}tag')) | ||||||||||||||||||||||||
| self.check(ET.Element(ET.QName('\x00', 'tag'))) | ||||||||||||||||||||||||
| self.check(ET.Element(ET.QName('\ud8ff', 'tag'))) | ||||||||||||||||||||||||
| self.check(ET.Element(ET.QName('\ufffe', 'tag'))) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| class HTMLValidationTest(unittest.TestCase): | ||||||||||||||||||||||||
|
serhiy-storchaka marked this conversation as resolved.
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def check(self, elem, expected=None): | ||||||||||||||||||||||||
| self.assertRaises(ValueError, | ||||||||||||||||||||||||
| ET.tostring, elem, method='html', validate=True) | ||||||||||||||||||||||||
| ET.tostring(elem, method='html') # no exception | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_comment(self): | ||||||||||||||||||||||||
| self.check(ET.Comment('>')) | ||||||||||||||||||||||||
| self.check(ET.Comment('->')) | ||||||||||||||||||||||||
| self.check(ET.Comment('a-->b')) | ||||||||||||||||||||||||
| self.check(ET.Comment('a--!>b')) | ||||||||||||||||||||||||
| self.check(ET.Comment('a\x00b')) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_processing_instruction(self): | ||||||||||||||||||||||||
| self.check(ET.PI('a>b')) | ||||||||||||||||||||||||
| self.check(ET.PI('a\x00b')) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_tag(self): | ||||||||||||||||||||||||
| self.check(ET.Element('')) | ||||||||||||||||||||||||
| self.check(ET.Element('?')) | ||||||||||||||||||||||||
| self.check(ET.Element('!')) | ||||||||||||||||||||||||
| self.check(ET.Element('0')) | ||||||||||||||||||||||||
| self.check(ET.Element(' a')) | ||||||||||||||||||||||||
| self.check(ET.Element('a b')) | ||||||||||||||||||||||||
| self.check(ET.Element('a\nb')) | ||||||||||||||||||||||||
| self.check(ET.Element('a/b')) | ||||||||||||||||||||||||
| self.check(ET.Element('a>b')) | ||||||||||||||||||||||||
| self.check(ET.Element('a\x00b')) | ||||||||||||||||||||||||
| self.check(ET.Element(ET.QName(''))) | ||||||||||||||||||||||||
| self.check(ET.Element(ET.QName('0'))) | ||||||||||||||||||||||||
| self.check(ET.Element(ET.QName('a/b'))) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_attr_name(self): | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'': 'value'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'a/b': 'value'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'a=b': 'value'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={ET.QName(''): 'value'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={ET.QName('a/b'): 'value'})) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_attr_value(self): | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'key': '\x00'})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'key': ET.QName('\x00')})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'key': ET.QName('a"b')})) | ||||||||||||||||||||||||
| self.check(ET.Element('tag', attrib={'key': ET.QName('a&b')})) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_text(self): | ||||||||||||||||||||||||
| elem = ET.Element('tag') | ||||||||||||||||||||||||
| elem.text = '\x00' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_tail(self): | ||||||||||||||||||||||||
| elem = ET.Element('tag') | ||||||||||||||||||||||||
| elem.tail = '\x00' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_text_without_tag(self): | ||||||||||||||||||||||||
| elem = ET.Element(None) | ||||||||||||||||||||||||
| elem.text = '\x00' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_subelements(self): | ||||||||||||||||||||||||
| elem = ET.Element('tag') | ||||||||||||||||||||||||
| subelem = ET.SubElement(elem, 'subtag') | ||||||||||||||||||||||||
| ET.SubElement(subelem, '\x00') | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.tag = None | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_namespace_uri(self): | ||||||||||||||||||||||||
| self.check(ET.Element('{\x00}tag')) | ||||||||||||||||||||||||
| self.check(ET.Element(ET.QName('\x00', 'tag'))) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @support.subTests('tag', ("script", "style", "xmp", "iframe", "noembed", "noframes")) | ||||||||||||||||||||||||
| def test_invalid_cdata_content(self, tag): | ||||||||||||||||||||||||
| elem = ET.Element(tag.upper()) | ||||||||||||||||||||||||
| elem.text = 'a</%s>b' % tag.title() | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.text = 'a</%s b' % tag.title() | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.text = 'a</%s/b' % tag.title() | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
| elem.text = 'a\x00b' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| @support.subTests('tag', ("script", "style", "xmp", "iframe", "noembed", "noframes")) | ||||||||||||||||||||||||
| def test_cdata_subelements(self, tag): | ||||||||||||||||||||||||
| elem = ET.Element(tag) | ||||||||||||||||||||||||
| ET.SubElement(elem, 'subtag') | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def test_invalid_plaintext_content(self): | ||||||||||||||||||||||||
| elem = ET.Element('plaintext') | ||||||||||||||||||||||||
| elem.text = 'a\x00b' | ||||||||||||||||||||||||
| self.check(elem) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| class IterparseTest(unittest.TestCase): | ||||||||||||||||||||||||
|
serhiy-storchaka marked this conversation as resolved.
|
||||||||||||||||||||||||
| # Test iterparse interface. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should now move this entry to Doc/whatsnew/3.16.rst.