tenuo 0.1.0-beta.22

Agent Capability Flow Control - Rust core library
Documentation
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
//! Red Team Security Tests - Binary-Level Attack Scenarios
//!
//! These tests verify security properties that can only be tested at the Rust level
//! where we have direct access to serialization, signatures, and internal structures.
//!
//! ## Test Categories
//!
//! 1. **Parent Hash Attacks** - Modify linkage, skip chain elements
//! 2. **CBOR Parser Attacks** - Duplicate keys, unknown fields, malformed payloads
//! 3. **Signature Reuse** - Use signature from one warrant for another
//! 4. **Cycle Detection** - Create circular delegation chains
//! 5. **Clearance Violations** - Bypass clearance ceiling constraints
//! 6. **PoP Timestamp** - Manipulate PoP timestamp windows
//! 7. **Depth Limits** - Bypass MAX_DELEGATION_DEPTH
//! 8. **TTL Attacks** - Excessive TTL, time traveler scenarios
//! 9. **Type Confusion** - Wrong types to constraints (NaN, string to Range)
//! 10. **ReDoS** - Regex denial of service attempts
//!
//! Run: cargo test --test red_team -- --nocapture

use chrono::{Duration as ChronoDuration, Utc};
use std::collections::{BTreeMap, HashMap};
use std::time::Duration;
use tenuo::{
    constraints::{All, Constraint, ConstraintSet, ConstraintValue, Exact, OneOf, Pattern},
    crypto::SigningKey,
    planes::{Authorizer, DataPlane},
    warrant::{Clearance, Warrant, WarrantType},
    wire, Range, RegexConstraint, MAX_DELEGATION_DEPTH, MAX_WARRANT_TTL_SECS,
};

// ============================================================================
// Parent Hash Linkage Attacks
// ============================================================================

/// Test: Verify parent_hash correctly links child to parent.
///
/// The parent_hash is SHA256(parent.payload_bytes), providing cryptographic
/// linkage without embedding the full parent chain. Verification requires
/// a WarrantStack to trace the full ancestry.
#[test]
fn test_parent_hash_linkage() {
    use sha2::{Digest, Sha256};

    let parent_kp = SigningKey::generate();
    let child_kp = SigningKey::generate();

    // Create parent with limited tools
    let parent = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(parent_kp.public_key())
        .build(&parent_kp)
        .unwrap();

    // Attenuate to child (POLA: inherit_all)
    let child = parent
        .attenuate()
        .inherit_all()
        .holder(child_kp.public_key())
        .build(&parent_kp)
        .unwrap();

    // Verify parent_hash is set and matches
    let expected_hash: [u8; 32] = {
        let mut hasher = Sha256::new();
        hasher.update(parent.payload_bytes());
        hasher.finalize().into()
    };

    assert_eq!(
        child.parent_hash(),
        Some(&expected_hash),
        "Child's parent_hash should be SHA256 of parent's payload_bytes"
    );

    println!("✅ parent_hash correctly links child to parent");
    println!("   (Chain verification requires WarrantStack)");
}

// ============================================================================
// CBOR Payload Tampering Attacks
// ============================================================================

/// Attack: Test that payload_bytes is bound to parsed payload during deserialization.
///
/// This verifies the TOCTOU fix: parsed payload must match payload_bytes exactly.
/// We test the property by verifying serialize→deserialize→serialize is deterministic.
#[test]
fn test_cbor_payload_canonical_binding() {
    let keypair = SigningKey::generate();

    let mut constraints = ConstraintSet::new();
    constraints.insert("path", Pattern::new("/data/*").unwrap());
    let warrant = Warrant::builder()
        .capability("read", constraints)
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // Serialize
    let bytes1 = wire::encode(&warrant).unwrap();

    // Deserialize
    let decoded = wire::decode(&bytes1).unwrap();

    // Re-serialize
    let bytes2 = wire::encode(&decoded).unwrap();

    // Must be byte-identical (deterministic CBOR)
    assert_eq!(
        bytes1, bytes2,
        "Serialization must be deterministic (canonical binding)"
    );

    // Verify payload_bytes matches what's stored
    // The deserialization already checked canonical binding
    // Here we verify round-trip is lossless

    println!("✅ CBOR canonical binding verified (round-trip deterministic)");
}

/// Attack: Create warrant with non-standard CBOR and see if canonical check catches it.
///
/// The deserialization enforces that payload_bytes == serialize(payload sans chain).
/// This test verifies the check is active.
#[test]
fn test_payload_bytes_mismatch_detection() {
    let keypair = SigningKey::generate();

    // The canonical binding check is enforced during deserialization in Warrant::deserialize
    // It verifies that: recomputed_canonical_bytes == stored_payload_bytes
    //
    // This test verifies the check exists by confirming that:
    // 1. Valid warrants pass (canonical bytes match)
    // 2. Round-trip preserves canonical bytes

    let mut constraints = ConstraintSet::new();
    constraints.insert("path", Pattern::new("/data/*").unwrap());
    let warrant = Warrant::builder()
        .capability("read", constraints)
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // Serialize
    let bytes1 = wire::encode(&warrant).unwrap();

    // Deserialize (canonical binding check happens here)
    let decoded = wire::decode(&bytes1).unwrap();

    // Re-serialize
    let bytes2 = wire::encode(&decoded).unwrap();

    // Must be identical
    assert_eq!(bytes1, bytes2, "Round-trip must preserve canonical bytes");

    // The payload_bytes field is set during build() to be canonical
    // The deserialize() checks that recomputed canonical matches stored payload_bytes
    // If there was a mismatch, deserialize() would have failed

    println!("✅ CBOR payload_bytes canonical binding enforced at deserialization");
    println!("   (See Warrant::deserialize for the check)");
}

// ============================================================================
// Signature Reuse Attacks
// ============================================================================

/// Attack: Use signature from warrant A on warrant B.
///
/// Expected: Signature verification fails (different payload_bytes).
#[test]
fn test_signature_reuse_across_warrants() {
    let keypair = SigningKey::generate();

    // Create two warrants with different tools
    let warrant_a = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let warrant_b = Warrant::builder()
        .capability("write", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // ATTACK: Different warrants have different payload_bytes
    // If we could reuse signature A on warrant B, it would be catastrophic

    // Verify payloads are different
    assert_ne!(
        warrant_a.payload_bytes(),
        warrant_b.payload_bytes(),
        "Different warrants must have different payload_bytes"
    );

    // Verify that each warrant's signature only validates its own payload
    assert!(warrant_a.verify_signature().is_ok());
    assert!(warrant_b.verify_signature().is_ok());

    // Test cross-verification (sig_a on warrant_b's bytes)
    let sig_a = warrant_a.signature();
    let verify_result = warrant_b.issuer().verify(warrant_b.payload_bytes(), sig_a);

    assert!(
        verify_result.is_err(),
        "Signature from warrant A should not verify warrant B's payload"
    );

    println!("✅ Signature reuse blocked (payload_bytes binding enforced)");
}

// ============================================================================
// Cycle Detection Attacks
// ============================================================================

/// Attack: Verify that delegation creates proper parent→child relationships.
///
/// Cycles are prevented structurally:
/// 1. Depth monotonically increases
/// 2. Parent must exist before child
/// 3. parent_id is set automatically by attenuate()
///
/// We test that parent_id is correctly set and depth increases.
#[test]
fn test_parent_child_relationship_integrity() {
    let keypair = SigningKey::generate();

    let parent = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // POLA: inherit_all
    let child = parent.attenuate().inherit_all().build(&keypair).unwrap();

    // Verify parent_id is set correctly
    {
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        hasher.update(parent.payload_bytes());
        let parent_hash: [u8; 32] = hasher.finalize().into();
        assert_eq!(child.parent_hash(), Some(&parent_hash));
    }

    // Verify parent_hash links to parent
    assert!(
        child.parent_hash().is_some(),
        "Child should have parent_hash"
    );

    println!("✅ Parent-child relationship correctly maintained (cycles prevented by design)");
}

// ============================================================================
// Clearance Ceiling Violations
// ============================================================================

/// Attack: Issuer warrant with clearance=INTERNAL issues execution warrant with clearance=SYSTEM.
///
/// Expected: Validation error (child clearance exceeds ceiling).
#[test]
fn test_clearance_level_escalation() {
    let issuer_kp = SigningKey::generate();
    let worker_kp = SigningKey::generate();

    // Create issuer warrant with INTERNAL ceiling
    let issuer = Warrant::builder()
        .r#type(WarrantType::Issuer)
        .issuable_tools(vec!["read".to_string()])
        .clearance(Clearance::INTERNAL)
        .ttl(Duration::from_secs(3600))
        .holder(issuer_kp.public_key())
        .build(&issuer_kp)
        .unwrap();

    // ATTACK: Try to issue execution warrant with SYSTEM clearance (exceeds INTERNAL ceiling)
    let result = issuer.issue_execution_warrant().and_then(|builder| {
        builder
            .capability("read", ConstraintSet::new())
            .ttl(Duration::from_secs(3600)) // Add required ttl
            .clearance(Clearance::SYSTEM) // Exceeds ceiling
            .holder(worker_kp.public_key())
            .build(&issuer_kp)
    });

    assert!(
        result.is_err(),
        "Clearance level should not exceed issuer's ceiling"
    );

    let err = result.unwrap_err();
    // Error message might vary
    println!("✅ Clearance ceiling violation blocked: {}", err);
}

/// Attack: Holder attenuates an INTERNAL warrant and tries to raise child to PRIVILEGED.
///
/// Expected: MonotonicityViolation — child clearance cannot exceed parent's.
#[test]
fn test_clearance_escalation_via_attenuation() {
    let holder_kp = SigningKey::generate();
    let delegate_kp = SigningKey::generate();

    // Parent has INTERNAL clearance
    let parent = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .clearance(Clearance::INTERNAL)
        .ttl(Duration::from_secs(3600))
        .holder(holder_kp.public_key())
        .build(&holder_kp)
        .unwrap();

    // ATTACK: Attenuate and try to raise clearance to PRIVILEGED
    let result = parent
        .attenuate()
        .inherit_all()
        .clearance(Clearance::PRIVILEGED) // Exceeds parent's INTERNAL ceiling
        .holder(delegate_kp.public_key())
        .build(&holder_kp);

    assert!(
        result.is_err(),
        "Clearance escalation via attenuation must be rejected"
    );
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("clearance cannot increase") || err.contains("monotonicity"),
        "Expected monotonicity error, got: {err}"
    );
    println!("✅ Clearance escalation via attenuation blocked: {err}");
}

/// Attack: Holder attenuates a warrant with no clearance and introduces SYSTEM clearance.
///
/// Expected: MonotonicityViolation — cannot introduce clearance that parent never established.
#[test]
fn test_clearance_introduction_from_none_parent() {
    let holder_kp = SigningKey::generate();
    let delegate_kp = SigningKey::generate();

    // Parent has NO clearance set
    let parent = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(holder_kp.public_key())
        .build(&holder_kp)
        .unwrap();

    assert!(
        parent.clearance().is_none(),
        "precondition: parent has no clearance"
    );

    // ATTACK: Attenuate and introduce SYSTEM clearance from thin air
    let result = parent
        .attenuate()
        .inherit_all()
        .clearance(Clearance::SYSTEM)
        .holder(delegate_kp.public_key())
        .build(&holder_kp);

    assert!(
        result.is_err(),
        "Introducing clearance when parent has none must be rejected"
    );
    let err = result.unwrap_err().to_string();
    assert!(
        err.contains("cannot introduce clearance") || err.contains("monotonicity"),
        "Expected monotonicity error, got: {err}"
    );
    println!("✅ Clearance introduction from unclearanced parent blocked: {err}");
}

// ============================================================================
// PoP Timestamp Manipulation
// ============================================================================

/// Attack: Create PoP signature with timestamp far in the future.
///
/// Expected: PoP verification rejects future timestamps.
#[test]
fn test_pop_future_timestamp() {
    let keypair = SigningKey::generate();

    let warrant = Warrant::builder()
        .capability("transfer", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // Create PoP with future timestamp
    let args: HashMap<String, ConstraintValue> =
        [("amount".to_string(), ConstraintValue::Integer(100))]
            .into_iter()
            .collect();

    // Manually construct PoP challenge with future timestamp
    // PoP challenge structure: (warrant_id, tool, sorted_args, timestamp_window)
    let future = Utc::now() + ChronoDuration::hours(1);
    let window_start = future.timestamp() / 30 * 30; // Align to 30s window

    let mut sorted_args: Vec<(&String, &ConstraintValue)> = args.iter().collect();
    sorted_args.sort_by_key(|(k, _)| *k);

    let challenge = (
        warrant.id().to_string(),
        "transfer",
        &sorted_args,
        window_start,
    );

    let mut challenge_bytes = Vec::new();
    ciborium::ser::into_writer(&challenge, &mut challenge_bytes).unwrap();

    let future_sig = keypair.sign(&challenge_bytes);

    // Try to authorize with future signature
    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());

    let result = authorizer.authorize_one(&warrant, "transfer", &args, Some(&future_sig), &[]);

    assert!(result.is_err(), "Future timestamp PoP should be rejected");

    let err = result.unwrap_err();
    println!("✅ Future timestamp PoP blocked: {}", err);
}

/// Attack: Replay PoP signature from an old timestamp window.
///
/// Expected: Rejected if outside max_windows (typically 4 * 30s = 2 minutes).
#[test]
fn test_pop_old_timestamp_replay() {
    let keypair = SigningKey::generate();

    let warrant = Warrant::builder()
        .capability("transfer", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let args: HashMap<String, ConstraintValue> =
        [("amount".to_string(), ConstraintValue::Integer(100))]
            .into_iter()
            .collect();

    // Create PoP with old timestamp (3 minutes ago - outside window)
    let old_time = Utc::now() - ChronoDuration::minutes(3);
    let window_start = old_time.timestamp() / 30 * 30;

    let mut sorted_args: Vec<(&String, &ConstraintValue)> = args.iter().collect();
    sorted_args.sort_by_key(|(k, _)| *k);

    let challenge = (
        warrant.id().to_string(),
        "transfer",
        &sorted_args,
        window_start,
    );

    let mut challenge_bytes = Vec::new();
    ciborium::ser::into_writer(&challenge, &mut challenge_bytes).unwrap();

    let old_sig = keypair.sign(&challenge_bytes);

    // Try to authorize with old signature
    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());

    let result = authorizer.authorize_one(&warrant, "transfer", &args, Some(&old_sig), &[]);

    assert!(
        result.is_err(),
        "Old timestamp PoP should be rejected (outside window)"
    );

    let err = result.unwrap_err();
    // PoP verification failure (could mention timestamp, window, or just "PoP failed")
    println!("✅ Old timestamp PoP replay blocked: {}", err);
}

/// Attack: Race condition at PoP timestamp window boundary.
///
/// Create PoP at window boundary, verify concurrently to exploit TOCTOU
/// between window check and signature verification.
///
/// Expected: No race condition (window check and sig verify are atomic).
///
/// Note: The 30-second window makes this attack impractical in practice.
/// This test documents the design property rather than exercising a real attack.
#[test]
fn test_pop_concurrent_window_boundary() {
    use std::sync::Arc;
    use std::thread;

    let keypair = Arc::new(SigningKey::generate());

    let warrant = Arc::new(
        Warrant::builder()
            .capability("transfer", ConstraintSet::new())
            .ttl(Duration::from_secs(3600))
            .holder(keypair.public_key())
            .build(&keypair)
            .unwrap(),
    );

    let args: HashMap<String, ConstraintValue> =
        [("amount".to_string(), ConstraintValue::Integer(100))]
            .into_iter()
            .collect();
    let args = Arc::new(args);

    let authorizer = Arc::new(Authorizer::new().with_trusted_root(keypair.public_key()));

    // Create PoP signature (at current window)
    let sig = warrant.sign(&keypair, "transfer", &args).unwrap();
    let sig = Arc::new(sig);

    // Spawn multiple threads to verify concurrently
    let mut handles = vec![];

    for _ in 0..10 {
        let w = Arc::clone(&warrant);
        let a = Arc::clone(&args);
        let s = Arc::clone(&sig);
        let auth = Arc::clone(&authorizer);

        handles.push(thread::spawn(move || {
            auth.authorize_one(&w, "transfer", &a, Some(&s), &[])
        }));
    }

    // All should either succeed or fail consistently (no TOCTOU)
    let results: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();

    let successes = results.iter().filter(|r| r.is_ok()).count();
    let failures = results.iter().filter(|r| r.is_err()).count();

    // Should be all success or all failure, not a mix
    assert!(
        successes == 10 || failures == 10,
        "Concurrent PoP verification should be consistent: {} success, {} failure",
        successes,
        failures
    );

    println!(
        "✅ Concurrent PoP verification is consistent ({} success, {} failure)",
        successes, failures
    );
}

// ============================================================================
// Depth Limit Bypass Attacks
// ============================================================================

/// Attack: Create delegation chain exceeding MAX_DELEGATION_DEPTH.
///
/// Expected: DepthExceeded error at depth 16.
#[test]
fn test_delegation_depth_limit() {
    let keypair = SigningKey::generate();

    let mut current = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .ttl(Duration::from_secs(36000)) // Long TTL for many delegations
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let mut depth = 0;

    // POLA: inherit_all for each delegation
    for i in 0..MAX_DELEGATION_DEPTH + 5 {
        match current.attenuate().inherit_all().build(&keypair) {
            Ok(child) => {
                current = child;
                depth = i + 1;
            }
            Err(e) => {
                // Should hit a limit (either chain length or depth)
                let err_str = e.to_string();
                assert!(
                    err_str.contains("depth")
                        || err_str.contains("chain")
                        || err_str.contains("exceed")
                        || err_str.contains("maximum"),
                    "Error should mention limit: {}",
                    err_str
                );
                println!(
                    "✅ Delegation limit enforced at iteration {}: {}",
                    depth + 1,
                    e
                );
                return;
            }
        }
    }

    panic!(
        "Should have hit MAX_DELEGATION_DEPTH ({}), but reached depth {}",
        MAX_DELEGATION_DEPTH, depth
    );
}

// ============================================================================
// Tool Narrowing Bypass Attacks
// ============================================================================

/// Attack: Execution warrant tries to add tools during attenuation.
///
/// Expected: Validation error (tools can only shrink).
///
/// SECURITY NOTE: This test directly validates the capabilities HashMap,
/// not the tools() helper method, to ensure we're testing the actual
/// security property (map keys in capabilities) rather than a derived list.
#[test]
fn test_execution_warrant_tool_addition() {
    let keypair = SigningKey::generate();

    let parent = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // ATTACK: Try to add "write" tool not in parent
    // AttenuationBuilder doesn't expose exec_tools publicly
    // But we can test via the internal validation by checking that
    // a child warrant with extra tools would fail authorization

    // The parent only has "read" - verify via capabilities map directly
    let parent_caps = parent.capabilities().expect("Should have capabilities");
    assert!(
        parent_caps.contains_key("read"),
        "Parent should have 'read' capability"
    );
    assert!(
        !parent_caps.contains_key("write"),
        "Parent should NOT have 'write' capability"
    );
    assert_eq!(
        parent_caps.len(),
        1,
        "Parent should have exactly 1 capability"
    );

    // Also verify tools() helper returns consistent result
    assert_eq!(parent.tools(), vec!["read".to_string()]);

    // POLA: Must explicitly inherit capability
    let child = parent.attenuate().inherit_all().build(&keypair).unwrap();

    // CRITICAL: Verify child capabilities map directly (the security property)
    let child_caps = child
        .capabilities()
        .expect("Child should have capabilities");
    assert!(
        child_caps.contains_key("read"),
        "Child should have 'read' capability"
    );
    assert!(
        !child_caps.contains_key("write"),
        "Child should NOT have 'write' capability"
    );
    assert_eq!(
        child_caps.len(),
        1,
        "Child should have exactly 1 capability"
    );

    // Also verify tools() helper returns consistent result
    assert_eq!(child.tools(), vec!["read".to_string()]);

    // If child tries to authorize "write", it should fail
    let args: HashMap<String, ConstraintValue> = HashMap::new();
    let sig = child.sign(&keypair, "write", &args).unwrap();

    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());
    let result = authorizer.authorize_one(&child, "write", &args, Some(&sig), &[]);

    assert!(
        result.is_err(),
        "Child should not have tools parent didn't have"
    );

    println!("✅ Tool addition prevented (capabilities map enforces monotonicity)");
}

/// Attack: Issuer warrant tries to add issuable_tools during attenuation.
///
/// Expected: Validation error.
///
/// NOTE: For ISSUER warrants, issuable_tools is a Vec<String> (not a HashMap).
/// This is different from EXECUTION warrants which use capabilities: HashMap<String, ConstraintSet>.
/// The "Ghost Field" concern doesn't apply here because issuable_tools is the actual data structure.
#[test]
fn test_issuer_warrant_tool_addition() {
    let keypair = SigningKey::generate();

    let parent = Warrant::builder()
        .r#type(WarrantType::Issuer)
        .issuable_tools(vec!["read".to_string()])
        .clearance(Clearance::INTERNAL)
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // ATTACK: Issuer warrants attenuate via same builder
    // The issuable_tools should not expand

    // Verify parent has only "read" as issuable (direct access to the field)
    let parent_issuable = parent.issuable_tools().expect("Should have issuable_tools");
    assert_eq!(
        parent_issuable.len(),
        1,
        "Parent should have exactly 1 issuable tool"
    );
    assert!(
        parent_issuable.contains(&"read".to_string()),
        "Parent should have 'read' as issuable"
    );

    // Attenuate (POLA: inherit_all for issuer warrants)
    let child = parent.attenuate().inherit_all().build(&keypair).unwrap();

    // Child should have same or fewer issuable_tools (direct verification)
    let child_issuable = child
        .issuable_tools()
        .expect("Child should have issuable_tools");
    assert_eq!(
        child_issuable.len(),
        1,
        "Child should have exactly 1 issuable tool"
    );
    assert!(
        child_issuable.contains(&"read".to_string()),
        "Child should have 'read' as issuable"
    );
    assert!(
        !child_issuable.contains(&"write".to_string()),
        "Child should NOT have 'write' as issuable"
    );

    println!("✅ Issuable tool addition prevented (monotonic attenuation)");
}

/// Attack: Exploit ambiguity between "empty constraints" and "no access".
///
/// In the capabilities API, `ping: {}` means "tool allowed with no constraints"
/// (i.e., allowed with ANY arguments). This test verifies:
///
/// 1. Empty constraints ({}) = ALLOWED for any arguments
/// 2. Missing tool = DENIED
/// 3. Deserialization doesn't crash on empty constraint maps
///
/// This is critical because an incorrect interpretation could either:
/// - Allow unauthorized access (if {} wrongly means "allowed")
/// - Block authorized access (if {} wrongly means "denied")
#[test]
fn test_empty_capabilities_semantics() {
    let keypair = SigningKey::generate();

    // Create warrant with "ping" tool having empty constraints
    let warrant = Warrant::builder()
        .capability("ping", ConstraintSet::new()) // Empty constraints = "allowed with any args"
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());

    // Test 1: Empty constraints should ALLOW any arguments
    let random_args: HashMap<String, ConstraintValue> = [
        (
            "any".to_string(),
            ConstraintValue::String("thing".to_string()),
        ),
        ("foo".to_string(), ConstraintValue::Integer(42)),
    ]
    .into_iter()
    .collect();

    let sig = warrant.sign(&keypair, "ping", &random_args).unwrap();

    let result = authorizer.authorize_one(&warrant, "ping", &random_args, Some(&sig), &[]);
    assert!(
        result.is_ok(),
        "Empty constraints should ALLOW any arguments: {:?}",
        result
    );
    println!("✅ Empty constraints ({{}}) = ALLOWED for any args");

    // Test 2: Should also work with truly empty args
    let empty_args: HashMap<String, ConstraintValue> = HashMap::new();
    let sig2 = warrant.sign(&keypair, "ping", &empty_args).unwrap();

    let result2 = authorizer.authorize_one(&warrant, "ping", &empty_args, Some(&sig2), &[]);
    assert!(
        result2.is_ok(),
        "Empty constraints should ALLOW empty args too: {:?}",
        result2
    );
    println!("✅ Empty constraints ({{}}) = ALLOWED for empty args");

    // Test 3: Other tools should be DENIED (not present in capabilities)
    let pong_sig = warrant.sign(&keypair, "pong", &empty_args).unwrap();

    let result3 = authorizer.authorize_one(&warrant, "pong", &empty_args, Some(&pong_sig), &[]);
    assert!(result3.is_err(), "Missing tool should be DENIED");
    println!("✅ Missing tool = DENIED");

    // Test 4: Round-trip serialization preserves empty constraint semantics
    let bytes = wire::encode(&warrant).unwrap();
    let decoded: Warrant = wire::decode(&bytes).unwrap();

    // Verify empty constraints are preserved, not dropped
    let caps = decoded.capabilities().expect("Should have capabilities");
    assert!(
        caps.contains_key("ping"),
        "ping should exist after deserialization"
    );
    let ping_constraints = caps.get("ping").unwrap();
    assert!(
        ping_constraints.is_empty(),
        "ping constraints should remain empty after deserialization"
    );

    println!("✅ Empty constraints preserved through serialization round-trip");
}

// ============================================================================
// Holder Binding Attacks
// ============================================================================

/// Attack: Use warrant with wrong holder keypair.
///
/// Expected: PoP signature verification fails.
#[test]
fn test_holder_mismatch_pop_fails() {
    let issuer_kp = SigningKey::generate();
    let holder_kp = SigningKey::generate();
    let attacker_kp = SigningKey::generate();

    // Create warrant bound to holder_kp
    let warrant = Warrant::builder()
        .capability("transfer", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(holder_kp.public_key())
        .build(&issuer_kp)
        .unwrap();

    // ATTACK: Attacker tries to use warrant with their keypair
    let args: HashMap<String, ConstraintValue> =
        [("amount".to_string(), ConstraintValue::Integer(100))]
            .into_iter()
            .collect();

    let attacker_sig = warrant.sign(&attacker_kp, "transfer", &args).unwrap();

    let authorizer = Authorizer::new().with_trusted_root(issuer_kp.public_key());

    let result = authorizer.authorize_one(&warrant, "transfer", &args, Some(&attacker_sig), &[]);

    assert!(
        result.is_err(),
        "Warrant should reject PoP from wrong keypair"
    );

    let err = result.unwrap_err();
    assert!(
        err.to_string().contains("holder") || err.to_string().contains("signature"),
        "Error should mention holder mismatch: {}",
        err
    );

    println!("✅ Holder mismatch blocked: {}", err);
}

// ============================================================================
// Constraint Depth DoS Attacks
// ============================================================================
//
// NOTE: This section tests the depth limit for recursive constraint types
// (All, Any, Not). These types STILL EXIST in the current architecture for
// expressing complex boolean logic (e.g., "path matches A AND (B OR C)").
//
// The capabilities map provides per-tool constraints, but within each tool's
// ConstraintSet, we still allow recursive Constraint types. The depth limit
// (MAX_CONSTRAINT_DEPTH = 16) prevents stack overflow attacks from deeply
// nested structures like All(All(All(...))).
//
// If we ever switch to a purely flat HashMap<String, Pattern>, these tests
// can be removed as the attack surface would no longer exist.

/// Attack: Create deeply nested All(All(All(...))) constraint to cause stack overflow.
///
/// Expected: ConstraintDepthExceeded during deserialization.
///
/// SECURITY NOTE: This test is still relevant because the Constraint enum
/// includes recursive types (All, Any, Not) for complex boolean logic.
/// The MAX_CONSTRAINT_DEPTH limit (16) prevents stack overflow attacks.
#[test]
fn test_constraint_depth_dos() {
    use tenuo::constraints::All;

    // Create deeply nested constraint (depth > MAX_CONSTRAINT_DEPTH which is 32)
    let mut nested = Constraint::Exact(Exact::new("value"));
    for _ in 0..40 {
        nested = Constraint::All(All::new(vec![nested]));
    }

    // Try to create warrant with this constraint
    let keypair = SigningKey::generate();

    let mut constraints = ConstraintSet::new();
    constraints.insert("key", nested);
    let result = Warrant::builder()
        .capability("test", constraints)
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair);

    assert!(
        result.is_err(),
        "Deeply nested constraints should be rejected"
    );

    let err = result.unwrap_err();
    assert!(
        err.to_string().contains("depth") || err.to_string().contains("recursion"),
        "Error should mention depth limit: {}",
        err
    );

    println!("✅ Constraint depth DoS blocked: {}", err);
}

/// Attack: Deserialize warrant with deeply nested constraint from CBOR.
///
/// Expected: Deserialization fails with ConstraintDepthExceeded.
///
/// Note: This tests the runtime deserialization guard. The build-time test
/// is in test_constraint_depth_dos().
#[test]
fn test_constraint_depth_deserialization_limit() {
    // Create deeply nested constraint programmatically
    let mut nested = Constraint::Exact(Exact::new("value"));
    for _ in 0..40 {
        // Nest 40 levels (> MAX_CONSTRAINT_DEPTH of 32)
        nested = Constraint::All(All::new(vec![nested]));
    }

    let keypair = SigningKey::generate();

    // Try to serialize and deserialize
    let mut constraints = ConstraintSet::new();
    constraints.insert("deep", nested.clone());
    let result = Warrant::builder()
        .capability("test", constraints)
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair);

    assert!(
        result.is_err(),
        "Deeply nested constraint should fail at build"
    );

    let err = result.unwrap_err();
    println!("✅ Constraint depth limit enforced at build: {}", err);

    // Also test round-trip if we could somehow bypass build check
    // (Verifies deserialization guard is also in place)

    // Manually create a ConstraintSet with deep nesting
    let mut constraints = ConstraintSet::new();
    constraints.insert("test", nested);

    // Validate depth
    let validate_result = constraints.validate_depth();
    assert!(
        validate_result.is_err(),
        "ConstraintSet.validate_depth() should catch deep nesting"
    );

    println!("✅ ConstraintSet.validate_depth() catches deep nesting");
}

// ============================================================================
// Warrant Size DoS Attacks
// ============================================================================

/// Attack: Create warrant with huge payload to cause memory exhaustion.
///
/// Expected: PayloadTooLarge error OR warrant under MAX_WARRANT_SIZE.
///
/// Note: This test documents the current behavior. Large warrants that fit
/// under MAX_WARRANT_SIZE are allowed - this is intentional as there's no
/// compelling security reason to limit tool count below the size limit.
#[test]
fn test_warrant_size_limit() {
    let keypair = SigningKey::generate();

    // Create warrant with many tools (should work up to a limit)
    let mut tools = Vec::new();
    for i in 0..2000 {
        // Try to exceed reasonable limit
        tools.push(format!("tool_{}", i));
    }

    let mut builder = Warrant::builder().ttl(Duration::from_secs(3600));
    for t in tools {
        builder = builder.capability(t, ConstraintSet::new());
    }
    let result = builder.holder(keypair.public_key()).build(&keypair);

    match result {
        Ok(warrant) => {
            let bytes = wire::encode(&warrant).unwrap();
            let tool_count = warrant.tools().len();

            // MUST NOT exceed MAX_WARRANT_SIZE
            assert!(
                bytes.len() <= tenuo::MAX_WARRANT_SIZE,
                "Warrant size {} exceeds MAX_WARRANT_SIZE {}",
                bytes.len(),
                tenuo::MAX_WARRANT_SIZE
            );

            println!(
                "✅ Large warrant under size limit ({} tools, {} bytes, max {})",
                tool_count,
                bytes.len(),
                tenuo::MAX_WARRANT_SIZE
            );
        }
        Err(e) => {
            println!("✅ Large warrant blocked at build time: {}", e);
        }
    }
}

// ============================================================================
// Cross-Warrant Attacks
// ============================================================================

/// Attack: Use child warrant without parent in chain.
///
/// Expected: SUCCEEDS - this is intentional design.
///
/// Test: Child warrant with parent_hash requires WarrantStack for full chain verification.
///
/// Tenuo uses parent_hash to cryptographically link child to parent.
/// Full chain verification requires the caller to provide a WarrantStack
/// containing all warrants in the ancestry.
///
/// The security is maintained because:
/// 1. parent_hash = SHA256(parent.payload_bytes) is tamper-proof
/// 2. Root trust is verified against trusted_roots
/// 3. Each link in the stack is verified for monotonicity
#[test]
fn test_child_warrant_with_parent_hash() {
    let parent_kp = SigningKey::generate();
    let child_kp = SigningKey::generate();

    let parent = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(parent_kp.public_key())
        .build(&parent_kp)
        .unwrap();

    // POLA: inherit_all
    let child = parent
        .attenuate()
        .inherit_all()
        .holder(child_kp.public_key())
        .build(&parent_kp)
        .unwrap();

    // Child has parent_hash linking it to parent
    assert!(
        child.parent_hash().is_some(),
        "Child should have parent_hash"
    );

    // Verify chain using verify_chain (which takes WarrantStack)
    let authorizer = Authorizer::new().with_trusted_root(parent_kp.public_key());

    // Verify the full chain [root, child]
    let chain_result = authorizer.verify_chain(&[parent.clone(), child.clone()]);

    assert!(
        chain_result.is_ok(),
        "Chain verification should work with WarrantStack: {:?}",
        chain_result
    );

    // Also verify authorization on the leaf warrant
    let args: HashMap<String, ConstraintValue> = HashMap::new();
    let sig = child.sign(&child_kp, "read", &args).unwrap();

    let auth_result = authorizer.authorize_one(&child, "read", &args, Some(&sig), &[]);

    assert!(
        auth_result.is_ok(),
        "Authorization should work on child warrant: {:?}",
        auth_result
    );

    println!("✅ Chain verification works with WarrantStack");
    println!("   (parent_hash links child to parent, verify_chain traces ancestry)");
}

/// Attack: Present warrants in wrong order (child before parent).
///
/// Expected: Chain verification fails.
#[test]
fn test_chain_wrong_order() {
    let parent_kp = SigningKey::generate();
    let child_kp = SigningKey::generate();

    let parent = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(parent_kp.public_key())
        .build(&parent_kp)
        .unwrap();

    // POLA: inherit_all
    let child = parent
        .attenuate()
        .inherit_all()
        .holder(child_kp.public_key())
        .build(&parent_kp)
        .unwrap();

    // ATTACK: Verify with reversed chain
    let data_plane = DataPlane::new();
    let result = data_plane.verify_chain(&[child.clone(), parent.clone()]);

    assert!(
        result.is_err(),
        "Chain in wrong order should fail verification"
    );

    let err = result.unwrap_err();
    println!("✅ Wrong chain order blocked: {}", err);
}

// ============================================================================
// PoP Args Binding Attacks
// ============================================================================

/// Attack: Create PoP for tool="read", args={file: "A"}, use for args={file: "B"}.
///
/// Expected: PoP signature verification fails.
#[test]
fn test_pop_args_binding() {
    let keypair = SigningKey::generate();

    let warrant = Warrant::builder()
        .capability("read_file", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());

    // Create PoP for file="safe.txt"
    let safe_args: HashMap<String, ConstraintValue> = [(
        "file".to_string(),
        ConstraintValue::String("safe.txt".to_string()),
    )]
    .into_iter()
    .collect();

    let safe_sig = warrant.sign(&keypair, "read_file", &safe_args).unwrap();

    // ATTACK: Use that signature with different args
    let malicious_args: HashMap<String, ConstraintValue> = [(
        "file".to_string(),
        ConstraintValue::String("/etc/passwd".to_string()),
    )]
    .into_iter()
    .collect();

    let result =
        authorizer.authorize_one(&warrant, "read_file", &malicious_args, Some(&safe_sig), &[]);

    assert!(
        result.is_err(),
        "PoP signature should not verify for different args"
    );

    let err = result.unwrap_err();
    println!("✅ PoP args swap blocked: {}", err);
}

/// Attack: Create PoP for tool="read", use for tool="write".
///
/// Expected: PoP signature verification fails.
#[test]
fn test_pop_tool_binding() {
    let keypair = SigningKey::generate();

    let warrant = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .capability("write", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());

    let args: HashMap<String, ConstraintValue> = [(
        "file".to_string(),
        ConstraintValue::String("test.txt".to_string()),
    )]
    .into_iter()
    .collect();

    // Create PoP for "read"
    let read_sig = warrant.sign(&keypair, "read", &args).unwrap();

    // ATTACK: Use that signature for "write"
    let result = authorizer.authorize_one(&warrant, "write", &args, Some(&read_sig), &[]);

    assert!(
        result.is_err(),
        "PoP signature should not verify for different tool"
    );

    let err = result.unwrap_err();
    println!("✅ PoP tool swap blocked: {}", err);
}

// ============================================================================
// Trust Level Attenuation Attacks
// ============================================================================

/// Attack: Raise trust level during execution warrant attenuation.
///
/// Expected: Validation error (trust can only shrink).
#[test]
fn test_clearance_amplification() {
    let keypair = SigningKey::generate();

    let parent = Warrant::builder()
        .capability("query", ConstraintSet::new())
        .clearance(Clearance::INTERNAL)
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // Attenuate (POLA: inherit_all, should inherit or lower trust)
    let child = parent.attenuate().inherit_all().build(&keypair).unwrap();

    // Verify parent has trust
    assert_eq!(parent.clearance(), Some(Clearance::INTERNAL));

    // Verify child inherited trust
    // Monotonicity ensures child trust <= parent trust
    // inherit_all() copies it unless overridden
    assert_eq!(child.clearance(), Some(Clearance::INTERNAL));

    println!("✅ Trust level amplification prevented (monotonic attenuation)");
}

// ============================================================================
// Terminal Warrant Attacks
// ============================================================================

/// Attack: Terminal warrant (max_depth reached) tries to delegate.
///
/// Expected: DepthExceeded error.
#[test]
fn test_terminal_warrant_delegation() {
    let keypair = SigningKey::generate();

    // Create warrant with max_depth=1
    let parent = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .max_depth(1)
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // First delegation (depth 0→1) should work (POLA: inherit_all)
    let child = parent.attenuate().inherit_all().build(&keypair).unwrap();
    assert_eq!(child.depth(), 1);

    // ATTACK: Try to delegate again (depth 1→2, but max_depth=1)
    let result = child.attenuate().inherit_all().build(&keypair);

    assert!(result.is_err(), "Terminal warrant should not delegate");

    let err = result.unwrap_err();
    assert!(
        err.to_string().contains("depth") || err.to_string().contains("terminal"),
        "Error should mention depth limit: {}",
        err
    );

    println!("✅ Terminal warrant delegation blocked: {}", err);
}

// ============================================================================
// Serialization Format Attacks
// ============================================================================

/// Attack: Use non-deterministic CBOR encoding (indefinite-length arrays).
///
/// Expected: Deserialization succeeds but signature fails canonical binding.
#[test]
fn test_non_deterministic_cbor() {
    // This is hard to test without manually crafting non-deterministic CBOR
    // The important property is that we use ciborium which enforces determinism

    // We can at least verify round-trip is deterministic
    let keypair = SigningKey::generate();

    let mut constraints = ConstraintSet::new();
    constraints.insert("key", OneOf::new(vec!["a", "b", "c"]));
    let warrant = Warrant::builder()
        .capability("read", constraints)
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let bytes1 = wire::encode(&warrant).unwrap();
    let decoded = wire::decode(&bytes1).unwrap();
    let bytes2 = wire::encode(&decoded).unwrap();

    assert_eq!(
        bytes1, bytes2,
        "Serialization should be deterministic (round-trip)"
    );

    println!("✅ CBOR serialization is deterministic");
}

// ============================================================================
// Multi-Chain Attacks
// ============================================================================

/// Attack: Mix warrants from different chains.
///
/// Expected: Chain verification fails (broken chain).
#[test]
fn test_mixed_chain_attack() {
    let root1_kp = SigningKey::generate();
    let root2_kp = SigningKey::generate();
    let child_kp = SigningKey::generate();

    // Create two separate chains
    let chain1_parent = Warrant::builder()
        .capability("read", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(root1_kp.public_key())
        .build(&root1_kp)
        .unwrap();

    let chain2_parent = Warrant::builder()
        .capability("write", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(root2_kp.public_key())
        .build(&root2_kp)
        .unwrap();

    // POLA: inherit_all
    let chain1_child = chain1_parent
        .attenuate()
        .inherit_all()
        .holder(child_kp.public_key())
        .build(&root1_kp)
        .unwrap();

    // ATTACK: Present chain [chain2_parent, chain1_child]
    // These are from different roots and unrelated
    let data_plane = DataPlane::new();
    let result = data_plane.verify_chain(&[chain2_parent, chain1_child]);

    assert!(result.is_err(), "Mixed chains should fail verification");

    let err = result.unwrap_err();
    // Error might be about signature, root trust, or chain mismatch
    println!("✅ Mixed chain attack blocked: {}", err);
}

// ============================================================================
// Canonicalization Attacks
// ============================================================================

/// Attack: Create two CBOR encodings of "same" payload with different byte representations.
///
/// Expected: Only the canonically signed bytes are accepted.
#[test]
fn test_cbor_canonical_map_key_ordering() {
    let keypair = SigningKey::generate();

    // Create warrant with multiple constraints (keys will be sorted)
    let mut constraints = ConstraintSet::new();
    constraints.insert("z_last", Exact::new("value"));
    constraints.insert("a_first", Exact::new("value"));
    constraints.insert("m_middle", Exact::new("value"));

    let warrant = Warrant::builder()
        .capability("test", constraints)
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // Serialize
    let bytes1 = wire::encode(&warrant).unwrap();

    // Deserialize and re-serialize (should produce same bytes)
    let decoded = wire::decode(&bytes1).unwrap();
    let bytes2 = wire::encode(&decoded).unwrap();

    assert_eq!(
        bytes1, bytes2,
        "Re-serialization must be byte-identical (deterministic CBOR)"
    );

    println!("✅ CBOR serialization is deterministic (sorted map keys)");

    // Verify signature still works after round-trip
    assert!(decoded.verify_signature().is_ok());
    println!("✅ Signature verifies after round-trip (canonical bytes preserved)");
}

/// Attack: Send capabilities with unsorted nested maps to exploit canonicalization.
///
/// With capabilities being a Map (Tools) of Maps (Args), an attacker could send:
/// {"b_tool": {"z_arg": ..., "a_arg": ...}, "a_tool": {}}
///
/// If the parser accepts non-sorted order, signature verification might pass
/// but the canonical hash differs, leading to cache poisoning or signature bypass.
///
/// Expected: CBOR canonicalization sorts at ALL nesting levels.
///
/// Both `capabilities` (BTreeMap) and `ConstraintSet.constraints` (BTreeMap)
/// use deterministic ordering to ensure canonical serialization.
#[test]
fn test_cbor_canonical_nested_map_ordering() {
    let keypair = SigningKey::generate();

    // Create warrant with multiple tools, each having multiple constraints
    // inserted in reverse alphabetical order to stress-test sorting

    // Tool "z_tool" has constraints in reverse order
    let mut z_constraints = ConstraintSet::new();
    z_constraints.insert("z_arg", Pattern::new("*").unwrap());
    z_constraints.insert("m_arg", Pattern::new("*").unwrap());
    z_constraints.insert("a_arg", Pattern::new("*").unwrap());

    // Tool "a_tool" is empty (should come first in sorted output)
    let a_constraints = ConstraintSet::new();

    // Tool "m_tool" has single constraint
    let mut m_constraints = ConstraintSet::new();
    m_constraints.insert("single", Exact::new("value"));

    let warrant = Warrant::builder()
        .capability("z_tool", z_constraints) // Added first but should serialize last
        .capability("a_tool", a_constraints) // Added second but should serialize first
        .capability("m_tool", m_constraints) // Added third, should serialize middle
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    // Verify initial state
    let caps = warrant.capabilities().expect("Should have capabilities");
    assert_eq!(caps.len(), 3, "Should have 3 tools");
    assert!(caps.contains_key("a_tool"), "Should have a_tool");
    assert!(caps.contains_key("m_tool"), "Should have m_tool");
    assert!(caps.contains_key("z_tool"), "Should have z_tool");

    // Serialize
    let bytes1 = wire::encode(&warrant).unwrap();

    // Deserialize and re-serialize
    let decoded = wire::decode(&bytes1).expect("Deserialization should succeed with BTreeMap");
    let bytes2 = wire::encode(&decoded).unwrap();

    // CRITICAL: Must be byte-identical (proves nested sorting is deterministic)
    assert_eq!(
        bytes1, bytes2,
        "Canonicalization MUST enforce sorting at ALL nesting levels"
    );

    // Verify the capabilities are correctly structured
    let caps = decoded.capabilities().expect("Should have capabilities");
    assert!(caps.contains_key("a_tool"), "Should have a_tool");
    assert!(caps.contains_key("m_tool"), "Should have m_tool");
    assert!(caps.contains_key("z_tool"), "Should have z_tool");
    assert_eq!(caps.len(), 3, "Should have exactly 3 tools");

    // Verify z_tool has all its constraints
    let z_tool_constraints = caps.get("z_tool").unwrap();
    assert_eq!(
        z_tool_constraints.len(),
        3,
        "z_tool should have 3 constraints"
    );

    // Verify signature still valid (proves signing used canonical bytes)
    assert!(decoded.verify_signature().is_ok());

    println!("✅ Nested map ordering is deterministic (capabilities sorted at all levels)");
    println!("   Tools: a_tool < m_tool < z_tool");
    println!("   Args in z_tool: a_arg < m_arg < z_arg");
}

// ============================================================================
// Root Trust Enforcement
// ============================================================================

/// Attack: Present valid warrant signed by untrusted key.
///
/// Expected: Authorizer rejects if trusted_roots don't include the issuer.
#[test]
fn test_untrusted_root_rejection() {
    let trusted_kp = SigningKey::generate();
    let attacker_kp = SigningKey::generate();

    // Attacker creates valid warrant with their key
    let attacker_warrant = Warrant::builder()
        .capability("admin", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(attacker_kp.public_key())
        .build(&attacker_kp)
        .unwrap();

    // Verify signature is valid (self-signed)
    assert!(attacker_warrant.verify_signature().is_ok());

    // But Authorizer with trusted_roots should reject it
    let authorizer = Authorizer::new().with_trusted_root(trusted_kp.public_key());

    let args: HashMap<String, ConstraintValue> = HashMap::new();
    let sig = attacker_warrant.sign(&attacker_kp, "admin", &args).unwrap();

    let result = authorizer.authorize_one(&attacker_warrant, "admin", &args, Some(&sig), &[]);

    // The warrant should be rejected because attacker_kp is not in trusted_roots
    // However, if the warrant has no parent_hash (root), it might verify against its own issuer key
    // The Authorizer should check that the root is trusted

    match result {
        Ok(_) => {
            println!(
                "⚠️ Untrusted root was accepted (check Authorizer.authorize root trust logic)"
            );
            println!("   This might be expected if the warrant is self-signed and has no chain");
            println!("   Applications MUST configure trusted_roots in production");
        }
        Err(e) => {
            println!("✅ Untrusted root rejected: {}", e);
        }
    }
}

/// Attack: Add trusted root after verification setup.
///
/// This tests that Authorizer.add_trusted_root is safe.
#[test]
fn test_dynamic_trusted_root_addition() {
    let trusted_kp = SigningKey::generate();
    let new_root_kp = SigningKey::generate();

    let mut authorizer = Authorizer::new().with_trusted_root(trusted_kp.public_key());

    // Create warrant from new root (not yet trusted)
    let warrant = Warrant::builder()
        .capability("test", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(new_root_kp.public_key())
        .build(&new_root_kp)
        .unwrap();

    let args: HashMap<String, ConstraintValue> = HashMap::new();
    let sig = warrant.sign(&new_root_kp, "test", &args).unwrap();

    // Try to authorize before root is trusted
    let before_result = authorizer.authorize_one(&warrant, "test", &args, Some(&sig), &[]);

    // Root warrants (no parent_hash) might still verify if trust check isn't enforced
    // This tests the Authorizer behavior
    if before_result.is_ok() {
        println!("⚠️ Warrant accepted before root trusted (self-signed root verification)");
        println!("   Note: Root trust enforcement depends on Authorizer configuration");
    } else {
        println!(
            "✅ Warrant rejected before root trusted: {:?}",
            before_result.err()
        );
    }

    // Add new root
    authorizer.add_trusted_root(new_root_kp.public_key());

    // Should succeed after adding root
    let after_result = authorizer.authorize_one(&warrant, "test", &args, Some(&sig), &[]);

    assert!(
        after_result.is_ok(),
        "Warrant should verify after root added"
    );

    println!("✅ Dynamic trusted root addition works (root can be added at runtime)");
}

// ============================================================================
// Constraint Satisfaction Bypass
// ============================================================================

/// Attack: Pattern constraint with Unicode lookalike characters.
///
/// Pattern("/data/*") vs path="/ⅆata/file" (Unicode U+2146 "ⅆ" vs "d").
///
/// Expected: Does not match (byte-wise comparison).
#[test]
fn test_unicode_lookalike_bypass() {
    let keypair = SigningKey::generate();

    let mut constraints = ConstraintSet::new();
    constraints.insert("path", Pattern::new("/data/*").unwrap());
    let warrant = Warrant::builder()
        .capability("read", constraints)
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());

    // Try Unicode lookalike
    let args: HashMap<String, ConstraintValue> = [(
        "path".to_string(),
        ConstraintValue::String("/ⅆata/passwd".to_string()), // U+2146 "ⅆ"
    )]
    .into_iter()
    .collect();

    let sig = warrant.sign(&keypair, "read", &args).unwrap();
    let result = authorizer.authorize_one(&warrant, "read", &args, Some(&sig), &[]);

    assert!(
        result.is_err(),
        "Unicode lookalike should not match /data/*"
    );
    println!("✅ Unicode lookalike blocked (byte-wise matching)");
}

/// Attack: Case variation to bypass constraint.
///
/// Pattern("staging-*") vs "Staging-web".
///
/// Expected: Does not match (case-sensitive).
#[test]
fn test_case_sensitivity_bypass() {
    let keypair = SigningKey::generate();

    let mut constraints = ConstraintSet::new();
    constraints.insert("cluster", Pattern::new("staging-*").unwrap());
    let warrant = Warrant::builder()
        .capability("deploy", constraints)
        .ttl(Duration::from_secs(3600))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());

    // Try uppercase
    let args: HashMap<String, ConstraintValue> = [(
        "cluster".to_string(),
        ConstraintValue::String("Staging-web".to_string()),
    )]
    .into_iter()
    .collect();

    let sig = warrant.sign(&keypair, "deploy", &args).unwrap();
    let result = authorizer.authorize_one(&warrant, "deploy", &args, Some(&sig), &[]);

    assert!(result.is_err(), "Case variation should not match pattern");
    println!("✅ Case variation blocked (case-sensitive matching)");
}

// ============================================================================
// Summary
// ============================================================================

/// Meta-test: Print summary of all security properties tested.
#[test]
fn test_000_red_team_summary() {
    println!("\n╔══════════════════════════════════════════════════════════════╗");
    println!("║  Tenuo Red Team Test Suite - Binary-Level Security Tests    ║");
    println!("╠══════════════════════════════════════════════════════════════╣");
    println!("║                                                              ║");
    println!("║  ChainLink Tampering:                                        ║");
    println!("║    • issuer_tools modification                               ║");
    println!("║    • issuer_constraints modification                         ║");
    println!("║    • issuer_expires_at extension                             ║");
    println!("║                                                              ║");
    println!("║  CBOR Payload Tampering:                                     ║");
    println!("║    • payload vs payload_bytes mismatch                       ║");
    println!("║    • Extra fields injection                                  ║");
    println!("║    • Constraint removal                                      ║");
    println!("║    • Non-deterministic encoding                              ║");
    println!("║                                                              ║");
    println!("║  Signature Attacks:                                          ║");
    println!("║    • Signature reuse across warrants                         ║");
    println!("║    • Untrusted root acceptance                               ║");
    println!("║                                                              ║");
    println!("║  PoP Binding:                                                ║");
    println!("║    • Tool swap (sign for A, use for B)                       ║");
    println!("║    • Args swap (sign for args A, use for args B)             ║");
    println!("║    • Holder mismatch (stolen warrant)                        ║");
    println!("║    • Future/old timestamp exploitation                       ║");
    println!("║                                                              ║");
    println!("║  Delegation Limits:                                          ║");
    println!("║    • MAX_DELEGATION_DEPTH (16) enforcement                   ║");
    println!("║    • Terminal warrant delegation                             ║");
    println!("║                                                              ║");
    println!("║  Monotonicity:                                               ║");
    println!("║    • Tool addition                                           ║");
    println!("║    • Trust level amplification                               ║");
    println!("║    • Constraint type substitution                            ║");
    println!("║                                                              ║");
    println!("║  Chain Verification:                                         ║");
    println!("║    • Wrong order (child before parent)                       ║");
    println!("║    • Mixed chains from different roots                       ║");
    println!("║    • Orphaned child warrants                                 ║");
    println!("║                                                              ║");
    println!("║  Constraint Bypasses:                                        ║");
    println!("║    • Unicode lookalike characters                            ║");
    println!("║    • Case variation                                          ║");
    println!("║    • Constraint depth DoS                                    ║");
    println!("║    • Warrant size DoS                                        ║");
    println!("║                                                              ║");
    println!("║  Parser & Protocol Attacks:                                  ║");
    println!("║    • CBOR duplicate key injection                            ║");
    println!("║    • Unknown field injection                                 ║");
    println!("║    • TTL bypass (time traveler)                              ║");
    println!("║    • ReDoS via regex constraints                             ║");
    println!("║    • Type confusion (NaN, string to Range)                   ║");
    println!("║    • Missing chain link                                      ║");
    println!("║    • Shuffled chain order                                    ║");
    println!("║                                                              ║");
    println!("╚══════════════════════════════════════════════════════════════╝");
    println!();
    println!("Run all tests: cargo test --test red_team -- --nocapture");
    println!();
}

// ============================================================================
// CBOR Parser Attacks
// ============================================================================

/// Attack: Craft CBOR with duplicate map keys to exploit parser differentials.
///
/// Defense: CBOR decoder MUST reject duplicate keys (RFC 8949 §5.6).
/// If the signature verifier and payload parser see different values for
/// the same key, an attacker could bypass authorization.
#[test]
fn test_attack_cbor_duplicate_key_injection() {
    println!("\n--- Attack: CBOR Duplicate Key Injection ---");

    // Craft a map with duplicate keys: {3: {}, 3: {"admin": {}}}
    let duplicate_payload: Vec<u8> = vec![
        0xA2, // Map with 2 items
        0x03, // Key: 3 (tools)
        0xA0, // Value: empty map {}
        0x03, // Key: 3 (tools) - DUPLICATE!
        0xA1, // Value: map with 1 item
        0x65, b'a', b'd', b'm', b'i', b'n', // Key: "admin"
        0xA0, // Value: empty map
    ];

    // Attempt to decode - MUST fail
    let result: Result<BTreeMap<u8, ciborium::Value>, _> =
        ciborium::de::from_reader(&duplicate_payload[..]);

    match result {
        Ok(map) => {
            // ciborium accepts duplicate keys (last wins)
            // Defense: BTreeMap deduplication + signature binding
            println!("  [WARNING] ciborium accepted duplicate keys!");
            println!("  Map contents: {:?}", map);
            println!("  [MITIGATION] BTreeMap keeps last value; signature prevents tampering");
        }
        Err(e) => {
            println!("  [PASS] Duplicate keys rejected: {}", e);
        }
    }
}

/// Attack: Inject unknown CBOR field to bypass fail-closed validation.
///
/// Defense: Unknown payload keys should be rejected, but signature binding
/// prevents tampering regardless.
#[test]
fn test_attack_cbor_unknown_field_trojan() {
    println!("\n--- Attack: CBOR Unknown Field Trojan ---");

    // Build a minimal payload with unknown field (key 99)
    let payload_with_unknown: Vec<u8> = {
        let mut buf = Vec::new();
        ciborium::ser::into_writer(
            &ciborium::Value::Map(vec![(
                ciborium::Value::Integer(99.into()),
                ciborium::Value::Bool(true),
            )]),
            &mut buf,
        )
        .unwrap();
        buf
    };

    // Try to decode as WarrantPayload
    let decode_result: Result<tenuo::payload::WarrantPayload, _> =
        ciborium::de::from_reader(&payload_with_unknown[..]);

    match decode_result {
        Ok(_) => {
            println!("  [INFO] Minimal payload decoded (missing required fields)");
        }
        Err(e) => {
            println!("  [PASS] Invalid payload rejected: {}", e);
            println!("  [INFO] Defense: signature binding prevents field injection");
        }
    }

    println!("  [MITIGATION] Signature covers original bytes - tampering detected");
}

// ============================================================================
// TTL / Time Attacks
// ============================================================================

/// Attack: Create warrant with extreme TTL to bypass time limits.
///
/// Defense: MAX_WARRANT_TTL_SECS (90 days) protocol limit.
#[test]
fn test_attack_ttl_time_traveler() {
    println!("\n--- Attack: TTL Time Traveler ---");

    let keypair = SigningKey::generate();

    // Try to create warrant with 1000 year TTL (exceeds MAX_WARRANT_TTL_SECS = 90 days)
    let excessive_ttl = 1000 * 365 * 24 * 60 * 60;

    let result = Warrant::builder()
        .capability("test", ConstraintSet::new())
        .ttl(Duration::from_secs(excessive_ttl))
        .holder(keypair.public_key())
        .build(&keypair);

    match result {
        Ok(warrant) => {
            let expires = warrant.expires_at();
            println!("  [FAIL] Created warrant expiring at: {}", expires);
            panic!("Excessive TTL should have been rejected");
        }
        Err(e) => {
            println!("  [PASS] Excessive TTL rejected: {}", e);
            assert!(e.to_string().contains("exceeds protocol maximum"));
        }
    }

    // Verify 90 days (protocol max) is accepted
    let valid_result = Warrant::builder()
        .capability("test", ConstraintSet::new())
        .ttl(Duration::from_secs(MAX_WARRANT_TTL_SECS))
        .holder(keypair.public_key())
        .build(&keypair);

    assert!(valid_result.is_ok(), "90-day TTL should be accepted");
    println!("  [PASS] Protocol max TTL (90 days) accepted");
}

// ============================================================================
// ReDoS Attacks
// ============================================================================

/// Attack: Test that Rust regex crate is ReDoS-resistant.
///
/// The Rust `regex` crate uses Thompson NFA, not backtracking,
/// making it inherently resistant to catastrophic backtracking.
#[test]
fn test_attack_redos_resistance() {
    println!("\n--- Attack: ReDoS Resistance ---");

    // Classic ReDoS pattern: (a+)+$
    // This would hang a backtracking engine on "aaaaaaaaaaX"
    let evil_regex = "(a+)+$";
    let evil_input = "aaaaaaaaaaaaaaaaaaaaaaaaaX"; // 25 a's + X

    // Create warrant with this regex constraint
    let keypair = SigningKey::generate();
    let mut constraints = ConstraintSet::new();
    constraints.insert("data", RegexConstraint::new(evil_regex).unwrap());

    let warrant = Warrant::builder()
        .capability("process", constraints)
        .ttl(Duration::from_secs(300))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());

    // Time the authorization
    let start = std::time::Instant::now();
    let timeout = Duration::from_secs(1);

    let mut args = HashMap::new();
    args.insert(
        "data".to_string(),
        ConstraintValue::String(evil_input.to_string()),
    );

    let sig = warrant.sign(&keypair, "process", &args).unwrap();
    let _ = authorizer.authorize_one(&warrant, "process", &args, Some(&sig), &[]);
    let elapsed = start.elapsed();

    if elapsed > timeout {
        println!("  [FAIL] Regex check took {:?} - ReDoS!", elapsed);
        panic!("ReDoS vulnerability detected");
    } else {
        println!("  [PASS] Regex check completed in {:?}", elapsed);
        println!("  [INFO] Rust regex crate uses Thompson NFA - ReDoS resistant");
    }
}

// ============================================================================
// Type Confusion Attacks
// ============================================================================

/// Attack: Pass wrong type to constraint (e.g., string to Range).
#[test]
fn test_attack_type_confusion_range_string() {
    println!("\n--- Attack: Type Confusion (Range + String) ---");

    let keypair = SigningKey::generate();
    let mut constraints = ConstraintSet::new();
    constraints.insert("amount", Range::new(Some(0.0), Some(100.0)).unwrap());

    let warrant = Warrant::builder()
        .capability("transfer", constraints)
        .ttl(Duration::from_secs(300))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());

    // Try passing a string where number is expected
    let mut args = HashMap::new();
    args.insert(
        "amount".to_string(),
        ConstraintValue::String("not a number".to_string()),
    );

    let sig = warrant.sign(&keypair, "transfer", &args).unwrap();
    let result = authorizer.authorize_one(&warrant, "transfer", &args, Some(&sig), &[]);

    match result {
        Ok(_) => {
            println!("  [FAIL] Type mismatch was accepted!");
            panic!("Type confusion vulnerability");
        }
        Err(e) => {
            println!("  [PASS] Type mismatch rejected: {}", e);
        }
    }
}

/// Attack: Pass NaN to Range constraint.
#[test]
fn test_attack_type_confusion_nan() {
    println!("\n--- Attack: Type Confusion (NaN) ---");

    let keypair = SigningKey::generate();
    let mut constraints = ConstraintSet::new();
    constraints.insert("value", Range::new(Some(0.0), Some(100.0)).unwrap());

    let warrant = Warrant::builder()
        .capability("process", constraints)
        .ttl(Duration::from_secs(300))
        .holder(keypair.public_key())
        .build(&keypair)
        .unwrap();

    let authorizer = Authorizer::new().with_trusted_root(keypair.public_key());

    let mut args = HashMap::new();
    args.insert("value".to_string(), ConstraintValue::Float(f64::NAN));

    let sig = warrant.sign(&keypair, "process", &args).unwrap();
    let result = authorizer.authorize_one(&warrant, "process", &args, Some(&sig), &[]);

    match result {
        Ok(_) => {
            println!("  [FAIL] NaN was accepted!");
            panic!("NaN vulnerability");
        }
        Err(e) => {
            println!("  [PASS] NaN rejected: {}", e);
        }
    }
}

// ============================================================================
// Chain Transport Attacks
// ============================================================================

/// Attack: Send WarrantStack missing intermediate warrant.
#[test]
fn test_attack_chain_missing_link() {
    println!("\n--- Attack: Missing Link in Chain ---");

    let root_kp = SigningKey::generate();
    let middle_kp = SigningKey::generate();
    let leaf_kp = SigningKey::generate();

    // Create chain: Root -> Middle -> Leaf
    let root = Warrant::builder()
        .capability("test", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(root_kp.public_key())
        .build(&root_kp)
        .unwrap();

    let middle = root
        .attenuate()
        .capability("test", ConstraintSet::new())
        .holder(middle_kp.public_key())
        .build(&root_kp) // root_kp is parent's holder
        .unwrap();

    let leaf = middle
        .attenuate()
        .capability("test", ConstraintSet::new())
        .holder(leaf_kp.public_key())
        .build(&middle_kp) // middle_kp is parent's holder
        .unwrap();

    let authorizer = Authorizer::new().with_trusted_root(root_kp.public_key());

    // Complete chain should verify
    let complete = vec![root.clone(), middle.clone(), leaf.clone()];
    assert!(authorizer.verify_chain(&complete).is_ok());

    // Attack: Skip middle warrant
    let incomplete = vec![root.clone(), leaf.clone()];
    let result = authorizer.verify_chain(&incomplete);

    match result {
        Ok(_) => panic!("Incomplete chain was accepted!"),
        Err(e) => {
            println!("  [PASS] Missing link rejected: {}", e);
        }
    }
}

/// Attack: Send WarrantStack in wrong order.
#[test]
fn test_attack_chain_shuffled_order() {
    println!("\n--- Attack: Shuffled Chain Order ---");

    let root_kp = SigningKey::generate();
    let child_kp = SigningKey::generate();

    let root = Warrant::builder()
        .capability("test", ConstraintSet::new())
        .ttl(Duration::from_secs(3600))
        .holder(root_kp.public_key())
        .build(&root_kp)
        .unwrap();

    let child = root
        .attenuate()
        .capability("test", ConstraintSet::new())
        .holder(child_kp.public_key())
        .build(&root_kp) // root_kp is parent's holder
        .unwrap();

    let authorizer = Authorizer::new().with_trusted_root(root_kp.public_key());

    // Correct order
    let correct = vec![root.clone(), child.clone()];
    assert!(authorizer.verify_chain(&correct).is_ok());

    // Attack: Reversed order
    let reversed = vec![child.clone(), root.clone()];
    let result = authorizer.verify_chain(&reversed);

    match result {
        Ok(_) => panic!("Reversed chain was accepted!"),
        Err(e) => {
            println!("  [PASS] Shuffled chain rejected: {}", e);
        }
    }
}