-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_m5_acceptance.py
More file actions
493 lines (405 loc) · 18.7 KB
/
Copy pathtest_m5_acceptance.py
File metadata and controls
493 lines (405 loc) · 18.7 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
"""M5 Acceptance Suite — Stronger Isolation certification checklist.
This is the single named acceptance suite for M5. If all tests in this
module pass, the appliance meets the M5 "Stronger Isolation" bar.
Verified areas:
1. Attestation and startup gating
2. Integrity degradation and containment
3. Policy centralization and denial
4. HSM/TPM key path behavior
5. Replay resistance
6. MCP taint enforcement
7. Adversarial prompt/tool regression
8. Supply-chain artifact verification
9. Operator recovery workflow
10. Workspace isolation
11. Step signature validation
"""
import os
import sys
import tempfile
import time
import unittest
from pathlib import Path
# Add services/ to path (same convention as test_agent.py)
_services_root = str(Path(__file__).resolve().parent.parent / "services")
if _services_root not in sys.path:
sys.path.insert(0, _services_root)
from agent.agent.models import (
CapabilityToken,
RiskLevel,
SensitivityLevel,
SessionMode,
Step,
StepAction,
)
from agent.agent.policy import PolicyEngine, classify_risk
from agent.agent.storage import StorageGateway
from agent.agent.capabilities import (
clear_nonce_cache,
create_token,
verify_token,
_reset_signing_key,
)
from agent.agent.sandbox import (
WorkspaceGuard,
sign_step,
verify_step_signature,
revalidate_step_capability,
)
# =========================================================================
# M5-ACC-1: Attestation and startup gating
# =========================================================================
class TestM5_Attestation(unittest.TestCase):
"""Verify that attestation primitives function correctly."""
def test_attestation_service_exists(self):
"""Runtime attestor service code must exist."""
attestor_path = os.path.join(
os.path.dirname(__file__), "..", "services", "runtime-attestor", "main.go"
)
self.assertTrue(os.path.isfile(attestor_path),
"runtime-attestor/main.go must exist")
def test_attestation_integration_exists(self):
"""Attestor → incident-recorder integration must exist."""
integration_path = os.path.join(
os.path.dirname(__file__), "..", "services", "runtime-attestor", "integrations.go"
)
self.assertTrue(os.path.isfile(integration_path),
"runtime-attestor/integrations.go must exist")
def test_startup_gating_systemd_dependency(self):
"""Attestor systemd unit must be Before: critical services."""
unit_path = os.path.join(
os.path.dirname(__file__), "..",
"files", "system", "usr", "lib", "systemd", "system",
"secure-ai-runtime-attestor.service",
)
if os.path.isfile(unit_path):
content = Path(unit_path).read_text()
self.assertIn("Before=", content,
"attestor must gate downstream services via Before=")
# =========================================================================
# M5-ACC-2: Integrity degradation and containment
# =========================================================================
class TestM5_IntegrityDegradation(unittest.TestCase):
"""Verify integrity monitoring and auto-containment."""
def test_integrity_monitor_exists(self):
"""Integrity monitor service code must exist."""
monitor_path = os.path.join(
os.path.dirname(__file__), "..", "services", "integrity-monitor", "main.go"
)
self.assertTrue(os.path.isfile(monitor_path))
def test_incident_recorder_exists(self):
"""Incident recorder with containment must exist."""
recorder_path = os.path.join(
os.path.dirname(__file__), "..", "services", "incident-recorder", "main.go"
)
self.assertTrue(os.path.isfile(recorder_path))
def test_containment_actions_defined(self):
"""Containment actions must be defined in containment.go."""
containment_path = os.path.join(
os.path.dirname(__file__), "..", "services", "incident-recorder", "containment.go"
)
self.assertTrue(os.path.isfile(containment_path))
content = Path(containment_path).read_text()
for action in ["freeze_agent", "disable_airlock", "force_vault_relock", "quarantine_model"]:
self.assertIn(action, content,
f"containment action {action} must be defined")
def test_enforcement_chain_tests_exist(self):
"""End-to-end enforcement chain tests must exist."""
test_path = os.path.join(
os.path.dirname(__file__), "..", "services", "incident-recorder",
"enforcement_chain_test.go",
)
self.assertTrue(os.path.isfile(test_path))
# =========================================================================
# M5-ACC-3: Policy centralization
# =========================================================================
class TestM5_PolicyCentralization(unittest.TestCase):
"""Verify unified policy decision engine."""
def test_policy_engine_service_exists(self):
"""Policy engine service code must exist."""
engine_path = os.path.join(
os.path.dirname(__file__), "..", "services", "policy-engine", "main.go"
)
self.assertTrue(os.path.isfile(engine_path))
def test_agent_policy_engine_works(self):
"""Agent-side policy engine correctly classifies risk."""
_engine = PolicyEngine() # Verify construction succeeds
# Auto-allow actions
for action in [StepAction.LOCAL_SEARCH, StepAction.SUMMARIZE, StepAction.DRAFT]:
risk = classify_risk(action)
self.assertEqual(risk, RiskLevel.AUTO,
f"{action.value} should be auto-allowed")
# Hard-approval actions
for action in [StepAction.OUTBOUND_REQUEST, StepAction.TRUST_CHANGE]:
risk = classify_risk(action)
self.assertEqual(risk, RiskLevel.APPROVAL_REQUIRED,
f"{action.value} should require approval")
def test_always_deny_enforced(self):
"""CHANGE_SECURITY must always be denied."""
cap = _make_cap()
step = Step(action=StepAction.CHANGE_SECURITY, params={})
engine = PolicyEngine()
decision, _ = engine.evaluate(step, cap)
self.assertEqual(decision, "deny")
# =========================================================================
# M5-ACC-4: HSM/TPM key path behavior
# =========================================================================
class TestM5_KeyManagement(unittest.TestCase):
"""Verify keystore abstraction and signing."""
def test_keystore_module_exists(self):
"""Keystore module must exist."""
keystore_path = os.path.join(
os.path.dirname(__file__), "..", "services", "agent", "agent", "keystore.py"
)
self.assertTrue(os.path.isfile(keystore_path),
"keystore.py must exist for HSM/TPM2 key management")
def test_token_signing_and_verification(self):
"""HMAC-signed tokens must verify correctly."""
clear_nonce_cache()
_reset_signing_key()
cap = create_token(
SessionMode.STANDARD,
task_id="m5-key-test",
intent="test HSM path",
)
self.assertNotEqual(cap.signature, "")
valid, reason = verify_token(cap)
self.assertTrue(valid, reason)
# =========================================================================
# M5-ACC-5: Replay resistance
# =========================================================================
class TestM5_ReplayResistance(unittest.TestCase):
"""Verify nonce-based replay protection."""
def test_nonce_replay_blocked(self):
"""Replayed tokens must be rejected."""
clear_nonce_cache()
_reset_signing_key()
cap = create_token(
SessionMode.STANDARD,
task_id="m5-replay",
intent="replay test",
)
valid1, _ = verify_token(cap)
self.assertTrue(valid1, "first use should succeed")
valid2, reason = verify_token(cap)
self.assertFalse(valid2, "replay should fail")
self.assertIn("replay", reason)
def test_expired_token_blocked(self):
"""Expired tokens must be rejected."""
clear_nonce_cache()
_reset_signing_key()
cap = create_token(
SessionMode.STANDARD,
task_id="m5-expiry",
intent="expiry test",
ttl_seconds=0.01,
)
time.sleep(0.05)
valid, _ = verify_token(cap)
self.assertFalse(valid)
# =========================================================================
# M5-ACC-6: MCP taint enforcement
# =========================================================================
class TestM5_MCPTaintEnforcement(unittest.TestCase):
"""Verify MCP taint tracking primitives exist."""
def test_mcp_firewall_taint_module_exists(self):
"""MCP firewall taint tracking must exist."""
taint_path = os.path.join(
os.path.dirname(__file__), "..", "services", "mcp-firewall", "taint.go"
)
self.assertTrue(os.path.isfile(taint_path))
def test_mcp_firewall_isolation_module_exists(self):
"""MCP firewall isolation enhancements must exist."""
isolation_path = os.path.join(
os.path.dirname(__file__), "..", "services", "mcp-firewall", "isolation.go"
)
self.assertTrue(os.path.isfile(isolation_path))
# =========================================================================
# M5-ACC-7: Adversarial prompt/tool regression
# =========================================================================
class TestM5_AdversarialRegression(unittest.TestCase):
"""Verify adversarial test suite exists and key checks pass."""
def test_adversarial_test_file_exists(self):
"""Adversarial test suite must exist."""
test_path = os.path.join(
os.path.dirname(__file__), "test_adversarial.py"
)
self.assertTrue(os.path.isfile(test_path))
def test_path_traversal_denied(self):
"""Path traversal must be blocked by storage gateway."""
gw = StorageGateway(vault_root=tempfile.mkdtemp())
cap = _make_cap(readable_paths=["/var/lib/secure-ai/vault/**"])
result = gw.read_file("../../../../etc/shadow", cap)
self.assertFalse(result["ok"])
def test_blocked_paths_enforced(self):
"""Critical system paths must be blocked."""
gw = StorageGateway(vault_root=tempfile.mkdtemp())
cap = _make_cap(readable_paths=["/**"])
for path in ["/etc/shadow", "/etc/passwd", "/run/secure-ai/service-token"]:
result = gw.read_file(path, cap)
self.assertFalse(result["ok"], f"{path} should be blocked")
# =========================================================================
# M5-ACC-8: Supply-chain artifact verification
# =========================================================================
class TestM5_SupplyChain(unittest.TestCase):
"""Verify supply-chain workflows and docs exist."""
def test_release_workflow_exists(self):
"""Release workflow with provenance must exist."""
workflow_path = os.path.join(
os.path.dirname(__file__), "..", ".github", "workflows", "release.yml"
)
self.assertTrue(os.path.isfile(workflow_path))
def test_build_workflow_exists(self):
"""Build workflow with SBOM must exist."""
workflow_path = os.path.join(
os.path.dirname(__file__), "..", ".github", "workflows", "build.yml"
)
self.assertTrue(os.path.isfile(workflow_path))
def test_ci_supply_chain_check_exists(self):
"""CI must have supply-chain-verify job."""
ci_path = os.path.join(
os.path.dirname(__file__), "..", ".github", "workflows", "ci.yml"
)
content = Path(ci_path).read_text()
self.assertIn("supply-chain-verify", content)
def test_provenance_doc_exists(self):
"""Supply chain provenance doc must exist."""
doc_path = os.path.join(
os.path.dirname(__file__), "..", "docs", "supply-chain-provenance.md"
)
self.assertTrue(os.path.isfile(doc_path))
# =========================================================================
# M5-ACC-9: Operator recovery workflow
# =========================================================================
class TestM5_RecoveryWorkflow(unittest.TestCase):
"""Verify recovery ceremony primitives exist."""
def test_recovery_module_exists(self):
"""Recovery ceremony code must exist."""
recovery_path = os.path.join(
os.path.dirname(__file__), "..", "services", "incident-recorder", "recovery.go"
)
self.assertTrue(os.path.isfile(recovery_path))
def test_recovery_test_exists(self):
"""Recovery ceremony tests must exist."""
test_path = os.path.join(
os.path.dirname(__file__), "..", "services", "incident-recorder", "recovery_test.go"
)
self.assertTrue(os.path.isfile(test_path))
# =========================================================================
# M5-ACC-10: Workspace isolation
# =========================================================================
class TestM5_WorkspaceIsolation(unittest.TestCase):
"""Verify workspace hard wall enforcement."""
def test_workspace_guard_blocks_cross_workspace(self):
"""WorkspaceGuard must block cross-workspace access."""
with tempfile.TemporaryDirectory() as tmpdir:
ws_a = os.path.join(tmpdir, "workspace_a")
ws_b = os.path.join(tmpdir, "workspace_b")
os.makedirs(ws_a)
os.makedirs(ws_b)
guard = WorkspaceGuard({"ws_a": ws_a, "ws_b": ws_b})
# Access within workspace
test_file = os.path.join(ws_a, "test.txt")
Path(test_file).write_text("data")
valid, _ = guard.validate_path(test_file, "ws_a")
self.assertTrue(valid)
# Cross-workspace access
valid, _ = guard.validate_path(test_file, "ws_b")
self.assertFalse(valid)
@unittest.skipIf(sys.platform == "win32", "symlinks require admin privileges on Windows")
def test_symlink_escape_blocked(self):
"""Symlink escape from workspace must be detected."""
with tempfile.TemporaryDirectory() as tmpdir:
workspace = os.path.join(tmpdir, "workspace")
os.makedirs(workspace)
secret = os.path.join(tmpdir, "secret.txt")
Path(secret).write_text("sensitive")
link = os.path.join(workspace, "escape_link")
os.symlink(secret, link)
guard = WorkspaceGuard({"ws": workspace})
valid, _ = guard.validate_path(link, "ws")
self.assertFalse(valid, "symlink escape should be blocked")
# =========================================================================
# M5-ACC-11: Step signature validation
# =========================================================================
class TestM5_StepSignatures(unittest.TestCase):
"""Verify step-level integrity protection."""
def test_step_sign_and_verify(self):
"""Signed steps must verify correctly."""
cap = _make_cap(task_id="m5-sig")
step = Step(action=StepAction.READ_FILE, params={"path": "/tmp/test"})
sig = sign_step(step, cap)
valid, _ = verify_step_signature(step, cap, sig)
self.assertTrue(valid)
def test_tampered_step_rejected(self):
"""Tampered steps must fail verification."""
cap = _make_cap(task_id="m5-sig-tamper")
step = Step(action=StepAction.READ_FILE, params={"path": "/tmp/test"})
sig = sign_step(step, cap)
step.params["path"] = "/etc/shadow"
valid, _ = verify_step_signature(step, cap, sig)
self.assertFalse(valid)
def test_revalidation_catches_mutation(self):
"""Re-validation must catch scope mutations."""
cap = _make_cap(readable_paths=["/var/lib/secure-ai/vault/**"])
step = Step(action=StepAction.READ_FILE, params={"path": "/etc/shadow"})
valid, _ = revalidate_step_capability(step, cap)
self.assertFalse(valid)
# =========================================================================
# M5-ACC-12: Control matrix doc
# =========================================================================
class TestM5_ControlMatrix(unittest.TestCase):
"""Verify M5 control matrix documentation exists."""
def test_control_matrix_exists(self):
"""M5 control matrix must exist."""
doc_path = os.path.join(
os.path.dirname(__file__), "..", "docs", "m5-control-matrix.md"
)
self.assertTrue(os.path.isfile(doc_path))
content = Path(doc_path).read_text()
self.assertIn("Control Matrix", content)
self.assertIn("Enforcing Component", content)
self.assertIn("Failure Mode", content)
# =========================================================================
# M5-ACC-SUMMARY: All services exist
# =========================================================================
class TestM5_ServiceInventory(unittest.TestCase):
"""Verify all required services exist in the repository."""
REQUIRED_SERVICES = [
"airlock", "registry", "tool-firewall", "gpu-integrity-watch",
"mcp-firewall", "policy-engine", "runtime-attestor",
"integrity-monitor", "incident-recorder",
]
def test_all_go_services_exist(self):
"""All 9 Go services must have main.go and main_test.go."""
services_dir = os.path.join(os.path.dirname(__file__), "..", "services")
for svc in self.REQUIRED_SERVICES:
main_go = os.path.join(services_dir, svc, "main.go")
self.assertTrue(os.path.isfile(main_go),
f"services/{svc}/main.go must exist")
test_go = os.path.join(services_dir, svc, "main_test.go")
self.assertTrue(os.path.isfile(test_go),
f"services/{svc}/main_test.go must exist")
# =========================================================================
# Helper
# =========================================================================
def _make_cap(**kwargs):
"""Create a test capability token."""
defaults = {
"readable_paths": ["/var/lib/secure-ai/vault/user_docs/**"],
"writable_paths": ["/var/lib/secure-ai/vault/outputs/**"],
"allowed_tools": ["filesystem.read", "filesystem.list"],
"allow_online": False,
"sensitivity_ceiling": SensitivityLevel.MEDIUM,
"session_mode": SessionMode.STANDARD,
"configurable_prefs": {},
"task_id": kwargs.pop("task_id", "m5-test"),
"intent_hash": "",
"policy_digest": "",
"issued_at": time.time(),
"expires_at": 0.0,
}
defaults.update(kwargs)
return CapabilityToken(**defaults)
if __name__ == "__main__":
unittest.main()