Skip to content

Commit 03794ce

Browse files
Use XML_SetHashSalt16Bytes from libExpat when possible
1 parent 5d41632 commit 03794ce

4 files changed

Lines changed: 22 additions & 3 deletions

File tree

Include/pyexpat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ struct PyExpat_CAPI
6262
XML_Parser parser, unsigned long long activationThresholdBytes);
6363
XML_Bool (*SetBillionLaughsAttackProtectionMaximumAmplification)(
6464
XML_Parser parser, float maxAmplificationFactor);
65+
/* might be NULL for expat < 2.8.0 */
66+
XML_Bool (*SetHashSalt16Bytes)(
67+
XML_Parser parser, const uint8_t entropy[16]);
6568
/* always add new stuff to the end! */
6669
};
6770

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improved protection against XML hash-flooding attacks in
2+
:mod:`xml.parsers.expat` and :mod:`xml.etree.ElementTree` when Python is
3+
compiled with libExpat 2.8.0 or later.

Modules/_elementtree.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3735,8 +3735,12 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
37353735
PyErr_NoMemory();
37363736
return -1;
37373737
}
3738-
/* expat < 2.1.0 has no XML_SetHashSalt() */
3739-
if (EXPAT(st, SetHashSalt) != NULL) {
3738+
// Prefer 16-byte entropy, only expat >= 2.8.0. See gh-149018
3739+
if (EXPAT(st, SetHashSalt16Bytes) != NULL) {
3740+
EXPAT(st, SetHashSalt16Bytes)(self->parser,
3741+
(const uint8_t *)_Py_HashSecret.uc);
3742+
}
3743+
else if (EXPAT(st, SetHashSalt) != NULL) {
37403744
EXPAT(st, SetHashSalt)(self->parser,
37413745
(unsigned long)_Py_HashSecret.expat.hashsalt);
37423746
}

Modules/pyexpat.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,11 @@ newxmlparseobject(pyexpat_state *state, const char *encoding,
15331533
Py_DECREF(self);
15341534
return NULL;
15351535
}
1536-
#if XML_COMBINED_VERSION >= 20100
1536+
#if XML_COMBINED_VERSION >= 20800
1537+
/* This feature was added upstream in libexpat 2.8.0. */
1538+
XML_SetHashSalt16Bytes(self->itself,
1539+
(const uint8_t *)_Py_HashSecret.uc);
1540+
#elif XML_COMBINED_VERSION >= 20100
15371541
/* This feature was added upstream in libexpat 2.1.0. */
15381542
XML_SetHashSalt(self->itself,
15391543
(unsigned long)_Py_HashSecret.expat.hashsalt);
@@ -2427,6 +2431,11 @@ pyexpat_exec(PyObject *mod)
24272431
#else
24282432
capi->SetHashSalt = NULL;
24292433
#endif
2434+
#if XML_COMBINED_VERSION >= 20800
2435+
capi->SetHashSalt16Bytes = XML_SetHashSalt16Bytes;
2436+
#else
2437+
capi->SetHashSalt16Bytes = NULL;
2438+
#endif
24302439
#if XML_COMBINED_VERSION >= 20600
24312440
capi->SetReparseDeferralEnabled = XML_SetReparseDeferralEnabled;
24322441
#else

0 commit comments

Comments
 (0)