Skip to content

🔒 fix(expand): keep values out of template errors - #335

Merged
gaborbernat merged 2 commits into
pytest-dev:mainfrom
gaborbernat:fix/template-error-hides-value
Nov 17, 2026
Merged

gaborbernat merged 2 commits into
pytest-dev:mainfrom
gaborbernat:fix/template-error-hides-value

Conversation

@gaborbernat

Copy link
Copy Markdown
Collaborator

_apply_entries expanded {VAR} templates with an unguarded entry.value.format(**os.environ) (old line). A template that nests one field inside another's format spec, such as {A:-{B}}, made that call raise a ValueError whose message quotes the value of B, and the traceback printed it in a run without --pytest-env-verbose. Anyone able to read the run's output, such as a CI log, could read that value. 🔒 After this PR the run stops with a usage error that names the entry and carries no value. This addresses draft advisory ANT-2026-5FWSIIT0.

The format error quoted an environment value

A format spec may hold nested replacement fields, and Python substitutes them before it interprets the spec. The expanded text is rarely a valid spec for a str, and CPython's error quotes it in full: Invalid format specifier '<expanded spec>' for object of type 'str'. The hook runs inside a try that catches only ConftestImportFailure, and pytest's main handles UsageError but not ValueError, so the interpreter printed the traceback with the message. INI entries expand unless they carry R:, and TOML entries expand with transform = true; both went through the same call.

The expansion now lives in _expand_value, which catches ValueError and raises pytest.UsageError with from None, so the original message is not chained into the output. The new message is built from the entry's name alone. It leaves out the template as well, because a template can hold a literal secret.

Behavior changes

A template that str.format rejects with ValueError stops pytest before collection with exit code 4 and one line:

ERROR: pytest-env: invalid {VAR} template in the value of 'API_KEY'

The run used to end with exit code 1 and a traceback. A template that references an unset variable still ends in a KeyError traceback; that message names the variable and holds no value, and this PR leaves it alone. Templates that expanded before expand to the same text.

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

A {VAR} template with a field nested in a format spec, such as
{A:-{B}}, made str.format raise a ValueError whose message quotes the
expanded spec, which is the value of B. Nothing caught it, so the
traceback printed a variable value in a run without
--pytest-env-verbose.

The expansion now reports a usage error that names the entry and
carries neither the template nor a value.

Reported by Anthropic's OSS Scanner: ANT-2026-5FWSIIT0.
@gaborbernat gaborbernat added bug Something isn't working security Security fix labels Nov 17, 2026
…hides-value

# Conflicts:
#	README.md
#	tests/test_env.py
@gaborbernat
gaborbernat merged commit 453f923 into pytest-dev:main Nov 17, 2026
19 checks passed
@gaborbernat
gaborbernat deleted the fix/template-error-hides-value 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 security Security fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant