Skip to content

Commit f7666e8

Browse files
bpo-34623: Use XML_SetHashSalt in _elementtree (GH-9146)
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]>
1 parent 1a107ee commit f7666e8

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
@@ -48,6 +48,8 @@ struct PyExpat_CAPI
4848
enum XML_Status (*SetEncoding)(XML_Parser parser, const XML_Char *encoding);
4949
int (*DefaultUnknownEncodingHandler)(
5050
void *encodingHandlerData, const XML_Char *name, XML_Encoding *info);
51+
/* might be none for expat < 2.1.0 */
52+
int (*SetHashSalt)(XML_Parser parser, unsigned long hash_salt);
5153
/* always add new stuff to the end! */
5254
};
5355

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
@@ -3261,6 +3261,11 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *html,
32613261
PyErr_NoMemory();
32623262
return -1;
32633263
}
3264+
/* expat < 2.1.0 has no XML_SetHashSalt() */
3265+
if (EXPAT(SetHashSalt) != NULL) {
3266+
EXPAT(SetHashSalt)(self->parser,
3267+
(unsigned long)_Py_HashSecret.expat.hashsalt);
3268+
}
32643269

32653270
if (target) {
32663271
Py_INCREF(target);

Modules/pyexpat.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,11 @@ MODULE_INITFUNC(void)
18871887
capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
18881888
capi.SetEncoding = XML_SetEncoding;
18891889
capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
1890+
#if XML_COMBINED_VERSION >= 20100
1891+
capi.SetHashSalt = XML_SetHashSalt;
1892+
#else
1893+
capi.SetHashSalt = NULL;
1894+
#endif
18901895

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

0 commit comments

Comments
 (0)