Skip to content

Commit c2e182a

Browse files
Potential fix for code scanning alert no. 13: Uncontrolled command line
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent a931976 commit c2e182a

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

core/remote_executor.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,11 @@ def execute_remote_winrm(
142142

143143
# ── Build PSSession wrapper ───────────────────────────────────────────────
144144

145-
# PS single-quote escape: ' → '' (belt-and-suspenders; host already
146-
# validated above, but applied in case the regex is ever relaxed).
147-
safe_host = target_host.replace("'", "''")
148-
149-
cred_block = ""
150-
session_cred_flag = ""
145+
username_arg = ""
146+
password_arg = ""
151147
if credential:
152-
username = str(credential.get("username", "")).replace("'", "''")
153-
password = str(credential.get("password", "")).replace("'", "''")
154-
cred_block = (
155-
f"$_cred = New-Object System.Management.Automation.PSCredential("
156-
f"'{username}', "
157-
f"(ConvertTo-SecureString '{password}' -AsPlainText -Force)); "
158-
)
159-
session_cred_flag = " -Credential $_cred"
148+
username_arg = str(credential.get("username", ""))
149+
password_arg = str(credential.get("password", ""))
160150

161151
# Enforce allowlist before embedding into PowerShell -Command payload.
162152
if not _is_allowed_atomic_command(command):
@@ -181,19 +171,32 @@ def execute_remote_winrm(
181171
)
182172

183173
ps_script = (
184-
f"{cred_block}"
185-
f"$_s = New-PSSession -ComputerName '{safe_host}'{session_cred_flag}; "
174+
"param([string]$ComputerName, [string]$Username, [string]$Password) "
175+
"if ($Username) { "
176+
" $_cred = New-Object System.Management.Automation.PSCredential("
177+
" $Username, (ConvertTo-SecureString $Password -AsPlainText -Force)"
178+
" ); "
179+
" $_s = New-PSSession -ComputerName $ComputerName -Credential $_cred; "
180+
"} else { "
181+
" $_s = New-PSSession -ComputerName $ComputerName; "
182+
"} "
186183
f"Invoke-Command -Session $_s -ScriptBlock {{ {resolved_command} }}; "
187-
f"Remove-PSSession -Session $_s"
184+
"Remove-PSSession -Session $_s"
188185
)
189186

190187
# ── Dispatch ──────────────────────────────────────────────────────────────
191188

192189
system = platform.system().lower()
193190
if system == "windows":
194-
cmd_list = ["powershell.exe", "-NonInteractive", "-NoProfile", "-Command", ps_script]
191+
cmd_list = [
192+
"powershell.exe", "-NonInteractive", "-NoProfile", "-Command", ps_script,
193+
"-ComputerName", target_host, "-Username", username_arg, "-Password", password_arg,
194+
]
195195
else:
196-
cmd_list = ["pwsh", "-NonInteractive", "-NoProfile", "-Command", ps_script]
196+
cmd_list = [
197+
"pwsh", "-NonInteractive", "-NoProfile", "-Command", ps_script,
198+
"-ComputerName", target_host, "-Username", username_arg, "-Password", password_arg,
199+
]
197200

198201
logger.info(
199202
"WinRM remote exec: target=%s executor=%s timeout=%ds",

0 commit comments

Comments
 (0)