fix(redact): mask spaced API Key = … keys; check bare keys without a fullmatch
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>
This commit is contained in:
@@ -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"(?<![A-Za-z0-9_.\-])(?P<key>(?P<name>[A-Za-z0-9_\-]*+\.[A-Za-z0-9_.\-]*+)){value}",
|
||||
rf"(?<![A-Za-z0-9_.\-])(?!(?<=[Aa][Pp][Ii] )[Kk][Ee][Yy])(?P<key>(?P<name>(?:[A-Za-z0-9_\-]|{sp})*+\.(?:[A-Za-z0-9_.\-]|{sp})*+)){value}",
|
||||
re.MULTILINE,
|
||||
)
|
||||
anchored = re.compile(
|
||||
rf"(?P<key>^[ \t]*{_LINE_NUMBER_GUTTER}(?:export[ \t]+)?(?P<name>[A-Za-z0-9_.\-]++)){value}",
|
||||
rf"(?P<key>^[ \t]*{_LINE_NUMBER_GUTTER}(?:export[ \t]+)?(?P<name>(?:[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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user