-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
gh-146169: correctly handle re-entrant parsing calls in Expat handlers #146566
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
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -276,6 +276,52 @@ def test_parse_again(self): | |||||||||||||||||||||
| self.assertEqual(expat.ErrorString(cm.exception.code), | ||||||||||||||||||||||
| expat.errors.XML_ERROR_FINISHED) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @support.subTests("encoding", ("utf-8", "utf-16")) | ||||||||||||||||||||||
| def test_parse_reentrancy_with_encoding(self, encoding): | ||||||||||||||||||||||
| # See https://github.com/python/cpython/issues/146169. | ||||||||||||||||||||||
| parser = expat.ParserCreate(encoding=encoding) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| CharacterDataHandler = lambda data: parser.Parse(data, False) | ||||||||||||||||||||||
| CharacterDataHandler = mock.Mock(wraps=CharacterDataHandler) | ||||||||||||||||||||||
| def StartElementHandler(name, attrs): | ||||||||||||||||||||||
| parser.CharacterDataHandler = CharacterDataHandler | ||||||||||||||||||||||
| parser.StartElementHandler = StartElementHandler | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| payload = "<a>x".encode(encoding) | ||||||||||||||||||||||
| msg = re.escape("cannot call Parse() from within a handler") | ||||||||||||||||||||||
| with self.assertRaisesRegex(RuntimeError, msg): | ||||||||||||||||||||||
| for i in range(len(payload)): | ||||||||||||||||||||||
| parser.Parse(payload[i:i+1], i == len(payload) - 1) | ||||||||||||||||||||||
| CharacterDataHandler.assert_called_once_with("x") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @support.subTests("encoding", ("utf-8", "utf-16")) | ||||||||||||||||||||||
| def test_parse_reentrancy_allowed_for_external_parser(self, encoding): | ||||||||||||||||||||||
| parser = expat.ParserCreate(encoding=encoding) | ||||||||||||||||||||||
| subparser = parser.ExternalEntityParserCreate(None, encoding) | ||||||||||||||||||||||
| payload_extstr = '<!ENTITY ext SYSTEM "entity.file">' | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def ExternalEntityRefHandler(*args): | ||||||||||||||||||||||
| subparser.Parse(payload_extstr, True) | ||||||||||||||||||||||
| return 1 # return an integer to indicate that parsing continues | ||||||||||||||||||||||
| ExternalEntityRefHandler = mock.Mock(wraps=ExternalEntityRefHandler) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def StartElementHandler(*args): | ||||||||||||||||||||||
| parser.ExternalEntityRefHandler = ExternalEntityRefHandler | ||||||||||||||||||||||
| parser.StartElementHandler = StartElementHandler | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| payload = f"""\ | ||||||||||||||||||||||
| <?xml version="1.0" standalone="no"?> | ||||||||||||||||||||||
| <!DOCTYPE quotations SYSTEM "quotations.dtd" [{payload_extstr}]> | ||||||||||||||||||||||
| <root>&ext;</root> | ||||||||||||||||||||||
| """.encode(encoding) | ||||||||||||||||||||||
|
Comment on lines
+312
to
+316
Contributor
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. Just an idea: Use of
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Check that external parsers be called from parent's handlers. | ||||||||||||||||||||||
| for i in range(len(payload)): | ||||||||||||||||||||||
| parser.Parse(payload[i:i+1], i == len(payload) - 1) | ||||||||||||||||||||||
| external_ref_args = ('ext', None, 'entity.file', None) | ||||||||||||||||||||||
| ExternalEntityRefHandler.assert_called_once_with(*external_ref_args) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class NamespaceSeparatorTest(unittest.TestCase): | ||||||||||||||||||||||
| def test_legal(self): | ||||||||||||||||||||||
| # Tests that make sure we get errors when the namespace_separator value | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| :mod:`xml.parsers.expat`: raise :exc:`RuntimeError` when an Expat handler | ||
| calls :meth:`parser.Parse <xml.parsers.expat.xmlparser.Parse>` on the parser | ||
| that called the handler. Patch by Bénédikt Tran. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -863,6 +863,12 @@ pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyTypeObject *cls, | |
| int rc; | ||
| pyexpat_state *state = PyType_GetModuleState(cls); | ||
|
|
||
| if (self->in_callback) { | ||
| PyErr_SetString(PyExc_RuntimeError, | ||
| "cannot call Parse() from within a handler"); | ||
| return NULL; | ||
| } | ||
|
|
||
|
Comment on lines
+866
to
+871
Contributor
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. Does PS: Also — other than
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. Oh maybe, I will check. As for XML_SetEncoding, I don't know. I'll have a look at it tomorrow! |
||
| if (PyUnicode_Check(data)) { | ||
| view.buf = NULL; | ||
| s = PyUnicode_AsUTF8AndSize(data, &slen); | ||
|
|
||
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.
This comment seems to nee fixing: