VS Code: AI 에이전트 터미널 명령 화이트리스트 지정

hackernews | | 🔬 연구
#ai 에이전트 #review #vs code #명령어 제한 #터미널 #화이트리스트
원문 출처: hackernews · Genesis Park에서 요약 및 분석

요약

VS Code 통합 터미널에서 AI 에이전트 등이 잠재적으로 위험한 명령어를 실행하는 것을 방지하기 위해 PowerShell의 PSReadLine 모듈과 프로필 설정을 활용한 화이트리스트 방식의 보안 방법이 소개되었습니다. 사용자는 settings.json에서 가드 스크립트를 자동 로드하도록 설정하고, Enter 키 입력을 가로채어 명령어를 미리 정의된 정규식 패턴과 대조함으로써 허용되지 않은 실행을 차단할 수 있습니다. 또한, 잘못된 설정으로 인해 작업이 불가능해지는 상황을 방지하기 위해 개발자가 수동으로 잠금을 해제할 수 있는 '이스케이프 해치' 기능을 포함하는 것이 권장됩니다.

본문

Motivation If you’ve ever needed to restrict which commands can be run inside a VS Code integrated terminal – nowadays mainly to prevent agents from wreaking havoc – you can achieve this using a combination of VS Code terminal profiles and PowerShell’s PSReadLine module. I’m not sure is/how this works with other terminals, however I’ve verified that it works both in Windows and Ubuntu with the snap package of PowerShell. The base idea is that any commands entered in the terminal go first through a script where some custom logic & regex do their magic. Bootstrapping the guard (settings.json ) First, we need to ensure that every new terminal automatically loads our guard script. We can do this by defining a custom terminal profile in settings.json and making it the default. { "terminal.integrated.profiles.linux": { "Guarded PowerShell": { "path": "pwsh", "args": [ "-NoExit", "-Command", ". ./.vscode/terminal-guard.ps1" ] } }, "terminal.integrated.defaultProfile.linux": "Guarded PowerShell"} This configuration launches pwsh , dot-sources our terminal-guard.ps1 script to keep its functions and variables in the global scope, and then keeps the session open (-NoExit ). Intercepting the Enter key (terminal-guard.ps1) The core trick relies on PSReadLine , which handles console input in PowerShell. By overriding the Enter key handler, we can capture the buffer line, validate it against an allowlist, and either accept or reject it. # Define approved regex patterns$script:ApprovedPatterns = @( "^\s*\.\\scripts\\[a-zA-Z0-9_-]+\.ps1\b" # Allow local scripts "^\s*Get-ChildItem\b" # Allow specific cmdlets "^\s*ls\b" # Allow basic utilities)# Intercept the Enter keySet-PSReadLineKeyHandler -Key Enter -ScriptBlock { $line = $null $cursor = $null [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor) # Custom logic to split the line by semicolons and match against $ApprovedPatterns if (Test-CommandApproved $line) { [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() } else { [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() Write-Host "`nBLOCKED: Command is not in the approved whitelist." -ForegroundColor Red }} Behind the scenes, the Test-CommandApproved function splits the input $line by semicolons to handle chained commands. It then evaluates the first word of each pipeline statement against the $ApprovedPatterns regex array. If the command doesn’t match, RevertLine() is called. The command is cleared, a warning is printed to the console, and execution is completely halted. The escape hatch To ensure you don’t permanently lock yourself out, it’s wise to include an escape hatch. A simple Disable-TerminalGuard function that requires an interactive Read-Host prompt (e.g., typing “UNLOCK”) prevents automated agents from bypassing the guard, while allowing human developers to lift the restrictions when necessary.

Genesis Park 편집팀이 AI를 활용하여 작성한 분석입니다. 원문은 출처 링크를 통해 확인할 수 있습니다.

공유

관련 저널 읽기

전체 보기 →