src/vs/platform/sandbox/common/terminalSandboxCommandRules.ts
67 LOC · 41 covered · 26 uncovered · 5 ranges · 1052 concepts · 2 introducers · 489 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.
/*---------------------------------------------------------------------------------------------
terminalSandboxEngine.ts ×71
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { OperatingSystem } from '../../../base/common/platform.js';
import type { ITerminalSandboxCommand } from './terminalSandboxService.js';
export interface ITerminalSandboxCommandRuleContext {
readonly os: OperatingSystem;
}
export interface ITerminalSandboxCommandRule<T> {
readonly keywords: readonly string[];
readonly value: T;
readonly subcommands?: readonly string[];
readonly optionsWithValue?: ReadonlySet<string>;
/** Context-wide guard for rules, such as OS-specific sandbox capabilities. */
readonly condition?: (context: ITerminalSandboxCommandRuleContext) => boolean;
/** Command-specific guard for argument-sensitive rules. */
readonly when?: (command: ITerminalSandboxCommand) => boolean;
}
export function matchesTerminalSandboxCommandRule<T>(command: ITerminalSandboxCommand, rule: ITerminalSandboxCommandRule<T>, context?: ITerminalSandboxCommandRuleContext): boolean {
if (!rule.keywords.includes(command.keyword.toLowerCase())) {
terminalSandboxReadAllowList.ts ×15
return false;
}
if (rule.condition && (!context || !rule.condition(context))) {
return false;
}
const subcommand = getCommandSubcommand(command.args, rule.optionsWithValue);
if (subcommand === undefined || !rule.subcommands.includes(subcommand)) {
return false;
}
}
}
/**
* Returns the first non-option argument, treating it as the command's subcommand.
* Options are skipped, and options listed in `optionsWithValue` also skip the
* following argument so global option values are not mistaken for subcommands.
*
* For example, with `-C` in `optionsWithValue`, `git -C repo commit` returns
* `commit` instead of `repo`.
*/
export function getCommandSubcommand(args: readonly string[], optionsWithValue?: ReadonlySet<string>): string | undefined {
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--') {
return undefined;
}
if (arg.startsWith('-')) {
const option = arg.includes('=') ? arg.substring(0, arg.indexOf('=')) : arg;
if (!arg.includes('=') && optionsWithValue?.has(option)) {
i++;
}
continue;
}
return arg.toLowerCase();
}
return undefined;
}