UltraJSON has an integer overflow handling large indent leads to buffer overflow or infinite loop
Description
UltraJSON is a fast JSON encoder and decoder written in pure C with bindings for Python 3.7+. Versions 5.10 through 5.11.0 are vulnerable to buffer overflow or infinite loop through large indent handling. ujson.dumps() crashes the Python interpreter (segmentation fault) when the product of the indent parameter and the nested depth of the input exceeds INT32_MAX. It can also get stuck in an infinite loop if the indent is a large negative number. Both are caused by an integer overflow/underflow whilst calculating how much memory to reserve for indentation. And both can be used to achieve denial of service. To be vulnerable, a service must call ujson.dump()/ujson.dumps()/ujson.encode() whilst giving untrusted users control over the indent parameter and not restrict that indentation to reasonably small non-negative values. A service may also be vulnerable to the infinite loop if it uses a fixed negative indent. An underflow always occurs for any negative indent when the input data is at least one level nested but, for small negative indents, the underflow is usually accidentally rectified by another overflow. This issue has been fixed in version 5.12.0.
AI Insight
LLM-synthesized narrative grounded in this CVE's description and references.
UltraJSON 5.10–5.11.0 vulnerable to denial of service via buffer overflow/infinite loop when processing large indent values; fixed in 5.12.0.
UltraJSON versions 5.10 through 5.11.0 contain an integer overflow and underflow vulnerability in the indent handling logic of the JSON encoder. When calculating the memory required for indentation, a large positive indent value multiplied by the nesting depth can exceed INT32_MAX, causing a buffer overflow that leads to a segmentation fault. Conversely, a negative indent value can cause an integer underflow, potentially resulting in an infinite loop [1][4].
To exploit this, an attacker must control the indent parameter passed to ujson.dump(), ujson.dumps(), or ujson.encode(), and the service must not restrict indentation to small non-negative numbers. The vulnerability can be triggered with deeply nested input structures, making it accessible to any user who can provide serialized data with controlled indentation [1][4].
The impact is denial of service: the Python interpreter either crashes due to a segmentation fault (large positive indent) or becomes stuck in an infinite loop (large negative indent). Both scenarios disrupt the availability of the affected service [1][4].
The issue has been fixed in UltraJSON 5.12.0. The fix, implemented in commit 486bd45, promotes the indent field from int to ptrdiff_t and adds bounds checks to prevent overflow and underflow [2]. Users are strongly advised to upgrade to version 5.12.0 or later. Additionally, the project is in maintenance-only mode, and users are encouraged to migrate to alternatives like orjson for better performance and security [3].
AI Insight generated on May 18, 2026. Synthesized from this CVE's description and the cited reference URLs; citations are validated against the source bundle.
Affected packages
Versions sourced from the GitHub Security Advisory.
| Package | Affected versions | Patched versions |
|---|---|---|
ujsonPyPI | >= 5.1.0, < 5.12.0 | 5.12.0 |
Affected products
2- ultrajson/ultrajsonv5Range: >= 5.1.0, < 5.12.0
Patches
1486bd4553dc4Fix buffer overflow/infinite loop from indent handling
4 files changed · +44 −11
src/ujson/lib/ultrajsonenc.c+9 −7 modified@@ -575,7 +575,7 @@ static void Buffer_AppendIndentNewlineUnchecked(JSONObjectEncoder *enc) static void Buffer_AppendIndentUnchecked(JSONObjectEncoder *enc, JSINT32 value) { - int i; + ptrdiff_t i; if (enc->indent > 0) while (value-- > 0) for (i = 0; i < enc->indent; i++) @@ -741,10 +741,11 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c Buffer_AppendCharUnchecked (enc, '['); + // The extra 1 byte covers the optional newline. + size_t per_item_reserve = (enc->indent > 0 ? enc->indent : 0) * (enc->level + 1) + enc->itemSeparatorLength + 1; while (enc->iterNext(obj, &tc)) { - // The extra 1 byte covers the optional newline. - Buffer_Reserve (enc, enc->indent * (enc->level + 1) + enc->itemSeparatorLength + 1); + Buffer_Reserve (enc, per_item_reserve); if (count > 0) { @@ -769,7 +770,7 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c enc->iterEnd(obj, &tc); - if (count > 0) { + if (count > 0 && enc->indent > 0) { // Reserve space for the indentation plus the newline. Buffer_Reserve (enc, enc->indent * enc->level + 1); Buffer_AppendIndentNewlineUnchecked (enc); @@ -786,10 +787,11 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c Buffer_AppendCharUnchecked (enc, '{'); + // The extra 1 byte covers the optional newline. + size_t reserve_size = (enc->indent > 0 ? enc->indent : 0) * (enc->level + 1) + enc->itemSeparatorLength + 1; while ((res = enc->iterNext(obj, &tc))) { - // The extra 1 byte covers the optional newline. - Buffer_Reserve (enc, enc->indent * (enc->level + 1) + enc->itemSeparatorLength + 1); + Buffer_Reserve (enc, reserve_size); if(res < 0) { @@ -823,7 +825,7 @@ static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t c enc->iterEnd(obj, &tc); - if (count > 0) { + if (count > 0 && enc->indent > 0) { Buffer_Reserve (enc, enc->indent * enc->level + 1); Buffer_AppendIndentNewlineUnchecked (enc); Buffer_AppendIndentUnchecked (enc, enc->level);
src/ujson/lib/ultrajson.h+2 −1 modified@@ -54,6 +54,7 @@ tree doesn't have cyclic references. #define __ULTRAJSON_H__ #include <stdio.h> +#include <stddef.h> // Max decimals to encode double floating point numbers with #ifndef JSON_DOUBLE_MAX_DECIMALS @@ -257,7 +258,7 @@ typedef struct __JSONObjectEncoder /* Configuration for spaces of indent */ - int indent; + ptrdiff_t indent; /* If true, NaN will be encoded as a string matching the Python standard library's JSON behavior.
src/ujson/python/objToJSON.c+15 −1 modified@@ -678,6 +678,7 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs) PyObject *separatorsKeyBytes = NULL; int allowNan = -1; int orejectBytes = -1; + int indent = 0; size_t retLen; JSONObjectEncoder encoder = @@ -714,7 +715,7 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs) PRINTMARK(); - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOOiiiOO", kwlist, &oinput, &oensureAscii, &oencodeHTMLChars, &oescapeForwardSlashes, &osortKeys, &encoder.indent, &allowNan, &orejectBytes, &odefaultFn, &oseparators)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OOOOiiiOO", kwlist, &oinput, &oensureAscii, &oencodeHTMLChars, &oescapeForwardSlashes, &osortKeys, &indent, &allowNan, &orejectBytes, &odefaultFn, &oseparators)) { return NULL; } @@ -761,6 +762,19 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs) encoder.rejectBytes = orejectBytes; } + if (indent < -1) + { + encoder.indent = -1; + } + else if (indent > 1000) + { + PyErr_SetString(PyExc_ValueError, "Maximum allowed indentation is 1000"); + return NULL; + } + else { + encoder.indent = indent; + } + if (oseparators != NULL && oseparators != Py_None) { if (!PyTuple_Check(oseparators))
tests/test_ujson.py+18 −2 modified@@ -1059,9 +1059,25 @@ def test_recursive_default(self): ujson.dumps(unjsonable_obj, default=self.default) -@pytest.mark.parametrize("indent", list(range(65537, 65542))) +@pytest.mark.parametrize("indent", [999, 1000, 1001, 1 << 30, 1 << 63, 1 << 128]) def test_dump_huge_indent(indent): - ujson.encode({"a": True}, indent=indent) + obj = {"list": [1, [2, 3], 4], "nested": {"key": "value", "a": True}} + if indent <= 1000: + assert ujson.loads(ujson.encode(obj, indent=indent)) == obj + else: + with pytest.raises((ValueError, OverflowError)): + ujson.encode(obj, indent=indent) + + +def test_negative_indent(): + obj = {"a": [1, 2], "b": "c"} + assert ujson.dumps(obj) == '{"a":[1,2],"b":"c"}' + assert ujson.dumps(obj, 0) == '{"a":[1,2],"b":"c"}' + assert ujson.dumps(obj, indent=-1) == '{"a": [1,2],"b": "c"}' + assert ujson.dumps(obj, indent=-1000000) == '{"a": [1,2],"b": "c"}' + assert ( + ujson.dumps(obj, indent=2) == '{\n "a": [\n 1,\n 2\n ],\n "b": "c"\n}' + ) @pytest.mark.parametrize("first_length", list(range(2, 7)))
Vulnerability mechanics
Generated on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
5- github.com/advisories/GHSA-c8rr-9gxc-jprvghsaADVISORY
- nvd.nist.gov/vuln/detail/CVE-2026-32875ghsaADVISORY
- github.com/ultrajson/ultrajson/commit/486bd4553dc471a1de11613bc7347a6b318e37eaghsax_refsource_MISCWEB
- github.com/ultrajson/ultrajson/issues/700ghsax_refsource_MISCWEB
- github.com/ultrajson/ultrajson/security/advisories/GHSA-c8rr-9gxc-jprvghsax_refsource_CONFIRMWEB
News mentions
0No linked articles in our index yet.