|
| 1 | +"""Conservative trust-boundary hardening (#540). |
| 2 | +
|
| 3 | +Covers the non-controversial parts: block private/dunder methods in |
| 4 | +call_method (BUG-11), reject non-estimator instantiate results (BUG-10), and |
| 5 | +cap run_command output (BUG-23). instantiate sandboxing (BUG-09) is |
| 6 | +intentionally out of scope — the server already exposes run_command. |
| 7 | +""" |
| 8 | + |
| 9 | +import contextlib |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from sktime_mcp.runtime.executor import get_executor |
| 14 | +from sktime_mcp.runtime.handles import get_handle_manager |
| 15 | +from sktime_mcp.tools.instantiate import instantiate_tool |
| 16 | +from sktime_mcp.tools.run_command import run_command_tool |
| 17 | + |
| 18 | + |
| 19 | +def _release(handle): |
| 20 | + with contextlib.suppress(KeyError): |
| 21 | + get_handle_manager().release_handle(handle) |
| 22 | + |
| 23 | + |
| 24 | +class TestCallMethodDenylist: |
| 25 | + def test_reduce_blocked(self): |
| 26 | + h = instantiate_tool(spec="NaiveForecaster()")["handle"] |
| 27 | + try: |
| 28 | + res = get_executor().call_method(handle_id=h, method_name="__reduce__", kwargs={}) |
| 29 | + assert not res["success"] |
| 30 | + assert "private" in res["error"].lower() |
| 31 | + finally: |
| 32 | + _release(h) |
| 33 | + |
| 34 | + @pytest.mark.parametrize("m", ["__class__", "__getattribute__", "_get_class_flags", "__init__"]) |
| 35 | + def test_private_methods_blocked(self, m): |
| 36 | + h = instantiate_tool(spec="NaiveForecaster()")["handle"] |
| 37 | + try: |
| 38 | + res = get_executor().call_method(handle_id=h, method_name=m, kwargs={}) |
| 39 | + assert not res["success"] |
| 40 | + assert "private" in res["error"].lower() |
| 41 | + finally: |
| 42 | + _release(h) |
| 43 | + |
| 44 | + def test_public_method_still_works(self): |
| 45 | + h = instantiate_tool(spec="NaiveForecaster(sp=12)")["handle"] |
| 46 | + try: |
| 47 | + res = get_executor().call_method(handle_id=h, method_name="get_params", kwargs={}) |
| 48 | + assert res["success"] |
| 49 | + assert res["result"]["sp"] == 12 |
| 50 | + finally: |
| 51 | + _release(h) |
| 52 | + |
| 53 | + |
| 54 | +class TestInstantiateTypeCheck: |
| 55 | + @pytest.mark.parametrize("spec", ["42", "[1, 2, 3]", "'just a string'", "None"]) |
| 56 | + def test_non_estimator_rejected(self, spec): |
| 57 | + res = instantiate_tool(spec=spec) |
| 58 | + assert not res["success"] |
| 59 | + assert "did not produce an sktime estimator" in res["error"] |
| 60 | + |
| 61 | + def test_real_estimator_still_instantiates(self): |
| 62 | + res = instantiate_tool(spec="NaiveForecaster(sp=12)") |
| 63 | + try: |
| 64 | + assert res["success"] |
| 65 | + finally: |
| 66 | + _release(res.get("handle")) |
| 67 | + |
| 68 | + |
| 69 | +class TestRunCommandCap: |
| 70 | + def test_large_output_truncated(self): |
| 71 | + res = run_command_tool("seq 1 100000") |
| 72 | + assert res["success"] |
| 73 | + assert res["truncated"] is True |
| 74 | + assert len(res["output"]) < 25_000 |
| 75 | + assert "truncated" in res["output"] |
| 76 | + |
| 77 | + def test_small_output_not_truncated(self): |
| 78 | + res = run_command_tool("echo hello") |
| 79 | + assert res["success"] |
| 80 | + assert res["truncated"] is False |
| 81 | + assert res["output"] == "hello" |
| 82 | + |
| 83 | + def test_nonzero_exit_has_error_key(self): |
| 84 | + res = run_command_tool("exit 3") |
| 85 | + assert not res["success"] |
| 86 | + assert res["returncode"] == 3 |
| 87 | + assert "error" in res |
0 commit comments