Merge pull request #67800 from twangboy/fix_59344
Some checks failed
CI / Prepare Workflow Run (push) Has been cancelled
CI / Pre-Commit (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / NSIS Tests (push) Has been cancelled
CI / Prepare Release: (push) Has been cancelled
CI / Documentation (push) Has been cancelled
CI / Build Source Tarball (push) Has been cancelled
CI / Build Salt Onedir (push) Has been cancelled
CI / Build Packages (push) Has been cancelled
CI / CI Deps (push) Has been cancelled
CI / Test Package (push) Has been cancelled
CI / Test Salt (push) Has been cancelled
CI / Combine Code Coverage (push) Has been cancelled
CI / Set the Pipeline Exit Status (push) Has been cancelled

Handle integer usernames in RunAs on Windows
This commit is contained in:
Daniel Wozniak 2025-03-10 13:52:50 -07:00 committed by GitHub
commit 0b17433478
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 51 additions and 1 deletions

2
changelog/59344.fixed.md Normal file
View file

@ -0,0 +1,2 @@
Fix issue with RunAs on Windows so that usernames of all numeric characters
are handled as strings

View file

@ -65,7 +65,7 @@ def _get_group_object(name):
"""
with salt.utils.winapi.Com():
nt = win32com.client.Dispatch("AdsNameSpaces")
return nt.GetObject("", "WinNT://./" + name + ",group")
return nt.GetObject("", f"WinNT://./{name},group")
def _get_all_groups():

View file

@ -96,6 +96,10 @@ def runas(cmdLine, username, password=None, cwd=None):
Commands are run in with the highest level privileges possible for the
account provided.
"""
# Sometimes this comes in as an int. LookupAccountName can't handle an int
# Let's make it a string if it's anything other than a string
if not isinstance(username, str):
username = str(username)
# Validate the domain and sid exist for the username
try:
_, domain, _ = win32security.LookupAccountName(None, username)
@ -265,6 +269,10 @@ def runas_unpriv(cmd, username, password, cwd=None):
"""
Runas that works for non-privileged users
"""
# Sometimes this comes in as an int. LookupAccountName can't handle an int
# Let's make it a string if it's anything other than a string
if not isinstance(username, str):
username = str(username)
# Validate the domain and sid exist for the username
try:
_, domain, _ = win32security.LookupAccountName(None, username)

View file

@ -2,8 +2,11 @@
Test the win_runas util
"""
from random import randint
import pytest
import salt.modules.win_useradd as win_useradd
import salt.utils.win_runas as win_runas
pytestmark = [
@ -18,6 +21,15 @@ def user():
yield account
@pytest.fixture
def int_user():
with pytest.helpers.create_account() as account:
int_name = randint(10000, 99999)
win_useradd.rename(account.username, int_name)
account.username = int_name
yield account
@pytest.mark.parametrize(
"cmd, expected",
[
@ -54,3 +66,31 @@ def test_compound_runas_unpriv(user, cmd, expected):
password=user.password,
)
assert expected in result["stdout"]
def test_runas_str_user(user):
result = win_runas.runas(
cmdLine="whoami", username=user.username, password=user.password
)
assert user.username in result["stdout"]
def test_runas_int_user(int_user):
result = win_runas.runas(
cmdLine="whoami", username=int(int_user.username), password=int_user.password
)
assert str(int_user.username) in result["stdout"]
def test_runas_unpriv_str_user(user):
result = win_runas.runas_unpriv(
cmd="whoami", username=user.username, password=user.password
)
assert user.username in result["stdout"]
def test_runas_unpriv_int_user(int_user):
result = win_runas.runas_unpriv(
cmd="whoami", username=int(int_user.username), password=int_user.password
)
assert str(int_user.username) in result["stdout"]