Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/shared/src/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,28 @@ describe("readEnvironmentFromWindowsShell", () => {
);
});

it("captures the persisted Windows PATH scopes after the process PATH", () => {
const execFile = vi.fn<
(
file: string,
args: ReadonlyArray<string>,
options: { encoding: "utf8"; timeout: number },
) => string
>(
() =>
"__T3CODE_ENV_PATH_START__\nC:\\Windows\\System32;C:\\Program Files\\nodejs;C:\\Users\\testuser\\AppData\\Local\\Microsoft\\WinGet\\Links\n__T3CODE_ENV_PATH_END__\n",
);

expect(readEnvironmentFromWindowsShell(["PATH"], execFile)).toEqual({
PATH: "C:\\Windows\\System32;C:\\Program Files\\nodejs;C:\\Users\\testuser\\AppData\\Local\\Microsoft\\WinGet\\Links",
});

const command = execFile.mock.calls[0]?.[1]?.at(-1);
expect(command).toContain("GetEnvironmentVariable('PATH', 'Process')");
expect(command).toContain("GetEnvironmentVariable('PATH', 'Machine')");
expect(command).toContain("GetEnvironmentVariable('PATH', 'User')");
});

it("strips CRLF delimiters from captured PowerShell values", () => {
const execFile = vi.fn<
(
Expand Down
14 changes: 12 additions & 2 deletions packages/shared/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,20 @@ function buildWindowsEnvironmentCaptureCommand(names: ReadonlyArray<string>): st
throw new Error(`Unsupported environment variable name: ${name}`);
}

const readValue =
name === "PATH"
? [
`$value = @([Environment]::GetEnvironmentVariable('${name}', 'Process'), [Environment]::GetEnvironmentVariable('${name}', 'Machine'), [Environment]::GetEnvironmentVariable('${name}', 'User')) | Where-Object { $null -ne $_ -and $_.Length -gt 0 }`,
"if (@($value).Count -gt 0) { Write-Output ($value -join ';') }",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PATH join corrupts single scope

Medium Severity

When only one of Process, Machine, or User PATH is non-empty, PowerShell unwraps the filtered result to a scalar string. $value -join ';' then joins individual characters, so the captured PATH becomes corrupted and Windows command resolution breaks.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 9b6490c. Configure here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Verified this against PowerShell join semantics: binary -join treats a scalar as one item, so a single non-empty PATH scope remains unchanged rather than being split into characters. Microsoft documents that -join handles single items (for example, 1 -join "-" returns 1). The filtered value is therefore safe without an additional array wrapper.

]
: [
`$value = [Environment]::GetEnvironmentVariable('${name}')`,
"if ($null -ne $value -and $value.Length -gt 0) { Write-Output $value }",
];

return [
`Write-Output '${envCaptureStart(name)}'`,
`$value = [Environment]::GetEnvironmentVariable('${name}')`,
"if ($null -ne $value -and $value.Length -gt 0) { Write-Output $value }",
...readValue,
`Write-Output '${envCaptureEnd(name)}'`,
];
}),
Expand Down
Loading