File size: 26,169 Bytes
49d1c75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dabed55
49d1c75
 
 
 
 
 
 
dabed55
 
 
49d1c75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dabed55
 
49d1c75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
"""Edge case tests for SnapshotRenderer.

Tests unusual/boundary specs: no flags, no users, no firewall rules,
db-only flags, file-only flags, multiple vulns, empty golden path.
"""

from __future__ import annotations

import tempfile
from pathlib import Path

import pytest

from open_range.builder.renderer import SnapshotRenderer, _build_context
from open_range.protocols import (
    FlagSpec,
    GoldenPathStep,
    NPCTrafficSpec,
    SnapshotSpec,
    TaskSpec,
    TruthGraph,
    Vulnerability,
)


@pytest.fixture
def renderer():
    return SnapshotRenderer()


def _minimal_topology(**overrides):
    """Build a minimal topology dict, with optional overrides."""
    topo = {
        "hosts": ["web", "db"],
        "zones": {"dmz": ["web"], "internal": ["db"]},
        "users": [],
        "firewall_rules": [],
    }
    topo.update(overrides)
    return topo


# ---------------------------------------------------------------------------
# Spec with no flags
# ---------------------------------------------------------------------------


class TestNoFlags:
    """Spec with zero flags should render without errors."""

    def test_renders_without_error(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(
                vulns=[Vulnerability(id="v1", type="sqli", host="web")]
            ),
            flags=[],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "no_flags"
            renderer.render(spec, out)
            for fname in ["docker-compose.yml", "Dockerfile.web", "init.sql"]:
                assert (out / fname).exists()

    def test_dockerfile_has_no_flag_lines(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "no_flags"
            renderer.render(spec, out)
            dockerfile = (out / "Dockerfile.web").read_text()
            assert "FLAG{" not in dockerfile

    def test_context_has_empty_flags(self):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx["flags"] == []


# ---------------------------------------------------------------------------
# Spec with no users
# ---------------------------------------------------------------------------


class TestNoUsers:
    """Spec with zero topology users should render cleanly."""

    def test_renders_without_error(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(users=[]),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "no_users"
            renderer.render(spec, out)
            assert (out / "Dockerfile.web").exists()

    def test_dockerfile_has_no_useradd(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(users=[]),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "no_users"
            renderer.render(spec, out)
            dockerfile = (out / "Dockerfile.web").read_text()
            assert "useradd" not in dockerfile

    def test_context_defaults_db_user(self):
        """With no users, context should synthesize a service DB account."""
        spec = SnapshotSpec(
            topology=_minimal_topology(users=[]),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx["db_user"] == "svc_db"
        assert ctx["db_pass"]
        assert ctx["db_pass"] != "AppUs3r!2024"


# ---------------------------------------------------------------------------
# Spec with no firewall rules
# ---------------------------------------------------------------------------


class TestNoFirewallRules:
    """Spec with no firewall rules should render iptables with defaults."""

    def test_renders_iptables_without_error(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(firewall_rules=[]),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "no_fw"
            renderer.render(spec, out)
            rules = (out / "iptables.rules").read_text()
            assert "*filter" in rules
            assert "COMMIT" in rules

    def test_context_has_empty_firewall_rules(self):
        spec = SnapshotSpec(
            topology=_minimal_topology(firewall_rules=[]),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx["firewall_rules"] == []


# ---------------------------------------------------------------------------
# Spec with db-only flags
# ---------------------------------------------------------------------------


class TestDBOnlyFlags:
    """Flags on the db host with db: paths should appear in SQL, not Dockerfile."""

    def test_flag_not_in_dockerfile(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(
                vulns=[Vulnerability(id="v1", type="idor", host="web")]
            ),
            flags=[
                FlagSpec(
                    id="flag1",
                    value="FLAG{db_only_flag}",
                    path="db:flags.secrets.flag",
                    host="db",
                ),
            ],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "db_only"
            renderer.render(spec, out)
            dockerfile = (out / "Dockerfile.web").read_text()
            assert "FLAG{db_only_flag}" not in dockerfile

    def test_flag_path_with_mysql_prefix_not_in_dockerfile(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[
                FlagSpec(
                    id="flag1",
                    value="FLAG{mysql_flag}",
                    path="mysql:flags.secrets.flag",
                    host="db",
                ),
            ],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "mysql_flag"
            renderer.render(spec, out)
            dockerfile = (out / "Dockerfile.web").read_text()
            assert "FLAG{mysql_flag}" not in dockerfile

    def test_db_flag_host_not_web(self, renderer):
        """Flag with host='db' should never appear in Dockerfile.web regardless of path."""
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[
                FlagSpec(
                    id="flag1",
                    value="FLAG{host_db}",
                    path="/some/path/flag.txt",
                    host="db",
                ),
            ],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "db_host"
            renderer.render(spec, out)
            dockerfile = (out / "Dockerfile.web").read_text()
            # Template checks flag.host == 'web', so db-hosted flag shouldn't be there
            assert "FLAG{host_db}" not in dockerfile


# ---------------------------------------------------------------------------
# Spec with file-only flags
# ---------------------------------------------------------------------------


class TestFileOnlyFlags:
    """Flags stored as files on web should appear in Dockerfile, not SQL."""

    def test_flag_in_dockerfile(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[
                FlagSpec(
                    id="flag1",
                    value="FLAG{file_only}",
                    path="/var/flags/flag1.txt",
                    host="web",
                ),
            ],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "file_only"
            renderer.render(spec, out)
            dockerfile = (out / "Dockerfile.web").read_text()
            assert "FLAG{file_only}" in dockerfile
            assert "/var/flags/flag1.txt" in dockerfile

    def test_flag_not_in_sql(self, renderer):
        """File-based flag should NOT appear in init.sql (it's template-generated, static)."""
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[
                FlagSpec(
                    id="flag1",
                    value="FLAG{file_only_check}",
                    path="/var/flags/flag1.txt",
                    host="web",
                ),
            ],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "file_only_sql"
            renderer.render(spec, out)
            sql = (out / "init.sql").read_text()
            assert "FLAG{file_only_check}" not in sql

    def test_multiple_file_flags(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[
                FlagSpec(
                    id="flag1",
                    value="FLAG{first}",
                    path="/var/flags/flag1.txt",
                    host="web",
                ),
                FlagSpec(
                    id="flag2",
                    value="FLAG{second}",
                    path="/var/flags/flag2.txt",
                    host="web",
                ),
            ],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "multi_flags"
            renderer.render(spec, out)
            dockerfile = (out / "Dockerfile.web").read_text()
            assert "FLAG{first}" in dockerfile
            assert "FLAG{second}" in dockerfile
            assert "/var/flags/flag1.txt" in dockerfile
            assert "/var/flags/flag2.txt" in dockerfile


# ---------------------------------------------------------------------------
# Spec with multiple vulnerability types
# ---------------------------------------------------------------------------


class TestMultipleVulnTypes:
    """Multiple vuln types should enable correct nginx endpoint blocks."""

    def test_sqli_and_path_traversal_both_enabled(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(
                vulns=[
                    Vulnerability(
                        id="v1", type="sqli", host="web",
                        injection_point="/search?q=",
                    ),
                    Vulnerability(
                        id="v2", type="path_traversal", host="web",
                        injection_point="/download?file=",
                    ),
                ]
            ),
            flags=[],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "multi_vuln"
            renderer.render(spec, out)
            nginx = (out / "nginx.conf").read_text()
            assert "/search" in nginx
            assert "/download" in nginx

    def test_context_enables_both_endpoints(self):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(
                vulns=[
                    Vulnerability(id="v1", type="sqli", host="web",
                                  injection_point="/search?q="),
                    Vulnerability(id="v2", type="path_traversal", host="web",
                                  injection_point="/download?file="),
                ]
            ),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx.get("search_endpoint") is True
        assert ctx.get("download_endpoint") is True

    def test_idor_does_not_enable_search_or_download(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(
                vulns=[
                    Vulnerability(
                        id="v1", type="idor", host="web",
                        injection_point="/api/users/{id}",
                    ),
                ]
            ),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert "search_endpoint" not in ctx
        assert "download_endpoint" not in ctx

    def test_three_vulns_render_without_error(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(
                vulns=[
                    Vulnerability(id="v1", type="sqli", host="web",
                                  injection_point="/search?q="),
                    Vulnerability(id="v2", type="path_traversal", host="web",
                                  injection_point="/download?file="),
                    Vulnerability(id="v3", type="xss", host="web",
                                  injection_point="/comment"),
                ]
            ),
            flags=[],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "three_vulns"
            renderer.render(spec, out)
            for fname in ["docker-compose.yml", "Dockerfile.web", "nginx.conf",
                          "init.sql", "iptables.rules"]:
                assert (out / fname).exists()


# ---------------------------------------------------------------------------
# Spec with empty golden path
# ---------------------------------------------------------------------------


class TestEmptyGoldenPath:
    """Spec with no golden path steps should render normally."""

    def test_renders_without_error(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(
                vulns=[Vulnerability(id="v1", type="sqli", host="web")]
            ),
            flags=[
                FlagSpec(id="f1", value="FLAG{x}", path="/var/flags/f.txt", host="web"),
            ],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "no_gp"
            renderer.render(spec, out)
            assert (out / "docker-compose.yml").exists()

    def test_compose_still_valid(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "no_gp_compose"
            renderer.render(spec, out)
            compose = (out / "docker-compose.yml").read_text()
            assert "services:" in compose
            assert "web:" in compose


# ---------------------------------------------------------------------------
# Spec with only one host
# ---------------------------------------------------------------------------


class TestSingleHost:
    """Spec with only one host in topology."""

    def test_renders_without_error(self, renderer):
        spec = SnapshotSpec(
            topology={
                "hosts": ["web"],
                "zones": {"dmz": ["web"]},
                "users": [],
                "firewall_rules": [],
            },
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "single_host"
            renderer.render(spec, out)
            assert (out / "docker-compose.yml").exists()

    def test_context_has_one_host(self):
        spec = SnapshotSpec(
            topology={
                "hosts": ["web"],
                "zones": {"dmz": ["web"]},
                "users": [],
                "firewall_rules": [],
            },
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx["host_names"] == ["web"]
        assert len(ctx["hosts"]) == 1


# ---------------------------------------------------------------------------
# Spec with dict-format hosts
# ---------------------------------------------------------------------------


class TestDictFormatHosts:
    """Topology with hosts as list of dicts rather than strings."""

    def test_renders_with_dict_hosts(self, renderer):
        spec = SnapshotSpec(
            topology={
                "hosts": [
                    {"name": "web", "zone": "dmz", "networks": ["dmz", "internal"]},
                    {"name": "db", "zone": "internal", "depends_on": ["ldap"]},
                ],
                "zones": {"dmz": ["web"], "internal": ["db"]},
                "users": [],
                "firewall_rules": [],
            },
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "dict_hosts"
            renderer.render(spec, out)
            assert (out / "docker-compose.yml").exists()

    def test_context_preserves_dict_host_info(self):
        spec = SnapshotSpec(
            topology={
                "hosts": [
                    {"name": "web", "zone": "dmz", "networks": ["dmz", "internal"]},
                    {"name": "db", "zone": "internal", "depends_on": ["ldap"]},
                ],
                "zones": {"dmz": ["web"], "internal": ["db"]},
                "users": [],
                "firewall_rules": [],
            },
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        web_host = next(h for h in ctx["hosts"] if h["name"] == "web")
        assert "dmz" in web_host["networks"]
        assert "internal" in web_host["networks"]

        db_host = next(h for h in ctx["hosts"] if h["name"] == "db")
        assert db_host["depends_on"] == ["ldap"]


# ---------------------------------------------------------------------------
# Zone CIDR mapping edge cases
# ---------------------------------------------------------------------------


class TestZoneCIDRMapping:
    """Verify zone-to-CIDR mapping for known and unknown zones."""

    def test_known_zones(self):
        spec = SnapshotSpec(
            topology={
                "hosts": ["web", "db"],
                "zones": {"external": ["web"], "dmz": ["web"], "internal": ["db"],
                          "management": ["db"]},
                "users": [],
                "firewall_rules": [],
            },
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx["zone_cidrs"]["external"] == "0.0.0.0/0"
        assert ctx["zone_cidrs"]["dmz"] == "10.0.1.0/24"
        assert ctx["zone_cidrs"]["internal"] == "10.0.2.0/24"
        assert ctx["zone_cidrs"]["management"] == "10.0.3.0/24"

    def test_unknown_zone_gets_default(self):
        spec = SnapshotSpec(
            topology={
                "hosts": ["web"],
                "zones": {"custom_zone": ["web"]},
                "users": [],
                "firewall_rules": [],
            },
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx["zone_cidrs"]["custom_zone"] == "0.0.0.0/0"


# ---------------------------------------------------------------------------
# DB user/password resolution edge cases
# ---------------------------------------------------------------------------


class TestDBUserResolution:
    """Verify _find_db_user and _find_db_pass edge cases."""

    def test_admin_user_not_picked_as_db_user(self):
        """Users in 'admins' group should not be picked as db_user."""
        spec = SnapshotSpec(
            topology=_minimal_topology(users=[
                {
                    "username": "root_admin",
                    "password": "AdminPass!",
                    "groups": ["admins"],
                    "hosts": ["db"],
                },
            ]),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        # Should use synthesized service account since only admin has db access.
        assert ctx["db_user"] == "svc_db"

    def test_non_admin_db_user_picked(self):
        """Non-admin user with db access should be picked."""
        spec = SnapshotSpec(
            topology=_minimal_topology(users=[
                {
                    "username": "dbworker",
                    "password": "Work3r!Pass",
                    "groups": ["users"],
                    "hosts": ["db", "web"],
                },
            ]),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx["db_user"] == "dbworker"
        assert ctx["db_pass"] == "Work3r!Pass"

    def test_mysql_root_pass_from_admin_user(self):
        """Admin user with db access provides mysql root password."""
        spec = SnapshotSpec(
            topology=_minimal_topology(users=[
                {
                    "username": "admin",
                    "password": "CustomR00t!",
                    "groups": ["admins"],
                    "hosts": ["db"],
                },
            ]),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx["mysql_root_password"] == "CustomR00t!"

    def test_mysql_root_pass_default(self):
        """No admin user should fall back to default root password."""
        spec = SnapshotSpec(
            topology=_minimal_topology(users=[]),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx["mysql_root_password"] == "r00tP@ss!"


# ---------------------------------------------------------------------------
# Context: app_files from spec.files
# ---------------------------------------------------------------------------


class TestAppFiles:
    """Verify app_files context variable from spec.files."""

    def test_app_files_populated_when_spec_has_files(self):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
            files={"web:/var/www/portal/test.php": "<?php echo 1; ?>"},
        )
        ctx = _build_context(spec)
        assert "web:/var/www/portal/test.php" in ctx["app_files"]

    def test_app_files_empty_when_no_files(self):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[],
            golden_path=[],
        )
        ctx = _build_context(spec)
        assert ctx["app_files"] == {}


# ---------------------------------------------------------------------------
# Mixed flag types: some db, some file
# ---------------------------------------------------------------------------


class TestMixedFlagTypes:
    """Spec with both db-hosted and file-hosted flags."""

    def test_only_file_flags_in_dockerfile(self, renderer):
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[
                FlagSpec(
                    id="flag_file",
                    value="FLAG{in_file}",
                    path="/var/flags/flag1.txt",
                    host="web",
                ),
                FlagSpec(
                    id="flag_db",
                    value="FLAG{in_db}",
                    path="db:flags.secrets.flag",
                    host="db",
                ),
            ],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "mixed"
            renderer.render(spec, out)
            dockerfile = (out / "Dockerfile.web").read_text()
            assert "FLAG{in_file}" in dockerfile
            assert "FLAG{in_db}" not in dockerfile

    def test_sql_template_does_not_contain_any_flags(self, renderer):
        """init.sql is static template, flags go in via db:sql at runtime."""
        spec = SnapshotSpec(
            topology=_minimal_topology(),
            truth_graph=TruthGraph(vulns=[]),
            flags=[
                FlagSpec(id="f1", value="FLAG{a}", path="/var/flags/f.txt", host="web"),
                FlagSpec(id="f2", value="FLAG{b}", path="db:flags.secrets.flag", host="db"),
            ],
            golden_path=[],
        )
        with tempfile.TemporaryDirectory() as tmpdir:
            out = Path(tmpdir) / "mixed_sql"
            renderer.render(spec, out)
            sql = (out / "init.sql").read_text()
            # Static template shouldn't have dynamic flag values
            assert "FLAG{a}" not in sql
            assert "FLAG{b}" not in sql