(betaHeaders?: string[])
| 274 | * @returns A JSON object representing the extra body parameters. |
| 275 | */ |
| 276 | export function getExtraBodyParams(betaHeaders?: string[]): JsonObject { |
| 277 | // Parse user's extra body parameters first |
| 278 | const extraBodyStr = process.env.CLAUDE_CODE_EXTRA_BODY |
| 279 | let result: JsonObject = {} |
| 280 | |
| 281 | if (extraBodyStr) { |
| 282 | try { |
| 283 | // Parse as JSON, which can be null, boolean, number, string, array or object |
| 284 | const parsed = safeParseJSON(extraBodyStr) |
| 285 | // We expect an object with key-value pairs to spread into API parameters |
| 286 | if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { |
| 287 | // Shallow clone — safeParseJSON is LRU-cached and returns the same |
| 288 | // object reference for the same string. Mutating `result` below |
| 289 | // would poison the cache, causing stale values to persist. |
| 290 | result = { ...(parsed as JsonObject) } |
| 291 | } else { |
| 292 | logForDebugging( |
| 293 | `CLAUDE_CODE_EXTRA_BODY env var must be a JSON object, but was given ${extraBodyStr}`, |
| 294 | { level: 'error' }, |
| 295 | ) |
| 296 | } |
| 297 | } catch (error) { |
| 298 | logForDebugging( |
| 299 | `Error parsing CLAUDE_CODE_EXTRA_BODY: ${errorMessage(error)}`, |
| 300 | { level: 'error' }, |
| 301 | ) |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Anti-distillation: send fake_tools opt-in for 1P CLI only |
| 306 | if ( |
| 307 | feature('ANTI_DISTILLATION_CC') |
| 308 | ? process.env.CLAUDE_CODE_ENTRYPOINT === 'cli' && |
| 309 | shouldIncludeFirstPartyOnlyBetas() && |
| 310 | getFeatureValue_CACHED_MAY_BE_STALE( |
| 311 | 'ncode_prompt_injection_fake_tool_guard', |
| 312 | false, |
| 313 | ) |
| 314 | : false |
| 315 | ) { |
| 316 | result.anti_distillation = ['fake_tools'] |
| 317 | } |
| 318 | |
| 319 | // Handle beta headers if provided |
| 320 | if (betaHeaders && betaHeaders.length > 0) { |
| 321 | if (result.anthropic_beta && Array.isArray(result.anthropic_beta)) { |
| 322 | // Add to existing array, avoiding duplicates |
| 323 | const existingHeaders = result.anthropic_beta as string[] |
| 324 | const newHeaders = betaHeaders.filter( |
| 325 | header => !existingHeaders.includes(header), |
| 326 | ) |
| 327 | result.anthropic_beta = [...existingHeaders, ...newHeaders] |
| 328 | } else { |
| 329 | // Create new array with the beta headers |
| 330 | result.anthropic_beta = betaHeaders |
| 331 | } |
| 332 | } |
| 333 |
no test coverage detected