DSPy: The framework for programming—not prompting—language models
Python
fix(dspy): prevent argument injection in LocalProvider subprocess calls (#9160)
Copilotfd93c38ca054
diff --git a/dspy/clients/lm_local.py b/dspy/clients/lm_local.pyindex c937ee6e5..efe62b6bc 100644--- a/dspy/clients/lm_local.py+++ b/dspy/clients/lm_local.py@@ -55,11 +55,21 @@ class LocalProvider(Provider):logger.info(f"We see that CUDA_VISIBLE_DEVICES is {os.environ.get('CUDA_VISIBLE_DEVICES', 'unset')}")port = get_free_port()timeout = launch_kwargs.get("timeout", 1800)- command = f"python -m sglang.launch_server --model-path {model} --port {port} --host 0.0.0.0"+ command = [+ "python",+ "-m",+ "sglang.launch_server",+ "--model-path",+ model,+ "--port",+ str(port),+ "--host",+ "0.0.0.0",+ ]# We will manually stream & capture logs.process = subprocess.Popen(- command.replace("\\\n", " ").replace("\\", " ").split(),+ command,text=True,stdout=subprocess.PIPE, # We'll read from pipestderr=subprocess.STDOUT, # Merge stderr into stdoutdiff --git a/tests/clients/test_lm_local.py b/tests/clients/test_lm_local.pynew file mode 100644index 000000000..e7f297f97--- /dev/null+++ b/tests/clients/test_lm_local.py@@ -0,0 +1,97 @@+from unittest import mock+from unittest.mock import patch++from dspy.clients.lm_local import LocalProvider+++@patch("dspy.clients.lm_local.threading.Thread")+@patch("dspy.clients.lm_local.subprocess.Popen")+@patch("dspy.clients.lm_local.get_free_port")+@patch("dspy.clients.lm_local.wait_for_server")+def test_command_with_spaces_in_path(mock_wait, mock_port, mock_popen, mock_thread):+ mock_port.return_value = 8000+ mock_process = mock.Mock()+ mock_process.pid = 12345+ mock_process.stdout.readline.return_value = ""+ mock_process.poll.return_value = 0+ mock_popen.return_value = mock_process++ lm = mock.Mock(spec=[])+ lm.model = "/path/to/my models/llama"+ lm.launch_kwargs = {}+ lm.kwargs = {}++ with mock.patch.dict("sys.modules", {"sglang": mock.Mock(), "sglang.utils": mock.Mock()}):+ LocalProvider.launch(lm, launch_kwargs={})++ assert mock_popen.called+ call_args = mock_popen.call_args+ command = call_args[0][0]++ assert isinstance(command, list)+ assert "--model-path" in command+ model_index = command.index("--model-path")+ assert command[model_index + 1] == "/path/to/my models/llama"+++@patch("dspy.clients.lm_local.threading.Thread")+@patch("dspy.clients.lm_local.subprocess.Popen")+@patch("dspy.clients.lm_local.get_free_port")+@patch("dspy.clients.lm_local.wait_for_server")+def test_command_construction_prevents_injection(mock_wait, mock_port, mock_popen, mock_thread):+ mock_port.return_value = 8000+ mock_process = mock.Mock()+ mock_process.pid = 12345+ mock_process.stdout.readline.return_value = ""+ mock_process.poll.return_value = 0+ mock_popen.return_value = mock_process++ lm = mock.Mock(spec=[])+ lm.model = "model --trust-remote-code"+ lm.launch_kwargs = {}+ lm.kwargs = {}++ with mock.patch.dict("sys.modules", {"sglang": mock.Mock(), "sglang.utils": mock.Mock()}):+ LocalProvider.launch(lm, launch_kwargs={})++ assert mock_popen.called+ call_args = mock_popen.call_args+ command = call_args[0][0]++ assert isinstance(command, list)+ assert "--model-path" in command+ model_index = command.index("--model-path")+ assert command[model_index + 1] == "model --trust-remote-code"+++@patch("dspy.clients.lm_local.threading.Thread")+@patch("dspy.clients.lm_local.subprocess.Popen")+@patch("dspy.clients.lm_local.get_free_port")+@patch("dspy.clients.lm_local.wait_for_server")+def test_command_is_list_not_string(mock_wait, mock_port, mock_popen, mock_thread):+ mock_port.return_value = 8000+ mock_process = mock.Mock()+ mock_process.pid = 12345+ mock_process.stdout.readline.return_value = ""+ mock_process.poll.return_value = 0+ mock_popen.return_value = mock_process++ lm = mock.Mock(spec=[])+ lm.model = "meta-llama/Llama-2-7b"+ lm.launch_kwargs = {}+ lm.kwargs = {}++ with mock.patch.dict("sys.modules", {"sglang": mock.Mock(), "sglang.utils": mock.Mock()}):+ LocalProvider.launch(lm, launch_kwargs={})++ assert mock_popen.called+ call_args = mock_popen.call_args+ command = call_args[0][0]++ assert isinstance(command, list)+ assert command[0] == "python"+ assert command[1] == "-m"+ assert command[2] == "sglang.launch_server"+ assert "--model-path" in command+ assert "--port" in command+ assert "--host" in command