|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (c) 2024-2026 Red Hat, Inc. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + ***********************************************************************/ |
| 10 | +/* eslint-disable header/header */ |
| 11 | + |
| 12 | +import { delimiter } from '../../base/common/path.js'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Merges the provided values. The first parameter is taken as the basis. |
| 16 | + * Items from the second parameter are appended to the main value, duplicates are filtered out. |
| 17 | + * For example: |
| 18 | + * - the first parameter is: "/checode/checode-linux-libc/ubi9/bin/remote-cli:/usr/local/bin:/usr/bin" |
| 19 | + * - the second parameter is: "/go/bin:/usr/bin" |
| 20 | + * - the function returns: "/checode/checode-linux-libc/ubi9/bin/remote-cli:/usr/local/bin:/usr/bin:/go/bin", |
| 21 | + * - note: "/usr/bin" is filtered out to avoud duplicates in the returned value |
| 22 | + * |
| 23 | + * @param currentPath current value of the PATH env variable |
| 24 | + * @param processEnvPath value of the process.env.PATH env variable |
| 25 | + * @returns |
| 26 | + * - merged value for the given parameters |
| 27 | + * - currentPath if processEnvPath is not provided (undefined or empty string) |
| 28 | + */ |
| 29 | +export function getResolvedPathEnvVar(currentPath: string, processEnvPath?: string): string { |
| 30 | + if (processEnvPath) { |
| 31 | + const currentPathArray: string[] = currentPath.split(delimiter); |
| 32 | + const processEnvPathArray: string[] = processEnvPath.split(delimiter); |
| 33 | + const processPathUniqueItems = processEnvPathArray.filter(path => !currentPathArray.includes(path)); |
| 34 | + return processPathUniqueItems.length > 0 ? currentPath + delimiter + processPathUniqueItems.join(delimiter) : currentPath; |
| 35 | + } |
| 36 | + return currentPath; |
| 37 | +} |
| 38 | + |
| 39 | +/* |
| 40 | + * The following logic was generated using AI assistance (Cursor AI) |
| 41 | + * and reviewed by the maintainers. |
| 42 | + */ |
| 43 | +export type LdSanitizeScope = 'all' | 'none' | 'shellEnv' | 'terminal'; |
| 44 | + |
| 45 | +const allLdLibPrefixes = new Set<string>([ |
| 46 | + '/checode/checode-linux-libc/ubi8/ld_libs', |
| 47 | + '/checode/checode-linux-libc/ubi9/ld_libs/core', |
| 48 | + '/checode/checode-linux-libc/ubi9/ld_libs/openssl' |
| 49 | +]); |
| 50 | + |
| 51 | +export function getLdSanitizeScope(): LdSanitizeScope { |
| 52 | + const scope = process.env['LD_SANITIZE_SCOPE']; |
| 53 | + if (scope === 'all' || scope === 'none' || scope === 'shellEnv' || scope === 'terminal') { |
| 54 | + return scope; |
| 55 | + } |
| 56 | + return 'terminal'; |
| 57 | +} |
| 58 | + |
| 59 | +export function shouldStripLdLibraryPathForShellEnv(): boolean { |
| 60 | + const scope = getLdSanitizeScope(); |
| 61 | + return scope === 'all' || scope === 'shellEnv'; |
| 62 | +} |
| 63 | + |
| 64 | +export function shouldStripLdLibraryPathForTerminal(): boolean { |
| 65 | + const scope = getLdSanitizeScope(); |
| 66 | + return scope === 'all' || scope === 'terminal'; |
| 67 | +} |
| 68 | + |
| 69 | +export function stripLdLibraryPath(value: string | undefined): string | undefined { |
| 70 | + if (!value) { |
| 71 | + return undefined; |
| 72 | + } |
| 73 | + |
| 74 | + const filtered = value |
| 75 | + .split(':') |
| 76 | + .map(entry => entry.trim()) |
| 77 | + .filter(entry => entry.length > 0 && !allLdLibPrefixes.has(entry)); |
| 78 | + |
| 79 | + return filtered.length > 0 ? filtered.join(':') : undefined; |
| 80 | +} |
| 81 | + |
| 82 | +export function sanitizeLdLibraryPathInEnvironment( |
| 83 | + environment: NodeJS.ProcessEnv, |
| 84 | + logger?: (message: string) => void, |
| 85 | + source = 'unknown' |
| 86 | +): void { |
| 87 | + const before = environment['LD_LIBRARY_PATH']; |
| 88 | + const sanitizedLdLibraryPath = stripLdLibraryPath(environment['LD_LIBRARY_PATH']); |
| 89 | + if (sanitizedLdLibraryPath) { |
| 90 | + environment['LD_LIBRARY_PATH'] = sanitizedLdLibraryPath; |
| 91 | + } else { |
| 92 | + delete environment['LD_LIBRARY_PATH']; |
| 93 | + } |
| 94 | + logger?.(`[che-code][ld-sanitize][${source}] scope=${getLdSanitizeScope()} before=${before ?? '<unset>'} after=${environment['LD_LIBRARY_PATH'] ?? '<unset>'}`); |
| 95 | +} |
0 commit comments