Skip to content

Handle environment variable syntax in terminal command auto-approval #259205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 4, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 1, 2025

The terminal command auto-approval feature was not correctly handling commands that start with environment variable assignments, such as FOO=bar env | grep FOO. The auto-approval logic would test the entire command string against the rules, causing commands like this to be denied even when the actual command part (env) was in the approved list.

Changes Made

  • Added _extractCommandFromEnvAssignments() method that parses environment variable assignments at the beginning of commands and extracts the actual command part
  • Updated _commandMatchesRegex() method to use the extracted command for pattern matching instead of the full command string
  • Shell-aware parsing with different behavior based on shell type:
    • Bash/sh/zsh/unknown shells: Parse VAR=value syntax (falls back to bourne shell for unknown shells)
    • PowerShell: No parsing (maintains existing behavior since PowerShell uses $env:VAR='value' syntax)
  • Robust regex pattern that handles quoted values, multiple variables, and whitespace properly

Before the Fix

# Command with auto-approve rule ["env"]
FOO=bar env | grep FOO
# Result: DENIED (full string doesn't start with "env")

After the Fix

# Same command with auto-approve rule ["env"] 
FOO=bar env | grep FOO
# Result: APPROVED (extracts "env | grep FOO" which matches "env" rule)

Examples of Supported Syntax

  • FOO=bar command → extracts command
  • FOO=bar BAZ=qux command → extracts command
  • MESSAGE="hello world" command → extracts command
  • GREETING='hello there' command → extracts command
  • FOO= command → extracts command (empty value)
  • VAR=value command → extracts command (handles whitespace)

The implementation maintains full backward compatibility and adds comprehensive test coverage for various edge cases.

Fixes #259201.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • electronjs.org
    • Triggering command: node-gyp (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI assigned Copilot and Tyriar Aug 1, 2025
@Tyriar Tyriar added this to the August 2025 milestone Aug 1, 2025
@Copilot Copilot AI changed the title [WIP] Run in terminal: Handle setting environment variable syntax before running commands Handle environment variable syntax in terminal command auto-approval Aug 1, 2025
@Copilot Copilot AI requested a review from Tyriar August 1, 2025 13:35
Copilot finished work on behalf of Tyriar August 1, 2025 13:35
@Tyriar Tyriar marked this pull request as ready for review August 3, 2025 14:25
@Tyriar Tyriar enabled auto-merge August 4, 2025 20:18
@Tyriar Tyriar merged commit 3cfb00b into main Aug 4, 2025
18 checks passed
@Tyriar Tyriar deleted the copilot/fix-259201 branch August 4, 2025 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Run in terminal: Handle setting environment variable syntax before running commands
4 participants