Skip to content

🐛 fix(ini): reject env lines with unknown flags - #334

Merged
gaborbernat merged 2 commits into
pytest-dev:mainfrom
gaborbernat:fix/ini-flag-validation
Nov 17, 2026
Merged

gaborbernat merged 2 commits into
pytest-dev:mainfrom
gaborbernat:fix/ini-flag-validation

Conversation

@gaborbernat

Copy link
Copy Markdown
Collaborator

The INI branch of _load_values split the text before the first = on :, took the last piece as the variable name and looked among the others only for D, R and U (old lines). Any other piece was dropped, so a mistyped line was applied as a different instruction and pytest exited 0: DR:API_TOKEN=local overwrote an existing API_TOKEN, and DB_HOST: test-db created an empty variable named test-db. 🐛 After this PR such a line stops pytest with a usage error. Fixes #323.

Unknown flags were dropped

The flags were consulted through three membership tests, so DR matched none of them and the entry ran as if it had no flag: expanded, and not skipped when the variable existed. A line without = went through the same split, which made DB_HOST a flag and test-db the name. The parser now rejects any flag outside D, R and U.

A line without a name crashed later

=value and D: produced an empty name, and the assignment to os.environ then failed with a bare OSError: [Errno 22] Invalid argument that named no line. The parser now rejects an empty name before applying anything.

Behavior changes

A line with a flag other than D, R or U, in any letter case, or with no variable name, stops pytest with exit code 4 and one of:

ERROR: pytest-env: env entry 1: unknown flag 'DR', expected D, R or U
ERROR: pytest-env: env entry 2: missing variable name

The message gives the entry's position among the env entries and the flag. It does not print the line, because the value may be a secret and pytest-env prints values only with --pytest-env-verbose. A configuration that relied on a dropped piece, such as APP:MODE=x setting MODE, now fails and has to be rewritten as MODE=x. Lines in the documented (FLAG:)NAME=VALUE form and the native [pytest_env] tables behave as before.

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

The INI parser kept only the D, R and U pieces before the name and dropped
the rest, so a typo changed what a line did without any sign of it:
`DR:API_TOKEN=x` overwrote an existing value and `DB_HOST: test-db`
created an empty variable named `test-db`. A line without a name crashed
later with a bare OSError.

The usage error gives the entry's position and the flag, never the line,
because the value may be a secret.

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

Fixes pytest-dev#323
@gaborbernat gaborbernat added the bug Something isn't working label Nov 17, 2026
@gaborbernat
gaborbernat merged commit 10d6e61 into pytest-dev:main Nov 17, 2026
19 checks passed
@gaborbernat
gaborbernat deleted the fix/ini-flag-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.

INI env lines with unknown flags or no = are applied instead of rejected

1 participant