Skip to content

Fix EnvironmentPropagationFilter to filter current process environment variables correctly #10902

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 11, 2025

Fixes the EnvironmentPropagationFilter property in DotNetCliRunnerInvocationOptions to correctly filter environment variables from the current process rather than only filtering additional environment variables passed by the caller.

Issue

The initial implementation incorrectly applied the filter only to the env parameter (additional environment variables), but the intended behavior was to filter the current process's environment variables that get propagated to the child dotnet process.

Solution

The implementation now correctly:

  1. First iterates over all current process environment variables via Environment.GetEnvironmentVariables()
  2. Filters those through the EnvironmentPropagationFilter function
  3. Adds the filtered current process environment variables to ProcessStartInfo.EnvironmentVariables
  4. Then layers additional environment variables from the env parameter on top (without filtering, since these are explicitly provided by the caller)

Usage

var options = new DotNetCliRunnerInvocationOptions
{
    // Only propagate environment variables starting with "ASPIRE_" from current process
    EnvironmentPropagationFilter = (name, value) => name.StartsWith("ASPIRE_")
};

await runner.RunAsync(projectFile, false, false, args, env, null, options, cancellationToken);

Benefits

  • Correct Filtering: Current process environment variables are now properly filtered
  • Security: Sensitive environment variables can be blocked from reaching child processes
  • Explicit Override: Additional environment variables passed via env parameter are always included
  • Backward Compatibility: When filter is null, all current process variables are propagated as before

This change ensures that the filter serves its intended purpose of controlling which environment variables from the current execution context reach the spawned dotnet processes, while preserving the ability for callers to explicitly set additional variables.


💡 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.

…nOptions with comprehensive tests

Co-authored-by: mitchdenny <[email protected]>
@Copilot Copilot AI changed the title [WIP] Add environment variable propagation filter and expose environment on CliExecutionContext for testability Add EnvironmentPropagationFilter property to DotNetCliRunner for flexible environment variable control Aug 11, 2025
@Copilot Copilot AI requested a review from mitchdenny August 11, 2025 02:39
Copilot finished work on behalf of mitchdenny August 11, 2025 02:39
@@ -441,7 +442,10 @@ public virtual async Task<int> ExecuteAsync(string[] args, IDictionary<string, s
{
foreach (var envKvp in env)
{
startInfo.EnvironmentVariables[envKvp.Key] = envKvp.Value;
if (options.EnvironmentPropagationFilter is null || options.EnvironmentPropagationFilter(envKvp.Key, envKvp.Value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot - the env argument here is for extra arguments that the caller of ExecuteAsync wants to add to the arguments launching the process. What we need to do here is iterate over all of the existing environment variables for the current process and then copy them into the startInfo.EnvironmentVariables collection but first filter them through options.EnvironmentPropogationFilter.

Then we layer the additional environment variables pass in via the env variable on top of those (no need to pass them through the filter).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 8a79e1b. The EnvironmentPropagationFilter now correctly:

  1. First iterates over current process environment variables via Environment.GetEnvironmentVariables()
  2. Filters those through the EnvironmentPropagationFilter function
  3. Adds filtered current process env vars to startInfo.EnvironmentVariables
  4. Then layers additional environment variables from the env parameter on top without filtering

This matches the expected behavior where the filter controls which current process environment variables get propagated, while additional environment variables passed explicitly by the caller are always included.

…t variables instead of additional env vars

Co-authored-by: mitchdenny <[email protected]>
@Copilot Copilot AI changed the title Add EnvironmentPropagationFilter property to DotNetCliRunner for flexible environment variable control Fix EnvironmentPropagationFilter to filter current process environment variables correctly Aug 11, 2025
@Copilot Copilot AI requested a review from mitchdenny August 11, 2025 06:42
Copilot finished work on behalf of mitchdenny August 11, 2025 06:42
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.

2 participants