Open History to see the last commit
1const APPIMAGE_RUNTIME_ENV_KEYS = ["APPIMAGE", "APPDIR", "ARGV0", "OWD"] as const;
2// ld.so also splits LD_LIBRARY_PATH on semicolons.
3const APPIMAGE_PATH_LIKE_ENV_SEPARATORS = {
4 PATH: /:/,
5 LD_LIBRARY_PATH: /[:;]/,
6 XDG_DATA_DIRS: /:/,
7 GSETTINGS_SCHEMA_DIR: /:/,
8} as const;
9
10function isPathSegmentUnderAppDir(segment: string, appDir: string): boolean {
11 return segment === appDir || segment.startsWith(`${appDir}/`);
12}
13
14// AppImage mount paths would make child commands resolve against the mount instead of the user's system.
15export function stripAppImageRuntimeEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
16 if (env.APPIMAGE === undefined && env.APPDIR === undefined) return env;
17
18 const scrubbed: NodeJS.ProcessEnv = { ...env };
19 for (const key of APPIMAGE_RUNTIME_ENV_KEYS) {
20 delete scrubbed[key];
21 }
22
23 const appDir = env.APPDIR?.replace(/\/+$/, "");
24 if (appDir) {
25 for (const [key, separator] of Object.entries(APPIMAGE_PATH_LIKE_ENV_SEPARATORS)) {
26 const value = scrubbed[key];
27 if (value === undefined) continue;
28 const segments = value.split(separator);
29 const kept = segments.filter((segment) => !isPathSegmentUnderAppDir(segment, appDir));
30 if (kept.length === segments.length) continue;
31 if (kept.length === 0) {
32 delete scrubbed[key];
33 } else {
34 // Empty entries mean the current directory; a lone one must stay ":" since "" means none.
35 scrubbed[key] = kept.length === 1 && kept[0] === "" ? ":" : kept.join(":");
36 }
37 }
38 }
39
40 return scrubbed;
41}