Skip to content

Commit 6ee5618

Browse files
committed
fix: use shutil.which to resolve sf path, remove shell=True
Addresses reviewer concern about command injection risk with shell=True. shutil.which resolves the sf executable (falling back to sf.cmd on Windows), keeping shell=False everywhere while still working on Windows.
1 parent c13014b commit 6ee5618

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

src/datacustomcode/token_provider.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,19 @@ def __init__(self, sf_cli_org: str):
9797
def get_token(self) -> "AccessTokenResponse":
9898
"""Get token from Salesforce SF CLI"""
9999
import json
100+
import shutil
100101
import subprocess
101-
import sys
102102

103103
from datacustomcode.deploy import AccessTokenResponse
104104

105+
sf_path = shutil.which("sf") or shutil.which("sf.cmd")
106+
if sf_path is None:
107+
raise RuntimeError(
108+
"The 'sf' command was not found. "
109+
"Install Salesforce CLI: "
110+
"https://developer.salesforce.com/tools/salesforcecli"
111+
)
112+
105113
def _run_sf_command(args: list[str], description: str) -> dict:
106114
try:
107115
result = subprocess.run(
@@ -110,14 +118,7 @@ def _run_sf_command(args: list[str], description: str) -> dict:
110118
text=True,
111119
check=True,
112120
timeout=30,
113-
shell=sys.platform == "win32",
114121
)
115-
except FileNotFoundError as exc:
116-
raise RuntimeError(
117-
"The 'sf' command was not found. "
118-
"Install Salesforce CLI: "
119-
"https://developer.salesforce.com/tools/salesforcecli"
120-
) from exc
121122
except subprocess.TimeoutExpired as exc:
122123
raise RuntimeError(
123124
f"'{description}' timed out for org '{self.sf_cli_org}'"
@@ -144,7 +145,7 @@ def _run_sf_command(args: list[str], description: str) -> dict:
144145

145146
# Get org info from sf org display
146147
display_data = _run_sf_command(
147-
["sf", "org", "display", "--target-org", self.sf_cli_org, "--json"],
148+
[sf_path, "org", "display", "--target-org", self.sf_cli_org, "--json"],
148149
"sf org display",
149150
)
150151
result_data = display_data.get("result", {})
@@ -161,7 +162,7 @@ def _run_sf_command(args: list[str], description: str) -> dict:
161162
try:
162163
token_data = _run_sf_command(
163164
[
164-
"sf",
165+
sf_path,
165166
"org",
166167
"auth",
167168
"show-access-token",

0 commit comments

Comments
 (0)