Fix env var leak when running extension commands with -e flag#7035
Draft
Fix env var leak when running extension commands with -e flag#7035
Conversation
Co-authored-by: jongio <2163001+jongio@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix environment variable leak into extension process
Fix env var leak when running extension commands with -e flag
Mar 6, 2026
Member
|
@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>
Contributor
Author
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>
- ParseGlobalFlags now uses os.Getenv("AZURE_ENV_NAME") as default for -e flag,
matching EnvFlag.Bind() behavior so CI pipelines setting AZURE_ENV_NAME work
for extension commands
- Export EnvNameEnvVarName constant for cross-package reuse
- Add test cases for env var fallback, flag override, concatenated -e, multi -e
- Use exported constant in tests instead of hardcoded string
- Remove non-idiomatic "Warning:" prefix from error message
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.