Skip to content

Mask the whole secret when another secret is its prefix - #76142

Merged
potiuk merged 1 commit into
apache:mainfrom
arxhr007:fix-secrets-masker-overlapping-secrets
Nov 1, 2026
Merged

potiuk merged 1 commit into
apache:mainfrom
arxhr007:fix-secrets-masker-overlapping-secrets

Conversation

@arxhr007

Copy link
Copy Markdown
Contributor

SecretsMasker joins every registered secret into a single regex alternation. Python's alternation takes the first alternative that matches, not the longest, and patterns is a set, so its order depends on the process hash seed. When one registered secret is a prefix of another, the longer secret is sometimes only partially masked and its tail is written to the logs or rendered output.

Reproduction on main (two secrets, password1 is a prefix of password1234567890):

m = SecretsMasker()
m.add_mask("password1")
m.add_mask("password1234567890")
m.redact("token=password1234567890;")
# PYTHONHASHSEED=0 -> 'token=***;'
# PYTHONHASHSEED=3 -> 'token=***234567890;'   <- tail of the secret leaks

This can happen with any pair of secrets sharing a prefix, for example a Variable and a Connection password where one extends the other, or a value and a secret_mask_adapter adaptation of it.

The fix compiles the alternation longest-first, so the longest registered secret always wins. If A is a prefix of B then re.escape(A) is a prefix of re.escape(B), so sorting the escaped patterns by length is enough.

The regression test registers every prefix of one secret, which makes the result independent of set iteration order (it fails on main for every hash seed I tried, 1-3).

Tests run
  • shared/secrets_masker/tests: 173 passed, 1 skipped, 1 xfailed under PYTHONHASHSEED 0-4 and on the rebased branch
  • task-sdk/tests -k "mask or redact": 46 passed. One test, test_rendered_templates_masks_secrets_in_complex_objects, fails identically on main because the kubernetes package is not installed in my environment
  • prek run --stage pre-commit on the changed files, including mypy-shared-secrets_masker: passed
Performance

Secret masking is on the hot path of every log line, so I measured both paths before and after (4 alternating runs each, best of 7 repeats, 200 random secrets of 8-32 chars, 200-char line containing one secret; control is sum(range(200))):

main this PR
redact(line) (per log line) 93.5-94.7 us 90.5-92.7 us
registering 200 secrets with add_mask 1.37-1.59 ms 2.69-2.77 ms
control 1.52-1.57 us 1.51-1.59 us

redact is unchanged within noise. add_mask is slower, about 7 us per registered secret at 200 secrets, because it now sorts the patterns each time it recompiles the regex. That runs once per newly masked secret, not per log line, and recompiling the whole regex per secret was already the dominant cost.


Was generative AI tooling used to co-author this PR?
  • Yes — Devin CLI (Claude Sonnet 5.5)

Generated-by: Devin CLI (Claude Sonnet 5.5) following the guidelines


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.

The masker joins every registered secret into one regex alternation, which
takes the first alternative that matches rather than the longest. When one
secret is a prefix of another, the order of the set-backed patterns decided
whether the longer secret was fully masked or had its tail left in the
output, so the leak varied from process to process with the hash seed.

Compile the alternation longest-first so the longest registered secret
always wins.
@boring-cyborg

boring-cyborg Bot commented Oct 31, 2026

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example Dag that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: [email protected]
    Slack: https://s.apache.org/airflow-slack

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks — a real leak with a clean fix: longest-first alternation is exactly what's needed, the every-prefix test makes the regression independent of set order, and the before/after numbers show redact on the per-line path is unchanged.


This review was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.

More on how Apache Airflow handles maintainer review:
Contributing guide.

@potiuk
potiuk merged commit 8400915 into apache:main Nov 1, 2026
71 checks passed
@boring-cyborg

boring-cyborg Bot commented Nov 1, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants