-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inherit environment variables deemed safe by default #36
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { ChildProcess, spawn } from "node:child_process"; | ||
import process from "node:process"; | ||
import { ReadBuffer, serializeMessage } from "../shared/stdio.js"; | ||
import { JSONRPCMessage } from "../types.js"; | ||
import { Transport } from "../shared/transport.js"; | ||
|
@@ -17,11 +18,55 @@ export type StdioServerParameters = { | |
/** | ||
* The environment to use when spawning the process. | ||
* | ||
* The environment is NOT inherited from the parent process by default. | ||
* If not specified, the result of getDefaultEnvironment() will be used. | ||
*/ | ||
env?: object; | ||
env?: Record<string, string>; | ||
}; | ||
|
||
/** | ||
* Environment variables to inherit by default, if an environment is not explicitly given. | ||
*/ | ||
export const DEFAULT_INHERITED_ENV_VARS = | ||
process.platform === "win32" | ||
? [ | ||
"APPDATA", | ||
"HOMEDRIVE", | ||
"HOMEPATH", | ||
"LOCALAPPDATA", | ||
"PATH", | ||
"PROCESSOR_ARCHITECTURE", | ||
"SYSTEMDRIVE", | ||
"SYSTEMROOT", | ||
"TEMP", | ||
"USERNAME", | ||
"USERPROFILE", | ||
] | ||
: /* list inspired by the default env inheritance of sudo */ | ||
["HOME", "LOGNAME", "PATH", "SHELL", "TERM", "USER"]; | ||
|
||
/** | ||
* Returns a default environment object including only environment variables deemed safe to inherit. | ||
*/ | ||
export function getDefaultEnvironment(): Record<string, string> { | ||
const env: Record<string, string> = {}; | ||
|
||
for (const key of DEFAULT_INHERITED_ENV_VARS) { | ||
const value = process.env[key]; | ||
if (value === undefined) { | ||
continue; | ||
} | ||
|
||
if (value.startsWith("()")) { | ||
// Skip functions, which are a security risk. | ||
continue; | ||
} | ||
|
||
env[key] = value; | ||
} | ||
|
||
return env; | ||
} | ||
|
||
/** | ||
* Client transport for stdio: this will connect to a server by spawning a process and communicating with it over stdin/stdout. | ||
* | ||
|
@@ -56,11 +101,7 @@ export class StdioClientTransport implements Transport { | |
this._serverParams.command, | ||
this._serverParams.args ?? [], | ||
{ | ||
// The parent process may have sensitive secrets in its env, so don't inherit it automatically. | ||
env: | ||
this._serverParams.env === undefined | ||
? {} | ||
: { ...this._serverParams.env }, | ||
env: this._serverParams.env ?? getDefaultEnvironment(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to merge this? something like: env: { ...this._serverParams.env, ...getDefaultEnvironment() } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, because then there's no way to clobber the default environment. Even if we flipped the order, it'd be very annoying to unset the default keys. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figure it's easy enough for callers to build their env on |
||
stdio: ["pipe", "pipe", "inherit"], | ||
signal: this._abortController.signal, | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of these are unnecessary, I'd nuke:
ALLUSERSPROFILE, NUMBER_OF_PROCESSORS, OS, PATHEXT, TMP, WINDIR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will definitely defer to you, but here was my reasoning:
NUMBER_OF_PROCESSORS
: Maybe needed to set reasonable concurrency defaults (e.g., for thread pools)?OS
: Maybe needed for compatibility checks?TMP
: Seemed like the same thing asTEMP
, maybe applications incorrectly use one or the otherThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OS is basically a legacy constant, it's always
Windows_NT
, and TMP is also like, a legacy env var that shouldn't exist. NUMBER_OF_PROCESSORS you can probably keep but usually people use a platform API likeos.cpus().length
on node