Fix env var leak when running extension commands with -e flag#7035
Fix env var leak when running extension commands with -e flag#7035
Conversation
Co-authored-by: jongio <2163001+jongio@users.noreply.github.com>
|
@copilot that looks like a lazy implementation, fix it with better design principles |
Instead of scattered parseEnvFlagFromArgs + DisableFlagParsing checks, follow the established pattern for --cwd/--debug/--no-prompt: 1. Add EnvironmentName to GlobalCommandOptions 2. Parse -e/--environment in ParseGlobalFlags() before cobra runs 3. Use globalOptions.EnvironmentName as fallback in EnvFlag DI resolver 4. Use a.globalOptions directly in extensionAction.Run() for InvokeOptions Co-authored-by: jongio <2163001+jongio@users.noreply.github.com>
Redesigned in 79d1f16. Instead of scattered
No more Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Co-authored-by: jongio <2163001+jongio@users.noreply.github.com>
|
Hi @@copilot. Thank you for your interest in helping to improve the Azure Developer CLI experience and for your contribution. We've noticed that there hasn't been recent engagement on this pull request. If this is still an active work stream, please let us know by pushing some changes or leaving a comment. Otherwise, we'll close this out in 7 days. |
Extension commands use
DisableFlagParsing: true, so cobra never parses-e/--environment. This causedlazyEnvto always resolve to the default environment, leaking its variables into the extension process even when a different environment was explicitly requested.AZD_ENVIRONMENTwas also never set in these cases.Root cause
Two call sites both relied on
cmd.Flags().GetString("environment")which returns""for any command withDisableFlagParsing: true:EnvFlagDI resolver incontainer.go— controls which environmentlazyEnvloadsextensionAction.Run()inextensions.go— controlsInvokeOptions.Environment(i.e.,AZD_ENVIRONMENTpropagation)Changes
Follows the established
GlobalCommandOptionspattern used by--cwd,--debug, and--no-prompt:internal/global_command_options.go— AddedEnvironmentNamefield toGlobalCommandOptionscmd/auto_install.go—ParseGlobalFlags()now pre-parses-e/--environmentfrom raw args before cobra runs, storing the value inGlobalCommandOptions.EnvironmentName. The flag is added only to the parsing flag set (not toCreateGlobalFlagSet()) so it doesn't appear in every command's help text.cmd/container.go—EnvFlagDI resolver now acceptsglobalOptions *internal.GlobalCommandOptionsand falls back toglobalOptions.EnvironmentNamewhencmd.Flags()doesn't have the value (e.g. extension commands withDisableFlagParsing: true)cmd/extensions.go—extensionAction.Run()now usesa.globalOptionsdirectly for allInvokeOptionsfields (Debug,Cwd,Environment,NoPrompt), removing the fragilecmd.Flags().GetString()calls that silently fail for extension commandsTestParseGlobalFlags_EnvironmentNamevalidates the early parsing with short flags, long flags, equals syntax, and mixed with unknown flagsOriginal prompt
💡 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.