Skip to content

Commit 73b30e6

Browse files
authored
Block auto-approve for tilde and env-var expansions in file write paths (#42) (#320676)
1 parent 0024d88 commit 73b30e6

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/tools/commandLineAnalyzer/commandLineFileWriteAnalyzer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ export class CommandLineFileWriteAnalyzer extends Disposable implements ICommand
146146
const fileUri = URI.isUri(fileWrite) ? fileWrite : URI.file(fileWrite);
147147
// TODO: Handle command substitutions/complex destinations properly https://github.com/microsoft/vscode/issues/274167
148148
// TODO: Handle environment variables properly https://github.com/microsoft/vscode/issues/274166
149-
if (fileUri.fsPath.match(/[$\(\){}`~]/)) {
149+
// `~` catches POSIX tilde expansion (e.g. `~/foo`) and `%` catches Windows
150+
// environment variable expansions (e.g. `%APPDATA%\foo`). Neither is
151+
// recognized as absolute by `posix.isAbsolute` / `win32.isAbsolute`, so
152+
// without this guard they would be joined onto cwd and incorrectly classified
153+
// as inside the workspace while expanding at runtime to a location outside it.
154+
if (fileUri.fsPath.match(/[$\(\){}`~%]/)) {
150155
isAutoApproveAllowed = false;
151156
this._log('File write blocked due to likely containing a variable, sub-command, or tilde expansion', fileUri.toString());
152157
break;

src/vs/workbench/contrib/terminalContrib/chatAgentTools/test/electron-browser/commandLineAnalyzer/commandLineFileWriteAnalyzer.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ suite('CommandLineFileWriteAnalyzer', () => {
118118
test('variable in filename - block', () => t('echo hello > $HOME/file.txt', 'outsideWorkspace', false, 1));
119119
test('command substitution - block', () => t('echo hello > $(pwd)/file.txt', 'outsideWorkspace', false, 1));
120120
test('brace expansion - block', () => t('echo hello > {a,b}.txt', 'outsideWorkspace', false, 1));
121+
test('tilde expansion - block', () => t('echo hello > ~/file.txt', 'outsideWorkspace', false, 1));
122+
test('percent-style variable - block', () => t('echo hello > %HOME%/file.txt', 'outsideWorkspace', false, 1));
121123
});
122124

123125
suite('blockDetectedFileWrites: all', () => {
@@ -300,6 +302,8 @@ suite('CommandLineFileWriteAnalyzer', () => {
300302
test('no redirections - allow', () => t('Write-Host "hello"', 'outsideWorkspace', true, 0));
301303
test('variable in filename - block', () => t('Write-Host "hello" > $env:TEMP\\file.txt', 'outsideWorkspace', false, 1));
302304
test('subexpression - block', () => t('Write-Host "hello" > $(Get-Date).log', 'outsideWorkspace', false, 1));
305+
test('percent-style variable - block', () => t('Write-Host "hello" > %APPDATA%\\file.txt', 'outsideWorkspace', false, 1));
306+
test('tilde expansion - block', () => t('Write-Host "hello" > ~\\file.txt', 'outsideWorkspace', false, 1));
303307
});
304308

305309
suite('blockDetectedFileWrites: all', () => {

0 commit comments

Comments
 (0)