Spaces:
Configuration error
Configuration error
File size: 1,457 Bytes
e4f3d12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | from __future__ import annotations
import json
from fastapi.testclient import TestClient
from commitguard_env.server import app
FORBIDDEN_KEYS = {
"is_vulnerable",
"label",
"ground_truth",
"cwe_type",
"cwe",
"target_file_with_label",
}
def _walk(obj):
if isinstance(obj, dict):
for k, v in obj.items():
yield ("key", k)
yield from _walk(v)
elif isinstance(obj, list):
for v in obj:
yield from _walk(v)
elif isinstance(obj, str):
yield ("str", obj)
def test_reset_and_step_do_not_leak_ground_truth() -> None:
client = TestClient(app)
r = client.post("/reset")
assert r.status_code == 200
reset_payload = r.json()
s = client.post("/step", json={"action": "<action><action_type>analyze</action_type></action>"})
assert s.status_code == 200
step_payload = s.json()
for payload in (reset_payload, step_payload):
flat = list(_walk(payload))
keys = {v for t, v in flat if t == "key"}
assert not (keys & FORBIDDEN_KEYS)
# Also guard against obvious label-bearing strings in any nested content.
strings = [v.lower() for t, v in flat if t == "str"]
suspicious = ("this sample is vulnerable", "ground truth", "label:")
assert not any(any(tok in s for tok in suspicious) for s in strings)
# Ensure payload is valid JSON-serializable.
json.dumps(payload)
|