Repository navigation
Add MaskedArg to hide a secret argument of a list command - #181
Merged
Merged
Conversation
cscutcher
force-pushed
the
masked-arg
branch
2 times, most recently
from
November 11, 2026 12:37
86a9727 to
f5439da
Compare
Collaborator
Coverage Report for CI Build 43413784220Coverage increased (+0.9%) to 89.583%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
log_mask_re matches the joined, shell-quoted command, so the rule has to
model shlex quoting: --pass=(\S+) leaks the rest of a value with spaces,
--pass=(.+) also hides the next arguments and still leaks after a
newline.
MaskedArg is masked per argument, before the command is joined:
helper.execute(["tool", MaskedArg.option("pass", password), "-v"])
# executed: tool '--pass=two words' -v
# logged: tool '--pass=<*masked*>' -v
- MaskedArg(arg, arg_for_log) is a frozen dataclass, its repr hides arg.
- Constructors: secret(arg), option(name, value) and regex(arg, rule).
option() takes the bare name and follows the CLI conventions: a long
name gives GNU's --name=value, a single character POSIX's -nvalue.
An option and its value as separate arguments need no helper:
["--pass", MaskedArg.secret(value)].
- CommandT accepts MaskedArg items. A list command is joined twice: with
arg for execution and with arg_for_log for the log, ExecResult.cmd,
the exceptions and the context repr. log_mask_re still applies on top.
- open_execute_context, _exec_command and _log_command_execute take a
keyword-only command_for_log. The context prepares it with chroot and
sudo like the command, so their extra quoting does not matter.
- Covers Subprocess (with and without shell), async Subprocess,
SSHClient and execute_together.
cscutcher
force-pushed
the
masked-arg
branch
from
November 11, 2026 13:17
f5439da to
8156ee8
Compare
cscutcher
marked this pull request as ready for review
November 11, 2026 13:26
penguinolog
reviewed
Nov 11, 2026
| """ | ||
| if not _OPTION_NAME.fullmatch(name): | ||
| raise ValueError(f"Option name is expected without leading dashes, as alphanumerics and dashes: {name!r}") | ||
| flag = f"-{name}" if len(name) == 1 else f"--{name}=" |
Collaborator
There was a problem hiding this comment.
Option may be fully without dashes: dd if=... of=...
Contributor
Author
There was a problem hiding this comment.
Fair point. With the factory methods I was mostly going for the common cases, and anything more unusual can still build a MaskedArg directly.
Happy to add dd-style support though if you feel it's important. How about a new MaskedArg.key_value(key, value, sep="=") that does no dash detection at all, so MaskedArg.key_value("if", path) gives if=<*masked*>.
WDYT?
Collaborator
There was a problem hiding this comment.
Sounds good. Not super critical now, but nice to have on cases like kubectl calls with values override
option() always adds - or --, so an argument like dd's if=... could
only be built with the raw constructor.
- key_value(key, value, sep="=") writes the key exactly as given:
key_value("if", path) is if=<path>, shown as if=<*masked*>.
- An empty key is rejected.
- option() now builds its argument through key_value, adding the
dashes and choosing the separator itself.
penguinolog
approved these changes
Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What do these changes do?
Adds
MaskedArg, so a caller can mark one element of a list command as secret.The process gets the real value; the logs,
ExecResult.cmd, the exceptions and the context repr get the masked one.Why:
log_mask_reruns over the command aftershlex.join, so the rule has to knowshlex.quote's rules to be correct.1The obvious rules don't hold up once the password has a space or a newline in it:
--pass=(\S+)--pass=(.+)two secret wordstool '--pass=<*masked*> secret words' --verbosetool '--pass=<*masked*>(eats the rest of the command)line1\nline2tool '--pass=<*masked*>\nline2' --verbose.stops at the newlineWe hit this for real with a DB root password (the first fix used
\S+).A correct rule is possible, but it's not something I'd expect anyone to get right first time:
r"""--pass=((?<='--pass=)(?:[^']|'"'"')*|(?<!'--pass=)[^\s']*)"""MaskedArgsidesteps that by masking per argument, before the join.The type:
It reuses the existing
<*masked*>marker, so the length of the secret isn't leaked either.option()takes the bare name and follows the usual conventions: a long name gives GNU's--name=value,2 a single character gives POSIX's-nvalue.3An option and its value as separate arguments don't need a helper at all:
["--pass", MaskedArg.secret(v)].How it's wired:
CommandTbecomesstr | Iterable[str | MaskedArg].A list command is materialised once, then joined twice: with
argfor execution, witharg_for_logfor everything else.command_for_logonopen_execute_context,_exec_commandand_log_command_execute(defaults toNone, meaning "same ascommand").open_execute_contextruns the log form through_prepare_commandtoo.So chroot and
sudo_mode, which quote the command again insidesh -c, don't break the masking in the context repr.log_mask_restill applies on top of the log form, so existing rules keep working.Subprocess(with and without a shell),async_api.Subprocess,SSHClientandexecute_together.Are there changes in behavior for the user?
Nothing changes unless you pass a
MaskedArg.strcommands can't carry aMaskedArg; they keep usinglog_mask_re.'--pass=<*masked*>'), becauseshlexquotes<*>.I left that alone so the logged command stays valid shell, but happy to special-case it if you'd rather.
Related issue number
None found for per-argument masking (searched issues/PRs for "mask").
Follows on from #180, which fixed
log_mask_reitself.Checklist
test/test_masked_arg.py: spaces, quotes, newline, shell syntax, a following argument, generator commands, chroot/sudo repr, shell=False, async, SSH,execute_together)log_mask_re, newdoc/source/MaskedArg.rst,commandtype in the Sphinx pages)A couple of things I wasn't sure about, so very open to steering:
MaskedArgand the three constructors are my best guess; happy to rename... versionadded:: 8.3.0everywhere, guessing the next minor.command_for_logonopen_execute_contextis public API now. The alternative was letting it accept the un-joined list, which felt like a bigger change to its contract.Footnotes
https://docs.python.org/3/library/shlex.html#shlex.quote ↩
https://sourceware.org/glibc/manual/latest/html_node/Argument-Syntax.html: "To specify an argument for a long option, write
--name=value." ↩https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap12.html, 12.1 item 2(a): implementations "shall also permit applications to specify the option and option-argument in the same argument string". ↩