From 82a50fcc7140b917754af7465491f64a71344c2e Mon Sep 17 00:00:00 2001 From: QianEric Date: Thu, 26 Nov 2026 15:29:20 +0800 Subject: [PATCH] fix(parser.py): validate the shape of the [project] table in pyproject.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. --- dparse/parser.py | 24 +++++++++++++++-- tests/test_parse.py | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/dparse/parser.py b/dparse/parser.py index ac4c481..78d8f1b 100644 --- a/dparse/parser.py +++ b/dparse/parser.py @@ -494,12 +494,32 @@ def parse(self) -> None: if not cfg or "project" not in cfg: return + project = cfg["project"] + if not isinstance(project, dict): + raise MalformedDependencyFileError( + info=f"'project' must be a table, not " + f"{type(project).__name__}" + ) + + optional_dependencies = project.get("optional-dependencies", {}) + if not isinstance(optional_dependencies, dict): + raise MalformedDependencyFileError( + info="'project.optional-dependencies' must be a table, not " + f"{type(optional_dependencies).__name__}" + ) + sections = { - "dependencies": cfg["project"].get("dependencies", []), - **cfg["project"].get("optional-dependencies", {}), + "dependencies": project.get("dependencies", []), + **optional_dependencies, } for section, lines in sections.items(): + if not isinstance(lines, list) or not all( + isinstance(line, str) for line in lines + ): + raise MalformedDependencyFileError( + info=f"'{section}' must be a list of strings" + ) for line in lines: req = RequirementsTXTLineParser.parse(line) if req: diff --git a/tests/test_parse.py b/tests/test_parse.py index a82b573..2e0d294 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -584,3 +584,68 @@ def test_pyproject_toml_no_extra() -> None: assert dep_file.dependencies[0].specs == SpecifierSet("") assert dep_file.dependencies[0].dependency_type == "pyproject.toml" assert dep_file.dependencies[0].section == "dependencies" + + +def test_pyproject_toml_invalid_dependencies_type() -> None: + """A scalar `dependencies` value must not be iterated character by + character, which silently turned "requests" into 8 bogus packages. + """ + content = """ + [project] + name = "my_package" + dependencies = "requests" + """ + throw = None + + try: + parse(content, file_type=filetypes.pyproject_toml) + except Exception as e: + throw = e + + assert isinstance(throw, MalformedDependencyFileError) + + +def test_pyproject_toml_invalid_dependencies_entry_type() -> None: + content = """ + [project] + name = "my_package" + dependencies = [1, 2] + """ + throw = None + + try: + parse(content, file_type=filetypes.pyproject_toml) + except Exception as e: + throw = e + + assert isinstance(throw, MalformedDependencyFileError) + + +def test_pyproject_toml_project_is_not_a_table() -> None: + content = """ + project = [] + """ + throw = None + + try: + parse(content, file_type=filetypes.pyproject_toml) + except Exception as e: + throw = e + + assert isinstance(throw, MalformedDependencyFileError) + + +def test_pyproject_toml_optional_dependencies_is_not_a_table() -> None: + content = """ + [project] + name = "my_package" + optional-dependencies = ["ReportLab>=1.2"] + """ + throw = None + + try: + parse(content, file_type=filetypes.pyproject_toml) + except Exception as e: + throw = e + + assert isinstance(throw, MalformedDependencyFileError)