Skip to content

The agent that grows with you

fix: redact '/'- and '~'-led secrets whose segments cannot be a path

teknium160f436b5f6fb
diff --git a/agent/redact.py b/agent/redact.py
index d839e6854bd..6ef67ef4ae7 100644
--- a/agent/redact.py
+++ b/agent/redact.py
@@ -323,6 +323,11 @@ _PASSWORD_KEY_RE = re.compile(r"passwd|password|pass|pw", re.IGNORECASE)
# agent-ssh-socket)``): the value token stops at whitespace, so only ``$(gpgconf`` is seen.
_SHELL_VAR_REF = r"\$(?:\{[A-Za-z_]\w*[^}]*\}|[A-Za-z_]\w*)"
_PATH_OR_VAR_VALUE_RE = re.compile(rf"^(?:{_SHELL_VAR_REF}|\$\(|~|/)(?:[\w./:-]|{_SHELL_VAR_REF})*$")
+# ``$VAR`` / ``$(cmd`` are unambiguous references. A ``/``- or ``~``-led value is a path only
+# while every segment reads like one: a 16+ char segment mixing case and digits with no ``.``
+# (``/wJalrXUtnFEMIK7MDENG/bPxRf…``) is a secret that happens to start with a path character,
+# whereas ``/home/u/.docker`` / ``~/.ssh/id_rsa`` / ``S.gpg-agent.ssh`` never clear that bar.
+_OPAQUE_PATH_SEGMENT_RE = re.compile(r"(?=[^.]*[a-z])(?=[^.]*[A-Z])(?=[^.]*[0-9])[^.]{16,}")
def _is_word_start(s: str, i: int) -> bool:
@@ -391,12 +396,11 @@ def _should_redact_assignment(key: str, value: str, *, check_keyword: bool) -> b
# A shell rc's ``SSH_AUTH_SOCK=$HOME/.ssh/agent.sock`` is configuration the agent must keep
# readable; only password-class keys mask a path/variable reference.
if _PATH_OR_VAR_VALUE_RE.match(value) and not _has_word_bounded_keyword(key, _PASSWORD_KEY_RE):
- # ``$VAR`` and ``~/`` are unambiguous references. A bare ``/...`` is not:
- # ``/home/u/.docker`` and ``/8f3kd9sKd0als...`` have the same shape, so a
- # single-segment absolute path still has to clear the opaque-credential bar
- # before it is treated as configuration.
- if not (value.startswith("/") and "/" not in value[1:]
- and _looks_like_opaque_credential(value)):
+ # ``$VAR`` is an unambiguous reference. A bare ``/...`` or ``~...`` is not:
+ # ``/home/u/.docker`` and ``/8f3kd9sKd0als...`` have the same shape, so every
+ # segment has to look like a path (see _OPAQUE_PATH_SEGMENT_RE) before the
+ # value is treated as configuration.
+ if value[0] == "$" or not any(_OPAQUE_PATH_SEGMENT_RE.fullmatch(seg) for seg in value.split("/")):
return False
return (_has_word_bounded_keyword(key, _STRONG_KEY_KEYWORD_RE)
or _looks_like_opaque_credential(value))
diff --git a/tests/agent/test_redact.py b/tests/agent/test_redact.py
index ef81801368c..a5f10ca87dc 100644
--- a/tests/agent/test_redact.py
+++ b/tests/agent/test_redact.py
@@ -151,6 +151,11 @@ class TestEnvAssignments:
"$argon2id$v=19$m=65536,t=3,p=4$c29tZXNhbHQ$aGFzaHZhbHVl"),
("SESSION_SECRET=/8f3kd9sKd0alsKDJ2mfkeisl3kdMc9dksla",
"8f3kd9sKd0alsKDJ2mfkeisl3kdMc9dksla"),
+ # A second '/' (~1 in 3 of the '/'-led AWS secrets) must not turn it into a "path".
+ ("AWS_SECRET_ACCESS_KEY=/wJalrXUtnFEMIK7MDENG/bPxRfiCYEXAMPLEKEY",
+ "wJalrXUtnFEMIK7MDENG/bPxRfiCYEXAMPLEKEY"),
+ ("API_SECRET=~wJalrXUtnFEMIK7MDENGbPxRfiCYEXAMPLEKEY",
+ "wJalrXUtnFEMIK7MDENGbPxRfiCYEXAMPLEKEY"),
],
)
def test_secret_that_only_starts_like_a_path_or_var_still_redacts(
@@ -170,6 +175,7 @@ class TestEnvAssignments:
"SSH_AUTH_SOCK=/run/user/$UID/keyring/ssh",
"export SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)",
"DOCKER_AUTH_CONFIG=/home/u/.docker",
+ "DOCKER_AUTH_CONFIG=/home/u/.docker/MyProject2024Build.d/config.json",
"MY_KEY_PATH=~/.ssh/id_rsa",
"SECRET_DIR=/etc",
],