63 lines
No EOL
2.4 KiB
JavaScript
63 lines
No EOL
2.4 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Sanitized environment builder for Claude Code child processes.
|
|
*
|
|
* Removes secrets from the inherited process environment before passing it to
|
|
* spawned subprocesses, so an agent cannot exfiltrate credentials via env vars.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.sanitizedEnv = sanitizedEnv;
|
|
/** Keys that are always removed from the child environment. */
|
|
const STATIC_BLOCKLIST = new Set([
|
|
"DISCORD_BOT_TOKEN",
|
|
"DISCORD_CLIENT_SECRET",
|
|
"DISCORD_PUBLIC_KEY",
|
|
]);
|
|
/** Pattern for keys that are removed regardless of exact name. */
|
|
const PATTERN_BLOCKLIST = /^(DISCLAW_SECRET_|SECRET_|TOKEN_|API_KEY_?)/i;
|
|
/**
|
|
* Returns a sanitized copy of `process.env` suitable for passing to a spawned
|
|
* Claude Code child process.
|
|
*
|
|
* - Removes all keys in the static blocklist.
|
|
* - Removes all keys matching the pattern blocklist.
|
|
* - Sets `CI=true` and `DISCLAW_AGENT=1` as runtime markers.
|
|
* - Merges `extra` last so callers can inject agent-specific variables
|
|
* (e.g. `DISCLAW_AGENT_NAME`) and override any of the defaults.
|
|
* - Removes any keys listed in `additionalBlocklist` (from `disclaw.yaml`
|
|
* `env_blocklist` field) after applying `extra`.
|
|
*
|
|
* This function is pure: it never mutates `process.env`.
|
|
*/
|
|
function sanitizedEnv(extra, additionalBlocklist) {
|
|
// Shallow copy — we only work with string/undefined values from process.env
|
|
const result = { ...process.env };
|
|
// Apply static blocklist
|
|
for (const key of STATIC_BLOCKLIST) {
|
|
delete result[key];
|
|
}
|
|
// Apply pattern blocklist
|
|
for (const key of Object.keys(result)) {
|
|
if (PATTERN_BLOCKLIST.test(key)) {
|
|
delete result[key];
|
|
}
|
|
}
|
|
// Mark as DisClaw agent — do NOT set CI=true: Claude Code interprets CI=true
|
|
// as a headless CI environment requiring ANTHROPIC_API_KEY, which breaks
|
|
// the Pro/Max subscription OAuth login stored in ~/.claude/.
|
|
result["DISCLAW_AGENT"] = "1";
|
|
// Merge caller-supplied extras (may override the defaults above)
|
|
if (extra) {
|
|
for (const [key, value] of Object.entries(extra)) {
|
|
result[key] = value;
|
|
}
|
|
}
|
|
// Apply additional blocklist from disclaw.yaml env_blocklist
|
|
if (additionalBlocklist) {
|
|
for (const key of additionalBlocklist) {
|
|
delete result[key];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
//# sourceMappingURL=env.js.map
|