Skip to content

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

Closed

Description

@gaborbernat

pytest-env applies a table-valued entry in [pytest_env] or [tool.pytest_env] without validating it. unset = "false" removes the variable, a misspelled value key replaces an existing variable with the empty string, and an unquoted dotted name such as DATABASE.HOST creates an empty DATABASE. The run passes and reports nothing.

Affected code

Mechanism

The table branch reads unset, transform and skip_if_set through bool(), so any non-empty string, "false" included, counts as true. It defaults a missing value to the empty string and ignores every other key, which is how { vaule = "test-token" } becomes an assignment of "". TOML reads an unquoted dotted key as a nested table, so DATABASE.HOST = "db.internal" arrives as an entry named DATABASE whose table has no value.

Reproduction

repro.sh

cd "$(mktemp -d)"
cat > pyproject.toml <<'TOML'
[tool.pytest_env]
DATABASE_URL = { value = "postgres://localhost/dev", unset = "false" }
DATABASE.HOST = "db.internal"
API_TOKEN = { vaule = "test-token" }
TOML
cat > test_env.py <<'PY'
import os


def test_env() -> None:
    print({name: os.environ.get(name) for name in ("DATABASE_URL", "DATABASE", "DATABASE.HOST", "API_TOKEN")})
PY
DATABASE_URL=postgres://ci-db/prod 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:

{'DATABASE_URL': None, 'DATABASE': '', 'DATABASE.HOST': None, 'API_TOKEN': ''}
.
1 passed in 0.00s

Expected: pytest stops with a usage error that names the entry and the key it rejects.

Impact

The author of a malformed entry gets an environment they did not write, with a passing run.

  • Reproducibility: easy. One command with default options.
  • Real-world exposure: niche. It needs an entry outside the four documented keys and their types.

Credits

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

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