bpo-34623: Use XML_SetHashSalt in _elementtree (GH-9146) · python/cpython@f7666e8 (original) (raw)
File tree
4 files changed
lines changed
- Misc/NEWS.d/next/Security
4 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,7 +3,7 @@ | ||
3 | 3 | |
4 | 4 | /* note: you must import expat.h before importing this module! */ |
5 | 5 | |
6 | -#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.0" | |
6 | +#define PyExpat_CAPI_MAGIC "pyexpat.expat_CAPI 1.1" | |
7 | 7 | #define PyExpat_CAPSULE_NAME "pyexpat.expat_CAPI" |
8 | 8 | |
9 | 9 | struct PyExpat_CAPI |
@@ -48,6 +48,8 @@ struct PyExpat_CAPI | ||
48 | 48 | enum XML_Status (*SetEncoding)(XML_Parser parser, const XML_Char *encoding); |
49 | 49 | int (*DefaultUnknownEncodingHandler)( |
50 | 50 | 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); | |
51 | 53 | /* always add new stuff to the end! */ |
52 | 54 | }; |
53 | 55 |
Original file line number | Diff line number | Diff 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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3261,6 +3261,11 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *html, | ||
3261 | 3261 | PyErr_NoMemory(); |
3262 | 3262 | return -1; |
3263 | 3263 | } |
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 | + } | |
3264 | 3269 | |
3265 | 3270 | if (target) { |
3266 | 3271 | Py_INCREF(target); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1887,6 +1887,11 @@ MODULE_INITFUNC(void) | ||
1887 | 1887 | capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler; |
1888 | 1888 | capi.SetEncoding = XML_SetEncoding; |
1889 | 1889 | capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler; |
1890 | +#if XML_COMBINED_VERSION >= 20100 | |
1891 | +capi.SetHashSalt = XML_SetHashSalt; | |
1892 | +#else | |
1893 | +capi.SetHashSalt = NULL; | |
1894 | +#endif | |
1890 | 1895 | |
1891 | 1896 | /* export using capsule */ |
1892 | 1897 | capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL); |