Skip to content

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

Closed

Description

@gaborbernat

A mistyped line in the INI env option is applied as a different instruction, and pytest exits 0 without a warning. DR:API_TOKEN=local-default overwrites an existing API_TOKEN, because DR is not read as D plus R. DB_HOST: test-db leaves DB_HOST alone and creates an empty variable named test-db.

Affected code

Mechanism

The parser splits the text before the first = on :, takes the last piece as the name and every earlier piece as a flag. It looks the flags up only by asking whether R, D or U is among them, so a piece that is none of the three is dropped and the entry is applied as if it carried no flag. A line without = goes through the same split: for DB_HOST: test-db, DB_HOST becomes a flag, test-db the name, and the value is empty.

Reproduction

repro.sh

cd "$(mktemp -d)"
printf '[pytest]\nenv =\n    DB_HOST: test-db\n    DR:API_TOKEN=local-default\n' > pytest.ini
cat > test_env.py <<'PY'
import os


def test_env() -> None:
    print({name: os.environ.get(name) for name in ("DB_HOST", "test-db", "API_TOKEN")})
PY
DB_HOST=prod-db API_TOKEN=ci-token pytest -q -s

Run with sh repro.sh at 3ebb6b4872fd0e450b6670d529e375e11c218e61, with pytest 9.2.0, python-dotenv 1.2.4 and CPython 3.14 on Linux:

{'DB_HOST': 'prod-db', 'test-db': '', 'API_TOKEN': 'local-default'}
.
1 passed in 0.00s

Expected: pytest stops with a usage error that names the malformed line.

Impact

A typo in a flag turns a default into an overwrite, or sets a variable other than the one the line was written for, and the run gives no sign of it. The native [pytest_env] tables use a separate parser and are not affected.

  • Reproducibility: easy. One command with default options.
  • Real-world exposure: niche. It needs a line outside the documented (FLAG:)NAME=VALUE form.

Credits

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

Activity

  1. added
    bug
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions