-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathchildProcessEnv.ts
More file actions
63 lines (52 loc) · 1.97 KB
/
Copy pathchildProcessEnv.ts
File metadata and controls
63 lines (52 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as path from "node:path";
import { getMuxHome } from "@/common/constants/paths";
const CHILD_ENV_KEYS_TO_STRIP = [
"AGENT_BROWSER_SESSION",
"AGENT_BROWSER_STREAM_PORT",
"MUX_VENDORED_BIN_DIR",
// Linux desktop identity (app_id source). Electron sets it in our process env
// (from package.json desktopName, or main.ts for launch modes without a
// package.json). Chromium/Electron apps launched from a mux terminal would
// inherit it and group under mux's taskbar entry.
"CHROME_DESKTOP",
] as const;
function normalizePathEntry(entry: string): string {
const resolved = path.resolve(entry);
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
}
function getMuxVendoredBinDirs(env: NodeJS.ProcessEnv): string[] {
const candidates = [env.MUX_VENDORED_BIN_DIR, path.join(getMuxHome(), "bin")];
return candidates
.filter((value): value is string => typeof value === "string" && value.trim().length > 0)
.map((value) => normalizePathEntry(value.trim()));
}
export function sanitizeMuxChildPath(
pathValue: string | undefined,
env: NodeJS.ProcessEnv
): string | undefined {
if (pathValue == null) {
return pathValue;
}
const vendoredBinDirs = getMuxVendoredBinDirs(env);
if (vendoredBinDirs.length === 0) {
return pathValue;
}
const sanitizedEntries = pathValue
.split(path.delimiter)
.map((entry) => entry.trim())
.filter((entry) => entry.length > 0)
.filter((entry) => !vendoredBinDirs.includes(normalizePathEntry(entry)));
return sanitizedEntries.join(path.delimiter);
}
export function sanitizeMuxChildEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
const sanitizedEnv: NodeJS.ProcessEnv = { ...env };
const sanitizedPath = sanitizeMuxChildPath(env.PATH ?? env.Path, env);
for (const key of CHILD_ENV_KEYS_TO_STRIP) {
delete sanitizedEnv[key];
}
if (sanitizedPath !== undefined) {
sanitizedEnv.PATH = sanitizedPath;
sanitizedEnv.Path = sanitizedPath;
}
return sanitizedEnv;
}