Skip to content

Fix minor issues with command masking - #180

Merged
penguinolog merged 2 commits into
python-useful-helpers:masterfrom
cscutcher:fix-mask-unmatched-groups
Nov 10, 2026
Merged

penguinolog merged 2 commits into
python-useful-helpers:masterfrom
cscutcher:fix-mask-unmatched-groups

Conversation

@cscutcher

Copy link
Copy Markdown
Contributor

What do these changes do?

Two small fixes to log_mask_re, which I bumped into while looking at masking a password argument.

1. Groups that don't take part in a match garble the output.
_mask_command replaces every group of a match, including ones that didn't participate.
For those, re.Match.span() returns (-1, -1),1 so a rule with | or an optional group splices a spurious mask in before the last character:

>>> mask_command("cmd --pass=abc --x", r"--pass=(abc)|--user=(\w+)")
'cmd --pass=<*masked*> --<*masked*>x'   # before
'cmd --pass=<*masked*> --x'             # after

Non-participating groups are now just skipped.

2. Two places reported the command without applying log_mask_re at all.

  • SSHClient.execute_together raised ParallelCallProcessError / ParallelCallExceptionsError with the raw command, which also ends up in the exception message.
    It now applies the per-call rule plus the rules of every remote.
    The exception is shared by all the remotes, so masking with all of them felt safest.
  • Subprocess logged the raw command at CRITICAL when the FIFO buffers were still open after the process exited.
    It now logs result.cmd, which is already masked.

Are there changes in behavior for the user?

Only in what gets logged / put in exceptions:

  • Rules using alternation or optional groups now mask correctly instead of mangling the text.
  • execute_together exceptions and the "FIFO buffers are still open" log line now show the masked command, like everything else already does.

No API changes.

Related issue number

None that I could find (searched issues/PRs for "mask").

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
    • new test/test_mask_command.py (alternation, optional group, several rules)
    • test_execute_together_failed_masked in test/test_ssh_client_execute.py
    • The FIFO log line isn't covered, tbh. That path is awkward to trigger with the current fakes and the change is a one-liner, but happy to add one if you'd prefer.
  • Documentation reflects the changes (docstring of _mask_command; nothing user-facing needed updating)

Footnotes

  1. https://docs.python.org/3/library/re.html#re.Match.span ↩

mask_command replaced every group of a match, including groups that did
not participate in it. Their span is (-1, -1), so a rule with
alternation or an optional group inserted a spurious mask before the
last character of the text:

    mask_command("cmd --pass=abc --x", r"--pass=(abc)|--user=(\w+)")
    # 'cmd --pass=<*masked*> --<*masked*>x'

- Groups that did not participate are skipped.
- Add tests for mask_command.
Two places reported the command without applying log_mask_re:

- execute_together raised ParallelCallProcessError and
  ParallelCallExceptionsError with the raw command, which is also in
  their message. The rules of every remote and of the call now apply.
- Subprocess logged the raw command when the FIFO buffers stayed open
  after the process exited. It now logs the masked command of the
  result.
@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 42835859078

Coverage increased (+0.02%) to 88.727%

Details

  • Coverage increased (+0.02%) from the base build.
  • Patch coverage: 4 of 4 lines across 2 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 1947
Covered Lines: 1775
Line Coverage: 91.17%
Relevant Branches: 448
Covered Branches: 350
Branch Coverage: 78.13%
Branches in Coverage %: Yes
Coverage Strength: 6.96 hits per line

💛 - Coveralls

@penguinolog
penguinolog merged commit 665e3f8 into python-useful-helpers:master Nov 10, 2026
17 checks passed
@cscutcher
cscutcher deleted the fix-mask-unmatched-groups branch November 11, 2026 12:31
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