@devries-cameron20 / composio Mirror · read-only / commits / 051c8c535
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 16:00 UTC

fix(telemetry): redact secrets inside JSON payloads (#4043)

## Summary Telemetry error text is redacted before it leaves the process, but the key/value rule only matches when the separator follows the key name directly: ``` \b(authorization|api[-_]?key|...|pwd)\b(\s*[:=]+\s*)(["']?)([^\s"',}&]+)\3 ``` In JSON — and in a Python `dict` repr — the key's own closing quote sits between the name and the colon, so `{"api_key": "..."}` never matches and the value is sent verbatim. That is the shape error messages usually carry: an API error envelope, a rejected request body echoed back, or an f-string interpolating a config dict. Both SDKs share the pattern and both are affected. The fix moves the optional quote into the separator group (`(["']?\s*[:=]+\s*)`). The key name, separator, and quoting are all preserved in the output; only the value is replaced. Measured with `redact_sensitive_text` / `redactSensitiveText` directly, before and after: | input | before | after | |---|---|---| | `api_key=sk-1` | redacted | redacted | | `api_key: "sk-1"` | redacted | redacted | | `x-api-key: sk-live-hdr` | redacted | redacted | | `{"api_key": "sk-live-abc"}` | **leaks** | `{"api_key": "[REDACTED]"}` | | `{"api_key":"sk-live-abc"}` | **leaks** | redacted | | `{"api_key" : "sk-live-abc"}` | **leaks** | redacted | | `{"refresh_token":"rt-abc.def-123"}` | **leaks** | redacted | | `{"x-api-key":"sk-hdr","user":"bob"}` | **leaks** | redacted, `"user":"bob"` kept | | `{'client_secret': 'cs-live-abc'}` | **leaks** | redacted | | `the password field is required` | untouched | untouched | | `no separator here "api_key" and nothing else` | untouched | untouched | The end-to-end path in Python is `composio/core/models/base.py:60-61`, which passes `str(e)` and `traceback.format_exc()` through the redactor into the telemetry `error` field. A tool failure whose message interpolates the rejected request body — `Error executing tool: request body was rejected: {"toolkit": "GMAIL", "arguments": {"api_key": "...", ...}}` — leaked the customer's key today. ## Changes - `python/composio/utils/redaction.py`, `ts/packages/core/src/telemetry/redact.ts`: allow a quote between the key name and the separator. - Tests on both sides for JSON, minified JSON, spaced-colon JSON, dict repr, the realistic serialized-payload shape, key/quote preservation, and no-false-positive cases. - Changeset for `@composio/core`. ## Type of change - [x] Bug fix ## How Has This Been Tested? Node 24.13.0 / pnpm 11.8.0 / Python 3.10.20 (uv), Windows. - `uv run pytest tests/test_redaction.py` — 13 passed. With `redaction.py` reverted and the new tests kept, 9 of them fail; the 4 that still pass are the pre-existing test plus the three no-false-positive controls. - `npx vitest run test/telemetry/redact.test.ts` in `ts/packages/core` — 10 passed. With `redact.ts` reverted, the 3 new cases fail. - Full Python suite: 913 passed, same 4 failures as on `next` unchanged (3 are `ModuleNotFoundError: composio_langchain` from providers not installed in my env; 1 is `test_empty_name_safe_fails_at_write_time`, which expects `IsADirectoryError` but Windows raises `PermissionError` when opening a directory). Collection 944 → 956, exactly the 12 tests added. - Full `@composio/core` vitest: 798 passed / 2 failed, byte-identical to the unchanged baseline (794 passed / same 2 failed — both Windows path-and-symlink cases). 11 test files fail to load in both runs because I installed with `--ignore-scripts`; the repo's `preinstall` hook doesn't run in my shell. - `ruff format --check` and `ruff check` clean on the two Python files; `prettier --write` applied to the two TypeScript files. ## Checklist - [x] I have read the Code of Conduct and this PR adheres to it - [x] I ran linters/tests locally and they passed - [ ] I updated documentation as needed — no user-facing behavior change to document - [x] I added tests or explain why not applicable - [x] I added a changeset if this change affects published packages ## Additional context This is defence-in-depth, so it stays deliberately narrow: same denylist of key names, same value character class, no new rules. Bare-prefix secrets (`sk-...`, `ghp_...`) and PEM blocks are still not matched by either SDK — worth a separate look, but widening the pattern is a different risk trade-off than closing a shape the rule already intends to cover.

LHMQ878 committed Aug 7, 2026 at 21:44 UTC 051c8c53573a602fa993cfb93695e70fb7c98fe9
5 files changed +156 -2
.changeset/quiet-json-secret-redaction.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +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.
python/composio/utils/redaction.py
+6 -1
@@ -7,8 +7,13 @@ import re
7 _REDACTED = "[REDACTED]"
8 _URL_QUERY = re.compile(r"(\bhttps?://[^\s?#'\"]+)\?[^\s'\"]*", re.IGNORECASE)
9 _AUTHORIZATION = re.compile(r"\b(bearer|basic)\s+[A-Za-z0-9._~+/=-]+", re.IGNORECASE)
10 +# The separator group allows a quote directly after the key name so JSON and
11 +# dict reprs are covered: in `{"api_key": "secret"}` the key's own closing quote
12 +# sits between the name and the colon.
13 _SECRET_PAIR = re.compile(
11 - r"\b(authorization|api[-_]?key|apikey|x-api-key|access[-_]?token|refresh[-_]?token|client[-_]?secret|secret|password|passwd|pwd)\b(\s*[:=]+\s*)([\"']?)([^\s\"',}&]+)\3",
14 + # Leading lookbehind (not \\b) so env-style names like COMPOSIO_API_KEY still
15 + # match: underscore is a word char, so \\b can't fire between COMPOSIO_ and API.
16 + 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",
17 re.IGNORECASE,
18 )
19
python/tests/test_redaction.py
+81
@@ -1,5 +1,7 @@
1 """Tests for telemetry secret redaction."""
2
3 +import pytest
4 +
5 from composio.utils.redaction import redact_sensitive_text
6
7
@@ -13,3 +15,82 @@ def test_redacts_url_queries_authorization_and_secret_pairs() -> None:
15 assert "api_key=abc" not in redacted
16 assert "password: hunter2" not in redacted
17 assert redacted.count("[REDACTED]") == 4
18 +
19 +
20 +@pytest.mark.parametrize(
21 + ("text", "secret"),
22 + [
23 + ('{"api_key": "sk-live-abc123"}', "sk-live-abc123"),
24 + ('{"api_key":"sk-live-abc123"}', "sk-live-abc123"),
25 + ('{"api_key" : "sk-live-abc123"}', "sk-live-abc123"),
26 + ('{"refresh_token":"rt-abc.def-123"}', "rt-abc.def-123"),
27 + ('{"x-api-key":"sk-hdr","user":"bob"}', "sk-hdr"),
28 + ("{'client_secret': 'cs-live-abc123'}", "cs-live-abc123"),
29 + ('{"password": "hunter2"}', "hunter2"),
30 + ],
31 +)
32 +def test_redacts_secrets_in_json_and_dict_reprs(text: str, secret: str) -> None:
33 + """Serialized payloads are the common shape in exception text.
34 +
35 + The key's own closing quote sits between the name and the colon, so a
36 + pattern anchored on ``name`` followed directly by ``:`` never matches.
37 + """
38 + redacted = redact_sensitive_text(text)
39 +
40 + assert secret not in redacted
41 + assert "[REDACTED]" in redacted
42 +
43 +
44 +def test_redacts_secret_in_serialized_error_payload() -> None:
45 + """The shape that reaches the telemetry error field via a tool failure."""
46 + text = (
47 + "Error executing tool: request body was rejected: "
48 + '{"toolkit": "GMAIL", "arguments": {"api_key": "sk-live-CUSTOMER", "to": "x@y.z"}}'
49 + )
50 +
51 + redacted = redact_sensitive_text(text)
52 +
53 + assert "sk-live-CUSTOMER" not in redacted
54 + assert "GMAIL" in redacted # non-secret context is preserved
55 +
56 +
57 +def test_json_keys_are_preserved() -> None:
58 + """Only the value is replaced; the key and quoting survive intact."""
59 + assert (
60 + redact_sensitive_text('{"api_key": "sk-live-abc123"}')
61 + == '{"api_key": "[REDACTED]"}'
62 + )
63 +
64 +
65 +@pytest.mark.parametrize(
66 + "text",
67 + [
68 + "the password field is required",
69 + "TypeError: cannot read property secret of undefined",
70 + 'no separator here "api_key" and nothing else',
71 + ],
72 +)
73 +def test_leaves_benign_text_untouched(text: str) -> None:
74 + """A key name with no value attached must not trigger redaction."""
75 + assert redact_sensitive_text(text) == text
76 +
77 +
78 +@pytest.mark.parametrize(
79 + ("text", "secret"),
80 + [
81 + ("COMPOSIO_API_KEY=sk_live_9f3c", "sk_live_9f3c"),
82 + ("OPENAI_API_KEY=sk_live_9f3c", "sk_live_9f3c"),
83 + ('export COMPOSIO_API_KEY="sk_live_9f3c"', "sk_live_9f3c"),
84 + ],
85 +)
86 +def test_redacts_env_style_prefixed_api_keys(text: str, secret: str) -> None:
87 + """Underscore before api_key is a word char, so a leading \\b would miss these."""
88 + redacted = redact_sensitive_text(text)
89 +
90 + assert secret not in redacted
91 + assert "[REDACTED]" in redacted
92 +
93 +
94 +def test_does_not_match_secret_name_embedded_in_letters() -> None:
95 + """Lookbehind still refuses letter-prefixed collisions like myapikey=x."""
96 + assert redact_sensitive_text("myapikey=sk_live_9f3c") == "myapikey=sk_live_9f3c"
ts/packages/core/src/telemetry/redact.ts
+6 -1
@@ -20,8 +20,13 @@ const REDACTION_RULES: ReadonlyArray<readonly [RegExp, string]> = [
20 // Authorization scheme + credential: `Bearer <token>`, `Basic <token>`.
21 [/\b(bearer|basic)\s+[A-Za-z0-9._~+/=-]+/gi, `$1 ${REDACTED}`],
22 // Secret-ish `key: value` / `key=value` / `key: "value"` / `key: 'value'` pairs.
23 + // The separator group allows a quote directly after the key name so JSON is
24 + // covered: in `{"api_key": "secret"}` the key's own closing quote sits
25 + // between the name and the colon.
26 + // Leading lookbehind (not \b) so env-style names like COMPOSIO_API_KEY still
27 + // match: underscore is a word char, so \b can't fire between COMPOSIO_ and API.
28 [
24 - /\b(authorization|api[-_]?key|apikey|x-api-key|access[-_]?token|refresh[-_]?token|client[-_]?secret|secret|password|passwd|pwd)\b(\s*[:=]+\s*)(["']?)([^\s"',}&]+)\3/gi,
29 + /(?<![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,
30 `$1$2$3${REDACTED}$3`,
31 ],
32 ];
ts/packages/core/test/telemetry/redact.test.ts
+58
@@ -41,8 +41,66 @@ describe('redactSensitiveText', () => {
41 expect(redactSensitiveText('x-api-key: "ck_secretvalue"')).toContain('"[REDACTED]"');
42 });
43
44 + // Serialized payloads are the common shape in error text: the key's own
45 + // closing quote sits between the name and the colon, so a pattern anchored on
46 + // `name` followed directly by `:` never matches.
47 + it('redacts secrets inside JSON payloads', () => {
48 + for (const [sample, secret] of [
49 + ['{"api_key": "ck_live_abc123"}', 'ck_live_abc123'],
50 + ['{"api_key":"ck_live_abc123"}', 'ck_live_abc123'],
51 + ['{"api_key" : "ck_live_abc123"}', 'ck_live_abc123'],
52 + ['{"refresh_token":"rt-abc.def-123"}', 'rt-abc.def-123'],
53 + ['{"x-api-key":"ck_hdr","user":"bob"}', 'ck_hdr'],
54 + [`{'client_secret': 'cs_live_abc123'}`, 'cs_live_abc123'],
55 + ['{"password": "hunter2"}', 'hunter2'],
56 + ] as const) {
57 + const out = redactSensitiveText(sample)!;
58 + expect(out, sample).toContain('[REDACTED]');
59 + expect(out, sample).not.toContain(secret);
60 + }
61 + });
62 +
63 + it('redacts a secret in a serialized error payload while keeping context', () => {
64 + const out = redactSensitiveText(
65 + 'Error executing tool: request body was rejected: ' +
66 + '{"toolkit": "GMAIL", "arguments": {"api_key": "ck_live_CUSTOMER", "to": "x@y.z"}}'
67 + )!;
68 + expect(out).not.toContain('ck_live_CUSTOMER');
69 + expect(out).toContain('GMAIL');
70 + });
71 +
72 + it('preserves JSON keys and quoting, replacing only the value', () => {
73 + expect(redactSensitiveText('{"api_key": "ck_live_abc123"}')).toBe('{"api_key": "[REDACTED]"}');
74 + });
75 +
76 + it('leaves a key name with no attached value untouched', () => {
77 + for (const benign of [
78 + 'the password field is required',
79 + 'no separator here "api_key" and nothing else',
80 + ]) {
81 + expect(redactSensitiveText(benign), benign).toBe(benign);
82 + }
83 + });
84 +
85 it('leaves benign error text untouched', () => {
86 const benign = 'TypeError: cannot read property foo of undefined at Object.<anonymous>';
87 expect(redactSensitiveText(benign)).toBe(benign);
88 });
89 +
90 + // Underscore before api_key is a word char, so a leading \b would miss these.
91 + it('redacts env-style prefixed API keys', () => {
92 + for (const [sample, secret] of [
93 + ['COMPOSIO_API_KEY=sk_live_9f3c', 'sk_live_9f3c'],
94 + ['OPENAI_API_KEY=sk_live_9f3c', 'sk_live_9f3c'],
95 + ['export COMPOSIO_API_KEY="sk_live_9f3c"', 'sk_live_9f3c'],
96 + ] as const) {
97 + const out = redactSensitiveText(sample)!;
98 + expect(out, sample).toContain('[REDACTED]');
99 + expect(out, sample).not.toContain(secret);
100 + }
101 + });
102 +
103 + it('does not match a secret name embedded in letters', () => {
104 + expect(redactSensitiveText('myapikey=sk_live_9f3c')).toBe('myapikey=sk_live_9f3c');
105 + });
106 });