💫 Toolkit to help you get started with SDD or any other process!
harden: remove shell parameter from run_command() (#3716)
Manfred Riem1631c0a50f33
diff --git a/src/specify_cli/_utils.py b/src/specify_cli/_utils.pyindex 6603d65c4..de7836698 100644--- a/src/specify_cli/_utils.py+++ b/src/specify_cli/_utils.py@@ -69,21 +69,14 @@ def run_command(cmd: list[str],check_return: bool = True,capture: bool = False,- shell: bool = False,) -> str | None:"""Run a command without invoking a shell and optionally capture output.- The ``shell`` parameter is kept in the signature so existing keyword- callers (and the re-export from ``specify_cli``) don't raise ``TypeError``,- but only the default ``shell=False`` is honoured. ``shell=True`` is- rejected with ``ValueError`` rather than silently ignored, so the- unsupported mode fails loudly instead of running with a different meaning.+ Commands are always executed with ``shell=False`` and must be passed as an+ argv ``list[str]``. There is deliberately no ``shell`` parameter: the+ argv-list contract makes shell interpolation impossible by construction, so+ the shell-injection surface cannot be re-enabled at a call site."""- if shell:- raise ValueError(- "run_command() does not support shell=True; pass argv as a list"- )-try:if capture:result = subprocess.run(cmd, check=check_return, capture_output=True, text=True)diff --git a/tests/test_utils.py b/tests/test_utils.pyindex 869c9ff9c..567e0096a 100644--- a/tests/test_utils.py+++ b/tests/test_utils.py@@ -9,7 +9,13 @@ import pytestfrom specify_cli import run_command-def test_run_command_rejects_shell_execution_compatibly():- assert inspect.signature(run_command).parameters["shell"].default is False- with pytest.raises(ValueError, match="does not support shell=True"):- run_command(["echo", "blocked"], shell=True) # noqa: S604+def test_run_command_has_no_shell_parameter():+ """The shell-injection surface is removed at the API level.++ ``run_command`` must never accept a ``shell`` parameter: the argv-list+ contract makes shell interpolation impossible by construction, and there is+ no runtime mode to re-enable it. Passing ``shell=`` is a hard ``TypeError``.+ """+ assert "shell" not in inspect.signature(run_command).parameters+ with pytest.raises(TypeError):+ run_command(["echo", "blocked"], shell=True) # type: ignore[call-arg] # noqa: S604