Skip to content

Batch activation changes inherited PATH under delayed expansion #3617

Description

@gaborbernat

With delayed expansion enabled in cmd.exe, activate.bat and deactivate.bat change an inherited PATH entry containing !name!. A Windows 11 check changed C:\Test!q!\bin to C:\Test\bin after activation and deactivation; with delayed expansion disabled, it preserved the entry.

Affected code

The batch activation template saves and prepends PATH. The deactivation template restores the saved value. These lines are also present at the report's commit, 8e09166ddfbdaed3a5dbccc216d38ff1d3dbb4c2.

Mechanism

The batch files insert %PATH% or %_OLD_VIRTUAL_PATH% into a set command. With delayed expansion enabled, cmd.exe interprets !q! in the inserted text as a variable reference. If q is unset, it removes those characters. The path comes from the invoking shell; it need not be the path of the environment whose batch scripts are running.

Reproduction

On Windows 11, create an environment from the source build at ebe06113f440731ab9c5bd6506a4793140532dc9 with CPython 3.14 and no seed packages. Run the following in PowerShell from the directory containing plainvenv:

python -m virtualenv --no-seed plainvenv
$env:Path = 'C:\Test!q!\bin;C:\Windows\System32'
Remove-Item Env:q -ErrorAction SilentlyContinue
foreach ($mode in 'OFF', 'ON') {
    Write-Output "/V:$mode"
    & "$env:SystemRoot\System32\cmd.exe" /D "/V:$mode" /C 'call plainvenv\Scripts\activate.bat & call plainvenv\Scripts\deactivate.bat & set PATH'
}

The native check returned these final values:

/V:OFF
PATH=C:\Test!q!\bin;C:\Windows\System32
/V:ON
PATH=C:\Test\bin;C:\Windows\System32

Expected: both modes restore PATH=C:\Test!q!\bin;C:\Windows\System32 after deactivation.

Impact

The caller loses the original PATH entry after this activation and deactivation sequence. Reproducibility: moderate, because it requires Windows, delayed expansion, and an inherited value containing exclamation marks. Real-world exposure: unlikely, because delayed expansion is disabled by default and the inherited path must also contain !.

Credits

Reported by oss-scanner: ANT-2026-0YRL0MX4.

Activity

  1. added 4 commits that reference this issue on Nov 20, 2026
    f669250
    06c4486
    f14fe28
    de5ec31
  2. added a commit that references this issue on Nov 20, 2026
    c71e1f2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions