Skip to content

Commit 18b20ba

Browse files
tiranmiss-islington
authored andcommitted
[2.7] bpo-34623: Use XML_SetHashSalt in _elementtree (GH-9146) (GH-9394)
The C accelerated _elementtree module now initializes hash randomization salt from _Py_HashSecret instead of libexpat's default CPRNG. Signed-off-by: Christian Heimes <[email protected]> https://bugs.python.org/issue34623. (cherry picked from commit cb5778f) Co-authored-by: Christian Heimes <[email protected]> https://bugs.python.org/issue34623
1 parent 5f883fc commit 18b20ba

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

Include/pyexpat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/* note: you must import expat.h before importing this module! */
55

6-
#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.0"
6+
#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.1"
77
#define PyExpat_CAPSULE_NAME "pyexpat.expat_CAPI"
88

99
struct PyExpat_CAPI
@@ -43,6 +43,8 @@ struct PyExpat_CAPI
4343
XML_Parser parser, XML_UnknownEncodingHandler handler,
4444
void *encodingHandlerData);
4545
void (*SetUserData)(XML_Parser parser, void *userData);
46+
/* might be none for expat < 2.1.0 */
47+
int (*SetHashSalt)(XML_Parser parser, unsigned long hash_salt);
4648
/* always add new stuff to the end! */
4749
};
4850

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The C accelerated _elementtree module now initializes hash randomization
2+
salt from _Py_HashSecret instead of libexpat's default CSPRNG.

Modules/_elementtree.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,6 +2574,11 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw)
25742574
PyErr_NoMemory();
25752575
return NULL;
25762576
}
2577+
/* expat < 2.1.0 has no XML_SetHashSalt() */
2578+
if (EXPAT(SetHashSalt) != NULL) {
2579+
EXPAT(SetHashSalt)(self->parser,
2580+
(unsigned long)_Py_HashSecret.prefix);
2581+
}
25772582

25782583
ALLOC(sizeof(XMLParserObject), "create expatparser");
25792584

Modules/pyexpat.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,11 @@ MODULE_INITFUNC(void)
20422042
capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
20432043
capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
20442044
capi.SetUserData = XML_SetUserData;
2045+
#if XML_COMBINED_VERSION >= 20100
2046+
capi.SetHashSalt = XML_SetHashSalt;
2047+
#else
2048+
capi.SetHashSalt = NULL;
2049+
#endif
20452050

20462051
/* export using capsule */
20472052
capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);

0 commit comments

Comments
 (0)