@devries-cameron20 / composio-1 / commits / 1d31c80ef
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.

fix(sdk): keep credentials private in storage and logs (#4318)

## Summary - write CLI user data, pending login sessions, and agent identities through one atomic `0600` helper - repair `0644` credential files created by older CLI versions before reading them - redact credential-shaped structured values from CLI user-context diagnostics - redact secret-shaped text at both TypeScript and Python SDK log-output boundaries, including Pusher `auth` responses and exception tracebacks - preserve Python logger compatibility: errors remain untruncated, disabled levels remain lazy, and malformed placeholders cannot expose arguments ## Local reproduction Under the normal `022` umask, `next` created a plaintext credential file with mode `0644`. The pre-fix CLI user-context and TypeScript SDK debug paths also emitted sentinel credentials. The private atomic writer changes an existing `0644` target to `0600`, and the upgrade tests now prove all three legacy credential files are tightened without changing their contents. ## Verification - CLI permission upgrade tests: 31 passed across user data, pending login, and agent identity paths - CLI source and test typechecks passed - TypeScript core logging, redaction, and Pusher tests: 17 passed - TypeScript core source and type-test typechecks passed - Python logging regression tests: 5 passed - focused Ruff, Prettier, Oxlint, and `git diff --check` passed The focused CLI runner needed a temporary local alias for the pre-existing missing `#ssrf_guard` mapping in the CLI Vitest config. The alias was removed after verification and is not part of this PR. ## Contributor context Credit to **Syed Anas Mohiuddin**, independent security researcher, for reporting the legacy CLI credential-file permission issue. Supersedes [#4300](https://github.com/ComposioHQ/composio/pull/4300) · [Glen review](https://app.tryglen.com/ComposioHQ/composio/pull/4300). The implementation also covers agent credentials, retains atomic writes, and applies redaction at the shared SDK logging boundary.

Alberto Schiabel committed Sep 3, 2026 at 01:45 UTC 1d31c80eff73be37ede5f1e75416d1f21b1c6a13
20 files changed +1398 -59
.changeset/quiet-logs-stay-private.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +Redact credential-shaped values at the SDK log boundary.
python/composio/utils/logging.py
+60 -5
@@ -4,9 +4,12 @@ Logging utilities.
4
5 import logging
6 import os
7 +import sys
8 import typing as t
9 from enum import Enum
10
11 +from composio.utils.redaction import redact_sensitive_text, redact_sensitive_value
12 +
13 ENV_COMPOSIO_LOGGING_LEVEL = "COMPOSIO_LOGGING_LEVEL"
14
15 _DEFAULT_FORMAT = "[%(asctime)s][%(levelname)s] %(message)s"
@@ -71,7 +74,7 @@ class _VerbosityWrapper:
74 self.size = _LOG_LINE_SIZE_BY_VERBOSITY[self.verbosity]
75
76 def _trim(self, msg) -> str:
74 - msg = str(msg)
77 + msg = redact_sensitive_text(str(msg))
78 if self.size == -1:
79 return msg
80
@@ -80,17 +83,69 @@ class _VerbosityWrapper:
83
84 return msg[: self.size] + "..."
85
86 + def _prepare(
87 + self,
88 + msg,
89 + args: t.Tuple[t.Any, ...],
90 + *,
91 + trim: bool = True,
92 + ) -> str:
93 + if args:
94 + try:
95 + format_args = (
96 + args[0] if len(args) == 1 and isinstance(args[0], dict) else args
97 + )
98 + msg = msg % format_args
99 + except (KeyError, TypeError, ValueError):
100 + msg = f"{msg} [logging arguments omitted: formatting failed]"
101 + return self._trim(msg) if trim else redact_sensitive_text(str(msg))
102 +
103 + def _prepare_exception(self, msg: str, kwargs):
104 + sanitized_kwargs = dict(kwargs)
105 + if "extra" in sanitized_kwargs:
106 + sanitized_kwargs["extra"] = redact_sensitive_value(
107 + sanitized_kwargs["extra"]
108 + )
109 +
110 + exc_info = sanitized_kwargs.get("exc_info")
111 + if not exc_info:
112 + return msg, sanitized_kwargs
113 +
114 + if isinstance(exc_info, BaseException):
115 + exc_info = (type(exc_info), exc_info, exc_info.__traceback__)
116 + elif not isinstance(exc_info, tuple):
117 + exc_info = sys.exc_info()
118 +
119 + exception = exc_info[1]
120 + if exception is None:
121 + sanitized_kwargs["exc_info"] = None
122 + return msg, sanitized_kwargs
123 +
124 + exception_text = logging.Formatter().formatException(exc_info)
125 + sanitized_kwargs["exc_info"] = None
126 + return f"{msg}\n{redact_sensitive_text(exception_text)}", sanitized_kwargs
127 +
128 def info(self, msg, *args, **kwargs):
84 - self.logger.info(self._trim(msg), *args, **kwargs)
129 + if self.logger.isEnabledFor(logging.INFO):
130 + msg, kwargs = self._prepare_exception(self._prepare(msg, args), kwargs)
131 + self.logger.info(msg, **kwargs)
132
133 def debug(self, msg, *args, **kwargs):
87 - self.logger.debug(self._trim(msg), *args, **kwargs)
134 + if self.logger.isEnabledFor(logging.DEBUG):
135 + msg, kwargs = self._prepare_exception(self._prepare(msg, args), kwargs)
136 + self.logger.debug(msg, **kwargs)
137
138 def warning(self, msg, *args, **kwargs):
90 - self.logger.warning(self._trim(msg), *args, **kwargs)
139 + if self.logger.isEnabledFor(logging.WARNING):
140 + msg, kwargs = self._prepare_exception(self._prepare(msg, args), kwargs)
141 + self.logger.warning(msg, **kwargs)
142
143 def error(self, msg, *args, **kwargs):
93 - self.logger.error(msg, *args, **kwargs)
144 + if self.logger.isEnabledFor(logging.ERROR):
145 + msg, kwargs = self._prepare_exception(
146 + self._prepare(msg, args, trim=False), kwargs
147 + )
148 + self.logger.error(msg, **kwargs)
149
150 def isEnabledFor(self, level: int):
151 return self.logger.isEnabledFor(level=level)
python/composio/utils/redaction.py
+198 -8
@@ -3,23 +3,213 @@
3 from __future__ import annotations
4
5 import re
6 +import typing as t
7 +from collections.abc import Mapping
8
9 _REDACTED = "[REDACTED]"
10 +_MAX_STRUCTURED_REDACTION_DEPTH = 32
11 +_MAX_STRUCTURED_REDACTION_NODES = 10_000
12 _URL_QUERY = re.compile(r"(\bhttps?://[^\s?#'\"]+)\?[^\s'\"]*", re.IGNORECASE)
13 _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(
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",
14 +_SECRET_KEY_PATTERN = (
15 + r"authorization|auth|api[-_]?key|apikey|x-api-key|access[-_]?token|"
16 + r"refresh[-_]?token|client[-_]?secret|secret|password|passwd|pwd"
17 +)
18 +_SECRET_KEY_WITH_PREFIX_PATTERN = rf"(?:[A-Za-z0-9]+_)*(?:{_SECRET_KEY_PATTERN})"
19 +_SECRET_KEY = re.compile(_SECRET_KEY_WITH_PREFIX_PATTERN, re.IGNORECASE)
20 +# Quoted keys are matched as a unit so a key-like word at the end of an error
21 +# string cannot treat the string's closing quote as the start of a secret value.
22 +_QUOTED_SECRET_KEY_PREFIX = (
23 + rf"([\"'])({_SECRET_KEY_WITH_PREFIX_PATTERN})\1(\s*[:=]+\s*)"
24 +)
25 +_BARE_SECRET_KEY_PREFIX = rf"(?<![A-Za-z0-9\"'])({_SECRET_KEY_PATTERN})\b(\s*[:=]+\s*)"
26 +_ESCAPED_SECRET_KEY_PREFIX = (
27 + rf"(?<![A-Za-z0-9])({_SECRET_KEY_PATTERN})\b((?:\\[\"'])?\s*[:=]+\s*)"
28 +)
29 +_QUOTED_KEY_DOUBLE_QUOTED_VALUE = re.compile(
30 + rf'{_QUOTED_SECRET_KEY_PREFIX}"(?:\\.|[^"\\\r\n])*"', re.IGNORECASE
31 +)
32 +_QUOTED_KEY_SINGLE_QUOTED_VALUE = re.compile(
33 + rf"{_QUOTED_SECRET_KEY_PREFIX}'(?:\\.|[^'\\\r\n])*'", re.IGNORECASE
34 +)
35 +_BARE_KEY_DOUBLE_QUOTED_VALUE = re.compile(
36 + rf'{_BARE_SECRET_KEY_PREFIX}"((?:\\.|[^"\\\r\n])*)"',
37 + re.IGNORECASE,
38 +)
39 +_BARE_KEY_SINGLE_QUOTED_VALUE = re.compile(
40 + rf"{_BARE_SECRET_KEY_PREFIX}'((?:\\.|[^'\\\r\n])*)'",
41 re.IGNORECASE,
42 )
43 +_BARE_KEY_ESCAPED_DOUBLE_QUOTED_VALUE = re.compile(
44 + rf'{_ESCAPED_SECRET_KEY_PREFIX}\\"((?:\\.|[^"\\\r\n])*)\\"',
45 + re.IGNORECASE,
46 +)
47 +_BARE_KEY_ESCAPED_SINGLE_QUOTED_VALUE = re.compile(
48 + rf"{_ESCAPED_SECRET_KEY_PREFIX}\\'((?:\\.|[^'\\\r\n])*)\\'",
49 + re.IGNORECASE,
50 +)
51 +_SECRET_PAIR_PREFIX = rf"(?<![A-Za-z0-9])({_SECRET_KEY_PATTERN})\b([\"']?\s*[:=]+\s*)"
52 +_SECRET_PAIR_UNQUOTED = re.compile(
53 + rf"{_SECRET_PAIR_PREFIX}(?!\\[\"'])([^\s\"',}}&]+)", re.IGNORECASE
54 +)
55 +_COMPLETES_DOUBLE_QUOTED_FIELD = re.compile(r'(?:\\.|[^"\\\r\n])*"\s*[:=]')
56 +_COMPLETES_SINGLE_QUOTED_FIELD = re.compile(r"(?:\\.|[^'\\\r\n])*'\s*[:=]")
57 +_CONTAINS_QUOTED_FIELD = re.compile(r"(?:^|[,{\s])([\"'])[^\"'\\\r\n]+\1\s*[:=]\s*$")
58 +
59 +
60 +def _active_quotes_at(value: str, offsets: set[int]) -> dict[int, str | None]:
61 + active_quotes: dict[int, str | None] = {}
62 + quote: str | None = None
63 + escaped = False
64 + last_offset = max(offsets)
65 +
66 + for position, character in enumerate(value):
67 + if position in offsets:
68 + active_quotes[position] = quote
69 + if position == last_offset:
70 + break
71 + if character in "\r\n":
72 + quote = None
73 + escaped = False
74 + continue
75 + previous = value[position - 1] if position > 0 else ""
76 + if previous in "\r\n":
77 + previous = ""
78 + following = value[position + 1] if position + 1 < len(value) else ""
79 + if quote is None:
80 + if character == '"' or (
81 + character == "'" and not (previous.isalnum() or previous == "_")
82 + ):
83 + quote = character
84 + continue
85 + if escaped:
86 + escaped = False
87 + elif character == "\\":
88 + escaped = True
89 + elif character == quote:
90 + if (
91 + quote == "'"
92 + and (previous.isalnum() or previous == "_")
93 + and (following.isalnum() or following == "_")
94 + ):
95 + continue
96 + quote = None
97 +
98 + return active_quotes
99 +
100 +
101 +def _redact_bare_quoted_value(value: str, pattern: re.Pattern[str], quote: str) -> str:
102 + matches = list(pattern.finditer(value))
103 + if not matches:
104 + return value
105 +
106 + active_quotes = _active_quotes_at(value, {match.start() for match in matches})
107 + completes_field = (
108 + _COMPLETES_DOUBLE_QUOTED_FIELD
109 + if quote == '"'
110 + else _COMPLETES_SINGLE_QUOTED_FIELD
111 + )
112 + parts: list[str] = []
113 + cursor = 0
114 + for match in matches:
115 + parts.append(value[cursor : match.start()])
116 + if active_quotes[match.start()] == quote and (
117 + completes_field.match(value, match.end())
118 + or _CONTAINS_QUOTED_FIELD.search(match.group(3))
119 + ):
120 + parts.append(match.group(0))
121 + else:
122 + parts.append(f"{match.group(1)}{match.group(2)}{quote}{_REDACTED}{quote}")
123 + cursor = match.end()
124 +
125 + parts.append(value[cursor:])
126 + return "".join(parts)
127
128
129 def redact_sensitive_text(value: str) -> str:
130 """Remove common URL, authorization, and key-value secret shapes."""
131 value = _URL_QUERY.sub(rf"\1?{_REDACTED}", value)
132 value = _AUTHORIZATION.sub(rf"\1 {_REDACTED}", value)
25 - return _SECRET_PAIR.sub(rf"\1\2\3{_REDACTED}\3", value)
133 + value = _QUOTED_KEY_DOUBLE_QUOTED_VALUE.sub(rf'\1\2\1\3"{_REDACTED}"', value)
134 + value = _QUOTED_KEY_SINGLE_QUOTED_VALUE.sub(rf"\1\2\1\3'{_REDACTED}'", value)
135 + value = _BARE_KEY_ESCAPED_DOUBLE_QUOTED_VALUE.sub(rf'\1\2\\"{_REDACTED}\\"', value)
136 + value = _BARE_KEY_ESCAPED_SINGLE_QUOTED_VALUE.sub(rf"\1\2\\'{_REDACTED}\\'", value)
137 + value = _redact_bare_quoted_value(value, _BARE_KEY_DOUBLE_QUOTED_VALUE, '"')
138 + value = _redact_bare_quoted_value(value, _BARE_KEY_SINGLE_QUOTED_VALUE, "'")
139 + return _SECRET_PAIR_UNQUOTED.sub(rf"\1\2{_REDACTED}", value)
140 +
141 +
142 +def redact_sensitive_value(value: t.Any) -> t.Any:
143 + """Redact secrets in structured logging metadata without changing safe scalar types."""
144 + active_containers: set[int] = set()
145 + remaining_nodes = _MAX_STRUCTURED_REDACTION_NODES
146 +
147 + def redact(item: t.Any, depth: int) -> t.Any:
148 + nonlocal remaining_nodes
149 + if depth >= _MAX_STRUCTURED_REDACTION_DEPTH or remaining_nodes <= 0:
150 + return _REDACTED
151 + remaining_nodes -= 1
152 +
153 + if isinstance(item, (Mapping, list, tuple)):
154 + identity = id(item)
155 + if identity in active_containers:
156 + return _REDACTED
157 + active_containers.add(identity)
158 + try:
159 + if isinstance(item, Mapping):
160 + redacted_mapping: dict[t.Any, t.Any] = {}
161 + for key, nested in item.items():
162 + if remaining_nodes <= 0:
163 + break
164 + if isinstance(key, str):
165 + redacted_key: t.Any = redact_sensitive_text(key)
166 + elif isinstance(key, (int, float, bool, type(None))):
167 + redacted_key = key
168 + else:
169 + redacted_key = _REDACTED
170 + if isinstance(key, str) and _SECRET_KEY.fullmatch(key):
171 + remaining_nodes -= 1
172 + redacted_mapping[redacted_key] = _REDACTED
173 + else:
174 + redacted_mapping[redacted_key] = redact(nested, depth + 1)
175 + return redacted_mapping
176 +
177 + redacted_items: list[t.Any] = []
178 + fields = getattr(item, "_fields", ()) if isinstance(item, tuple) else ()
179 + for index, nested in enumerate(item):
180 + if remaining_nodes <= 0:
181 + return _REDACTED
182 + if (
183 + index < len(fields)
184 + and isinstance(fields[index], str)
185 + and _SECRET_KEY.fullmatch(fields[index])
186 + ):
187 + remaining_nodes -= 1
188 + redacted_items.append(_REDACTED)
189 + else:
190 + redacted_items.append(redact(nested, depth + 1))
191 + if isinstance(item, list):
192 + if type(item) is list:
193 + return redacted_items
194 + try:
195 + return type(item)(redacted_items)
196 + except Exception:
197 + return redacted_items
198 + if hasattr(item, "_fields"):
199 + try:
200 + return type(item)(*redacted_items)
201 + except Exception:
202 + pass
203 + return tuple(redacted_items)
204 + finally:
205 + active_containers.remove(identity)
206 +
207 + if isinstance(item, str):
208 + return redact_sensitive_text(item)
209 + if isinstance(item, (int, float, bool, type(None))):
210 + return item
211 + if isinstance(item, (bytes, bytearray, memoryview)):
212 + return _REDACTED
213 + return _REDACTED
214 +
215 + return redact(value, 0)
python/tests/test_logging_redaction.py new
+219
@@ -0,0 +1,219 @@
1 +import io
2 +import logging
3 +
4 +from composio.utils.logging import _VerbosityWrapper
5 +
6 +
7 +class _CaptureHandler(logging.StreamHandler):
8 + def __init__(self, stream: io.StringIO) -> None:
9 + super().__init__(stream)
10 + self.records: list[logging.LogRecord] = []
11 +
12 + def emit(self, record: logging.LogRecord) -> None:
13 + self.records.append(record)
14 + super().emit(record)
15 +
16 +
17 +class _TokenError(ValueError):
18 + def __init__(self, message: str, token: str | None = None) -> None:
19 + self.token = token
20 + super().__init__(message)
21 +
22 +
23 +class _ConstructorSensitiveError(Exception):
24 + def __init__(self, key: str) -> None:
25 + super().__init__({"known": "api_key=constructor-secret"}[key])
26 +
27 +
28 +def test_sdk_logger_redacts_structured_and_interpolated_credentials() -> None:
29 + output = io.StringIO()
30 + logger = logging.getLogger("composio-test-credential-redaction")
31 + logger.handlers = [logging.StreamHandler(output)]
32 + logger.propagate = False
33 + logger.setLevel(logging.DEBUG)
34 + wrapped = _VerbosityWrapper(logger, verbosity_level=3)
35 +
36 + wrapped.debug("api_key=%s", "uak_test_secret")
37 + wrapped.error({"nested": {"access_token": "oauth_test_secret"}, "safe": "visible"})
38 +
39 + logged = output.getvalue()
40 + assert "uak_test_secret" not in logged
41 + assert "oauth_test_secret" not in logged
42 + assert "[REDACTED]" in logged
43 + assert "visible" in logged
44 +
45 +
46 +def test_sdk_logger_redacts_extra_metadata_before_formatting() -> None:
47 + output = io.StringIO()
48 + logger = logging.getLogger("composio-test-extra-redaction")
49 + handler = logging.StreamHandler(output)
50 + handler.setFormatter(
51 + logging.Formatter("%(message)s %(api_key)s %(context)s %(binary)s")
52 + )
53 + logger.handlers = [handler]
54 + logger.propagate = False
55 + logger.setLevel(logging.INFO)
56 + wrapped = _VerbosityWrapper(logger, verbosity_level=3)
57 +
58 + wrapped.info(
59 + "request metadata",
60 + extra={
61 + "api_key": "uak_test_secret",
62 + "context": {
63 + "access_token": "oauth_test_secret",
64 + "api_key=nested_key_test_secret": "hidden key",
65 + "safe": "visible",
66 + },
67 + "binary": b"api_key=binary_test_secret",
68 + },
69 + )
70 +
71 + logged = output.getvalue()
72 + assert "uak_test_secret" not in logged
73 + assert "oauth_test_secret" not in logged
74 + assert "nested_key_test_secret" not in logged
75 + assert "binary_test_secret" not in logged
76 + assert logged.count("[REDACTED]") == 4
77 + assert "visible" in logged
78 +
79 +
80 +def test_sdk_logger_keeps_wide_extra_metadata_mapping_compatible() -> None:
81 + output = io.StringIO()
82 + logger = logging.getLogger("composio-test-wide-extra-redaction")
83 + logger.handlers = [logging.StreamHandler(output)]
84 + logger.propagate = False
85 + logger.setLevel(logging.INFO)
86 + wrapped = _VerbosityWrapper(logger, verbosity_level=3)
87 +
88 + wrapped.info(
89 + "wide metadata", extra={f"field_{index}": index for index in range(10_001)}
90 + )
91 +
92 + assert "wide metadata" in output.getvalue()
93 +
94 +
95 +def test_sdk_logger_redacts_errors_without_truncating_them() -> None:
96 + output = io.StringIO()
97 + logger = logging.getLogger("composio-test-error-redaction")
98 + logger.handlers = [logging.StreamHandler(output)]
99 + logger.propagate = False
100 + logger.setLevel(logging.ERROR)
101 + wrapped = _VerbosityWrapper(logger, verbosity_level=0)
102 +
103 + wrapped.error("api_key=%s %s", "uak_test_secret", "tail" * 100)
104 +
105 + logged = output.getvalue()
106 + assert "uak_test_secret" not in logged
107 + assert "[REDACTED]" in logged
108 + assert logged.rstrip().endswith("tail")
109 +
110 +
111 +def test_sdk_logger_redacts_exception_tracebacks() -> None:
112 + output = io.StringIO()
113 + logger = logging.getLogger("composio-test-exception-redaction")
114 + handler = _CaptureHandler(output)
115 + logger.handlers = [handler]
116 + logger.propagate = False
117 + logger.setLevel(logging.ERROR)
118 + wrapped = _VerbosityWrapper(logger, verbosity_level=3)
119 +
120 + try:
121 + raise RuntimeError("Authorization: Bearer oauth_test_secret")
122 + except RuntimeError:
123 + wrapped.error("request failed", exc_info=True)
124 +
125 + logged = output.getvalue()
126 + assert "oauth_test_secret" not in logged
127 + assert "Traceback (most recent call last)" in logged
128 + assert "RuntimeError" in logged
129 + assert "[REDACTED]" in logged
130 + assert logged.count("Traceback (most recent call last)") == 1
131 + assert handler.records[0].exc_info is None
132 +
133 +
134 +def test_sdk_logger_ignores_empty_implicit_exception_metadata() -> None:
135 + output = io.StringIO()
136 + logger = logging.getLogger("composio-test-empty-exception-metadata")
137 + handler = _CaptureHandler(output)
138 + logger.handlers = [handler]
139 + logger.propagate = False
140 + logger.setLevel(logging.ERROR)
141 + wrapped = _VerbosityWrapper(logger, verbosity_level=3)
142 +
143 + wrapped.error("request failed", exc_info=True)
144 +
145 + assert output.getvalue() == "request failed\n"
146 + assert handler.records[0].exc_info is None
147 +
148 +
149 +def test_sdk_logger_omits_exception_metadata_and_original_state() -> None:
150 + output = io.StringIO()
151 + logger = logging.getLogger("composio-test-exception-state-redaction")
152 + handler = _CaptureHandler(output)
153 + logger.handlers = [handler]
154 + logger.propagate = False
155 + logger.setLevel(logging.ERROR)
156 + wrapped = _VerbosityWrapper(logger, verbosity_level=3)
157 + error = _TokenError("api_key=message_test_secret", "attribute_test_secret")
158 + # ``BaseException.add_note`` is 3.11+; ``__notes__`` is what traceback reads.
159 + error.__notes__ = ["password=note_test_secret"]
160 +
161 + wrapped.error("request failed", exc_info=error)
162 +
163 + logged = output.getvalue()
164 + assert handler.records[0].exc_info is None
165 + for secret in (
166 + "message_test_secret",
167 + "attribute_test_secret",
168 + "note_test_secret",
169 + ):
170 + assert secret not in logged
171 +
172 +
173 +def test_sdk_logger_does_not_rebuild_custom_exception_types() -> None:
174 + output = io.StringIO()
175 + logger = logging.getLogger("composio-test-exception-constructor-fallback")
176 + handler = _CaptureHandler(output)
177 + logger.handlers = [handler]
178 + logger.propagate = False
179 + logger.setLevel(logging.ERROR)
180 + wrapped = _VerbosityWrapper(logger, verbosity_level=3)
181 +
182 + wrapped.error("request failed", exc_info=_ConstructorSensitiveError("known"))
183 +
184 + logged = output.getvalue()
185 + assert handler.records[0].exc_info is None
186 + assert "constructor-secret" not in logged
187 +
188 +
189 +def test_sdk_logger_omits_arguments_when_placeholder_formatting_fails() -> None:
190 + output = io.StringIO()
191 + logger = logging.getLogger("composio-test-placeholder-redaction")
192 + logger.handlers = [logging.StreamHandler(output)]
193 + logger.propagate = False
194 + logger.setLevel(logging.INFO)
195 + wrapped = _VerbosityWrapper(logger, verbosity_level=3)
196 +
197 + wrapped.info("api_key=%d", "uak_test_secret")
198 +
199 + logged = output.getvalue()
200 + assert "uak_test_secret" not in logged
201 + assert "logging arguments omitted" in logged
202 +
203 +
204 +def test_sdk_logger_tolerates_bad_placeholders_and_skips_disabled_levels() -> None:
205 + class RaisesOnString:
206 + def __str__(self) -> str:
207 + raise AssertionError("disabled log arguments must remain lazy")
208 +
209 + output = io.StringIO()
210 + logger = logging.getLogger("composio-test-logging-compatibility")
211 + logger.handlers = [logging.StreamHandler(output)]
212 + logger.propagate = False
213 + logger.setLevel(logging.INFO)
214 + wrapped = _VerbosityWrapper(logger, verbosity_level=3)
215 +
216 + wrapped.info("missing=%(missing)s", {"present": "safe"})
217 + wrapped.debug("api_key=%s", RaisesOnString())
218 +
219 + assert "missing" in output.getvalue()
python/tests/test_redaction.py
+156 -1
@@ -1,8 +1,21 @@
1 """Tests for telemetry secret redaction."""
2
3 +import json
4 +from typing import NamedTuple
5 +
6 import pytest
7
5 -from composio.utils.redaction import redact_sensitive_text
8 +from composio.utils.redaction import redact_sensitive_text, redact_sensitive_value
9 +
10 +
11 +class _RequestContext(NamedTuple):
12 + request_id: str
13 + api_key: str
14 +
15 +
16 +class _RenderedContext:
17 + def __repr__(self) -> str:
18 + return "RenderedContext(api_key='object_test_secret')"
19
20
21 def test_redacts_url_queries_authorization_and_secret_pairs() -> None:
@@ -24,9 +37,17 @@ def test_redacts_url_queries_authorization_and_secret_pairs() -> None:
37 ('{"api_key":"sk-live-abc123"}', "sk-live-abc123"),
38 ('{"api_key" : "sk-live-abc123"}', "sk-live-abc123"),
39 ('{"refresh_token":"rt-abc.def-123"}', "rt-abc.def-123"),
40 + ('{"auth":"app-key:pusher-secret"}', "app-key:pusher-secret"),
41 + ('{"COMPOSIO_API_KEY":"prefixed-double-secret"}', "prefixed-double-secret"),
42 + ("{'OPENAI_API_KEY': 'prefixed single secret'}", "prefixed single secret"),
43 ('{"x-api-key":"sk-hdr","user":"bob"}', "sk-hdr"),
44 ("{'client_secret': 'cs-live-abc123'}", "cs-live-abc123"),
45 ('{"password": "hunter2"}', "hunter2"),
46 + (
47 + '{"password": "correct horse battery staple"}',
48 + "correct horse battery staple",
49 + ),
50 + ("{'client_secret': 'multi word secret'}", "multi word secret"),
51 ],
52 )
53 def test_redacts_secrets_in_json_and_dict_reprs(text: str, secret: str) -> None:
@@ -62,6 +83,49 @@ def test_json_keys_are_preserved() -> None:
83 )
84
85
86 +def test_redacts_secret_without_consuming_the_adjacent_field() -> None:
87 + assert (
88 + redact_sensitive_text('{"api_key":"secret","user":"bob"}')
89 + == '{"api_key":"[REDACTED]","user":"bob"}'
90 + )
91 +
92 +
93 +def test_redacts_escaped_quoted_secret_in_serialized_message() -> None:
94 + source = json.dumps({"error": 'password: "secret"'})
95 + expected = json.dumps({"error": 'password: "[REDACTED]"'})
96 +
97 + assert redact_sensitive_text(source) == expected
98 + assert redact_sensitive_text("password: \\'secret\\'") == (
99 + "password: \\'[REDACTED]\\'"
100 + )
101 +
102 + double_encoded = json.dumps({"body": json.dumps({"api_key": "secret"})})
103 + double_encoded_expected = json.dumps(
104 + {"body": json.dumps({"api_key": "[REDACTED]"})}
105 + )
106 + assert redact_sensitive_text(double_encoded) == double_encoded_expected
107 +
108 +
109 +def test_preserves_serialized_adjacent_keys_with_escapes() -> None:
110 + source = json.dumps(
111 + {"error": "unknown field auth:", 'quoted"key': "safe", "path\\key": "safe"}
112 + )
113 +
114 + assert redact_sensitive_text(source) == source
115 +
116 +
117 +def test_redacts_unquoted_secrets_containing_backslashes() -> None:
118 + assert redact_sensitive_text("password=abc\\def") == "password=[REDACTED]"
119 + assert redact_sensitive_text("password=\\leading") == "password=[REDACTED]"
120 +
121 +
122 +def test_redacts_many_quoted_secrets_in_one_pass() -> None:
123 + source = " ".join(f'password: "secret-{index}"' for index in range(1_000))
124 + expected = " ".join('password: "[REDACTED]"' for _ in range(1_000))
125 +
126 + assert redact_sensitive_text(source) == expected
127 +
128 +
129 @pytest.mark.parametrize(
130 "text",
131 [
@@ -94,3 +158,94 @@ def test_redacts_env_style_prefixed_api_keys(text: str, secret: str) -> None:
158 def test_does_not_match_secret_name_embedded_in_letters() -> None:
159 """Lookbehind still refuses letter-prefixed collisions like myapikey=x."""
160 assert redact_sensitive_text("myapikey=sk_live_9f3c") == "myapikey=sk_live_9f3c"
161 +
162 +
163 +@pytest.mark.parametrize(
164 + "text",
165 + [
166 + '{"error": "unknown field auth:", "user": "bob"}',
167 + "{'note': 'unknown field auth:', \"user\": 'bob'}",
168 + '{"error": "unknown field auth:", "$schema": "safe"}',
169 + '{"error": "unknown field auth:", "@type": "safe"}',
170 + '{"error": "unknown field auth:", "üser": "safe"}',
171 + '{"error": "unknown field auth:", ".well-known": "safe"}',
172 + '{"error": "unknown field auth:", ":type": "safe"}',
173 + '{"error": "unknown field auth:", "?field": "safe"}',
174 + ],
175 +)
176 +def test_does_not_consume_adjacent_fields_after_key_like_text(text: str) -> None:
177 + assert redact_sensitive_text(text) == text
178 +
179 +
180 +@pytest.mark.parametrize(
181 + ("text", "secret"),
182 + [
183 + ('password: "punctuated secret";', "punctuated secret"),
184 + ("client_secret='period secret'.", "period secret"),
185 + ('api_key = "prose secret" followed by context', "prose secret"),
186 + ('password: "escaped \\"secret\\" value"', 'escaped \\"secret\\" value'),
187 + ('can\'t connect password: "secret"', "secret"),
188 + ('users\' password: "secret"', "secret"),
189 + ('Chris\' password: "secret"', "secret"),
190 + ('Andrés\' password: "secret"', "secret"),
191 + ('{"error":"request failed password: \'secret\'"}', "secret"),
192 + ('5" from target password: "secret"', "secret"),
193 + ('unmatched " before password: "secret"', "secret"),
194 + ("unmatched ' before password: 'secret'", "secret"),
195 + ('password: "}abc"', "}abc"),
196 + ('api_key: ",abc"', ",abc"),
197 + ],
198 +)
199 +def test_redacts_bare_quoted_values_before_punctuation_or_prose(
200 + text: str, secret: str
201 +) -> None:
202 + redacted = redact_sensitive_text(text)
203 +
204 + assert secret not in redacted
205 + assert "[REDACTED]" in redacted
206 +
207 +
208 +def test_structured_redaction_bounds_recursive_metadata() -> None:
209 + cyclic: dict[str, object] = {}
210 + cyclic["self"] = cyclic
211 +
212 + deeply_nested: object = "safe"
213 + for _ in range(40):
214 + deeply_nested = {"next": deeply_nested}
215 +
216 + assert redact_sensitive_value(cyclic) == {"self": "[REDACTED]"}
217 + assert "[REDACTED]" in str(redact_sensitive_value(deeply_nested))
218 +
219 +
220 +def test_structured_redaction_handles_sequences_and_rendered_objects() -> None:
221 + context = _RequestContext("request-1", "namedtuple_test_secret")
222 + redacted = redact_sensitive_value(
223 + {
224 + "items": [{"access_token": "list_test_secret"}],
225 + "tuple": ("Authorization: Bearer bearer_value_secret",),
226 + "context": context,
227 + "rendered": _RenderedContext(),
228 + "binary": b"api_key=binary_test_secret",
229 + }
230 + )
231 +
232 + assert isinstance(redacted["context"], _RequestContext)
233 + assert redacted["context"].request_id == "request-1"
234 + rendered = str(redacted)
235 + for secret in (
236 + "list_test_secret",
237 + "bearer_value_secret",
238 + "namedtuple_test_secret",
239 + "object_test_secret",
240 + "binary_test_secret",
241 + ):
242 + assert secret not in rendered
243 +
244 +
245 +def test_structured_redaction_stops_at_the_node_budget() -> None:
246 + assert redact_sensitive_value(list(range(10_001))) == "[REDACTED]"
247 + wide_mapping = redact_sensitive_value(
248 + {f"field_{index}": index for index in range(10_001)}
249 + )
250 + assert isinstance(wide_mapping, dict)
251 + assert len(wide_mapping) < 10_001
ts/packages/cli/src/commands/login.cmd.ts
+8 -2
@@ -16,6 +16,7 @@ import { commandHintStep } from 'src/services/command-hints';
16 import { runOrgSelection } from 'src/effects/select-org-project';
17 import { linkAnalyticsIdentityForOrg } from 'src/effects/link-analytics-identity';
18 import { setupCacheDir } from 'src/effects/setup-cache-dir';
19 +import { atomicWritePrivateFileString, ensurePrivateFileMode } from 'src/utils/atomic-write';
20 import { primeConsumerConnectedToolkitsCacheInBackground } from 'src/services/consumer-short-term-cache';
21 import { inferSkillReleaseChannel, installSkillSafe } from 'src/effects/install-skill';
22 import { handleAgentAuthError } from 'src/effects/handle-agent-auth-error';
@@ -131,7 +132,11 @@ const writePendingLoginSession = (session: Omit<PendingLoginSession, 'cachedAt'>
132 ...session,
133 cachedAt: new Date().toISOString(),
134 };
134 - yield* fs.writeFileString(filePath, `${JSON.stringify(payload, null, 2)}\n`);
135 + yield* atomicWritePrivateFileString({
136 + fs,
137 + target: filePath,
138 + contents: `${JSON.stringify(payload, null, 2)}\n`,
139 + });
140 });
141
142 const clearPendingLoginSession = Effect.gen(function* () {
@@ -151,7 +156,8 @@ const readPendingLoginSession = Effect.gen(function* () {
156 });
157 }
158
154 - const rawSession = yield* fs.readFileString(filePath, 'utf8').pipe(
159 + const rawSession = yield* ensurePrivateFileMode({ fs, target: filePath }).pipe(
160 + Effect.andThen(fs.readFileString(filePath, 'utf8')),
161 Effect.mapError(
162 cause =>
163 new PendingLoginError({
ts/packages/cli/src/services/agents.ts
+8 -2
@@ -3,6 +3,7 @@ import { Data, Effect, Either, Option, Predicate, Schema } from 'effect';
3 import { APP_CONFIG } from 'src/effects/app-config';
4 import { JsonRecordSchema } from 'src/effects/json';
5 import { setupCacheDir } from 'src/effects/setup-cache-dir';
6 +import { atomicWritePrivateFileString, ensurePrivateFileMode } from 'src/utils/atomic-write';
7 import { ComposioUserContext } from 'src/services/user-context';
8 import { getSessionInfoByUserApiKey } from 'src/services/composio-clients';
9 import { primeConsumerConnectedToolkitsCacheInBackground } from 'src/services/consumer-short-term-cache';
@@ -174,7 +175,8 @@ export const readStoredAgentIdentity = Effect.gen(function* () {
175 const exists = yield* fs.exists(configPath);
176 if (!exists) return Option.none<AgentIdentity>();
177
177 - return yield* fs.readFileString(configPath, 'utf8').pipe(
178 + return yield* ensurePrivateFileMode({ fs, target: configPath }).pipe(
179 + Effect.andThen(fs.readFileString(configPath, 'utf8')),
180 Effect.flatMap(Schema.decodeUnknown(Schema.parseJson(AgentIdentity))),
181 Effect.map(normalizeDecodedAgentIdentity),
182 Effect.tapError(error => Effect.logDebug('Failed to read agent identity:', error)),
@@ -187,7 +189,11 @@ export const writeStoredAgentIdentity = (identity: AgentIdentity) =>
189 const fs = yield* FileSystem.FileSystem;
190 const configPath = yield* agentConfigPath;
191 const normalized = normalizeDecodedAgentIdentity(identity);
190 - yield* fs.writeFileString(configPath, `${JSON.stringify(normalized, null, 2)}\n`);
192 + yield* atomicWritePrivateFileString({
193 + fs,
194 + target: configPath,
195 + contents: `${JSON.stringify(normalized, null, 2)}\n`,
196 + });
197 return normalized;
198 });
199
ts/packages/cli/src/services/user-context.ts
+24 -8
@@ -16,6 +16,8 @@ import { KeyringService, KeyringLiveWithBackend } from '@composio/cli-keyring/ef
16 import type { KeyringServiceShape } from '@composio/cli-keyring/effect';
17 import { KeyringError, type MacOSBackend } from '@composio/cli-keyring';
18 import { ComposioCliUserConfig, ComposioCliUserConfigLive } from 'src/services/cli-user-config';
19 +import { atomicWritePrivateFileString, ensurePrivateFileMode } from 'src/utils/atomic-write';
20 +import { redactSensitiveLogValue } from 'src/utils/redact-sensitive';
21
22 /**
23 * Keyring specifier for the Composio API key. `service` is a reverse
@@ -195,8 +197,12 @@ export const rawComposioUserContextLive = Layer.effect(
197 : { ...snapshot, apiKey: Option.none() };
198 const encoded = yield* userDataToJSON(onDisk);
199 const normalized = yield* normalizeEncodedUserData(encoded, !useLegacyStorage);
198 - yield* Effect.logDebug('Saving user data:', normalized);
199 - yield* fs.writeFileString(jsonUserConfigPath, normalized);
200 + yield* Effect.logDebug('Saving user data:', redactSensitiveLogValue(onDisk));
201 + yield* atomicWritePrivateFileString({
202 + fs,
203 + target: jsonUserConfigPath,
204 + contents: normalized,
205 + });
206 });
207
208 const logout = Effect.gen(function* () {
@@ -238,8 +244,15 @@ export const rawComposioUserContextLive = Layer.effect(
244 // temporarily writing with the legacy-storage codepath.
245 const onDisk = yield* userDataToJSON(next);
246 const normalized = yield* normalizeEncodedUserData(onDisk, false);
241 - yield* Effect.logDebug('Saving user data (keyring fallback):', normalized);
242 - yield* fs.writeFileString(jsonUserConfigPath, normalized);
247 + yield* Effect.logDebug(
248 + 'Saving user data (keyring fallback):',
249 + redactSensitiveLogValue(next)
250 + );
251 + yield* atomicWritePrivateFileString({
252 + fs,
253 + target: jsonUserConfigPath,
254 + contents: normalized,
255 + });
256 }
257 });
258
@@ -248,15 +261,15 @@ export const rawComposioUserContextLive = Layer.effect(
261 const nextUserData = { ...userData, ...data } satisfies UserData;
262 userData = nextUserData;
263 yield* writeJson(nextUserData);
251 - yield* Effect.logDebug('User data updated:', userData);
264 + yield* Effect.logDebug('User data updated:', redactSensitiveLogValue(userData));
265 });
266
267 const load = Effect.gen(function* () {
268 yield* Effect.logDebug('Loading user data from', jsonUserConfigPath);
269 + yield* ensurePrivateFileMode({ fs, target: jsonUserConfigPath });
270 const userDataJson = yield* fs.readFileString(jsonUserConfigPath, 'utf8');
257 - yield* Effect.logDebug('User data (raw):', userDataJson);
271 const parsedUserData = (yield* userDataFromJSON(userDataJson)) satisfies UserData;
259 - yield* Effect.logDebug('User data (parsed):', parsedUserData);
272 + yield* Effect.logDebug('User data (parsed):', redactSensitiveLogValue(parsedUserData));
273
274 const overriddenUserData = {
275 ...userData,
@@ -269,7 +282,10 @@ export const rawComposioUserContextLive = Layer.effect(
282 testUserId: parsedUserData.testUserId,
283 } satisfies UserData;
284
272 - yield* Effect.logDebug('User data (overridden from env vars):', overriddenUserData);
285 + yield* Effect.logDebug(
286 + 'User data (overridden from env vars):',
287 + redactSensitiveLogValue(overriddenUserData)
288 + );
289 userData = overriddenUserData;
290 return userData;
291 });
ts/packages/cli/src/utils/atomic-write.ts
+84 -18
@@ -4,23 +4,28 @@
4 *
5 * The tmp file lives next to the target so the final rename(2) stays on one
6 * filesystem and is atomic: readers observe either the old or the new
7 - * contents, never a partial write. The tmp name embeds the pid so concurrent
8 - * CLI processes writing the same target never share (and clobber) one tmp
9 - * file. On any failure — including a failure of the initial write itself,
7 + * contents, never a partial write. The tmp name contains a random UUID and is
8 + * created exclusively so concurrent or hostile processes cannot share,
9 + * replace, or redirect it. On any failure — including a failure of the initial write itself,
10 * e.g. ENOSPC mid-write — the tmp file is removed best-effort before the
11 * original error propagates, so no tmp litter is left next to the target.
12 */
13
14 import type { FileSystem } from '@effect/platform';
15 import type { PlatformError } from '@effect/platform/Error';
16 -import { Effect, Option } from 'effect';
16 +import { Effect, Either, Option, Predicate } from 'effect';
17
18 -export const atomicTmpPath = (target: string): string => `${target}.composio-tmp.${process.pid}`;
18 +const MAX_ATOMIC_WRITE_ATTEMPTS = 5;
19 +
20 +export const atomicTmpPath = (target: string): string =>
21 + `${target}.composio-tmp.${crypto.randomUUID()}`;
22
23 export const atomicWriteFileString = (params: {
24 readonly fs: FileSystem.FileSystem;
25 readonly target: string;
26 readonly contents: string;
27 + /** Create the replacement with this exact mode, including over an existing target. */
28 + readonly mode?: number;
29 /**
30 * Preserve an existing target's file mode across the replacement: the tmp
31 * copy is created with the target's mode from the start so private contents
@@ -36,21 +41,82 @@ export const atomicWriteFileString = (params: {
41 const preservedMode = params.preserveMode
42 ? Option.map(yield* fs.stat(target).pipe(Effect.option), info => info.mode & 0o7777)
43 : Option.none<number>();
39 - const tmpPath = atomicTmpPath(target);
44 + const targetMode = Option.fromNullable(params.mode).pipe(Option.orElse(() => preservedMode));
45 + const stage = (attempt: number): Effect.Effect<string, PlatformError> =>
46 + Effect.gen(function* () {
47 + const tmpPath = atomicTmpPath(target);
48 + const created = yield* Option.match(targetMode, {
49 + onNone: () => fs.writeFileString(tmpPath, contents, { flag: 'wx' }),
50 + onSome: mode => fs.writeFileString(tmpPath, contents, { flag: 'wx', mode }),
51 + }).pipe(Effect.either);
52
41 - const writeAndPromote = Effect.gen(function* () {
42 - yield* Option.match(preservedMode, {
43 - onNone: () => fs.writeFileString(tmpPath, contents),
44 - onSome: mode => fs.writeFileString(tmpPath, contents, { mode }),
45 - });
46 - yield* Option.match(preservedMode, {
47 - onNone: () => Effect.void,
48 - onSome: mode => fs.chmod(tmpPath, mode),
53 + if (Either.isLeft(created)) {
54 + const isCollision =
55 + Predicate.isTagged('SystemError')(created.left) &&
56 + created.left.reason === 'AlreadyExists';
57 + if (isCollision && attempt < MAX_ATOMIC_WRITE_ATTEMPTS) {
58 + return yield* stage(attempt + 1);
59 + }
60 + if (!isCollision) {
61 + yield* fs.remove(tmpPath, { force: true }).pipe(Effect.ignore);
62 + }
63 + return yield* Effect.fail(created.left);
64 + }
65 +
66 + return tmpPath;
67 });
50 - yield* fs.rename(tmpPath, target);
51 - });
68
53 - yield* writeAndPromote.pipe(
54 - Effect.onError(() => fs.remove(tmpPath, { force: true }).pipe(Effect.ignore))
69 + // Effect keeps acquisition and release uninterruptible. The promotion stays
70 + // interruptible, but cancellation cannot occur between staging and the
71 + // cleanup responsibility being registered.
72 + yield* Effect.acquireUseRelease(
73 + stage(1),
74 + tmpPath =>
75 + Effect.gen(function* () {
76 + yield* Option.match(targetMode, {
77 + onNone: () => Effect.void,
78 + onSome: mode => fs.chmod(tmpPath, mode),
79 + });
80 + yield* fs.rename(tmpPath, target);
81 + }),
82 + tmpPath => fs.remove(tmpPath, { force: true }).pipe(Effect.ignore)
83 );
84 });
85 +
86 +export const atomicWritePrivateFileString = (params: {
87 + readonly fs: FileSystem.FileSystem;
88 + readonly target: string;
89 + readonly contents: string;
90 +}): Effect.Effect<void, PlatformError> => atomicWriteFileString({ ...params, mode: 0o600 });
91 +
92 +/**
93 + * Tighten a credential-bearing file written by an older CLI before reading it.
94 + * This repairs existing files because a write mode only applies when creating
95 + * a new file and cannot remove permissions from an existing one.
96 + */
97 +export const ensurePrivateFileMode = (params: {
98 + readonly fs: FileSystem.FileSystem;
99 + readonly target: string;
100 +}): Effect.Effect<void> =>
101 + Effect.gen(function* () {
102 + const fileInfo = yield* params.fs.stat(params.target).pipe(
103 + Effect.tapError(error =>
104 + Effect.logWarning(
105 + `Could not inspect permissions for credential file at ${params.target}; continuing with the read: ${String(error)}`
106 + )
107 + ),
108 + Effect.option
109 + );
110 +
111 + if (Option.isNone(fileInfo) || (fileInfo.value.mode & 0o077) === 0) return;
112 +
113 + yield* params.fs
114 + .chmod(params.target, 0o600)
115 + .pipe(
116 + Effect.catchAll(error =>
117 + Effect.logWarning(
118 + `Could not tighten permissions for credential file at ${params.target}; continuing with the read: ${String(error)}`
119 + )
120 + )
121 + );
122 + });
ts/packages/cli/src/utils/redact-sensitive.ts new
+38
@@ -0,0 +1,38 @@
1 +const REDACTED = '[REDACTED]';
2 +
3 +const SENSITIVE_KEYS = new Set([
4 + 'access_token',
5 + 'api_key',
6 + 'apikey',
7 + 'authorization',
8 + 'client_secret',
9 + 'composio_agent_key',
10 + 'password',
11 + 'refresh_token',
12 + 'secret',
13 + 'token',
14 + 'user_api_key',
15 +]);
16 +
17 +const normalizeKey = (key: string): string =>
18 + key
19 + .replace(/([a-z0-9])([A-Z])/g, '$1_$2')
20 + .replace(/-/g, '_')
21 + .toLowerCase();
22 +
23 +const redactStructuredValue = (value: unknown): unknown => {
24 + if (Array.isArray(value)) return value.map(redactStructuredValue);
25 + if (value === null || typeof value !== 'object') return value;
26 +
27 + return Object.fromEntries(
28 + Object.entries(value).map(([key, nested]) => [
29 + key,
30 + SENSITIVE_KEYS.has(normalizeKey(key)) ? REDACTED : redactStructuredValue(nested),
31 + ])
32 + );
33 +};
34 +
35 +/** Redact credential-shaped fields from structured values before logging. */
36 +export const redactSensitiveLogValue = (value: unknown): unknown => {
37 + return redactStructuredValue(value);
38 +};
ts/packages/cli/test/src/commands/agent.cmd.test.ts
+22 -1
@@ -5,7 +5,7 @@ import path from 'node:path';
5 import { afterEach, vi } from 'vitest';
6 import * as constants from 'src/constants';
7 import { setupCacheDir } from 'src/effects/setup-cache-dir';
8 -import { writeStoredAgentIdentity } from 'src/services/agents';
8 +import { readStoredAgentIdentity, writeStoredAgentIdentity } from 'src/services/agents';
9 import { ComposioUserContext } from 'src/services/user-context';
10 import { cli, MockConsole, TestLive } from 'test/__utils__';
11
@@ -29,6 +29,24 @@ describe('CLI: composio agent', () => {
29 });
30
31 layer(TestLive())(it => {
32 + it.scoped('repairs permissions on an existing agent identity before reading it', () =>
33 + Effect.gen(function* () {
34 + const fs = yield* FileSystem.FileSystem;
35 + const cacheDir = yield* setupCacheDir;
36 + const agentConfigPath = path.join(cacheDir, 'agent.json');
37 + const encodedIdentity = `${JSON.stringify(agentSignupResponse, null, 2)}\n`;
38 + yield* fs.writeFileString(agentConfigPath, encodedIdentity);
39 + yield* fs.chmod(agentConfigPath, 0o644);
40 + expect((yield* fs.stat(agentConfigPath)).mode & 0o777).toBe(0o644);
41 +
42 + const identity = yield* readStoredAgentIdentity;
43 +
44 + expect(Option.getOrUndefined(identity)?.composio_agent_key).toBe('cak_test_agent');
45 + expect(yield* fs.readFileString(agentConfigPath, 'utf8')).toBe(encodedIdentity);
46 + expect((yield* fs.stat(agentConfigPath)).mode & 0o777).toBe(0o600);
47 + })
48 + );
49 +
50 it.scoped('exposes agent signup as a subcommand', () =>
51 Effect.gen(function* () {
52 yield* cli(['agent', '--help']);
@@ -133,6 +151,9 @@ describe('CLI: composio agent', () => {
151 it.scoped('agent inbox prints only JSON', () =>
152 Effect.gen(function* () {
153 yield* writeStoredAgentIdentity(agentSignupResponse);
154 + const fs = yield* FileSystem.FileSystem;
155 + const cacheDir = yield* setupCacheDir;
156 + expect((yield* fs.stat(path.join(cacheDir, 'agent.json'))).mode & 0o777).toBe(0o600);
157 vi.spyOn(globalThis, 'fetch').mockImplementation(async (requestInput, init) => {
158 const url =
159 typeof requestInput === 'string'
ts/packages/cli/test/src/commands/login.cmd.test.ts
+39
@@ -163,6 +163,9 @@ describe('CLI: composio login', () => {
163 );
164 const pendingLogin = JSON.parse(pendingLoginRaw) as Record<string, unknown>;
165 expect(pendingLogin.key).toBe('te00st11-d0c4-4efa-8117-c638886063e0');
166 + expect(
167 + (yield* fs.stat(path.join(cacheDir, 'pending-login-session.json'))).mode & 0o777
168 + ).toBe(0o600);
169
170 expect(output).not.toContain('-- composio login --');
171 expect(output).not.toContain('Please login using the following URL');
@@ -357,6 +360,41 @@ describe('CLI: composio login', () => {
360 );
361 });
362
363 + const stopBeforeSessionPollUI = TerminalUI.of({
364 + ...headlessStdinUI,
365 + useMakeSpinner: () => Effect.die(new Error('test: stop before session poll')),
366 + });
367 +
368 + layer(TestLive({ terminalUI: stopBeforeSessionPollUI }))(it => {
369 + it.scoped('repairs permissions on an existing pending login session before reading it', () =>
370 + Effect.gen(function* () {
371 + const fs = yield* FileSystem.FileSystem;
372 + const cacheDir = yield* setupCacheDir;
373 + const pendingPath = path.join(cacheDir, 'pending-login-session.json');
374 + const cachedAt = new Date().toISOString();
375 + const pendingLogin = `${JSON.stringify(
376 + {
377 + key: 'legacy-session-id',
378 + loginUrl: 'https://dashboard.composio.dev/?cliKey=legacy-session-id',
379 + expiresAt: cachedAt,
380 + cachedAt,
381 + },
382 + null,
383 + 2
384 + )}\n`;
385 + yield* fs.writeFileString(pendingPath, pendingLogin);
386 + yield* fs.chmod(pendingPath, 0o644);
387 + expect((yield* fs.stat(pendingPath)).mode & 0o777).toBe(0o644);
388 +
389 + const exit = yield* Effect.exit(cli(['login', '--poll']));
390 +
391 + expect(Exit.isFailure(exit)).toBe(true);
392 + expect(yield* fs.readFileString(pendingPath, 'utf8')).toBe(pendingLogin);
393 + expect((yield* fs.stat(pendingPath)).mode & 0o777).toBe(0o600);
394 + })
395 + );
396 + });
397 +
398 layer(TestLive())(it => {
399 it.scoped('[When] logging in with --user-api-key --org [Then] stores the chosen org', () =>
400 Effect.gen(function* () {
@@ -428,6 +466,7 @@ describe('CLI: composio login', () => {
466 // `~/.composio/config.json`.
467 expect(userConfig.api_key).toBe('uak_direct_key');
468 expect(userConfig.org_id).toBe('org_selected');
469 + expect((yield* fs.stat(userConfigPath)).mode & 0o777).toBe(0o600);
470
471 // ComposioUserContext also exposes the resolved key in-memory
472 // for subsequent API calls in this process.
ts/packages/cli/test/src/services/user-context.test.ts
+6 -1
@@ -180,8 +180,11 @@ describe('ComposioUserContext', () => {
180 const userDataAsJson = yield* userDataToJSON(expectedUserData);
181
182 const fs = yield* FileSystem.FileSystem;
183 + const userDataPath = path.join(cwd, '.composio', 'user_data.json');
184 yield* fs.makeDirectory(path.join(cwd, '.composio'), { recursive: true });
184 - yield* fs.writeFileString(path.join(cwd, '.composio', 'user_data.json'), userDataAsJson);
185 + yield* fs.writeFileString(userDataPath, userDataAsJson);
186 + yield* fs.chmod(userDataPath, 0o644);
187 + assertEquals((yield* fs.stat(userDataPath)).mode & 0o777, 0o644);
188
189 const ctx = yield* ComposioUserContext;
190 assertEquals(
@@ -193,6 +196,8 @@ describe('ComposioUserContext', () => {
196 })
197 );
198 assertEquals(ctx.isLoggedIn(), true);
199 + assertEquals(yield* fs.readFileString(userDataPath, 'utf8'), userDataAsJson);
200 + assertEquals((yield* fs.stat(userDataPath)).mode & 0o777, 0o600);
201 }).pipe(Effect.provide(ComposioUserContextTest));
202 });
203 });
ts/packages/cli/test/src/utils/atomic-write.test.ts new
+264
@@ -0,0 +1,264 @@
1 +import { FileSystem, Path } from '@effect/platform';
2 +import * as PlatformError from '@effect/platform/Error';
3 +import { BunFileSystem, BunPath } from '@effect/platform-bun';
4 +import { describe, expect, layer } from '@effect/vitest';
5 +import { Deferred, Effect, Fiber, Layer } from 'effect';
6 +import { atomicWritePrivateFileString, ensurePrivateFileMode } from 'src/utils/atomic-write';
7 +
8 +const TestPlatform = Layer.mergeAll(BunFileSystem.layer, BunPath.layer);
9 +
10 +const failChmod = (fs: FileSystem.FileSystem, onAttempt: () => void): FileSystem.FileSystem =>
11 + new Proxy(fs, {
12 + get(target, property, receiver) {
13 + if (property !== 'chmod') {
14 + return Reflect.get(target, property, receiver);
15 + }
16 +
17 + return (path: string) => {
18 + onAttempt();
19 + return Effect.fail(
20 + new PlatformError.SystemError({
21 + reason: 'PermissionDenied',
22 + module: 'FileSystem',
23 + method: 'chmod',
24 + pathOrDescriptor: path,
25 + })
26 + );
27 + };
28 + },
29 + });
30 +
31 +const collideWithTmpWrites = (
32 + fs: FileSystem.FileSystem,
33 + maxCollisions: number,
34 + onCollision: (path: string) => void
35 +): FileSystem.FileSystem => {
36 + let collisions = 0;
37 +
38 + return new Proxy(fs, {
39 + get(target, property, receiver) {
40 + if (property !== 'writeFileString') {
41 + return Reflect.get(target, property, receiver);
42 + }
43 +
44 + return (path: string, contents: string, options?: FileSystem.WriteFileOptions) => {
45 + if (collisions >= maxCollisions || !path.includes('.composio-tmp.')) {
46 + return target.writeFileString(path, contents, options);
47 + }
48 +
49 + collisions += 1;
50 + onCollision(path);
51 + return target
52 + .writeFileString(path, 'pre-existing contents')
53 + .pipe(Effect.andThen(target.writeFileString(path, contents, options)));
54 + };
55 + },
56 + });
57 +};
58 +
59 +const delayAfterTmpWrite = (
60 + fs: FileSystem.FileSystem,
61 + onStaged: Effect.Effect<void>,
62 + release: Effect.Effect<void>
63 +): FileSystem.FileSystem =>
64 + new Proxy(fs, {
65 + get(target, property, receiver) {
66 + if (property !== 'writeFileString') {
67 + return Reflect.get(target, property, receiver);
68 + }
69 +
70 + return (path: string, contents: string, options?: FileSystem.WriteFileOptions) =>
71 + target.writeFileString(path, contents, options).pipe(
72 + Effect.tap(() => onStaged),
73 + Effect.andThen(release)
74 + );
75 + },
76 + });
77 +
78 +const failAfterTmpWrite = (fs: FileSystem.FileSystem): FileSystem.FileSystem =>
79 + new Proxy(fs, {
80 + get(target, property, receiver) {
81 + if (property !== 'writeFileString') {
82 + return Reflect.get(target, property, receiver);
83 + }
84 +
85 + return (path: string, contents: string, options?: FileSystem.WriteFileOptions) => {
86 + if (!path.includes('.composio-tmp.')) {
87 + return target.writeFileString(path, contents, options);
88 + }
89 +
90 + return target.writeFileString(path, contents, options).pipe(
91 + Effect.andThen(
92 + Effect.fail(
93 + new PlatformError.SystemError({
94 + reason: 'PermissionDenied',
95 + module: 'FileSystem',
96 + method: 'writeFileString',
97 + pathOrDescriptor: path,
98 + })
99 + )
100 + )
101 + );
102 + };
103 + },
104 + });
105 +
106 +describe('atomicWritePrivateFileString', () => {
107 + layer(TestPlatform)(it => {
108 + it.scoped('retries an exclusive random staging path without touching a collision', () =>
109 + Effect.gen(function* () {
110 + const fs = yield* FileSystem.FileSystem;
111 + const path = yield* Path.Path;
112 + const directory = yield* fs.makeTempDirectoryScoped();
113 + const target = path.join(directory, 'credentials.json');
114 + let collisionPath: string | undefined;
115 + const collidingFs = collideWithTmpWrites(fs, 1, collidedPath => {
116 + collisionPath = collidedPath;
117 + });
118 +
119 + yield* atomicWritePrivateFileString({
120 + fs: collidingFs,
121 + target,
122 + contents: '{"api_key":"valid"}\n',
123 + });
124 +
125 + expect(collisionPath).toBeDefined();
126 + expect(yield* fs.readFileString(collisionPath!, 'utf8')).toBe('pre-existing contents');
127 + expect(yield* fs.readFileString(target, 'utf8')).toBe('{"api_key":"valid"}\n');
128 + expect((yield* fs.stat(target)).mode & 0o777).toBe(0o600);
129 + })
130 + );
131 +
132 + it.scoped('stops after bounded collisions without deleting foreign files', () =>
133 + Effect.gen(function* () {
134 + const fs = yield* FileSystem.FileSystem;
135 + const path = yield* Path.Path;
136 + const directory = yield* fs.makeTempDirectoryScoped();
137 + const target = path.join(directory, 'credentials.json');
138 + const collisionPaths: string[] = [];
139 + const collidingFs = collideWithTmpWrites(fs, Number.POSITIVE_INFINITY, collisionPath => {
140 + collisionPaths.push(collisionPath);
141 + });
142 +
143 + const error = yield* atomicWritePrivateFileString({
144 + fs: collidingFs,
145 + target,
146 + contents: '{"api_key":"valid"}\n',
147 + }).pipe(Effect.flip);
148 +
149 + expect(error).toMatchObject({ _tag: 'SystemError', reason: 'AlreadyExists' });
150 + expect(collisionPaths).toHaveLength(5);
151 + expect(new Set(collisionPaths)).toHaveLength(5);
152 + expect(yield* fs.exists(target)).toBe(false);
153 + for (const collisionPath of collisionPaths) {
154 + expect(yield* fs.readFileString(collisionPath, 'utf8')).toBe('pre-existing contents');
155 + }
156 + })
157 + );
158 +
159 + it.scoped('cleans staging before honoring interruption', () =>
160 + Effect.gen(function* () {
161 + const fs = yield* FileSystem.FileSystem;
162 + const path = yield* Path.Path;
163 + const directory = yield* fs.makeTempDirectoryScoped();
164 + const target = path.join(directory, 'credentials.json');
165 + yield* fs.writeFileString(target, '{"api_key":"old"}\n');
166 + yield* fs.chmod(target, 0o600);
167 + const staged = yield* Deferred.make<void>();
168 + const release = yield* Deferred.make<void>();
169 + const delayedFs = delayAfterTmpWrite(
170 + fs,
171 + Deferred.succeed(staged, undefined),
172 + Deferred.await(release)
173 + );
174 + const fiber = yield* atomicWritePrivateFileString({
175 + fs: delayedFs,
176 + target,
177 + contents: '{"api_key":"valid"}\n',
178 + }).pipe(Effect.fork);
179 +
180 + yield* Deferred.await(staged);
181 + const interruptFiber = yield* Fiber.interrupt(fiber).pipe(Effect.fork);
182 + yield* Effect.yieldNow();
183 + yield* Deferred.succeed(release, undefined);
184 + yield* Fiber.join(interruptFiber);
185 +
186 + expect(yield* fs.readFileString(target, 'utf8')).toBe('{"api_key":"old"}\n');
187 + expect((yield* fs.stat(target)).mode & 0o777).toBe(0o600);
188 + expect(
189 + (yield* fs.readDirectory(directory)).filter(name => name.includes('.composio-tmp.'))
190 + ).toEqual([]);
191 + })
192 + );
193 +
194 + it.scoped('cleans a partially written staging file on non-collision failure', () =>
195 + Effect.gen(function* () {
196 + const fs = yield* FileSystem.FileSystem;
197 + const path = yield* Path.Path;
198 + const directory = yield* fs.makeTempDirectoryScoped();
199 + const target = path.join(directory, 'credentials.json');
200 + yield* fs.writeFileString(target, '{"api_key":"old"}\n');
201 + yield* fs.chmod(target, 0o600);
202 +
203 + const error = yield* atomicWritePrivateFileString({
204 + fs: failAfterTmpWrite(fs),
205 + target,
206 + contents: '{"api_key":"new"}\n',
207 + }).pipe(Effect.flip);
208 +
209 + expect(error).toMatchObject({ _tag: 'SystemError', reason: 'PermissionDenied' });
210 + expect(yield* fs.readFileString(target, 'utf8')).toBe('{"api_key":"old"}\n');
211 + expect(
212 + (yield* fs.readDirectory(directory)).filter(name => name.includes('.composio-tmp.'))
213 + ).toEqual([]);
214 + })
215 + );
216 + });
217 +});
218 +
219 +describe('ensurePrivateFileMode', () => {
220 + layer(TestPlatform)(it => {
221 + it.scoped('keeps valid contents readable when chmod fails', () =>
222 + Effect.gen(function* () {
223 + const fs = yield* FileSystem.FileSystem;
224 + const path = yield* Path.Path;
225 + const directory = yield* fs.makeTempDirectoryScoped();
226 + const target = path.join(directory, 'credentials.json');
227 + const contents = '{"api_key":"valid"}\n';
228 + yield* fs.writeFileString(target, contents);
229 + yield* fs.chmod(target, 0o644);
230 +
231 + let chmodAttempts = 0;
232 + const failingFs = failChmod(fs, () => {
233 + chmodAttempts += 1;
234 + });
235 + const readContents = yield* ensurePrivateFileMode({ fs: failingFs, target }).pipe(
236 + Effect.andThen(failingFs.readFileString(target, 'utf8'))
237 + );
238 +
239 + expect(chmodAttempts).toBe(1);
240 + expect(readContents).toBe(contents);
241 + expect((yield* fs.stat(target)).mode & 0o777).toBe(0o644);
242 + })
243 + );
244 +
245 + it.scoped('does not call chmod for an already-private file', () =>
246 + Effect.gen(function* () {
247 + const fs = yield* FileSystem.FileSystem;
248 + const path = yield* Path.Path;
249 + const directory = yield* fs.makeTempDirectoryScoped();
250 + const target = path.join(directory, 'credentials.json');
251 + yield* fs.writeFileString(target, '{"api_key":"valid"}\n');
252 + yield* fs.chmod(target, 0o600);
253 +
254 + let chmodAttempts = 0;
255 + const failingFs = failChmod(fs, () => {
256 + chmodAttempts += 1;
257 + });
258 + yield* ensurePrivateFileMode({ fs: failingFs, target });
259 +
260 + expect(chmodAttempts).toBe(0);
261 + })
262 + );
263 + });
264 +});
ts/packages/cli/test/src/utils/credential-hygiene.test.ts new
+18
@@ -0,0 +1,18 @@
1 +import { describe, expect, it } from 'vitest';
2 +import { redactSensitiveLogValue } from 'src/utils/redact-sensitive';
3 +
4 +describe('credential hygiene', () => {
5 + it('redacts nested credential keys in structured values', () => {
6 + const value = {
7 + apiKey: 'uak_test_secret',
8 + nested: { user_api_key: 'agent_test_secret' },
9 + safe: 'visible',
10 + };
11 +
12 + const redactedObject = redactSensitiveLogValue(value);
13 +
14 + expect(JSON.stringify(redactedObject)).not.toContain('uak_test_secret');
15 + expect(JSON.stringify(redactedObject)).not.toContain('agent_test_secret');
16 + expect(JSON.stringify(redactedObject)).toContain('visible');
17 + });
18 +});
ts/packages/core/src/telemetry/redact.ts
+133 -6
@@ -13,6 +13,33 @@
13 */
14
15 const REDACTED = '[REDACTED]';
16 +const SECRET_KEY_PATTERN = String.raw`authorization|auth|api[-_]?key|apikey|x-api-key|access[-_]?token|refresh[-_]?token|client[-_]?secret|secret|password|passwd|pwd`;
17 +const SECRET_KEY_WITH_PREFIX_PATTERN = String.raw`(?:[A-Za-z0-9]+_)*(?:${SECRET_KEY_PATTERN})`;
18 +const QUOTED_SECRET_KEY_PREFIX = String.raw`(["'])(${SECRET_KEY_WITH_PREFIX_PATTERN})\1(\s*[:=]+\s*)`;
19 +const BARE_SECRET_KEY_PREFIX = String.raw`(?<![A-Za-z0-9"'])(${SECRET_KEY_PATTERN})\b(\s*[:=]+\s*)`;
20 +const ESCAPED_SECRET_KEY_PREFIX = String.raw`(?<![A-Za-z0-9])(${SECRET_KEY_PATTERN})\b((?:\\["'])?\s*[:=]+\s*)`;
21 +const SECRET_PAIR_PREFIX = String.raw`(?<![A-Za-z0-9])(${SECRET_KEY_PATTERN})\b(["']?\s*[:=]+\s*)`;
22 +const BARE_KEY_DOUBLE_QUOTED_VALUE = new RegExp(
23 + String.raw`${BARE_SECRET_KEY_PREFIX}"((?:\\.|[^"\\\r\n])*)"`,
24 + 'gi'
25 +);
26 +const BARE_KEY_SINGLE_QUOTED_VALUE = new RegExp(
27 + String.raw`${BARE_SECRET_KEY_PREFIX}'((?:\\.|[^'\\\r\n])*)'`,
28 + 'gi'
29 +);
30 +const BARE_KEY_ESCAPED_DOUBLE_QUOTED_VALUE = new RegExp(
31 + String.raw`${ESCAPED_SECRET_KEY_PREFIX}\\"((?:\\.|[^"\\\r\n])*)\\"`,
32 + 'gi'
33 +);
34 +const BARE_KEY_ESCAPED_SINGLE_QUOTED_VALUE = new RegExp(
35 + String.raw`${ESCAPED_SECRET_KEY_PREFIX}\\'((?:\\.|[^'\\\r\n])*)\\'`,
36 + 'gi'
37 +);
38 +const SECRET_PAIR_UNQUOTED = new RegExp(
39 + String.raw`${SECRET_PAIR_PREFIX}(?!\\["'])([^\s"',}&]+)`,
40 + 'gi'
41 +);
42 +const CONTAINS_QUOTED_FIELD = /(?:^|[,{\s])(["'])[^"'\\\r\n]+\1\s*[:=]\s*$/;
43
44 const REDACTION_RULES: ReadonlyArray<readonly [RegExp, string]> = [
45 // URL query strings — tokens, presigned signatures, one-time codes.
@@ -20,17 +47,113 @@ const REDACTION_RULES: ReadonlyArray<readonly [RegExp, string]> = [
47 // Authorization scheme + credential: `Bearer <token>`, `Basic <token>`.
48 [/\b(bearer|basic)\s+[A-Za-z0-9._~+/=-]+/gi, `$1 ${REDACTED}`],
49 // 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.
50 + // Match quoted keys separately so key-like prose cannot treat an enclosing
51 + // string's closing quote as the start of a secret value.
52 // Leading lookbehind (not \b) so env-style names like COMPOSIO_API_KEY still
53 // match: underscore is a word char, so \b can't fire between COMPOSIO_ and API.
54 [
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`,
55 + new RegExp(String.raw`${QUOTED_SECRET_KEY_PREFIX}"(?:\\.|[^"\\\r\n])*"`, 'gi'),
56 + `$1$2$1$3"${REDACTED}"`,
57 + ],
58 + [
59 + new RegExp(String.raw`${QUOTED_SECRET_KEY_PREFIX}'(?:\\.|[^'\\\r\n])*'`, 'gi'),
60 + `$1$2$1$3'${REDACTED}'`,
61 ],
62 ];
63
64 +const isWordCharacter = (character: string): boolean => /[\p{L}\p{N}_]/u.test(character);
65 +
66 +const activeQuotesAt = (
67 + input: string,
68 + offsets: ReadonlySet<number>
69 +): ReadonlyMap<number, '"' | "'" | undefined> => {
70 + const activeQuotes = new Map<number, '"' | "'" | undefined>();
71 + let quote: '"' | "'" | undefined;
72 + let escaped = false;
73 + let lastOffset = -1;
74 + for (const offset of offsets) lastOffset = Math.max(lastOffset, offset);
75 +
76 + for (let position = 0; position < input.length; position += 1) {
77 + const character = input[position];
78 + if (offsets.has(position)) {
79 + activeQuotes.set(position, quote);
80 + if (position === lastOffset) break;
81 + }
82 + if (character === '\n' || character === '\r') {
83 + quote = undefined;
84 + escaped = false;
85 + continue;
86 + }
87 + const previous =
88 + position > 0 && !['\n', '\r'].includes(input[position - 1]) ? input[position - 1] : '';
89 + const following = input[position + 1] ?? '';
90 + if (!quote) {
91 + if (character === '"' || (character === "'" && !isWordCharacter(previous))) {
92 + quote = character;
93 + }
94 + continue;
95 + }
96 + if (escaped) escaped = false;
97 + else if (character === '\\') escaped = true;
98 + else if (character === quote) {
99 + if (quote === "'" && isWordCharacter(previous) && isWordCharacter(following)) continue;
100 + quote = undefined;
101 + }
102 + }
103 +
104 + return activeQuotes;
105 +};
106 +
107 +const completesQuotedFieldAt = (input: string, index: number, quote: '"' | "'"): boolean => {
108 + let escaped = false;
109 + for (let position = index; position < input.length; position += 1) {
110 + const character = input[position];
111 + if (character === '\n' || character === '\r') return false;
112 + if (escaped) {
113 + escaped = false;
114 + continue;
115 + }
116 + if (character === '\\') {
117 + escaped = true;
118 + continue;
119 + }
120 + if (character !== quote) continue;
121 +
122 + let separator = position + 1;
123 + while (separator < input.length && /\s/.test(input[separator])) separator += 1;
124 + return input[separator] === ':' || input[separator] === '=';
125 + }
126 + return false;
127 +};
128 +
129 +const redactBareQuotedValue = (input: string, pattern: RegExp, quote: '"' | "'"): string => {
130 + pattern.lastIndex = 0;
131 + const matches = Array.from(input.matchAll(pattern));
132 + if (matches.length === 0) return input;
133 +
134 + const activeQuotes = activeQuotesAt(input, new Set(matches.map(match => match.index)));
135 + const parts: string[] = [];
136 + let cursor = 0;
137 +
138 + for (const match of matches) {
139 + const offset = match.index;
140 + parts.push(input.slice(cursor, offset));
141 + if (
142 + activeQuotes.get(offset) === quote &&
143 + (completesQuotedFieldAt(input, offset + match[0].length, quote) ||
144 + CONTAINS_QUOTED_FIELD.test(match[3]))
145 + ) {
146 + parts.push(match[0]);
147 + } else {
148 + parts.push(`${match[1]}${match[2]}${quote}${REDACTED}${quote}`);
149 + }
150 + cursor = offset + match[0].length;
151 + }
152 +
153 + parts.push(input.slice(cursor));
154 + return parts.join('');
155 +};
156 +
157 /**
158 * Redact common secret shapes from a free-form string. Returns the input
159 * unchanged when it is empty or `undefined`.
@@ -41,5 +164,9 @@ export const redactSensitiveText = (input: string | undefined): string | undefin
164 for (const [pattern, replacement] of REDACTION_RULES) {
165 output = output.replace(pattern, replacement);
166 }
44 - return output;
167 + output = output.replace(BARE_KEY_ESCAPED_DOUBLE_QUOTED_VALUE, `$1$2\\"${REDACTED}\\"`);
168 + output = output.replace(BARE_KEY_ESCAPED_SINGLE_QUOTED_VALUE, `$1$2\\'${REDACTED}\\'`);
169 + output = redactBareQuotedValue(output, BARE_KEY_DOUBLE_QUOTED_VALUE, '"');
170 + output = redactBareQuotedValue(output, BARE_KEY_SINGLE_QUOTED_VALUE, "'");
171 + return output.replace(SECRET_PAIR_UNQUOTED, `$1$2${REDACTED}`);
172 };
ts/packages/core/src/utils/logger.ts
+6 -6
@@ -1,6 +1,6 @@
1 import pc from 'picocolors';
2 -import { getEnvVariable } from './env';
2 import { COMPOSIO_LOG_LEVEL } from './constants';
3 +import { redactSensitiveText } from '../telemetry/redact';
4
5 // Define log levels with corresponding priorities
6 export const LOG_LEVELS = {
@@ -28,7 +28,7 @@ interface LoggerOptions {
28 includeTimestamp?: boolean;
29 }
30
31 -class Logger {
31 +export class Logger {
32 private readonly level: LogLevel;
33 private readonly includeTimestamp: boolean;
34 private readonly console: Console;
@@ -57,12 +57,12 @@ class Logger {
57 })
58 .join('\n');
59
60 - if (!this.includeTimestamp) {
61 - return formattedArgs;
62 - }
60 + const redactedArgs = redactSensitiveText(formattedArgs) ?? formattedArgs;
61 +
62 + if (!this.includeTimestamp) return redactedArgs;
63
64 const timestamp = new Date().toISOString();
65 - return `${pc.gray(timestamp)} - ${formattedArgs}`;
65 + return `${pc.gray(timestamp)} - ${redactedArgs}`;
66 }
67
68 private shouldLog(level: LogLevel): boolean {
ts/packages/core/src/utils/sdk.ts
+1 -1
@@ -53,7 +53,7 @@ export function getSDKConfig(baseUrl?: string | null, apiKey?: string | null) {
53 ComposioError.handleAndThrow(new ComposioNoAPIKeyError());
54 }
55
56 - logger.debug('Environment', `API Key: ${apiKeyParsed}`);
56 + logger.debug('Environment', 'API Key: [REDACTED]');
57 logger.debug('Environment', `Base URL: ${baseURLParsed}`);
58
59 return { baseURL: baseURLParsed, apiKey: apiKeyParsed };
ts/packages/core/test/telemetry/redact.test.ts
+82
@@ -30,6 +30,7 @@ describe('redactSensitiveText', () => {
30 "client_secret: 'topsecret'",
31 'password=hunter2',
32 'access_token=ya29.a0Afoobar',
33 + 'auth=app_key:pusher_signature',
34 ]) {
35 const out = redactSensitiveText(sample)!;
36 expect(out, sample).toContain('[REDACTED]');
@@ -50,9 +51,14 @@ describe('redactSensitiveText', () => {
51 ['{"api_key":"ck_live_abc123"}', 'ck_live_abc123'],
52 ['{"api_key" : "ck_live_abc123"}', 'ck_live_abc123'],
53 ['{"refresh_token":"rt-abc.def-123"}', 'rt-abc.def-123'],
54 + ['{"auth":"app_key:pusher_signature"}', 'app_key:pusher_signature'],
55 + ['{"COMPOSIO_API_KEY":"prefixed-double-secret"}', 'prefixed-double-secret'],
56 + [`{'OPENAI_API_KEY': 'prefixed single secret'}`, 'prefixed single secret'],
57 ['{"x-api-key":"ck_hdr","user":"bob"}', 'ck_hdr'],
58 [`{'client_secret': 'cs_live_abc123'}`, 'cs_live_abc123'],
59 ['{"password": "hunter2"}', 'hunter2'],
60 + ['{"password": "correct horse battery staple"}', 'correct horse battery staple'],
61 + [`{'client_secret': 'multi word secret'}`, 'multi word secret'],
62 ] as const) {
63 const out = redactSensitiveText(sample)!;
64 expect(out, sample).toContain('[REDACTED]');
@@ -71,6 +77,44 @@ describe('redactSensitiveText', () => {
77
78 it('preserves JSON keys and quoting, replacing only the value', () => {
79 expect(redactSensitiveText('{"api_key": "ck_live_abc123"}')).toBe('{"api_key": "[REDACTED]"}');
80 + expect(redactSensitiveText('{"api_key":"secret","user":"bob"}')).toBe(
81 + '{"api_key":"[REDACTED]","user":"bob"}'
82 + );
83 + });
84 +
85 + it('redacts escaped quoted secrets in serialized messages', () => {
86 + const source = JSON.stringify({ error: 'password: "secret"' });
87 + const expected = JSON.stringify({ error: 'password: "[REDACTED]"' });
88 + expect(redactSensitiveText(source)).toBe(expected);
89 + expect(redactSensitiveText("password: \\'secret\\'")).toBe("password: \\'[REDACTED]\\'");
90 +
91 + const doubleEncoded = JSON.stringify({ body: JSON.stringify({ api_key: 'secret' }) });
92 + const doubleEncodedExpected = JSON.stringify({
93 + body: JSON.stringify({ api_key: '[REDACTED]' }),
94 + });
95 + expect(redactSensitiveText(doubleEncoded)).toBe(doubleEncodedExpected);
96 + });
97 +
98 + it('preserves serialized adjacent keys with escapes', () => {
99 + const source = JSON.stringify({
100 + error: 'unknown field auth:',
101 + 'quoted"key': 'safe',
102 + 'path\\key': 'safe',
103 + });
104 + expect(redactSensitiveText(source)).toBe(source);
105 + });
106 +
107 + it('redacts unquoted secrets containing backslashes', () => {
108 + expect(redactSensitiveText('password=abc\\def')).toBe('password=[REDACTED]');
109 + expect(redactSensitiveText('password=\\leading')).toBe('password=[REDACTED]');
110 + });
111 +
112 + it('redacts many quoted secrets in one pass', () => {
113 + const source = Array.from({ length: 1_000 }, (_, index) => `password: "secret-${index}"`).join(
114 + ' '
115 + );
116 + const expected = Array.from({ length: 1_000 }, () => 'password: "[REDACTED]"').join(' ');
117 + expect(redactSensitiveText(source)).toBe(expected);
118 });
119
120 it('leaves a key name with no attached value untouched', () => {
@@ -103,4 +147,42 @@ describe('redactSensitiveText', () => {
147 it('does not match a secret name embedded in letters', () => {
148 expect(redactSensitiveText('myapikey=sk_live_9f3c')).toBe('myapikey=sk_live_9f3c');
149 });
150 +
151 + it('does not consume adjacent fields after key-like text', () => {
152 + for (const benign of [
153 + '{"error": "unknown field auth:", "user": "bob"}',
154 + `{'note': 'unknown field auth:', "user": 'bob'}`,
155 + '{"error": "unknown field auth:", "$schema": "safe"}',
156 + '{"error": "unknown field auth:", "@type": "safe"}',
157 + '{"error": "unknown field auth:", "üser": "safe"}',
158 + '{"error": "unknown field auth:", ".well-known": "safe"}',
159 + '{"error": "unknown field auth:", ":type": "safe"}',
160 + '{"error": "unknown field auth:", "?field": "safe"}',
161 + ]) {
162 + expect(redactSensitiveText(benign), benign).toBe(benign);
163 + }
164 + });
165 +
166 + it('redacts bare quoted values before punctuation or prose', () => {
167 + for (const [sample, secret] of [
168 + ['password: "punctuated secret";', 'punctuated secret'],
169 + ["client_secret='period secret'.", 'period secret'],
170 + ['api_key = "prose secret" followed by context', 'prose secret'],
171 + ['password: "escaped \\"secret\\" value"', 'escaped \\"secret\\" value'],
172 + [`can't connect password: "secret"`, 'secret'],
173 + [`users' password: "secret"`, 'secret'],
174 + [`Chris' password: "secret"`, 'secret'],
175 + [`Andrés' password: "secret"`, 'secret'],
176 + [`{"error":"request failed password: 'secret'"}`, 'secret'],
177 + ['5" from target password: "secret"', 'secret'],
178 + ['unmatched " before password: "secret"', 'secret'],
179 + ["unmatched ' before password: 'secret'", 'secret'],
180 + ['password: "}abc"', '}abc'],
181 + ['api_key: ",abc"', ',abc'],
182 + ] as const) {
183 + const out = redactSensitiveText(sample)!;
184 + expect(out, sample).toContain('[REDACTED]');
185 + expect(out, sample).not.toContain(secret);
186 + }
187 + });
188 });
ts/packages/core/test/utils/logger.test.ts new
+27
@@ -0,0 +1,27 @@
1 +import { afterEach, describe, expect, it, vi } from 'vitest';
2 +import { Logger } from '../../src/utils/logger';
3 +
4 +describe('Logger credential redaction', () => {
5 + afterEach(() => vi.restoreAllMocks());
6 +
7 + it('redacts secret-shaped fields at the output boundary', () => {
8 + const debug = vi.spyOn(console, 'debug').mockImplementation(() => undefined);
9 + const logger = new Logger({ level: 'debug', includeTimestamp: false });
10 +
11 + logger.debug('credentials', {
12 + apiKey: 'uak_test_secret',
13 + auth: 'app_key:pusher_signature',
14 + password: 'correct horse battery staple',
15 + nested: { access_token: 'oauth_test_secret' },
16 + safe: 'visible',
17 + });
18 +
19 + const output = String(debug.mock.calls[0]?.[0]);
20 + expect(output).not.toContain('uak_test_secret');
21 + expect(output).not.toContain('pusher_signature');
22 + expect(output).not.toContain('correct horse battery staple');
23 + expect(output).not.toContain('oauth_test_secret');
24 + expect(output).toContain('[REDACTED]');
25 + expect(output).toContain('visible');
26 + });
27 +});