Skip to content

Fix Windows sudo detection to use IsInRole for effective elevation check#339

Merged
kke merged 3 commits into
mainfrom
fix/windows-sudo-elevation
May 7, 2026
Merged

Fix Windows sudo detection to use IsInRole for effective elevation check#339
kke merged 3 commits into
mainfrom
fix/windows-sudo-elevation

Conversation

@kke
Copy link
Copy Markdown
Contributor

@kke kke commented May 7, 2026

Replace the raw token Groups SID check with WindowsPrincipal.IsInRole, which calls CheckTokenMembership and returns true only when the Administrators SID is present and not marked deny-only. This correctly handles SSH sessions (Windows OpenSSH always provides a full elevated token for Administrators group members, regardless of UAC) as well as WinRM domain accounts and local accounts with LocalAccountTokenFilterPolicy=1. The previous Groups check could also produce false positives for WinRM local accounts with filtered tokens, where the Administrators SID is present but deny-only.

Fixes #332

Copy link
Copy Markdown

Copilot AI left a comment

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 updates Windows “sudo/no-op” capability detection to rely on an effective-elevation check (WindowsPrincipal.IsInRole(Administrator)) rather than inspecting token group SIDs directly, addressing false positives/negatives in UAC-filtered WinRM/local sessions and Windows OpenSSH sessions (per #332).

Changes:

  • Replace the token .Groups -contains BuiltinAdministratorsSid check (and whoami heuristic) with a PowerShell WindowsPrincipal.IsInRole(Administrator) check.
  • Update the function comment to describe the new elevation semantics and rationale.

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

Comment thread sudo/windows.go Outdated
Comment thread sudo/windows.go Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread sudo/windows.go
Replace the raw token Groups SID check with WindowsPrincipal.IsInRole, which
calls CheckTokenMembership and returns true only when the Administrators SID
is present and not marked deny-only. This correctly handles SSH sessions
(Windows OpenSSH always provides a full elevated token for Administrators group
members, regardless of UAC) as well as WinRM domain accounts and local accounts
with LocalAccountTokenFilterPolicy=1. The previous Groups check could also
produce false positives for WinRM local accounts with filtered tokens, where
the Administrators SID is present but deny-only.

Fixes #332

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke kke force-pushed the fix/windows-sudo-elevation branch from 0c72e35 to efe2c84 Compare May 7, 2026 10:27
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread sudo/windows.go
Comment thread sudo/windows_test.go
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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

@kke kke merged commit 4b20934 into main May 7, 2026
15 checks passed
@kke kke deleted the fix/windows-sudo-elevation branch May 7, 2026 11:18
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.

Windows SSH sudo detection rejects elevated non-Administrator sessions when UAC is enabled

2 participants