Skip to content

--pytest-env-verbose prints multi-line values as extra header entries #322

Closed

Description

@gaborbernat

With --pytest-env-verbose, a value that holds a newline is split across header lines, and the continuation can read like a separate SET, SKIP or UNSET entry. The variables the tests receive are correct; only the header is wrong.

Affected code

Mechanism

_format_actions interpolates the name, the value and the source into {action} {key}={value} (from {source}) without escaping, and pytest writes each returned string to the terminal. A newline in the value ends the line early, and the rest of the value starts the next one. The README's own .env example, MESSAGE="hello\nworld", prints as two lines.

Reproduction

repro.sh

cd "$(mktemp -d)"
printf '[pytest]\nenv_files = .env\n' > pytest.ini
printf 'MESSAGE="hello\\nSET   DB_URL=postgres://other-db"\n' > .env
printf 'def test_pass() -> None:\n    pass\n' > test_pass.py
pytest --pytest-env-verbose | sed -n '/^pytest-env:/,/^rootdir/p'

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

pytest-env:
  SET   MESSAGE=hello
SET   DB_URL=postgres://other-db  (from /tmp/tmp.amdadN/.env)
rootdir: /tmp/tmp.amdadN

Expected: one header line per action, with control characters in the value escaped.

Impact

A reader of the header sees an assignment that pytest-env did not make, attributed to a real configuration file.

  • Reproducibility: easy. One command with the flag and a multi-line value.
  • Real-world exposure: niche. It needs --pytest-env-verbose and a value with a newline or another control character.

Credits

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

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