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

next
ts 172 lines 6.58 KB
Raw
1 /**
2 * Best-effort redaction of secrets from free-form error text before it is sent
3 * to the telemetry endpoint.
4 *
5 * Error `message` and `stack` fields routinely interpolate URLs (with
6 * query-string tokens / presigned signatures), `Authorization` headers, API
7 * keys, and connected-account identifiers. None of that should leave the
8 * process. Structured telemetry fields never carry raw secrets, so only the two
9 * free-form strings are passed through here.
10 *
11 * This is defence-in-depth, not a proof: it targets the shapes we have seen
12 * leak. When in doubt it over-redacts rather than under-redacts.
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.
46 [/(\bhttps?:\/\/[^\s?#'"]+)\?[^\s'"]*/gi, `$1?${REDACTED}`],
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.
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 [
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`.
160 */
161 export const redactSensitiveText = (input: string | undefined): string | undefined => {
162 if (!input) return input;
163 let output = input;
164 for (const [pattern, replacement] of REDACTION_RULES) {
165 output = output.replace(pattern, replacement);
166 }
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 };