Chat: apply network filter when agent sandbox is enabled#310355
Chat: apply network filter when agent sandbox is enabled#310355dileepyavan wants to merge 4 commits intomicrosoft:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aligns agent tool safety behavior with chat agent sandboxing by (1) activating network domain filtering whenever sandboxing is enabled and (2) ensuring terminal command auto-approval isn’t implicitly forced when auto-approve is disabled. It also centralizes sandbox setting IDs and adds unit coverage for the updated behaviors.
Changes:
- Activate
AgentNetworkFilterServicedomain filtering whenchat.agent.sandbox.enabled(or deprecatedchat.agent.sandbox) is enabled, even ifchat.agent.networkFilteris disabled. - Update
CommandLineSandboxAnalyzerto respectchat.tools.terminal.enableAutoApprovewhen deciding whether to force auto-approval. - Introduce shared sandbox setting IDs in
src/vs/platform/sandbox/commonand expand unit tests for network filtering and terminal sandbox analyzer behavior.
Show a summary per file
| File | Description |
|---|---|
| src/vs/workbench/contrib/terminalContrib/chatAgentTools/test/browser/commandLineSandboxAnalyzer.test.ts | Adds unit tests for sandbox analyzer auto-approval behavior. |
| src/vs/workbench/contrib/terminalContrib/chatAgentTools/common/terminalChatAgentToolsConfiguration.ts | Switches sandbox setting IDs to shared AgentSandboxSettingId constants. |
| src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/tools/commandLineAnalyzer/commandLineSandboxAnalyzer.ts | Updates sandbox analyzer to consult EnableAutoApprove before forcing auto-approval. |
| src/vs/workbench/contrib/chat/browser/chat.contribution.ts | Updates setting descriptions to reflect sandbox-triggered domain filtering behavior. |
| src/vs/platform/sandbox/common/settings.ts | Adds shared AgentSandboxSettingId setting ID constants. |
| src/vs/platform/networkFilter/test/common/networkFilterService.test.ts | Adds unit tests for sandbox-triggered filtering and configuration-change behavior. |
| src/vs/platform/networkFilter/common/networkFilterService.ts | Enables network filtering when sandbox settings indicate sandboxing is enabled. |
Copilot's findings
- Files reviewed: 7/7 changed files
- Comments generated: 4
| } | ||
|
|
||
| private _isAutoApproveEnabled(): boolean { | ||
| return this._configurationService.getValue(TerminalChatAgentToolsSettingId.EnableAutoApprove) !== false; |
There was a problem hiding this comment.
_isAutoApproveEnabled uses an untyped getValue(...) !== false check, which will treat any non-false value (including invalid types like 'true'/null) as enabled and is inconsistent with other call sites that use getValue<boolean>(...) === true. Consider reading a boolean (getValue<boolean>(...)) and explicitly falling back to the setting default (currently true) to avoid accidentally enabling auto-approve on malformed configuration values.
| return this._configurationService.getValue(TerminalChatAgentToolsSettingId.EnableAutoApprove) !== false; | |
| const isAutoApproveEnabled = this._configurationService.getValue<boolean>(TerminalChatAgentToolsSettingId.EnableAutoApprove); | |
| return isAutoApproveEnabled ?? true; |
| async analyze(_options: ICommandLineAnalyzerOptions): Promise<ICommandLineAnalyzerResult> { | ||
| const isAutoApproveEnabled = this._isAutoApproveEnabled(); |
There was a problem hiding this comment.
analyze takes _options but the parameter is actually used. Dropping the leading underscore would better match the convention of using _ only for intentionally-unused parameters.
| function setConfig(key: string, value: unknown) { | ||
| configurationService.setUserConfiguration(key, value); | ||
| configurationService.onDidChangeConfigurationEmitter.fire({ | ||
| affectsConfiguration: () => true, | ||
| affectedKeys: new Set([key]), | ||
| source: ConfigurationTarget.USER, | ||
| change: null!, | ||
| }); |
There was a problem hiding this comment.
The helper setConfig manually fires onDidChangeConfigurationEmitter with affectsConfiguration: () => true and change: null!. Because workbenchInstantiationService wires up many services that may listen to configuration changes, this broad event (and null change) can make the test brittle or cause unrelated listeners to throw. Consider either not firing an event at all (the analyzer reads the config on-demand), or firing a well-formed change event scoped to the specific key (like other tests in the repo) with a non-null change payload and affectsConfiguration only returning true for that key.
| const networkFilterEnabled = this.configurationService.getValue<boolean>(AgentNetworkDomainSettingId.NetworkFilter) ?? false; | ||
| const sandboxEnabled = this.configurationService.getValue<'off' | 'on'>(AgentSandboxSettingId.Enabled) === 'on'; | ||
| const deprecatedSandboxEnabled = this.configurationService.getValue<boolean>(AgentSandboxSettingId.DeprecatedEnabled) ?? false; | ||
|
|
||
| this.enabled = networkFilterEnabled || sandboxEnabled || deprecatedSandboxEnabled; |
There was a problem hiding this comment.
chat.agent.sandbox.enabled is treated as enabled only when the value is the string 'on'. Elsewhere (e.g. TerminalSandboxService) the setting is interpreted as enabled for both 'on' and boolean true, so a user configuration like "chat.agent.sandbox.enabled": true would enable the sandbox but not activate network domain filtering here. Consider normalizing the setting value (accept 'on' and true as enabled) so network filtering reliably follows sandbox enablement, and add a unit test for the boolean case.
See below for a potential fix:
private isSandboxEnabled(value: 'off' | 'on' | boolean | undefined): boolean {
return value === 'on' || value === true;
}
private readConfiguration(): void {
const networkFilterEnabled = this.configurationService.getValue<boolean>(AgentNetworkDomainSettingId.NetworkFilter) ?? false;
const sandboxEnabled = this.isSandboxEnabled(this.configurationService.getValue<'off' | 'on' | boolean>(AgentSandboxSettingId.Enabled));
Summary
platform/sandbox/commonValidation
git diff --cached --checkbefore commitgit commitFixes #309954