Skip to content

The agent that grows with you

fix(agent): redact Context7 API keys by their vendor prefix

Sahil Vishnalyaa480a27e76dc
diff --git a/agent/redact.py b/agent/redact.py
index 7529bf76bc1..245e14e295e 100644
--- a/agent/redact.py
+++ b/agent/redact.py
@@ -153,6 +153,11 @@ _PREFIX_PATTERNS = [
# ``***``. Kept free of nested unbounded repeats so the pattern passes the
# same structural gate plugins must.
r"sk-[A-Za-z0-9_-](?:\.?[A-Za-z0-9_-]){9,}",
+ # Context7 API keys: a fixed vendor prefix (``ctx7sk-``) plus an opaque body.
+ # The generic ``sk-`` family cannot catch it: the matcher's lookbehind
+ # rejects a prefix preceded by a word char, and here the ``sk-`` sits inside
+ # ``ctx7sk-`` - so a bare Context7 key passed through unmasked (#142576).
+ r"ctx7sk-[A-Za-z0-9_-]{10,}",
r"ghp_[A-Za-z0-9]{10,}", # GitHub PAT (classic)
r"github_pat_[A-Za-z0-9_]{10,}", # GitHub PAT (fine-grained)
r"gho_[A-Za-z0-9]{10,}", # GitHub OAuth access token
diff --git a/tests/agent/test_redact.py b/tests/agent/test_redact.py
index 97b010a12e7..6742a00cc9d 100644
--- a/tests/agent/test_redact.py
+++ b/tests/agent/test_redact.py
@@ -104,6 +104,36 @@ class TestKnownPrefixes:
]:
assert redact_sensitive_text(benign) == benign
+ def test_context7_api_key_prefix(self):
+ """Context7 keys carry a fixed vendor prefix (``ctx7sk-``) and must redact on every surface.
+
+ The generic ``sk-`` family cannot catch them: the matcher's lookbehind
+ rejects a prefix preceded by a word char, and here the ``sk-`` sits
+ INSIDE ``ctx7sk-`` - so a bare Context7 key passed through unmasked
+ (#142576).
+ """
+ # NOTE: prefix + suffix CONCATENATION so no contiguous token literal
+ # exists in this file - GitHub push protection blocks realistic
+ # vendor-token-shaped literals.
+ token = ("ctx7sk-" + "0" * 8 + "-" + "0" * 4 + "-" + "0" * 4
+ + "-" + "0" * 4 + "-" + "0" * 12)
+ for surface in (
+ f"key is {token}\n",
+ f'["{token}"]\n',
+ f"headers:\n CONTEXT7_API_KEY: {token}\nurl: https://mcp.context7.com/mcp\n",
+ f"CONTEXT7_API_KEY={token}\n",
+ ):
+ assert token not in redact_sensitive_text(surface, force=True), surface
+
+ def test_context7_prefix_requires_word_boundary_and_length(self):
+ """Prose and embedded identifiers must not false-positive."""
+ for benign in [
+ "the glossary explains context7 keys",
+ "ctx7sk-short",
+ "myctx7sk-" + "A" * 20,
+ ]:
+ assert redact_sensitive_text(benign) == benign
+
def test_agentmail_prefix_needs_a_key_shaped_hex_suffix(self):
"""``am_`` is a common identifier prefix; only the documented hex key body is a secret (#10983)."""
for benign in ["schema.am_example_identifier_123", "path/to/am_monthly_report.sql"]: