Repository navigation
Limit number of parts of a key - #286
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
hukkin
reviewed
Mar 25, 2026
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)
Owner
|
Thank you! This is now released in versions 2.3.1 and 2.4.1. |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.getrecursionlimitandRecursionErroris already (and in the non-mypyc version, implicitly) used forMAX_INLINE_NESTING. Keys happen to not be parsed by recursive functions,but that's an implementation detail;
RecursionErrorworks nicely for names of recursivestructures.