Skip to content

feat(operations.files.template): render HiddenValue unmasked in templates - #2019

Open
Renerick wants to merge 5 commits into
pyinfra-dev:3.xfrom
Renerick:unmask-hidden-values-in-templates
Open

Renerick wants to merge 5 commits into
pyinfra-dev:3.xfrom
Renerick:unmask-hidden-values-in-templates

Conversation

@Renerick

@Renerick Renerick commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

HiddenValue is already allowed to be passed as-is to environment variables (#1853). I think it's sensible to be able to pass it to templates as well

Current workaround is to call .unmask() in the template body, which leaks knowledge of secrets and pyinfra api. Existing templates with .unmask() calls will not be broken by this change

  • Pull request is based on the default branch (3.x at this time)
  • Pull request includes tests for any new/updated operations/facts
  • Pull request includes documentation for any new/updated operations/facts
  • Tests pass (see scripts/dev-test.sh)
  • Type checking & code style passes (see scripts/dev-lint.sh)
  • Pull request title follows the
    conventional commits format

AI disclosure

Codex has been used for code review and test generation, implementation changes were done by hand

@wowi42 wowi42 added new feature operations Issues with operations. API API mode specific issues. labels Oct 14, 2026

@wowi42 wowi42 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the formal review below.

@wowi42 wowi42 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this. Being able to use HiddenValue naturally in templates is useful, and the rendered value reaches the upload correctly.

I spotted two things that I think we should fix before merging.

First, with DIFF enabled, the unmasked content reaches files.put, which logs the desired lines through generate_color_diff. That can print plaintext secrets with pyinfra --diff. Could we suppress or redact those diffs when hidden inputs are present, and add coverage showing that the secret is uploaded but absent from the logs?

Second, this truthiness check can skip a valid callable finalizer whose __bool__ returns False:

if user_finalize:
    v = user_finalize(v)

Using if user_finalize is not None: would preserve the previous callable API behavior.

Thanks again for contributing this.

@Renerick

Copy link
Copy Markdown
Contributor Author

To support logs content coverage, I opened a PR in pyinfra-testing repo

@wowi42 wowi42 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The template change can expose hidden values in diff logs. That needs fixing before this can merge.

Issues

  • Secrets leak through diff logs. files.template turns the rendered output into a plain StringIO and passes it to files.put (src/pyinfra/operations/files.py:1491-1504). With DIFF enabled, put logs the desired lines on both create and change (src/pyinfra/operations/files.py:1192-1193 and src/pyinfra/operations/files.py:1241-1242). A template containing {{ secret }} therefore prints the raw secret. Suppress or redact these diffs when hidden values are present, and add regression coverage that checks both the uploaded content and the logs.
  • Falsey custom finalizers are skipped. src/pyinfra/api/util.py:176 uses if user_finalize:. A callable whose __bool__ returns False was valid before this change but is no longer called. Use if user_finalize is not None: and add a regression test for this case.

Done at PyCon FR.

@Renerick

Renerick commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

I would like to highlight that current implementation will also expose the secret, since the user is forced to use .unwrap() anyway. Please see the linked PR in the test library repo so that I can submit fixes for diffs with the test coverage

For the falsey finalize handling, I have pushed the fix

@Renerick

Copy link
Copy Markdown
Contributor Author

@wowi42 Hello. I just realized that I opened related PR to the wrong repo (my own fork). Sorry about it. I reopened it here pyinfra-dev/pyinfra-testing#5, could you take a look before I fix log assertions in this PR?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API API mode specific issues. new feature operations Issues with operations.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants