Repository navigation
fix(parser.py): validate the shape of the [project] table in pyproject.toml - #86
Draft
qianeric-backup wants to merge 1 commit into
Draft
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
…t.toml PyprojectTomlParser.parse assumed that `project` is a table and that dependencies / optional-dependencies.* are lists of strings. A scalar `dependencies` was iterated character by character, so `dependencies = "requests"` reported the eight bogus packages ['r','e','q','u','e','s','t','s'] with is_valid=True; a non-table `project` and non-list sections raised a raw AttributeError / TypeError. Validate the shape and report it through the same MalformedDependencyFileError the TOML syntax error path already uses.
This branch has not been deployed
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.
Pull Request type
Please check the type of change your PR introduces:
What is the current behavior?
Issue Number: N/A
PyprojectTomlParser.parseassumes that the value of the top-levelprojectkey is a table and that
project.dependencies/project.optional-dependencies.*are lists of strings. Nothing validates thatassumption, so a
pyproject.tomlthat is valid TOML but invalid per thepyproject specification is handled in three different wrong ways:
Silently wrong result. A scalar
dependenciesvalue is iteratedcharacter by character, so the file reports bogus packages:
is_validisTrue, so callers feed those eight fake names into theirvulnerability lookups.
Raw
AttributeError.project = [],project = 42andproject = "x"all raiseAttributeError: '<type>' object has no attribute 'get'fromparser.py:498.Raw
TypeError.dependencies = 42,dependencies = [1, 2]andoptional-dependencies = ["ReportLab>=1.2"]raiseTypeError: ... is not iterable/TypeError: 'list' object is not a mapping.Both 2 and 3 surface as an internal error even though the input is a user
mistake, and they are inconsistent with the two handlers that already exist
three lines above in the same method: a TOML syntax error is reported as a
single-line
MalformedDependencyFileError(parser.py:489-492), and a filewithout a
[project]table returns an emptyFilewithis_valid == False(parser.py:494-495).What is the new behavior?
PyprojectTomlParser.parsenow checks the shape of the[project]tablebefore using it, and reports a wrong shape through the same
MalformedDependencyFileErrorthat the TOML syntax error path alreadyuses:
No more silently fabricated packages: the
dependencies = "requests"caseno longer yields
['r', 'e', 'q', 'u', 'e', 's', 't', 's'].Well-formed files are untouched. A differential run over nine valid
pyproject.tomlvariants (the fixture fromtest_pyproject_toml, nodependencies, empty list, empty file, comment-only, no
[project]table,PEP 508 with extras and environment markers, empty
optional-dependenciestable,optional-dependencies = {dev = []})produces byte-identical results before and after.
Four regression tests added in the style of the existing
test_pipfile_lock_with_invalid_json/test_poetry_lock_version_lower_than_1_5tests. All four fail on
mainand pass with the patch.Does this introduce a breaking change?
Malformed files now raise
MalformedDependencyFileErrorinstead ofAttributeError/TypeError, which is the documented exception type forthis parser (see
dparse/errors.py). No valid input changes behavior.Other information
Test suite, before → after:
51 passed, 6 deselected→55 passed, 6 deselected(
pytest tests -q), and56 passed, 1 deselected→60 passed, 1 deselectedwith
-m conda. TheFAILEDsets of the two trees are identical (bothempty).
ruff check dparseandflake8 dparsereport the same findings before andafter the patch (34 and 11 respectively, all pre-existing).