Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/test/test_tomllib/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ def test_inline_table_recursion_limit(self):
recursive_table_toml = nest_count * "key = {" + nest_count * "}"
tomllib.loads(recursive_table_toml)

def test_key_recursion_limit(self):
nest_count = tomllib._parser.MAX_KEY_PARTS - 2
nested_key_toml = "a." * nest_count + "a = 1"
tomllib.loads(nested_key_toml)

nest_count = tomllib._parser.MAX_KEY_PARTS + 2
nested_key_toml = "a." * nest_count + "a = 1"
with self.assertRaisesRegex(
RecursionError,
r"TOML key has more than the allowed [0-9]+ parts",
):
tomllib.loads(nested_key_toml)

def test_types_import(self):
"""Test that `_types` module runs.

Expand Down
11 changes: 11 additions & 0 deletions Lib/tomllib/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@

from ._types import Key, ParseFloat, Pos

# Pathologically excessive number of parts in a key runs into quadratic
# behavior (e.g. in Flags.is_).
# Even if keys aren't currently parsed using recursion, they name a
# recursive structure, so it makes sense to limit it using getrecursionlimit()
# and RecursionError.
MAX_KEY_PARTS: Final = sys.getrecursionlimit()

ASCII_CTRL: Final = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))

# Neither of these sets include quotation mark or backslash. They are
Expand Down Expand Up @@ -470,6 +477,10 @@ def parse_key(src: str, pos: Pos) -> tuple[Pos, Key]:
pos = skip_chars(src, pos, TOML_WS)
pos, key_part = parse_key_part(src, pos)
key += (key_part,)
if len(key) > MAX_KEY_PARTS:
raise RecursionError(
f"TOML key has more than the allowed {MAX_KEY_PARTS} parts"
)
pos = skip_chars(src, pos, TOML_WS)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In :mod:`tomllib`, the number of parts in TOML keys is now limited.
Loading