From 2ad4d4e059dc983e7621bedf625d91e79dd660a0 Mon Sep 17 00:00:00 2001 From: Maciej Lisiewski Date: Sat, 14 Nov 2026 17:12:34 +0100 Subject: [PATCH] =?UTF-8?q?fix(redact):=20mask=20spaced=20`API=20Key=20=3D?= =?UTF-8?q?=20=E2=80=A6`=20keys;=20check=20bare=20keys=20without=20a=20ful?= =?UTF-8?q?lmatch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 8d5b2c6's possessive key runs excluded the space that _SECRET_CFG_NAMES allows in `api key`, so `API Key = …` stopped being masked in the spaced form. Let the run cross exactly that one space, and keep a dotted key from restarting at a `key` a joined `api ` precedes (that rescan was quadratic). The bare-key fullmatch backtracked per keyword once a value followed (`"password"*n + '. = "x"'`); check its one allowed separator directly. Timing bound raised to the 2 s floor tests/AGENTS.md sets. Refs #130827 Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com> --- agent/redact_spaced_cfg.py | 21 +++++++++------------ tests/agent/test_redact_spaced_cfg.py | 9 +++++++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/agent/redact_spaced_cfg.py b/agent/redact_spaced_cfg.py index 0eac406ec6..f38bace2b1 100644 --- a/agent/redact_spaced_cfg.py +++ b/agent/redact_spaced_cfg.py @@ -27,12 +27,13 @@ def _patterns() -> tuple["re.Pattern[str]", ...]: # Keys are one possessive run checked by _key_names_secret: the key must reach the separator, so # the run is the only candidate, and `[..]*NAME[..]*` backtracking is superlinear on long runs. value = rf"{_SPACED_SEP}(?:{_QUOTED}|{_UNQUOTED})" + sp = r"(?<=[Aa][Pp][Ii]) (?=[Kk][Ee][Yy])" # _SECRET_CFG_NAMES' ``api key``: the only space a key holds dotted = re.compile( - rf"(?(?P[A-Za-z0-9_\-]*+\.[A-Za-z0-9_.\-]*+)){value}", + rf"(?(?P(?:[A-Za-z0-9_\-]|{sp})*+\.(?:[A-Za-z0-9_.\-]|{sp})*+)){value}", re.MULTILINE, ) anchored = re.compile( - rf"(?P^[ \t]*{_LINE_NUMBER_GUTTER}(?:export[ \t]+)?(?P[A-Za-z0-9_.\-]++)){value}", + rf"(?P^[ \t]*{_LINE_NUMBER_GUTTER}(?:export[ \t]+)?(?P(?:[A-Za-z0-9_.\-]|{sp})++)){value}", re.IGNORECASE | re.MULTILINE, ) return dotted, anchored @@ -44,21 +45,17 @@ def _key_names_secret(name: str, dotted: bool) -> bool: a non-empty first segment, or before a later dot that is not the last character.""" from agent.redact import _CFG_SECRET_WORD_RE - if not dotted: - return bool(_bare_key_re().fullmatch(name)) + if not dotted: # only ``api.key`` / ``api key`` puts a non-word character in a bare key + seps = [i for i, c in enumerate(name) if c in ". "] + if seps and (len(seps) > 1 or name[max(seps[0] - 3, 0):seps[0]].lower() != "api" + or name[seps[0] + 1:seps[0] + 4].lower() != "key"): + return False # checked directly: a fullmatch backtracks per keyword on ``password…password.`` + return bool(_CFG_SECRET_WORD_RE.search(name)) first_dot, last_dot = name.find("."), name.rfind(".", 0, len(name) - 1) return bool((first_dot > 0 and _CFG_SECRET_WORD_RE.search(name, first_dot + 1)) or (last_dot > 0 and _CFG_SECRET_WORD_RE.search(name, 0, last_dot))) -@functools.cache -def _bare_key_re() -> "re.Pattern[str]": - # One fixed start (fullmatch), so the keyword backtrack is linear in the key, not per offset. - from agent.redact import _SECRET_CFG_NAMES - - return re.compile(rf"[A-Za-z0-9_\-]*{_SECRET_CFG_NAMES}[A-Za-z0-9_\-]*", re.IGNORECASE) - - def _is_code_expression(key: str, value: str) -> bool: """``getpass.getpass(...)``, ``password``, ``self.x``, ``None``, ``secret;``, ``0``: code, not config. An unquoted value is masked only when it looks opaque; an opaque-length identifier diff --git a/tests/agent/test_redact_spaced_cfg.py b/tests/agent/test_redact_spaced_cfg.py index f24f3970f0..f6a74a54e6 100644 --- a/tests/agent/test_redact_spaced_cfg.py +++ b/tests/agent/test_redact_spaced_cfg.py @@ -17,6 +17,9 @@ def _ensure_redaction_enabled(monkeypatch): ('[server]\ndb.password = "S3cretVal" # prod\n', "S3cretVal"), # TOML dotted + comment (' 5|password = "abc123def456"\r\n', "abc123def456"), # read_file gutter, CRLF ("secret = a1b2c3d4e5f6a7b8c9d0e1f2\n", "a1b2c3d4e5f6a7b8c9d0e1f2"), # unquoted opaque + ("[openai]\nAPI Key = 9f8e7d6c5b4a39281706f5e4d3c2b1a0\n", "9f8e7d6c5b4a39281706f5e4d3c2b1a0"), # spaced keyword + ("service.API Key = a1b2c3d4e5f6a7b8c9d0e1f2\n", "a1b2c3d4e5f6a7b8c9d0e1f2"), # spaced keyword, dotted + ('api db.password = "hunter2"\n', "hunter2"), # a dotted key may still start after a plain ``api `` ]) def test_spaced_config_secret_masked(text, secret): assert secret not in redact_for_egress(text) @@ -92,7 +95,9 @@ def test_spaced_equality_comparison_unchanged(): "password" * 1500 + "." * 1500 + " = ", # `[..]*NAME[..]*` keys backtracked per start offset "a." + "password" * 6000 + " = ", " 5|" + "password" * 6000 + " = ", -], ids=["dotted-run", "dotted-prefix", "anchored-gutter"]) + "api key." * 10000 + " = ", # a key starting after every joined ``api `` rescanned the run + "password" * 12000 + '. = "hunter2"', # a valid value reaches the bare-key keyword check +], ids=["dotted-run", "dotted-prefix", "anchored-gutter", "dotted-api-key-joins", "bare-key-check"]) def test_spaced_pass_linear_on_long_keyword_runs(text): import time @@ -101,4 +106,4 @@ def test_spaced_pass_linear_on_long_keyword_runs(text): start = time.perf_counter() redact_spaced_cfg_assignments(text, _mask_token) - assert time.perf_counter() - start < 1.0 + assert time.perf_counter() - start < 2.0