src/vs/platform/terminal/common/terminalEnvironment.ts
128 LOC · 122 covered · 6 uncovered · 31 ranges · 73 concepts · 18 introducers · 57 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
terminalEnvironment.ts ×4
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { OperatingSystem, OS } from '../../../base/common/platform.js';
import { IShellLaunchConfig, TerminalShellType, PosixShellType, WindowsShellType, GeneralShellType } from './terminal.js';
/**
* Aggressively escape non-windows paths to prepare for being sent to a shell. This will do some
* escaping inaccurately to be careful about possible script injection via the file path. For
* example, we're trying to prevent this sort of attack: `/foo/file$(echo evil)`.
*/
export function escapeNonWindowsPath(path: string, shellType?: TerminalShellType): string {
if (newPath.includes('\\')) {
newPath = newPath.replace(/\\/g, '\\\\');
}
// Define shell-specific escaping rules
interface ShellEscapeConfig {
// How to handle paths with both single and double quotes
bothQuotes: (path: string) => string;
// How to handle paths with only single quotes
singleQuotes: (path: string) => string;
// How to handle paths with no single quotes (may have double quotes)
noSingleQuotes: (path: string) => string;
}
let escapeConfig: ShellEscapeConfig;
switch (shellType) {
case PosixShellType.Bash:
case PosixShellType.Sh:
case PosixShellType.Zsh:
case WindowsShellType.GitBash:
bothQuotes: (path) => `$'${path.replace(/'/g, '\\\'')}'`,
singleQuotes: (path) => `'${path.replace(/'/g, '\\\'')}'`,
noSingleQuotes: (path) => `'${path}'`
};
break;
bothQuotes: (path) => `"${path.replace(/"/g, '\\"')}"`,
singleQuotes: (path) => `'${path.replace(/'/g, '\\\'')}'`,
noSingleQuotes: (path) => `'${path}'`
};
break;
// but if we get here, use PowerShell escaping
escapeConfig = {
bothQuotes: (path) => `"${path.replace(/"/g, '`"')}"`,
singleQuotes: (path) => `'${path.replace(/'/g, '\'\'')}'`,
noSingleQuotes: (path) => `'${path}'`
};
break;
escapeConfig = {
bothQuotes: (path) => `$'${path.replace(/'/g, '\\\'')}'`,
singleQuotes: (path) => `'${path.replace(/'/g, '\\\'')}'`,
noSingleQuotes: (path) => `'${path}'`
};
break;
// Remove dangerous characters except single and double quotes, which we'll escape properly
const bannedChars = /[\`\$\|\&\>\~\#\!\^\*\;\<]/g;
newPath = newPath.replace(bannedChars, '');
// Apply shell-specific escaping based on quote content
if (newPath.includes('\'') && newPath.includes('"')) {
return escapeConfig.noSingleQuotes(newPath);
}
}
/**
* Collapses the user's home directory into `~` if it exists within the path, this gives a shorter
* path that is more suitable within the context of a terminal.
*/
export function collapseTildePath(path: string | undefined, userHome: string | undefined, separator: string): string {
}
}
if (userHome.match(/[\/\\]$/)) {
}
const normalizedUserHome = userHome.replace(/\\/g, '/').toLowerCase();
if (!normalizedPath.includes(normalizedUserHome)) {
}
}
/**
* Sanitizes a cwd string, removing any wrapping quotes and making the Windows drive letter
* uppercase.
* @param cwd The directory to sanitize.
*/
export function sanitizeCwd(cwd: string): string {
if (cwd.match(/^['"].*['"]$/)) {
}
if (OS === OperatingSystem.Windows && cwd && cwd[1] === ':') {
return cwd[0].toUpperCase() + cwd.substring(1);
}
}
/**
* Determines whether the given shell launch config should use the environment variable collection.
* @param slc The shell launch config to check.
*/
export function shouldUseEnvironmentVariableCollection(slc: IShellLaunchConfig): boolean {
return !slc.strictEnv;
}