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 <christian@python.org>

https://bugs.python.org/issue34623
(cherry picked from commit cb5778f00ce48631c7140f33ba242496aaf7102b)

Co-authored-by: Christian Heimes <christian@python.org>
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 1dfdb3c..4b86f96 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -3305,6 +3305,11 @@
         PyErr_NoMemory();
         return -1;
     }
+    /* expat < 2.1.0 has no XML_SetHashSalt() */
+    if (EXPAT(SetHashSalt) != NULL) {
+        EXPAT(SetHashSalt)(self->parser,
+                           (unsigned long)_Py_HashSecret.expat.hashsalt);
+    }
 
     if (target) {
         Py_INCREF(target);
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index c8a01d4..c52079e 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -1877,6 +1877,11 @@
     capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
     capi.SetEncoding = XML_SetEncoding;
     capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
+#if XML_COMBINED_VERSION >= 20100
+    capi.SetHashSalt = XML_SetHashSalt;
+#else
+    capi.SetHashSalt = NULL;
+#endif
 
     /* export using capsule */
     capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);