Skip to content

🐛 fix(verbose): escape non-printable header text - #331

Merged
gaborbernat merged 1 commit into
pytest-dev:mainfrom
gaborbernat:fix/verbose-escape
Nov 17, 2026
Merged

gaborbernat merged 1 commit into
pytest-dev:mainfrom
gaborbernat:fix/verbose-escape

Conversation

@gaborbernat

Copy link
Copy Markdown
Collaborator

_format_actions built each line of the --pytest-env-verbose header by interpolating the variable name, its value and the source path as they were (old lines). A value holding a newline was split across header lines, and the continuation could read like a separate SET, SKIP or UNSET entry attributed to a real configuration file. The variables the tests received were correct; only the header was wrong. 🐛 After this PR each action stays on one line. Fixes #322.

Values reached the terminal unescaped

pytest writes every string a pytest_report_header hook returns to the terminal on its own line, so a newline inside one of them ends the line early. The README's own .env example, MESSAGE="hello\nworld", printed SET MESSAGE=hello followed by world (from …/.env). _format_actions now passes the name, the value and the source through _escape, which replaces every character that str.isprintable rejects with its unicode_escape form and leaves printable characters, non-ASCII letters included, as they are.

Behavior changes

In the header a newline shows as \n, a carriage return as \r and an escape character as \x1b. Output for values made of printable characters is unchanged, and so are the variables pytest-env sets. Anything that parses the header and expected a raw control character now sees the escaped form.

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

A value holding a newline was split across lines of the
--pytest-env-verbose header, and the continuation could read like a
separate SET entry attributed to a real configuration file. The README's
own MESSAGE="hello\nworld" example printed as two lines.

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

Fixes pytest-dev#322
@gaborbernat gaborbernat added the bug Something isn't working label Nov 17, 2026
@gaborbernat
gaborbernat merged commit 799ed2d into pytest-dev:main Nov 17, 2026
20 checks passed
@gaborbernat
gaborbernat deleted the fix/verbose-escape 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.

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

1 participant