Skip to content

Commit d723102

Browse files
committed
Fix Guard config mode and strict allowlist loading
1 parent 903ff9a commit d723102

4 files changed

Lines changed: 83 additions & 2 deletions

File tree

‎sdk/src/unplug/config/loader.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,47 @@
2121
from unplug.config.messages import MessageConfig
2222
from unplug.config.policy import MlGateConfig, ScanPolicy
2323
from unplug.config.tools import ToolPolicyConfig
24+
from unplug.exceptions import ConfigError
25+
26+
_GUARD_SECTION_KEYS: frozenset[str] = frozenset(
27+
{
28+
"scanners",
29+
"mode",
30+
"server_url",
31+
"server_api_key",
32+
"policy",
33+
"cache",
34+
"fail_closed",
35+
"pipeline",
36+
"scanners_config",
37+
"limits",
38+
"messages",
39+
"judge_enabled",
40+
"judge_low",
41+
"judge_high",
42+
"models",
43+
"active_model",
44+
"auto_download_model",
45+
"require_ml",
46+
"tools",
47+
"boundaries",
48+
"trajectory",
49+
"intent",
50+
"degradation",
51+
"toolchain",
52+
"collusion",
53+
"strict_scanner_allowlist",
54+
}
55+
)
56+
57+
58+
def _reject_unknown_guard_keys(data: dict[str, Any]) -> None:
59+
if "guard" not in data:
60+
return
61+
unknown = sorted(set(data["guard"]) - _GUARD_SECTION_KEYS)
62+
if unknown:
63+
msg = f"Unknown [guard] config keys: {', '.join(unknown)}"
64+
raise ConfigError(msg)
2465

2566

2667
def load_from_file(path: str | Path) -> dict[str, Any]:
@@ -218,6 +259,7 @@ def _build_collusion(data: dict[str, Any]) -> CollusionConfig:
218259

219260
def build_config(data: dict[str, Any]) -> GuardConfig:
220261
"""Build a GuardConfig from a raw dict (from TOML or env)."""
262+
_reject_unknown_guard_keys(data)
221263
guard_data = data.get("guard", data)
222264
kwargs: dict[str, Any] = {}
223265

@@ -237,6 +279,8 @@ def build_config(data: dict[str, Any]) -> GuardConfig:
237279
)
238280
if "fail_closed" in guard_data:
239281
kwargs["fail_closed"] = guard_data["fail_closed"]
282+
if "strict_scanner_allowlist" in guard_data:
283+
kwargs["strict_scanner_allowlist"] = bool(guard_data["strict_scanner_allowlist"])
240284

241285
pipeline_data = guard_data.get("pipeline", data.get("pipeline", {}))
242286
if pipeline_data:

‎sdk/src/unplug/guard.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __init__(
100100
self,
101101
*,
102102
scanners: list[str] | None = None,
103-
mode: str = "local",
103+
mode: str | None = None,
104104
server_url: str | None = None,
105105
server_api_key: str | None = None,
106106
fail_mode: str = "closed",
@@ -123,7 +123,9 @@ def __init__(
123123
DeprecationWarning,
124124
stacklevel=2,
125125
)
126-
overrides: dict[str, Any] = {"mode": mode, "fail_closed": fail_mode == "closed"}
126+
overrides: dict[str, Any] = {"fail_closed": fail_mode == "closed"}
127+
if mode is not None:
128+
overrides["mode"] = mode
127129
if scanners is not None:
128130
overrides["scanners"] = scanners
129131
if server_url is not None:

‎sdk/tests/integration/test_guard_server_mode.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from unplug import Guard
88
from unplug.api.enums import Action
9+
from unplug.config.guard import GuardConfig
910
from unplug.models import ScanResult
1011

1112

@@ -50,3 +51,18 @@ def test_scan_request_uses_client(self) -> None:
5051
out = guard.scan_request(req)
5152
assert out.safe is True
5253
mock_cls.return_value.scan_request.assert_called_once()
54+
55+
def test_config_server_mode_not_overridden_by_default(self) -> None:
56+
cfg = GuardConfig(mode="server", server_url="http://unplug.test")
57+
with patch("unplug.guard.UnplugClient") as mock_cls:
58+
guard = Guard(config=cfg)
59+
assert guard.config.mode == "server"
60+
assert guard.is_server_mode is True
61+
mock_cls.assert_called_once_with(base_url="http://unplug.test", api_key=None)
62+
63+
def test_explicit_mode_overrides_config(self) -> None:
64+
cfg = GuardConfig(mode="server", server_url="http://unplug.test")
65+
with patch("unplug.guard.UnplugClient"):
66+
guard = Guard(config=cfg, mode="local")
67+
assert guard.config.mode == "local"
68+
assert guard.is_server_mode is False

‎sdk/tests/unit/config/test_config_loader.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ def test_with_scanner_configs(self):
134134
assert "injection" in cfg.scanner_configs
135135
assert cfg.scanner_configs["injection"].base_score == 0.9
136136

137+
def test_strict_scanner_allowlist_from_guard_section(self) -> None:
138+
cfg = build_config({"guard": {"strict_scanner_allowlist": True}})
139+
assert cfg.strict_scanner_allowlist is True
140+
141+
def test_unknown_guard_key_raises(self) -> None:
142+
from unplug.exceptions import ConfigError
143+
144+
with pytest.raises(ConfigError, match="Unknown \\[guard\\] config keys"):
145+
build_config({"guard": {"strict_scanner_allowlist_typo": True}})
146+
137147

138148
class TestLoad:
139149
def test_from_file(self, toml_file: Path):
@@ -246,6 +256,15 @@ def test_limits_from_toml(self, tmp_path: Path) -> None:
246256
assert cfg.limits.max_input_chars == 100
247257
assert cfg.limits.blocked_tools == ["danger"]
248258

259+
def test_strict_scanner_allowlist_from_toml(self, tmp_path: Path) -> None:
260+
p = tmp_path / "unplug.toml"
261+
p.write_text("""\
262+
[guard]
263+
strict_scanner_allowlist = true
264+
""")
265+
cfg = load(file_path=p)
266+
assert cfg.strict_scanner_allowlist is True
267+
249268
def test_messages_from_toml(self, tmp_path: Path) -> None:
250269
p = tmp_path / "unplug.toml"
251270
p.write_text("""\

0 commit comments

Comments
 (0)