Open History to see the last commit
1/**
2 * Single words that on their own signal "this is a credential".
3 * Matched after splitting a parameter name into camelCase/snake/kebab words.
4 * So `accessToken` → `['access', 'token']` → matches `token`,
5 * but `tokenizer` → `['tokenizer']` → does NOT match `token`.
6 */
7const DANGEROUS_WORDS = new Set<string>([
8 'password',
9 'passwd',
10 'pwd',
11 'passcode',
12 'secret',
13 'token',
14 'credential',
15 'credentials',
16 'authorization',
17 'jwt',
18 'bearer',
19])
20
21/**
22 * Compound patterns matched as substrings of the normalized (lowercased,
23 * separator-stripped) parameter name. Catches forms like `openai_api_key`,
24 * `x-api-key`, and `webhookSecret`.
25 */
26const COMPOUND_PATTERNS = [
27 'apikey',
28 'accesskey',
29 'authkey',
30 'privatekey',
31 'clientsecret',
32 'webhooksecret',
33] as const
34
35export interface SecretParameterInfo {
36 toolName: string
37 paramName: string
38 paramPath: Array<string>
39}
40
41export type SecretParameterHandler =
42 | 'warn'
43 | 'throw'
44 | 'ignore'
45 | ((info: SecretParameterInfo) => void)
46
47interface ToolLike {
48 name: string
49 inputSchema?: Record<string, unknown>
50}
51
52interface JsonSchemaLike {
53 type?: string
54 properties?: Record<string, JsonSchemaLike>
55 items?: JsonSchemaLike | Array<JsonSchemaLike>
56 anyOf?: Array<JsonSchemaLike>
57 oneOf?: Array<JsonSchemaLike>
58 allOf?: Array<JsonSchemaLike>
59 additionalProperties?: boolean | JsonSchemaLike
60 $ref?: string
61 $defs?: Record<string, JsonSchemaLike>
62 definitions?: Record<string, JsonSchemaLike>
63}
64
65function splitIntoWords(name: string): Array<string> {
66 return name
67 .replace(/[_\-\s]+/g, ' ')
68 .replace(/([a-z0-9])([A-Z])/g, '$1 $2')
69 .toLowerCase()
70 .split(/\s+/)
71 .filter(Boolean)
72}
73
74function looksLikeSecret(name: string): boolean {
75 const words = splitIntoWords(name)
76 if (words.some((w) => DANGEROUS_WORDS.has(w))) return true
77 const normalized = name.replace(/[_\-\s]/g, '').toLowerCase()
78 return COMPOUND_PATTERNS.some((p) => normalized.includes(p))
79}
80
81function resolveRef(
82 ref: string,
83 root: JsonSchemaLike,
84): JsonSchemaLike | undefined {
85 const match = ref.match(/^#\/(\$defs|definitions)\/(.+)$/)
86 if (!match) return undefined
87 const bucket = match[1] as '$defs' | 'definitions'
88 const key = match[2]
89 return key === undefined ? undefined : root[bucket]?.[key]
90}
91
92function findSecretParams(
93 schema: JsonSchemaLike | undefined,
94 root: JsonSchemaLike,
95 seen: Set<object>,
96 path: Array<string>,
97 found: Array<{ path: Array<string>; name: string }>,
98): void {
99 if (!schema || typeof schema !== 'object' || seen.has(schema)) return
100 seen.add(schema)
101
102 if (schema.properties && typeof schema.properties === 'object') {
103 for (const [paramName, sub] of Object.entries(schema.properties)) {
104 if (looksLikeSecret(paramName)) {
105 found.push({ path: [...path, paramName], name: paramName })
106 }
107 findSecretParams(sub, root, seen, [...path, paramName], found)
108 }
109 }
110
111 if (Array.isArray(schema.items)) {
112 schema.items.forEach((s, i) =>
113 findSecretParams(s, root, seen, [...path, `[${i}]`], found),
114 )
115 } else if (schema.items && typeof schema.items === 'object') {
116 findSecretParams(schema.items, root, seen, [...path, '[]'], found)
117 }
118
119 if (
120 schema.additionalProperties &&
121 typeof schema.additionalProperties === 'object'
122 ) {
123 findSecretParams(schema.additionalProperties, root, seen, path, found)
124 }
125
126 for (const key of ['anyOf', 'oneOf', 'allOf'] as const) {
127 const arr = schema[key]
128 if (Array.isArray(arr)) {
129 arr.forEach((s) => findSecretParams(s, root, seen, path, found))
130 }
131 }
132
133 if (typeof schema.$ref === 'string') {
134 const target = resolveRef(schema.$ref, root)
135 if (target) findSecretParams(target, root, seen, path, found)
136 }
137}
138
139function buildMessage(toolName: string, paramPath: Array<string>): string {
140 return (
141 `[TanStack AI Code Mode] Tool "${toolName}" has parameter "${paramPath.join('.')}" ` +
142 `that looks like a secret. Code Mode executes LLM-generated code — any ` +
143 `value passed through this parameter is accessible to generated code and ` +
144 `could be exfiltrated. Keep secrets in your server-side tool implementation ` +
145 `instead of passing them as tool parameters.`
146 )
147}
148
149/**
150 * Scan tool input schemas for parameter names that look like secrets.
151 * Emits a warning (or invokes the configured handler) for each match.
152 *
153 * Recurses into nested object properties, array items, union branches
154 * (anyOf/oneOf/allOf), additionalProperties, and `$ref` targets that
155 * resolve within the same schema's `$defs`/`definitions`.
156 *
157 * Best-effort heuristic, not a security boundary.
158 */
159export function warnIfBindingsExposeSecrets(
160 tools: Array<ToolLike>,
161 options: {
162 handler?: SecretParameterHandler
163 dedupCache?: Set<string>
164 } = {},
165): void {
166 const { handler = 'warn', dedupCache } = options
167 if (handler === 'ignore') return
168
169 for (const tool of tools) {
170 const schema = tool.inputSchema as JsonSchemaLike | undefined
171 if (!schema) continue
172
173 const found: Array<{ path: Array<string>; name: string }> = []
174 findSecretParams(schema, schema, new Set(), [], found)
175
176 for (const entry of found) {
177 const dedupKey = `${tool.name}::${entry.path.join('.')}`
178 if (dedupCache) {
179 if (dedupCache.has(dedupKey)) continue
180 dedupCache.add(dedupKey)
181 }
182
183 const info: SecretParameterInfo = {
184 toolName: tool.name,
185 paramName: entry.name,
186 paramPath: entry.path,
187 }
188
189 if (typeof handler === 'function') {
190 handler(info)
191 } else if (handler === 'throw') {
192 throw new Error(buildMessage(tool.name, entry.path))
193 } else {
194 console.warn(buildMessage(tool.name, entry.path))
195 }
196 }
197 }
198}