Skip to content

Commit 4125d70

Browse files
committed
bpo-34623: Use XML_SetHashSalt in _elementtree
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]>
1 parent 7484bdf commit 4125d70

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
@@ -3272,6 +3272,11 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
32723272
PyErr_NoMemory();
32733273
return -1;
32743274
}
3275+
/* expat < 2.1.0 has no XML_SetHashSalt() */
3276+
if (EXPAT(SetHashSalt) != NULL) {
3277+
EXPAT(SetHashSalt)(self->parser,
3278+
(unsigned long)_Py_HashSecret.expat.hashsalt);
3279+
}
32753280

32763281
if (target) {
32773282
Py_INCREF(target);

Modules/pyexpat.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,11 @@ MODULE_INITFUNC(void)
18771877
capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
18781878
capi.SetEncoding = XML_SetEncoding;
18791879
capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
1880+
#if XML_COMBINED_VERSION >= 20100
1881+
capi.SetHashSalt = XML_SetHashSalt;
1882+
#else
1883+
capi.SetHashSalt = NULL;
1884+
#endif
18801885

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

0 commit comments

Comments
 (0)