Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(telemetry): redact secrets inside JSON payloads (#4043)
LHMQ878051c8c53573a
diff --git a/.changeset/quiet-json-secret-redaction.md b/.changeset/quiet-json-secret-redaction.mdnew file mode 100644index 000000000..b1464135f--- /dev/null+++ b/.changeset/quiet-json-secret-redaction.md@@ -0,0 +1,5 @@+---+'@composio/core': patch+---++Redact secrets that appear inside JSON payloads in telemetry error text. The key/value rule required the separator to follow the key name directly, so a serialized body such as `{"api_key": "..."}` — the shape error messages usually carry — was sent unredacted.diff --git a/python/composio/utils/redaction.py b/python/composio/utils/redaction.pyindex c101d601c..f0762580b 100644--- a/python/composio/utils/redaction.py+++ b/python/composio/utils/redaction.py@@ -7,8 +7,13 @@ import re_REDACTED = "[REDACTED]"_URL_QUERY = re.compile(r"(\bhttps?://[^\s?#'\"]+)\?[^\s'\"]*", re.IGNORECASE)_AUTHORIZATION = re.compile(r"\b(bearer|basic)\s+[A-Za-z0-9._~+/=-]+", re.IGNORECASE)+# The separator group allows a quote directly after the key name so JSON and+# dict reprs are covered: in `{"api_key": "secret"}` the key's own closing quote+# sits between the name and the colon._SECRET_PAIR = re.compile(- r"\b(authorization|api[-_]?key|apikey|x-api-key|access[-_]?token|refresh[-_]?token|client[-_]?secret|secret|password|passwd|pwd)\b(\s*[:=]+\s*)([\"']?)([^\s\"',}&]+)\3",+ # Leading lookbehind (not \\b) so env-style names like COMPOSIO_API_KEY still+ # match: underscore is a word char, so \\b can't fire between COMPOSIO_ and API.+ r"(?<![A-Za-z0-9])(authorization|api[-_]?key|apikey|x-api-key|access[-_]?token|refresh[-_]?token|client[-_]?secret|secret|password|passwd|pwd)\b([\"']?\s*[:=]+\s*)([\"']?)([^\s\"',}&]+)\3",re.IGNORECASE,)diff --git a/python/tests/test_redaction.py b/python/tests/test_redaction.pyindex ddce4a7ba..04cbfce9a 100644--- a/python/tests/test_redaction.py+++ b/python/tests/test_redaction.py@@ -1,5 +1,7 @@"""Tests for telemetry secret redaction."""+import pytest+from composio.utils.redaction import redact_sensitive_text@@ -13,3 +15,82 @@ def test_redacts_url_queries_authorization_and_secret_pairs() -> None:assert "api_key=abc" not in redactedassert "password: hunter2" not in redactedassert redacted.count("[REDACTED]") == 4+++ ("text", "secret"),+ [+ ('{"api_key": "sk-live-abc123"}', "sk-live-abc123"),+ ('{"api_key":"sk-live-abc123"}', "sk-live-abc123"),+ ('{"api_key" : "sk-live-abc123"}', "sk-live-abc123"),+ ('{"refresh_token":"rt-abc.def-123"}', "rt-abc.def-123"),+ ('{"x-api-key":"sk-hdr","user":"bob"}', "sk-hdr"),+ ("{'client_secret': 'cs-live-abc123'}", "cs-live-abc123"),+ ('{"password": "hunter2"}', "hunter2"),+ ],+)+def test_redacts_secrets_in_json_and_dict_reprs(text: str, secret: str) -> None:+ """Serialized payloads are the common shape in exception text.++ The key's own closing quote sits between the name and the colon, so a+ pattern anchored on ``name`` followed directly by ``:`` never matches.+ """+ redacted = redact_sensitive_text(text)++ assert secret not in redacted+ assert "[REDACTED]" in redacted+++def test_redacts_secret_in_serialized_error_payload() -> None:+ """The shape that reaches the telemetry error field via a tool failure."""+ text = (+ "Error executing tool: request body was rejected: "+ '{"toolkit": "GMAIL", "arguments": {"api_key": "sk-live-CUSTOMER", "to": "[email protected]"}}'+ )++ redacted = redact_sensitive_text(text)++ assert "sk-live-CUSTOMER" not in redacted+ assert "GMAIL" in redacted # non-secret context is preserved+++def test_json_keys_are_preserved() -> None:+ """Only the value is replaced; the key and quoting survive intact."""+ assert (+ redact_sensitive_text('{"api_key": "sk-live-abc123"}')+ == '{"api_key": "[REDACTED]"}'+ )+++ "text",+ [+ "the password field is required",+ "TypeError: cannot read property secret of undefined",+ 'no separator here "api_key" and nothing else',+ ],+)+def test_leaves_benign_text_untouched(text: str) -> None:+ """A key name with no value attached must not trigger redaction."""+ assert redact_sensitive_text(text) == text+++ ("text", "secret"),+ [+ ("COMPOSIO_API_KEY=sk_live_9f3c", "sk_live_9f3c"),+ ("OPENAI_API_KEY=sk_live_9f3c", "sk_live_9f3c"),+ ('export COMPOSIO_API_KEY="sk_live_9f3c"', "sk_live_9f3c"),+ ],+)+def test_redacts_env_style_prefixed_api_keys(text: str, secret: str) -> None:+ """Underscore before api_key is a word char, so a leading \\b would miss these."""+ redacted = redact_sensitive_text(text)++ assert secret not in redacted+ assert "[REDACTED]" in redacted+++def test_does_not_match_secret_name_embedded_in_letters() -> None:+ """Lookbehind still refuses letter-prefixed collisions like myapikey=x."""+ assert redact_sensitive_text("myapikey=sk_live_9f3c") == "myapikey=sk_live_9f3c"diff --git a/ts/packages/core/src/telemetry/redact.ts b/ts/packages/core/src/telemetry/redact.tsindex f538cf72a..a38e437ee 100644--- a/ts/packages/core/src/telemetry/redact.ts+++ b/ts/packages/core/src/telemetry/redact.ts@@ -20,8 +20,13 @@ const REDACTION_RULES: ReadonlyArray<readonly [RegExp, string]> = [// Authorization scheme + credential: `Bearer <token>`, `Basic <token>`.[/\b(bearer|basic)\s+[A-Za-z0-9._~+/=-]+/gi, `$1 ${REDACTED}`],// Secret-ish `key: value` / `key=value` / `key: "value"` / `key: 'value'` pairs.+ // The separator group allows a quote directly after the key name so JSON is+ // covered: in `{"api_key": "secret"}` the key's own closing quote sits+ // between the name and the colon.+ // Leading lookbehind (not \b) so env-style names like COMPOSIO_API_KEY still+ // match: underscore is a word char, so \b can't fire between COMPOSIO_ and API.[- /\b(authorization|api[-_]?key|apikey|x-api-key|access[-_]?token|refresh[-_]?token|client[-_]?secret|secret|password|passwd|pwd)\b(\s*[:=]+\s*)(["']?)([^\s"',}&]+)\3/gi,+ /(?<![A-Za-z0-9])(authorization|api[-_]?key|apikey|x-api-key|access[-_]?token|refresh[-_]?token|client[-_]?secret|secret|password|passwd|pwd)\b(["']?\s*[:=]+\s*)(["']?)([^\s"',}&]+)\3/gi,`$1$2$3${REDACTED}$3`,],];diff --git a/ts/packages/core/test/telemetry/redact.test.ts b/ts/packages/core/test/telemetry/redact.test.tsindex 31050533a..9fece668e 100644--- a/ts/packages/core/test/telemetry/redact.test.ts+++ b/ts/packages/core/test/telemetry/redact.test.ts@@ -41,8 +41,66 @@ describe('redactSensitiveText', () => {expect(redactSensitiveText('x-api-key: "ck_secretvalue"')).toContain('"[REDACTED]"');});+ // Serialized payloads are the common shape in error text: the key's own+ // closing quote sits between the name and the colon, so a pattern anchored on+ // `name` followed directly by `:` never matches.+ it('redacts secrets inside JSON payloads', () => {+ for (const [sample, secret] of [+ ['{"api_key": "ck_live_abc123"}', 'ck_live_abc123'],+ ['{"api_key":"ck_live_abc123"}', 'ck_live_abc123'],+ ['{"api_key" : "ck_live_abc123"}', 'ck_live_abc123'],+ ['{"refresh_token":"rt-abc.def-123"}', 'rt-abc.def-123'],+ ['{"x-api-key":"ck_hdr","user":"bob"}', 'ck_hdr'],+ [`{'client_secret': 'cs_live_abc123'}`, 'cs_live_abc123'],+ ['{"password": "hunter2"}', 'hunter2'],+ ] as const) {+ const out = redactSensitiveText(sample)!;+ expect(out, sample).toContain('[REDACTED]');+ expect(out, sample).not.toContain(secret);+ }+ });++ it('redacts a secret in a serialized error payload while keeping context', () => {+ const out = redactSensitiveText(+ 'Error executing tool: request body was rejected: ' ++ '{"toolkit": "GMAIL", "arguments": {"api_key": "ck_live_CUSTOMER", "to": "[email protected]"}}'+ )!;+ expect(out).not.toContain('ck_live_CUSTOMER');+ expect(out).toContain('GMAIL');+ });++ it('preserves JSON keys and quoting, replacing only the value', () => {+ expect(redactSensitiveText('{"api_key": "ck_live_abc123"}')).toBe('{"api_key": "[REDACTED]"}');+ });++ it('leaves a key name with no attached value untouched', () => {+ for (const benign of [+ 'the password field is required',+ 'no separator here "api_key" and nothing else',+ ]) {+ expect(redactSensitiveText(benign), benign).toBe(benign);+ }+ });+it('leaves benign error text untouched', () => {const benign = 'TypeError: cannot read property foo of undefined at Object.<anonymous>';expect(redactSensitiveText(benign)).toBe(benign);});++ // Underscore before api_key is a word char, so a leading \b would miss these.+ it('redacts env-style prefixed API keys', () => {+ for (const [sample, secret] of [+ ['COMPOSIO_API_KEY=sk_live_9f3c', 'sk_live_9f3c'],+ ['OPENAI_API_KEY=sk_live_9f3c', 'sk_live_9f3c'],+ ['export COMPOSIO_API_KEY="sk_live_9f3c"', 'sk_live_9f3c'],+ ] as const) {+ const out = redactSensitiveText(sample)!;+ expect(out, sample).toContain('[REDACTED]');+ expect(out, sample).not.toContain(secret);+ }+ });++ it('does not match a secret name embedded in letters', () => {+ expect(redactSensitiveText('myapikey=sk_live_9f3c')).toBe('myapikey=sk_live_9f3c');+ });});