Skip to content

Limit number of parts of a key - #286

Merged
hukkin merged 3 commits into
hukkin:masterfrom
encukou:limit-key-parts
Mar 25, 2026
Merged

hukkin merged 3 commits into
hukkin:masterfrom
encukou:limit-key-parts

Conversation

@encukou

@encukou encukou commented Mar 25, 2026

Copy link
Copy Markdown
Collaborator

tomli has quadratic time complexity when handling parts of a key (via Flags.is_ calls in key_value_rule's loop, and maybe elsewhere). This isn't a problem unless a document has an excessive number (10_000-ish) of parts in a key.
Limiting input size seems like the best fix here.

sys.getrecursionlimit and RecursionError is already (and in the non-mypyc version, implicitly) used for MAX_INLINE_NESTING. Keys happen to not be parsed by recursive functions,
but that's an implementation detail; RecursionError works nicely for names of recursive
structures.

tomli has quadratic time complexity when handling parts of a key
(via Flags.is_ calls in key_value_rule's loop, and maybe elsewhere).
This isn't a problem unless a document has an excessive number (10_000-ish)
of parts in a key. Limiting input seems like the best fix here.

`sys.getrecursionlimit` and `RecursionError` is already (and in the non-mypyc
version, implicitly) used for `MAX_INLINE_NESTING`.
Keys aren't parsed recursively, but that's an implementation detail;
`RecursionError` works nicely for names recursive structures.
@codecov

codecov Bot commented Mar 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (c20c491) to head (490e6bb).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #286   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines          532       535    +3     
  Branches        99       100    +1     
=========================================
+ Hits           532       535    +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread tests/test_misc.py Resolved
@hukkin
hukkin merged commit e1fdb94 into hukkin:master Mar 25, 2026
40 checks passed
hukkin pushed a commit that referenced this pull request Mar 25, 2026
* Limit number of parts of a key

tomli has quadratic time complexity when handling parts of a key
(via Flags.is_ calls in key_value_rule's loop, and maybe elsewhere).
This isn't a problem unless a document has an excessive number (10_000-ish)
of parts in a key. Limiting input seems like the best fix here.

`sys.getrecursionlimit` and `RecursionError` is already (and in the non-mypyc
version, implicitly) used for `MAX_INLINE_NESTING`.
Keys aren't parsed recursively, but that's an implementation detail;
`RecursionError` works nicely for names recursive structures.

(cherry picked from commit e1fdb94)
@hukkin

hukkin commented Mar 25, 2026

Copy link
Copy Markdown
Owner

Thank you! This is now released in versions 2.3.1 and 2.4.1.

@encukou
encukou deleted the limit-key-parts branch March 26, 2026 10:06
arp242 added a commit to toml-lang/toml that referenced this pull request May 24, 2026
Recently there have been a few TOML implementations that have added a
nesting limit depth to prevent using up huge amount of CPU/memory with a
document like "a.«10,000 more» = 1". For example:

hukkin/tomli#286
python-poetry/tomlkit#468
BurntSushi/toml@2e90ced6

There's probably more. In a casual quick look, many implementations
already have such a limit and if they don't, they probably should.

I added a comment in the toml-test README for it, but I think mentioning
it in the spec would also be useful. If only to set a recommended lower
limit like we do for 64bit ints. This is helpful both for implementation
authors (as a reminder they should add such a limit) and users (as a
reminder there may be a limit).

For example BurntSushi/toml has never had such a limit until recently,
even though I've always known this sort of thing is a problem. I ...
just never thought of it. This would have been fixed years ago if there
had been a comment in the spec. That's probably the same for tomli.
arp242 added a commit to toml-lang/toml that referenced this pull request May 24, 2026
Recently there have been a few TOML implementations that have added a
nesting limit depth to prevent using up huge amount of CPU/memory with a
document like "a.«10,000 more» = 1". For example:

hukkin/tomli#286
python-poetry/tomlkit#468
BurntSushi/toml@2e90ced6

There's probably more. In a casual quick look, many implementations
already have such a limit and if they don't, they probably should.

I added a comment in the toml-test README for it, but I think mentioning
it in the spec would also be useful. If only to set a recommended lower
limit like we do for 64bit ints. This is helpful both for implementation
authors (as a reminder they should add such a limit) and users (as a
reminder there may be a limit).

For example BurntSushi/toml has never had such a limit until recently,
even though I've always known this sort of thing is a problem. I ...
just never thought of it. This would have been fixed years ago if there
had been a comment in the spec. That's probably the same for tomli.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants