Skip to content

🐛 fix(toml): validate native entry tables - #338

Merged
gaborbernat merged 1 commit into
pytest-dev:mainfrom
gaborbernat:fix/toml-entry-validation
Nov 17, 2026
Merged

gaborbernat merged 1 commit into
pytest-dev:mainfrom
gaborbernat:fix/toml-entry-validation

Conversation

@gaborbernat

Copy link
Copy Markdown
Collaborator

The table branch of _parse_toml_config applied an entry in [pytest_env] or [tool.pytest_env] without validating it (old lines). A malformed entry produced an environment its author did not write, and the run passed: unset = "false" removed the variable, { vaule = "x" } replaced an existing variable with the empty string, and an unquoted DATABASE.HOST = "x" created an empty DATABASE. 🐛 After this PR such an entry stops pytest with a usage error that names it. Fixes #324.

Flags were read through bool()

unset, transform and skip_if_set went through bool(), so any non-empty string counted as true, "false" included. _parse_toml_table now requires each flag that is present to be a TOML boolean.

Unknown keys and a missing value were accepted

A table without value defaulted to the empty string, and keys other than the four documented ones were ignored, which is how a misspelled value became an assignment of "". TOML reads an unquoted dotted key as a nested table, so DATABASE.HOST = "x" arrived as an entry named DATABASE whose table holds only HOST. The parser now rejects any key outside value, transform, skip_if_set and unset, and a table that has neither value nor unset = true.

Behavior changes

A malformed table stops pytest before collection with exit code 4 and one of:

ERROR: pytest-env: entry 'API_TOKEN' has unknown key 'val'
ERROR: pytest-env: entry 'DATABASE_URL': unset must be true or false
ERROR: pytest-env: entry 'API_TOKEN' needs a value or unset = true

The messages hold the entry name and the key, not the value. A configuration that relied on a table without value to assign the empty string has to write value = "". A variable name with a dot has to be quoted, as in "DATABASE.HOST" = "x". Plain values, valid tables and INI env lines behave as before.

Reported by Anthropic's OSS Scanner: ANT-2026-VTKFPKVK.

An entry table was applied whatever it held. Flags went through bool(),
so `unset = "false"` removed the variable; a missing or misspelled
`value` assigned the empty string; and an unquoted dotted name, which
TOML reads as a nested table, created an empty variable named after its
first part. The run passed each time.

Such a table now stops pytest with a usage error that names the entry.

Reported by Anthropic's OSS Scanner: ANT-2026-VTKFPKVK.

Fixes pytest-dev#324
@gaborbernat gaborbernat added the bug Something isn't working label Nov 17, 2026
@gaborbernat
gaborbernat merged commit b6d5020 into pytest-dev:main Nov 17, 2026
20 checks passed
@gaborbernat
gaborbernat deleted the fix/toml-entry-validation branch November 17, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native TOML entry tables accept unknown keys and non-boolean flags

1 participant