Skip to content

Fix user environment block construction - #813

Merged
Tess Gauthier (tgauth) merged 17 commits into
PowerShell:latestw_allfrom
o-sdn-o:Fix-user-environment-block-construction
Nov 4, 2025
Merged

Tess Gauthier (tgauth) merged 17 commits into
PowerShell:latestw_allfrom
o-sdn-o:Fix-user-environment-block-construction

Conversation

@o-sdn-o

Copy link
Copy Markdown

PR Summary

Fix construction of user environment block when user connects.

PR Context

Currently, when a user connects, the environment block from the LocalSystem context (in which sshd.exe
is running) is used to create the user session environment.

The fix ensures that the user environment block is loaded for users with names without the sshd prefix.

Fixes PowerShell/Win32-OpenSSH#2407

Currently, when a user connects, the environment block from the LocalSystem context in which sshd.exe is running is used to create the user session environment. The fix ensures that the user environment block is loaded for users with names without the sshd prefix.

Fixes PowerShell/Win32-OpenSSH#2407
@tgauth

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment thread contrib/win32/win32compat/w32fd.c Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors Windows environment handling for SSH sessions to use the Windows CreateEnvironmentBlock API instead of manually setting environment variables from the registry. The key changes simplify environment setup by leveraging Windows native functionality while maintaining session state.

  • Introduces CreateEnvironmentBlock to automatically load user environment blocks with session state inheritance
  • Refactors registry access code to be more focused and efficient
  • Adds username extraction from user tokens to conditionally apply environment blocks

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
contrib/win32/win32compat/w32fd.c Adds get_username_from_token function and updates spawn_child_internal to conditionally create user environment blocks, excluding sshd service accounts
contrib/win32/win32compat/w32-doexec.c Refactors setup_session_user_vars to only set HOMEPATH/HOMEDRIVE and PATH, removes manual registry enumeration of all environment variables, adds get_registry_key_value helper function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread contrib/win32/win32compat/w32fd.c Resolved
Comment thread contrib/win32/win32compat/w32fd.c Resolved
Comment thread contrib/win32/win32compat/w32fd.c Resolved
Comment thread contrib/win32/win32compat/w32fd.c Resolved
Comment thread contrib/win32/win32compat/w32-doexec.c Resolved
Comment thread contrib/win32/win32compat/w32-doexec.c Resolved
@tgauth

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@tgauth

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@tgauth Tess Gauthier (tgauth) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@tgauth
Tess Gauthier (tgauth) merged commit 18133aa into PowerShell:latestw_all Nov 4, 2025
Hazel Duvall (hazelduvall) pushed a commit to hazelduvall/openssh-portable that referenced this pull request May 6, 2026
* Fix user environment block construction.

Currently, when a user connects, the environment block from the LocalSystem context in which sshd.exe is running is used to create the user session environment. The fix ensures that the user environment block is loaded for users with names without the sshd prefix.

Fixes PowerShell/Win32-OpenSSH#2407

* Simplify user environment block loading

* Do not reassign environment variables

* Edit comments

* Set the HOMEPATH and HOMEDRIVE variables only if they are not assigned

* Edit comments

* Fix error_message releasing

* Fix error_message releasing

* Fix user_path releasing

* Fix user_name/domain_name lookup

* Fix incorrect size calculation (name_length/domain_length)

* Use wcslen(L"sshd") for clarity

* Reverse the condition to match the comment (don't create environment block for 'sshd' users)

* Fix cases where only HKLM or only HKCU PATH is available

* Fix incorrect spelling ('preppended' -> 'prepended')

* Revert reversing the condition to match the comment

* Use wcslen(L"sshd") for clarity (again)
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.

USERDOMAIN Environmental Variable Returns COMPUTERDOMAIN Value

3 participants