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
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
//! Inter-procedural Taint Analysis
//!
//! Extends the intra-procedural taint analysis with function summaries to track
//! taint flow across function boundaries. This enables detection of:
//! - Cross-function taint flows (source in one function, sink in another)
//! - Library function taint behavior
//! - Callback taint propagation
//! - **Cross-file taint flows** via CallGraph integration
//!
//! The analysis works in three phases:
//! 1. Build function summaries: for each function, determine how taint flows
//! from parameters to return value and side effects
//! 2. Apply summaries: at each call site, use the callee's summary to propagate
//! taint from arguments to the call result
//! 3. Cross-file propagation: use CallGraph to track taint across file boundaries
use crate::callgraph::CallGraph;
use crate::flow::cfg::CFG;
use crate::flow::sources::TaintConfig;
use crate::flow::symbol_table::{SymbolTable, ValueOrigin};
use crate::semantics::LanguageSemantics;
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
/// Kind of taint (for categorizing vulnerabilities)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TaintKind {
/// User input (query params, body, headers)
UserInput,
/// File system paths
FilePath,
/// SQL query components
SqlQuery,
/// Command/shell input
Command,
/// HTML/DOM content
Html,
/// URL components
Url,
/// Generic/unknown taint
Unknown,
}
impl TaintKind {
/// Infer taint kind from a source pattern
///
/// Order matters: more specific patterns (like "sql") must be checked
/// before more general patterns (like "query").
pub fn from_source_name(name: &str) -> Self {
let lower = name.to_lowercase();
// Check specific patterns first (order matters!)
if lower.contains("sql") {
TaintKind::SqlQuery
} else if lower.contains("cmd") || lower.contains("exec") || lower.contains("shell") {
TaintKind::Command
} else if lower.contains("html") || lower.contains("dom") {
TaintKind::Html
} else if lower.contains("path") || lower.contains("file") {
TaintKind::FilePath
} else if lower.contains("url") || lower.contains("href") {
TaintKind::Url
} else if lower.contains("query") || lower.contains("body") || lower.contains("param") {
// Generic user input patterns last (most general)
TaintKind::UserInput
} else {
TaintKind::Unknown
}
}
}
/// How a function affects taint flow
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ParamEffect {
/// Parameter taint flows to return value
TaintsReturn,
/// Parameter taint flows to another parameter (by index)
TaintsParam(usize),
/// Parameter taint flows to receiver/this
TaintsReceiver,
/// Parameter taint is sanitized
Sanitized,
/// No taint effect
None,
}
/// Represents the calling context for context-sensitive analysis.
///
/// The calling context captures which parameters are tainted at a call site.
/// This allows different summaries for calls like:
/// - `func(tainted, safe)` - context: [0]
/// - `func(safe, tainted)` - context: [1]
/// - `func(tainted, tainted)` - context: [0, 1]
#[derive(Debug, Clone, Default)]
pub struct CallContext {
/// Set of parameter indices that are tainted in this context
pub tainted_params: HashSet<usize>,
/// Optional taint kinds for each tainted parameter
pub param_taint_kinds: HashMap<usize, TaintKind>,
}
impl PartialEq for CallContext {
fn eq(&self, other: &Self) -> bool {
self.tainted_params == other.tainted_params
}
}
impl Eq for CallContext {}
impl Hash for CallContext {
fn hash<H: Hasher>(&self, state: &mut H) {
// Create a sorted vector for deterministic hashing
let mut params: Vec<_> = self.tainted_params.iter().copied().collect();
params.sort_unstable();
params.hash(state);
}
}
impl CallContext {
/// Create a new empty call context (all parameters safe)
pub fn new() -> Self {
Self {
tainted_params: HashSet::new(),
param_taint_kinds: HashMap::new(),
}
}
/// Create a context from a list of tainted parameter indices
pub fn from_tainted_params(params: impl IntoIterator<Item = usize>) -> Self {
Self {
tainted_params: params.into_iter().collect(),
param_taint_kinds: HashMap::new(),
}
}
/// Create a context with taint kinds
pub fn with_taint_kinds(params: impl IntoIterator<Item = (usize, TaintKind)>) -> Self {
let items: Vec<_> = params.into_iter().collect();
Self {
tainted_params: items.iter().map(|(idx, _)| *idx).collect(),
param_taint_kinds: items.into_iter().collect(),
}
}
/// Check if a specific parameter is tainted in this context
pub fn is_param_tainted(&self, param_idx: usize) -> bool {
self.tainted_params.contains(¶m_idx)
}
/// Mark a parameter as tainted
pub fn mark_tainted(&mut self, param_idx: usize) {
self.tainted_params.insert(param_idx);
}
/// Mark a parameter as tainted with a specific kind
pub fn mark_tainted_with_kind(&mut self, param_idx: usize, kind: TaintKind) {
self.tainted_params.insert(param_idx);
self.param_taint_kinds.insert(param_idx, kind);
}
/// Get the taint kind for a parameter (if known)
pub fn get_taint_kind(&self, param_idx: usize) -> Option<TaintKind> {
self.param_taint_kinds.get(¶m_idx).copied()
}
/// Check if this context has any tainted parameters
pub fn has_tainted_params(&self) -> bool {
!self.tainted_params.is_empty()
}
/// Get the number of tainted parameters
pub fn tainted_count(&self) -> usize {
self.tainted_params.len()
}
/// Create a canonical string representation for use as a map key
pub fn to_key(&self) -> String {
let mut params: Vec<_> = self.tainted_params.iter().copied().collect();
params.sort_unstable();
format!(
"ctx[{}]",
params
.iter()
.map(|p| p.to_string())
.collect::<Vec<_>>()
.join(",")
)
}
/// Check if this context is a subset of another (less specific)
pub fn is_subset_of(&self, other: &CallContext) -> bool {
self.tainted_params.is_subset(&other.tainted_params)
}
/// Check if this context is a superset of another (more specific)
pub fn is_superset_of(&self, other: &CallContext) -> bool {
self.tainted_params.is_superset(&other.tainted_params)
}
}
/// Context-specific summary result for a function.
///
/// This represents what happens when a function is called with a specific
/// taint context (which parameters are tainted).
#[derive(Debug, Clone, Default)]
pub struct ContextSpecificResult {
/// Whether the return value is tainted given this context
pub return_tainted: bool,
/// The taint kind of the return value (if tainted)
pub return_taint_kind: Option<TaintKind>,
/// Which parameters (by index) contribute to tainting the return
pub contributing_params: HashSet<usize>,
/// Side effects: which other values become tainted
pub side_effect_taints: HashMap<String, TaintKind>,
}
impl ContextSpecificResult {
/// Create a result indicating the return is tainted
pub fn tainted_return(kind: TaintKind) -> Self {
Self {
return_tainted: true,
return_taint_kind: Some(kind),
contributing_params: HashSet::new(),
side_effect_taints: HashMap::new(),
}
}
/// Create a result indicating the return is safe (sanitized)
pub fn safe_return() -> Self {
Self {
return_tainted: false,
return_taint_kind: None,
contributing_params: HashSet::new(),
side_effect_taints: HashMap::new(),
}
}
/// Mark that a specific parameter contributes to the tainted return
pub fn with_contributing_param(mut self, param_idx: usize) -> Self {
self.contributing_params.insert(param_idx);
self
}
/// Add a side effect taint
pub fn with_side_effect(mut self, name: String, kind: TaintKind) -> Self {
self.side_effect_taints.insert(name, kind);
self
}
}
/// Context-sensitive function summary.
///
/// Unlike the basic `FunctionSummary` which provides a single summary for all calls,
/// `ContextSensitiveSummary` maintains different summaries for different calling contexts.
///
/// For example, a function `process(a, b)` might:
/// - Return tainted when `a` is tainted (context [0])
/// - Return safe when `b` is tainted (sanitizes param 1) (context [1])
/// - Return tainted when both are tainted (context [0, 1])
#[derive(Debug, Clone)]
pub struct ContextSensitiveSummary {
/// Function name
pub name: String,
/// The base (context-insensitive) summary
pub base_summary: FunctionSummary,
/// Context-specific summaries: context -> result
pub context_summaries: HashMap<CallContext, ContextSpecificResult>,
/// Context-specific parameter effects
/// Maps (context, param_index) -> effects for that param in that context
pub context_param_effects: HashMap<(CallContext, usize), Vec<ParamEffect>>,
/// Parameters that always sanitize (regardless of context)
pub always_sanitizes: HashSet<usize>,
/// Parameters that always taint return (regardless of context)
pub always_taints_return: HashSet<usize>,
/// Number of parameters this function accepts
pub param_count: usize,
}
impl ContextSensitiveSummary {
/// Create a new context-sensitive summary from a base summary
pub fn new(base_summary: FunctionSummary) -> Self {
let name = base_summary.name.clone();
// Determine which params always taint return based on base summary
let always_taints_return: HashSet<usize> = base_summary
.param_effects
.iter()
.filter_map(|(&idx, effects)| {
if effects.contains(&ParamEffect::TaintsReturn) {
Some(idx)
} else {
None
}
})
.collect();
Self {
name,
base_summary,
context_summaries: HashMap::new(),
context_param_effects: HashMap::new(),
always_sanitizes: HashSet::new(),
always_taints_return,
param_count: 0,
}
}
/// Create from a base summary with explicit param count
pub fn with_param_count(base_summary: FunctionSummary, param_count: usize) -> Self {
let mut summary = Self::new(base_summary);
summary.param_count = param_count;
summary
}
/// Add or update a context-specific summary
pub fn add_context_summary(&mut self, context: CallContext, result: ContextSpecificResult) {
self.context_summaries.insert(context, result);
}
/// Add context-specific parameter effects
pub fn add_context_param_effect(
&mut self,
context: CallContext,
param_idx: usize,
effect: ParamEffect,
) {
self.context_param_effects
.entry((context, param_idx))
.or_default()
.push(effect);
}
/// Mark a parameter as always sanitizing
pub fn mark_always_sanitizes(&mut self, param_idx: usize) {
self.always_sanitizes.insert(param_idx);
}
/// Mark a parameter as always tainting return
pub fn mark_always_taints_return(&mut self, param_idx: usize) {
self.always_taints_return.insert(param_idx);
}
/// Query the summary for a specific calling context.
///
/// This is the main entry point for context-sensitive taint analysis at call sites.
/// Given the taint status of arguments, returns what happens to the return value.
pub fn query(&self, context: &CallContext) -> ContextSpecificResult {
// First, check for an exact match
if let Some(result) = self.context_summaries.get(context) {
return result.clone();
}
// If no exact match, compute based on rules
self.compute_result_for_context(context)
}
/// Compute the result for a context that doesn't have an explicit entry
fn compute_result_for_context(&self, context: &CallContext) -> ContextSpecificResult {
let mut result = ContextSpecificResult::default();
// Check if the function is a source (always taints return)
if self.base_summary.is_source {
result.return_tainted = true;
result.return_taint_kind = self.base_summary.source_kind;
return result;
}
// Check each tainted parameter
for ¶m_idx in &context.tainted_params {
// Check if this param always sanitizes
if self.always_sanitizes.contains(¶m_idx) {
// This param sanitizes, so it doesn't contribute to return taint
continue;
}
// Check if this param always taints return
if self.always_taints_return.contains(¶m_idx) {
result.return_tainted = true;
result.contributing_params.insert(param_idx);
if result.return_taint_kind.is_none() {
result.return_taint_kind = context.get_taint_kind(param_idx);
}
continue;
}
// Check base summary for this param's effects
if let Some(effects) = self.base_summary.param_effects.get(¶m_idx) {
for effect in effects {
match effect {
ParamEffect::TaintsReturn => {
result.return_tainted = true;
result.contributing_params.insert(param_idx);
if result.return_taint_kind.is_none() {
result.return_taint_kind = context.get_taint_kind(param_idx);
}
}
ParamEffect::Sanitized => {
// This specific param is sanitized in this call
}
ParamEffect::TaintsParam(other_idx) => {
// Track that param_idx taints another param
result.side_effect_taints.insert(
format!("param_{}", other_idx),
context
.get_taint_kind(param_idx)
.unwrap_or(TaintKind::Unknown),
);
}
ParamEffect::TaintsReceiver => {
result.side_effect_taints.insert(
"receiver".to_string(),
context
.get_taint_kind(param_idx)
.unwrap_or(TaintKind::Unknown),
);
}
ParamEffect::None => {}
}
}
}
}
// Handle sanitizer functions - if the function is a sanitizer, output is safe
if self.base_summary.is_sanitizer {
result.return_tainted = false;
result.return_taint_kind = None;
}
result
}
/// Check if the return value would be tainted given this context
pub fn is_return_tainted(&self, context: &CallContext) -> bool {
self.query(context).return_tainted
}
/// Get all known contexts for this summary
pub fn known_contexts(&self) -> impl Iterator<Item = &CallContext> {
self.context_summaries.keys()
}
/// Build a summary for a specific context from the base summary
pub fn build_for_context(&mut self, context: CallContext) {
let result = self.compute_result_for_context(&context);
self.context_summaries.insert(context, result);
}
/// Merge another context-sensitive summary into this one
pub fn merge(&mut self, other: &ContextSensitiveSummary) {
for (context, result) in &other.context_summaries {
self.context_summaries
.entry(context.clone())
.or_insert_with(|| result.clone());
}
for ((context, param_idx), effects) in &other.context_param_effects {
self.context_param_effects
.entry((context.clone(), *param_idx))
.or_default()
.extend(effects.clone());
}
self.always_sanitizes.extend(&other.always_sanitizes);
self.always_taints_return
.extend(&other.always_taints_return);
}
}
/// Summary of a function's taint behavior
#[derive(Debug, Clone)]
pub struct FunctionSummary {
/// Function name (fully qualified if possible)
pub name: String,
/// Effects of each parameter (index -> effects)
pub param_effects: HashMap<usize, Vec<ParamEffect>>,
/// Whether the function is a taint source
pub is_source: bool,
/// Whether the function is a taint sink (and which param is sensitive)
pub sink_params: Vec<usize>,
/// Whether the function sanitizes its input
pub is_sanitizer: bool,
/// The kind of taint this function produces (if source)
pub source_kind: Option<TaintKind>,
/// Line number of function definition
pub line: usize,
/// Node ID of function definition
pub node_id: usize,
/// File containing this function (for cross-file tracking)
pub file: Option<PathBuf>,
/// Whether this function is exported (visible to other files)
pub is_exported: bool,
}
/// Summary specifically for cross-file taint tracking
///
/// This extends FunctionSummary with additional information needed for
/// cross-file analysis via the CallGraph.
#[derive(Debug, Clone)]
pub struct TaintSummary {
/// The underlying function summary
pub function: FunctionSummary,
/// Which parameter indices flow to the return value (for quick lookup)
pub params_to_return: HashSet<usize>,
/// Whether any parameter can taint the return value
pub propagates_taint: bool,
/// Taint kinds this function can introduce (if source)
pub introduced_taint_kinds: Vec<TaintKind>,
/// Taint kinds this function sanitizes
pub sanitized_taint_kinds: Vec<TaintKind>,
/// Functions this function calls (for transitive analysis)
pub callees: Vec<String>,
}
impl TaintSummary {
/// Create a TaintSummary from a FunctionSummary
pub fn from_function_summary(summary: FunctionSummary) -> Self {
let params_to_return: HashSet<usize> = summary
.param_effects
.iter()
.filter_map(|(&idx, effects)| {
if effects.contains(&ParamEffect::TaintsReturn) {
Some(idx)
} else {
None
}
})
.collect();
let propagates_taint = !params_to_return.is_empty();
let introduced_taint_kinds = if summary.is_source {
summary.source_kind.into_iter().collect()
} else {
Vec::new()
};
Self {
function: summary,
params_to_return,
propagates_taint,
introduced_taint_kinds,
sanitized_taint_kinds: Vec::new(),
callees: Vec::new(),
}
}
/// Check if taint from a specific parameter reaches the return value
pub fn param_taints_return(&self, param_idx: usize) -> bool {
self.params_to_return.contains(¶m_idx)
}
/// Check if this function is a taint source
pub fn is_source(&self) -> bool {
self.function.is_source
}
/// Check if this function is a sanitizer
pub fn is_sanitizer(&self) -> bool {
self.function.is_sanitizer
}
/// Get the function name
pub fn name(&self) -> &str {
&self.function.name
}
/// Get the file path if available
pub fn file(&self) -> Option<&Path> {
self.function.file.as_deref()
}
}
impl FunctionSummary {
/// Create an empty summary for a function
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
param_effects: HashMap::new(),
is_source: false,
sink_params: Vec::new(),
is_sanitizer: false,
source_kind: None,
line: 0,
node_id: 0,
file: None,
is_exported: false,
}
}
/// Set the file path for this function
pub fn with_file(mut self, file: PathBuf) -> Self {
self.file = Some(file);
self
}
/// Mark this function as exported
pub fn as_exported(mut self) -> Self {
self.is_exported = true;
self
}
/// Mark this function as a taint source
pub fn as_source(mut self, kind: TaintKind) -> Self {
self.is_source = true;
self.source_kind = Some(kind);
self
}
/// Mark this function as a sanitizer
pub fn as_sanitizer(mut self) -> Self {
self.is_sanitizer = true;
self
}
/// Mark a parameter as flowing to return value
pub fn param_to_return(mut self, param_idx: usize) -> Self {
self.param_effects
.entry(param_idx)
.or_default()
.push(ParamEffect::TaintsReturn);
self
}
/// Mark a parameter as a sink
pub fn with_sink_param(mut self, param_idx: usize) -> Self {
self.sink_params.push(param_idx);
self
}
/// Check if taint from a parameter flows to return
pub fn param_taints_return(&self, param_idx: usize) -> bool {
self.param_effects
.get(¶m_idx)
.map(|effects| effects.contains(&ParamEffect::TaintsReturn))
.unwrap_or(false)
}
/// Check if any parameter taints the return value
pub fn any_param_taints_return(&self) -> bool {
self.param_effects
.values()
.any(|effects| effects.contains(&ParamEffect::TaintsReturn))
}
}
/// Represents a call site in the program
#[derive(Debug, Clone)]
pub struct CallSite {
/// Node ID of the call expression
pub node_id: usize,
/// Name of the called function
pub callee_name: String,
/// Arguments at this call site
pub arguments: Vec<CallArg>,
/// Line number
pub line: usize,
/// Block ID in CFG (if available)
pub block_id: Option<usize>,
/// The variable receiving the call result (if any)
pub result_var: Option<String>,
}
/// An argument at a call site
#[derive(Debug, Clone)]
pub struct CallArg {
/// Argument index (0-based)
pub index: usize,
/// The expression text
pub expr: String,
/// If the argument is a simple variable, its name
pub var_name: Option<String>,
/// Whether this argument is tainted
pub is_tainted: bool,
/// The kind of taint (if tainted)
pub taint_kind: Option<TaintKind>,
}
/// An endpoint in a taint flow (source or sink)
#[derive(Debug, Clone)]
pub struct TaintEndpoint {
/// Variable or expression name
pub name: String,
/// Line number
pub line: usize,
/// Node ID
pub node_id: usize,
/// Function containing this endpoint
pub function: Option<String>,
/// Kind of taint
pub kind: TaintKind,
/// File containing this endpoint (for cross-file tracking)
pub file: Option<PathBuf>,
}
/// A complete taint flow from source to sink
#[derive(Debug, Clone)]
pub struct TaintFlow {
/// The source of taint
pub source: TaintEndpoint,
/// The sink where taint reaches
pub sink: TaintEndpoint,
/// Intermediate variables/expressions in the flow (if tracked)
pub path: Vec<String>,
/// Whether this flow crosses function boundaries
pub is_interprocedural: bool,
/// Functions involved in the flow
pub functions_involved: Vec<String>,
/// Whether this flow crosses file boundaries
pub is_cross_file: bool,
/// Files involved in the flow (for cross-file flows)
pub files_involved: Vec<PathBuf>,
}
impl TaintFlow {
/// Create a simple intraprocedural flow
pub fn intraprocedural(source: TaintEndpoint, sink: TaintEndpoint) -> Self {
let func = source.function.clone();
let file = source.file.clone();
Self {
source,
sink,
path: Vec::new(),
is_interprocedural: false,
functions_involved: func.into_iter().collect(),
is_cross_file: false,
files_involved: file.into_iter().collect(),
}
}
/// Create an interprocedural flow
pub fn interprocedural(
source: TaintEndpoint,
sink: TaintEndpoint,
functions: Vec<String>,
) -> Self {
let is_cross_file = source.file != sink.file;
let mut files = Vec::new();
if let Some(ref f) = source.file {
files.push(f.clone());
}
if let Some(ref f) = sink.file
&& !files.contains(f)
{
files.push(f.clone());
}
Self {
source,
sink,
path: Vec::new(),
is_interprocedural: true,
functions_involved: functions,
is_cross_file,
files_involved: files,
}
}
/// Create a cross-file flow
pub fn cross_file(
source: TaintEndpoint,
sink: TaintEndpoint,
functions: Vec<String>,
files: Vec<PathBuf>,
) -> Self {
Self {
source,
sink,
path: Vec::new(),
is_interprocedural: true,
functions_involved: functions,
is_cross_file: true,
files_involved: files,
}
}
/// Add intermediate path elements
pub fn with_path(mut self, path: Vec<String>) -> Self {
self.path = path;
self
}
/// Add files involved in the flow
pub fn with_files(mut self, files: Vec<PathBuf>) -> Self {
self.files_involved = files;
self.is_cross_file = self.files_involved.len() > 1;
self
}
}
/// Result of inter-procedural taint analysis
#[derive(Debug, Default)]
pub struct InterproceduralResult {
/// Function summaries (function name -> summary)
pub summaries: HashMap<String, FunctionSummary>,
/// Context-sensitive summaries (function name -> context-sensitive summary)
pub context_sensitive_summaries: HashMap<String, ContextSensitiveSummary>,
/// Taint summaries for cross-file analysis (file:function -> summary)
pub taint_summaries: HashMap<String, TaintSummary>,
/// Detected taint flows from sources to sinks
pub flows: Vec<TaintFlow>,
/// Call sites in the program
pub call_sites: Vec<CallSite>,
/// Variables tainted at each function (function name -> set of tainted vars)
pub function_taint: HashMap<String, HashSet<String>>,
/// Number of analysis iterations
pub iterations: usize,
/// Cross-file taint flows (detected via CallGraph)
pub cross_file_flows: Vec<TaintFlow>,
/// File path for this result (if single-file analysis)
pub file: Option<PathBuf>,
}
impl InterproceduralResult {
/// Get summary for a function
pub fn get_summary(&self, func_name: &str) -> Option<&FunctionSummary> {
self.summaries.get(func_name)
}
/// Get context-sensitive summary for a function
pub fn get_context_sensitive_summary(
&self,
func_name: &str,
) -> Option<&ContextSensitiveSummary> {
self.context_sensitive_summaries.get(func_name)
}
/// Get mutable context-sensitive summary for a function
pub fn get_context_sensitive_summary_mut(
&mut self,
func_name: &str,
) -> Option<&mut ContextSensitiveSummary> {
self.context_sensitive_summaries.get_mut(func_name)
}
/// Query a function with a specific calling context.
///
/// This is the primary way to use context-sensitive analysis.
/// Given which arguments are tainted, returns the taint result.
///
/// # Example
/// ```ignore
/// // For a call site: result = func(tainted_var, safe_var)
/// let context = CallContext::from_tainted_params([0]);
/// let result = analysis_result.query_with_context("func", &context);
/// if result.return_tainted {
/// // The result is tainted
/// }
/// ```
pub fn query_with_context(
&self,
func_name: &str,
context: &CallContext,
) -> ContextSpecificResult {
// First try context-sensitive summary
if let Some(cs_summary) = self.context_sensitive_summaries.get(func_name) {
return cs_summary.query(context);
}
// Fall back to basic summary
if let Some(summary) = self.summaries.get(func_name) {
let mut result = ContextSpecificResult::default();
// Check if function is a source
if summary.is_source {
result.return_tainted = true;
result.return_taint_kind = summary.source_kind;
return result;
}
// Check if function is a sanitizer
if summary.is_sanitizer {
return result; // Safe return
}
// Check each tainted param
for ¶m_idx in &context.tainted_params {
if summary.param_taints_return(param_idx) {
result.return_tainted = true;
result.contributing_params.insert(param_idx);
if result.return_taint_kind.is_none() {
result.return_taint_kind = context.get_taint_kind(param_idx);
}
}
}
return result;
}
// Unknown function - conservative: tainted input -> tainted output
let mut result = ContextSpecificResult::default();
if context.has_tainted_params() {
result.return_tainted = true;
result.return_taint_kind = Some(TaintKind::Unknown);
result.contributing_params = context.tainted_params.clone();
}
result
}
/// Create or get the context-sensitive summary for a function
pub fn ensure_context_sensitive_summary(
&mut self,
func_name: &str,
) -> &mut ContextSensitiveSummary {
if !self.context_sensitive_summaries.contains_key(func_name) {
let base_summary = self
.summaries
.get(func_name)
.cloned()
.unwrap_or_else(|| FunctionSummary::new(func_name));
let cs_summary = ContextSensitiveSummary::new(base_summary);
self.context_sensitive_summaries
.insert(func_name.to_string(), cs_summary);
}
self.context_sensitive_summaries.get_mut(func_name).unwrap()
}
/// Get taint summary for a function (with cross-file info)
pub fn get_taint_summary(&self, func_name: &str) -> Option<&TaintSummary> {
self.taint_summaries.get(func_name)
}
/// Get taint summary by file and function name
pub fn get_taint_summary_by_file(&self, file: &Path, func_name: &str) -> Option<&TaintSummary> {
let key = format!("{}:{}", file.display(), func_name);
self.taint_summaries.get(&key)
}
/// Check if a function is a known source
pub fn is_source(&self, func_name: &str) -> bool {
self.summaries
.get(func_name)
.map(|s| s.is_source)
.unwrap_or(false)
}
/// Check if a function is a known sanitizer
pub fn is_sanitizer(&self, func_name: &str) -> bool {
self.summaries
.get(func_name)
.map(|s| s.is_sanitizer)
.unwrap_or(false)
}
/// Get all detected flows
pub fn get_flows(&self) -> &[TaintFlow] {
&self.flows
}
/// Get flows crossing function boundaries
pub fn interprocedural_flows(&self) -> Vec<&TaintFlow> {
self.flows.iter().filter(|f| f.is_interprocedural).collect()
}
/// Get flows crossing file boundaries
pub fn cross_file_flows(&self) -> Vec<&TaintFlow> {
self.flows
.iter()
.chain(self.cross_file_flows.iter())
.filter(|f| f.is_cross_file)
.collect()
}
/// Get flows of a specific taint kind
pub fn flows_by_kind(&self, kind: TaintKind) -> Vec<&TaintFlow> {
self.flows
.iter()
.filter(|f| f.source.kind == kind)
.collect()
}
/// Count total flows detected
pub fn flow_count(&self) -> usize {
self.flows.len() + self.cross_file_flows.len()
}
/// Add a taint summary for a function
pub fn add_taint_summary(&mut self, summary: TaintSummary) {
let key = if let Some(ref file) = summary.function.file {
format!("{}:{}", file.display(), summary.function.name)
} else {
summary.function.name.clone()
};
self.taint_summaries.insert(key, summary);
}
/// Merge another result into this one (for cross-file analysis)
pub fn merge(&mut self, other: InterproceduralResult) {
self.summaries.extend(other.summaries);
self.taint_summaries.extend(other.taint_summaries);
self.flows.extend(other.flows);
self.call_sites.extend(other.call_sites);
for (func, vars) in other.function_taint {
self.function_taint.entry(func).or_default().extend(vars);
}
self.cross_file_flows.extend(other.cross_file_flows);
// Merge context-sensitive summaries
for (func_name, other_summary) in other.context_sensitive_summaries {
if let Some(existing) = self.context_sensitive_summaries.get_mut(&func_name) {
existing.merge(&other_summary);
} else {
self.context_sensitive_summaries
.insert(func_name, other_summary);
}
}
}
}
/// Inter-procedural taint analyzer
pub struct InterproceduralAnalyzer<'a> {
/// Language semantics
semantics: &'static LanguageSemantics,
/// Taint configuration
config: &'a TaintConfig,
/// Source code bytes
source: &'a [u8],
/// Parsed tree
tree: &'a tree_sitter::Tree,
/// Optional call graph for cross-file analysis
call_graph: Option<&'a CallGraph>,
/// Current file path (for cross-file tracking)
file_path: Option<PathBuf>,
}
impl<'a> InterproceduralAnalyzer<'a> {
/// Create a new analyzer
pub fn new(
semantics: &'static LanguageSemantics,
config: &'a TaintConfig,
source: &'a [u8],
tree: &'a tree_sitter::Tree,
) -> Self {
Self {
semantics,
config,
source,
tree,
call_graph: None,
file_path: None,
}
}
/// Create an analyzer with a call graph for cross-file analysis
pub fn with_call_graph(
semantics: &'static LanguageSemantics,
config: &'a TaintConfig,
source: &'a [u8],
tree: &'a tree_sitter::Tree,
call_graph: &'a CallGraph,
file_path: PathBuf,
) -> Self {
Self {
semantics,
config,
source,
tree,
call_graph: Some(call_graph),
file_path: Some(file_path),
}
}
/// Run the inter-procedural analysis
pub fn analyze(&self, symbols: &SymbolTable, cfg: &CFG) -> InterproceduralResult {
let mut result = InterproceduralResult {
file: self.file_path.clone(),
..Default::default()
};
// Phase 1: Build initial function summaries from knowledge base
self.build_known_summaries(&mut result);
// Phase 2: Extract function definitions and build local summaries
self.extract_function_summaries(&mut result);
// Phase 3: Extract call sites
self.extract_call_sites(symbols, &mut result);
// Phase 4: Build context-sensitive summaries from call sites
self.build_context_sensitive_summaries(&mut result);
// Phase 5: Propagate taint through call graph with context-sensitivity (fixed-point iteration)
self.propagate_taint_context_sensitive(symbols, &mut result);
// Phase 6: Detect source-to-sink flows (now context-aware)
self.detect_flows_context_sensitive(symbols, cfg, &mut result);
// Phase 7: Cross-file taint propagation (if call graph available)
if let (Some(call_graph), Some(file_path)) = (self.call_graph, &self.file_path) {
self.propagate_cross_file_taint(&mut result);
// Phase 8: Event-based taint propagation
self.propagate_event_taint(call_graph, file_path, &mut result);
}
// Build taint summaries from function summaries
self.build_taint_summaries(&mut result);
result
}
/// Run analysis with a call graph for cross-file taint tracking
pub fn analyze_with_call_graph(
&self,
symbols: &SymbolTable,
cfg: &CFG,
call_graph: &CallGraph,
file_path: &Path,
) -> InterproceduralResult {
let mut result = InterproceduralResult {
file: Some(file_path.to_path_buf()),
..Default::default()
};
// Phase 1: Build initial function summaries from knowledge base
self.build_known_summaries(&mut result);
// Phase 2: Extract function definitions and build local summaries
self.extract_function_summaries(&mut result);
// Phase 3: Extract call sites
self.extract_call_sites(symbols, &mut result);
// Phase 4: Build context-sensitive summaries from call sites
self.build_context_sensitive_summaries(&mut result);
// Phase 5: Propagate taint through call graph with context-sensitivity (fixed-point iteration)
self.propagate_taint_context_sensitive(symbols, &mut result);
// Phase 6: Detect source-to-sink flows (now context-aware)
self.detect_flows_context_sensitive(symbols, cfg, &mut result);
// Phase 7: Cross-file taint propagation using the call graph
self.propagate_cross_file_taint_with_graph(call_graph, file_path, &mut result);
// Phase 8: Event-based taint propagation
self.propagate_event_taint(call_graph, file_path, &mut result);
// Build taint summaries from function summaries
self.build_taint_summaries(&mut result);
result
}
/// Build TaintSummary objects from FunctionSummary objects
fn build_taint_summaries(&self, result: &mut InterproceduralResult) {
// Collect summaries first to avoid borrowing conflict
let summaries_to_add: Vec<TaintSummary> = result
.summaries
.values()
.map(|summary| {
let mut func_summary = summary.clone();
func_summary.file = self.file_path.clone();
TaintSummary::from_function_summary(func_summary)
})
.collect();
for taint_summary in summaries_to_add {
result.add_taint_summary(taint_summary);
}
}
/// Build context-sensitive summaries for all functions based on observed call sites.
///
/// For each function, we create a ContextSensitiveSummary that tracks how different
/// combinations of tainted parameters affect the return value.
fn build_context_sensitive_summaries(&self, result: &mut InterproceduralResult) {
// First, create context-sensitive wrappers for all base summaries
let func_names: Vec<String> = result.summaries.keys().cloned().collect();
for func_name in func_names {
let base_summary = result.summaries.get(&func_name).unwrap().clone();
let cs_summary = ContextSensitiveSummary::new(base_summary);
result
.context_sensitive_summaries
.insert(func_name, cs_summary);
}
// Collect unique calling contexts observed at call sites
let call_contexts: Vec<(String, CallContext)> = result
.call_sites
.iter()
.map(|cs| {
let mut context = CallContext::new();
for arg in &cs.arguments {
if arg.is_tainted {
let kind = arg.taint_kind.unwrap_or(TaintKind::Unknown);
context.mark_tainted_with_kind(arg.index, kind);
}
}
(cs.callee_name.clone(), context)
})
.collect();
// Build summaries for each observed context
for (func_name, context) in call_contexts {
if let Some(cs_summary) = result.context_sensitive_summaries.get_mut(&func_name) {
cs_summary.build_for_context(context);
}
}
// Also build common contexts (all single-param tainted) for discovered functions
let discovered_funcs: Vec<(String, usize)> = result
.summaries
.iter()
.map(|(name, summary)| {
// Estimate param count from effects or default to 2
let param_count = summary
.param_effects
.keys()
.copied()
.max()
.map(|m| m + 1)
.unwrap_or(2);
(name.clone(), param_count)
})
.collect();
for (func_name, param_count) in discovered_funcs {
if let Some(cs_summary) = result.context_sensitive_summaries.get_mut(&func_name) {
// Build single-param contexts
for i in 0..param_count {
let context = CallContext::from_tainted_params([i]);
cs_summary.build_for_context(context);
}
}
}
}
/// Propagate taint through the call graph with context-sensitivity.
///
/// This is an enhanced version of `propagate_taint` that uses context-sensitive
/// summaries to more precisely track taint flow.
fn propagate_taint_context_sensitive(
&self,
symbols: &SymbolTable,
result: &mut InterproceduralResult,
) {
// Initialize with locally tainted variables
for (name, info) in symbols.iter() {
if self.is_initially_tainted(&info.initializer) {
result
.function_taint
.entry(String::new())
.or_default()
.insert(name.clone());
}
}
// Track taint kinds for variables
let mut var_taint_kinds: HashMap<String, TaintKind> = HashMap::new();
// Initialize taint kinds from symbols
for (name, info) in symbols.iter() {
if let ValueOrigin::FunctionCall(func_name) = &info.initializer
&& let Some(summary) = result.summaries.get(func_name)
&& summary.is_source
{
var_taint_kinds.insert(
name.clone(),
summary.source_kind.unwrap_or(TaintKind::Unknown),
);
}
}
// Fixed-point iteration with context-sensitivity
let mut changed = true;
let mut iterations = 0;
const MAX_ITERATIONS: usize = 100;
while changed && iterations < MAX_ITERATIONS {
changed = false;
iterations += 1;
// Process each call site with context-sensitivity
for i in 0..result.call_sites.len() {
let call_site = &result.call_sites[i];
let callee_name = call_site.callee_name.clone();
let result_var = call_site.result_var.clone();
// Build the calling context based on current taint state
let mut context = CallContext::new();
for arg in &call_site.arguments {
// Check if argument is tainted (either directly or by lookup)
let is_tainted = arg.is_tainted
|| arg.var_name.as_ref().is_some_and(|name| {
result
.function_taint
.values()
.any(|vars| vars.contains(name))
});
if is_tainted {
let kind = arg
.taint_kind
.or_else(|| {
arg.var_name
.as_ref()
.and_then(|n| var_taint_kinds.get(n).copied())
})
.unwrap_or(TaintKind::Unknown);
context.mark_tainted_with_kind(arg.index, kind);
}
}
// Query the function with this context
let query_result = result.query_with_context(&callee_name, &context);
// If result is tainted and assigned to a variable, mark it
if query_result.return_tainted
&& let Some(ref result_var_name) = result_var
{
let func_taint = result.function_taint.entry(String::new()).or_default();
if !func_taint.contains(result_var_name) {
func_taint.insert(result_var_name.clone());
changed = true;
// Track the taint kind
if let Some(kind) = query_result.return_taint_kind {
var_taint_kinds.insert(result_var_name.clone(), kind);
}
}
}
// Handle side effects (tainting other params/receiver)
for (target, kind) in &query_result.side_effect_taints {
let func_taint = result.function_taint.entry(String::new()).or_default();
if !func_taint.contains(target) {
func_taint.insert(target.clone());
changed = true;
var_taint_kinds.insert(target.clone(), *kind);
}
}
}
}
result.iterations = iterations;
}
/// Detect source-to-sink flows with context-sensitivity.
///
/// This enhanced flow detection uses context-sensitive summaries to avoid
/// false positives where sanitization depends on which parameter is tainted.
fn detect_flows_context_sensitive(
&self,
symbols: &SymbolTable,
_cfg: &CFG,
result: &mut InterproceduralResult,
) {
// Find all sinks and check if their arguments are tainted
for call_site in &result.call_sites {
if let Some(summary) = result.summaries.get(&call_site.callee_name)
&& !summary.sink_params.is_empty()
{
// This is a sink
for &sink_param in &summary.sink_params {
if let Some(arg) = call_site.arguments.get(sink_param) {
// Check if this argument is tainted
let is_tainted = arg.is_tainted
|| arg.var_name.as_ref().is_some_and(|name| {
result
.function_taint
.values()
.any(|vars| vars.contains(name))
});
if is_tainted {
// Check if the taint was sanitized using context-sensitive analysis
let var_name = arg.var_name.as_deref().unwrap_or(&arg.expr);
// Trace back through call sites to see if taint was sanitized
if self.is_taint_sanitized_context_sensitive(var_name, result) {
// Taint was sanitized, no flow to report
continue;
}
// Find the source of taint
if let Some(source) = self.find_taint_source(var_name, symbols, result)
{
let sink = TaintEndpoint {
name: call_site.callee_name.clone(),
line: call_site.line,
node_id: call_site.node_id,
function: None,
kind: TaintKind::from_source_name(&call_site.callee_name),
file: self.file_path.clone(),
};
let flow = TaintFlow::intraprocedural(source, sink);
result.flows.push(flow);
}
}
}
}
}
}
}
/// Check if taint on a variable was sanitized using context-sensitive analysis.
///
/// This traces back through the call chain to see if any sanitizing function
/// was called in a context that would sanitize the taint.
fn is_taint_sanitized_context_sensitive(
&self,
var_name: &str,
result: &InterproceduralResult,
) -> bool {
// Find call sites that assign to this variable
for call_site in &result.call_sites {
if call_site.result_var.as_deref() == Some(var_name) {
// Check if the callee is a sanitizer
if let Some(summary) = result.summaries.get(&call_site.callee_name)
&& summary.is_sanitizer
{
return true;
}
// Check context-sensitive sanitization
if let Some(cs_summary) = result
.context_sensitive_summaries
.get(&call_site.callee_name)
{
// Build the context for this call
let mut context = CallContext::new();
for arg in &call_site.arguments {
if arg.is_tainted {
context.mark_tainted(arg.index);
}
}
// Query the summary
let query_result = cs_summary.query(&context);
// If the result is not tainted, the function sanitized the input
if !query_result.return_tainted && context.has_tainted_params() {
return true;
}
}
}
}
false
}
/// Propagate taint across file boundaries using the call graph
fn propagate_cross_file_taint(&self, result: &mut InterproceduralResult) {
if let Some(call_graph) = self.call_graph
&& let Some(ref file_path) = self.file_path
{
self.propagate_cross_file_taint_with_graph(call_graph, file_path, result);
}
}
/// Propagate taint across file boundaries using a provided call graph
fn propagate_cross_file_taint_with_graph(
&self,
call_graph: &CallGraph,
file_path: &Path,
result: &mut InterproceduralResult,
) {
// For each call site, check if the callee is in another file
for call_site in &result.call_sites {
// Try to find the callee in the call graph
let callees = call_graph.get_functions_by_name(&call_site.callee_name);
for callee in callees {
// Skip if it's in the same file
if callee.file == file_path {
continue;
}
// Check if the callee is a known source
if let Some(summary) = result.summaries.get(&call_site.callee_name) {
if summary.is_source {
// If calling a source function from another file, the result is tainted
if let Some(ref result_var) = call_site.result_var {
result
.function_taint
.entry(String::new())
.or_default()
.insert(result_var.clone());
}
}
// Check if any tainted argument flows through a cross-file function
for arg in &call_site.arguments {
if arg.is_tainted
&& summary.param_taints_return(arg.index)
&& let Some(ref result_var) = call_site.result_var
{
result
.function_taint
.entry(String::new())
.or_default()
.insert(result_var.clone());
}
}
}
}
}
// Detect cross-file flows
self.detect_cross_file_flows(call_graph, file_path, result);
}
/// Detect taint flows that cross file boundaries
fn detect_cross_file_flows(
&self,
call_graph: &CallGraph,
file_path: &Path,
result: &mut InterproceduralResult,
) {
// For each call site that calls a sink
for call_site in &result.call_sites {
if let Some(summary) = result.summaries.get(&call_site.callee_name)
&& !summary.sink_params.is_empty()
{
// This is a sink - check if any argument is tainted via cross-file call
for &sink_param in &summary.sink_params {
if let Some(arg) = call_site.arguments.get(sink_param) {
// Check if the argument variable was tainted by a cross-file source
if let Some(ref var_name) = arg.var_name
&& let Some(source_info) =
self.find_cross_file_source(var_name, call_graph, result)
{
let source = TaintEndpoint {
name: source_info.0.clone(),
line: source_info.1,
node_id: 0,
function: Some(source_info.2.clone()),
kind: source_info.3,
file: Some(source_info.4.clone()),
};
let sink = TaintEndpoint {
name: call_site.callee_name.clone(),
line: call_site.line,
node_id: call_site.node_id,
function: None,
kind: TaintKind::from_source_name(&call_site.callee_name),
file: Some(file_path.to_path_buf()),
};
let flow = TaintFlow::cross_file(
source,
sink,
vec![source_info.2, call_site.callee_name.clone()],
vec![source_info.4, file_path.to_path_buf()],
);
result.cross_file_flows.push(flow);
}
}
}
}
}
}
/// Find if a variable was tainted by a cross-file source function
/// Returns (source_name, line, function_name, taint_kind, source_file)
fn find_cross_file_source(
&self,
var_name: &str,
call_graph: &CallGraph,
result: &InterproceduralResult,
) -> Option<(String, usize, String, TaintKind, PathBuf)> {
// Check each call site to see if this variable was assigned from a source
for cs in &result.call_sites {
if cs.result_var.as_deref() == Some(var_name) {
// Check if the callee is a source in another file
let callees = call_graph.get_functions_by_name(&cs.callee_name);
for callee in callees {
if let Some(summary) = result.summaries.get(&cs.callee_name)
&& summary.is_source
{
return Some((
var_name.to_string(),
cs.line,
cs.callee_name.clone(),
summary.source_kind.unwrap_or(TaintKind::Unknown),
callee.file.clone(),
));
}
}
}
}
None
}
/// Propagate taint through event emit/listen patterns
///
/// When `emit('event', tainted_data)` is called:
/// - Find all `on('event', handler)` registrations
/// - Mark handler parameters as tainted from the event source
///
/// This enables cross-file taint tracking for event-driven architectures.
fn propagate_event_taint(
&self,
call_graph: &CallGraph,
file_path: &Path,
result: &mut InterproceduralResult,
) {
use crate::flow::events::{EventPatterns, extract_emit_args, extract_event_name};
let language = self.semantics.language_enum();
let content = String::from_utf8_lossy(self.source);
// Detect event patterns in this file
let patterns = EventPatterns::for_language(language);
for (line_num, line) in content.lines().enumerate() {
let line_num = line_num + 1;
// Check for emit patterns with tainted data
if patterns.is_emit(line)
&& let Some(event_name) = extract_event_name(line, language)
{
let emit_args = extract_emit_args(line, language);
// Check if any emit arg is tainted
for arg in &emit_args {
// Check if this variable is in the tainted set
let is_tainted = result
.function_taint
.values()
.any(|vars| vars.contains(arg));
if is_tainted {
// Mark all listeners of this event as receiving tainted data
for listener in call_graph.listeners_of(&event_name) {
// Create a cross-file flow from emit to listen
if listener.file != file_path {
let source = TaintEndpoint {
name: format!("emit('{}')", event_name),
line: line_num,
node_id: 0,
function: None,
kind: TaintKind::UserInput,
file: Some(file_path.to_path_buf()),
};
let sink = TaintEndpoint {
name: format!("on('{}')", event_name),
line: listener.line,
node_id: 0,
function: listener.function.clone(),
kind: TaintKind::UserInput,
file: Some(listener.file.clone()),
};
let flow = TaintFlow::cross_file(
source,
sink,
vec![format!("event:{}", event_name)],
vec![file_path.to_path_buf(), listener.file.clone()],
);
result.cross_file_flows.push(flow);
}
// Mark handler parameters as tainted
for handler_arg in &listener.arguments {
result
.function_taint
.entry(String::new())
.or_default()
.insert(handler_arg.clone());
}
}
}
}
}
}
}
/// Build summaries for known library functions
fn build_known_summaries(&self, result: &mut InterproceduralResult) {
// Sources
for source in &self.config.sources {
let func_name = source.pattern.as_function_name();
if let Some(name) = func_name {
let kind = TaintKind::from_source_name(&name);
let summary = FunctionSummary::new(&name).as_source(kind);
result.summaries.insert(name, summary);
}
}
// Sinks
for sink in &self.config.sinks {
let func_name = sink.pattern.as_function_name();
if let Some(name) = func_name {
let mut summary = result
.summaries
.remove(&name)
.unwrap_or_else(|| FunctionSummary::new(&name));
// First parameter is typically the sensitive one
summary.sink_params.push(0);
result.summaries.insert(name, summary);
}
}
// Sanitizers
for sanitizer in &self.config.sanitizers {
let mut summary = result
.summaries
.remove(sanitizer)
.unwrap_or_else(|| FunctionSummary::new(sanitizer));
summary.is_sanitizer = true;
result.summaries.insert(sanitizer.clone(), summary);
}
// Common patterns: functions that pass taint through
let passthrough_funcs = [
"toString",
"String",
"trim",
"toLowerCase",
"toUpperCase",
"slice",
"substring",
"substr",
"concat",
"split",
"join",
"replace", // replace without proper escaping doesn't sanitize
"format",
"sprintf",
];
for func in passthrough_funcs {
if !result.summaries.contains_key(func) {
let summary = FunctionSummary::new(func).param_to_return(0);
result.summaries.insert(func.to_string(), summary);
}
}
}
/// Extract function definitions and build summaries
fn extract_function_summaries(&self, result: &mut InterproceduralResult) {
let root = self.tree.root_node();
self.walk_for_functions(root, result);
}
fn walk_for_functions(&self, node: tree_sitter::Node, result: &mut InterproceduralResult) {
if self.semantics.is_function_def(node.kind())
&& let Some(summary) = self.build_function_summary(node)
{
// Don't overwrite known summaries
if !result.summaries.contains_key(&summary.name) {
result.summaries.insert(summary.name.clone(), summary);
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.walk_for_functions(child, result);
}
}
fn build_function_summary(&self, node: tree_sitter::Node) -> Option<FunctionSummary> {
// Get function name
let name_node = node.child_by_field_name(self.semantics.name_field)?;
let name = name_node.utf8_text(self.source).ok()?;
let mut summary = FunctionSummary::new(name);
summary.line = node.start_position().row + 1;
summary.node_id = node.id();
// Analyze function body for taint flow patterns
if let Some(body) = node.child_by_field_name("body") {
self.analyze_function_body(body, &mut summary);
}
Some(summary)
}
fn analyze_function_body(&self, body: tree_sitter::Node, summary: &mut FunctionSummary) {
// Simple heuristic: if return statement references a parameter,
// that parameter taints the return value
self.walk_for_returns(body, summary);
}
fn walk_for_returns(&self, node: tree_sitter::Node, summary: &mut FunctionSummary) {
if (node.kind() == "return_statement" || node.kind() == "return")
&& let Some(value) = node
.child_by_field_name("value")
.or_else(|| node.named_child(0))
{
// Check if return value references any parameters
let refs = self.collect_identifiers(value);
for _ref_name in refs {
// Heuristic: assume first param if any identifier is returned
// More precise analysis would track param names
summary
.param_effects
.entry(0)
.or_default()
.push(ParamEffect::TaintsReturn);
}
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
// Don't recurse into nested function definitions
if !self.semantics.is_function_def(child.kind()) {
self.walk_for_returns(child, summary);
}
}
}
fn collect_identifiers(&self, node: tree_sitter::Node) -> Vec<String> {
let mut ids = Vec::new();
if (self.semantics.is_identifier(node.kind()) || node.kind() == "identifier")
&& let Ok(name) = node.utf8_text(self.source)
{
ids.push(name.to_string());
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
ids.extend(self.collect_identifiers(child));
}
ids
}
/// Extract call sites from the AST
fn extract_call_sites(&self, symbols: &SymbolTable, result: &mut InterproceduralResult) {
let root = self.tree.root_node();
self.walk_for_calls(root, symbols, result);
}
fn walk_for_calls(
&self,
node: tree_sitter::Node,
symbols: &SymbolTable,
result: &mut InterproceduralResult,
) {
if self.semantics.is_call(node.kind())
&& let Some(call_site) = self.extract_call_site(node, symbols, result)
{
result.call_sites.push(call_site);
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
self.walk_for_calls(child, symbols, result);
}
}
fn extract_call_site(
&self,
node: tree_sitter::Node,
_symbols: &SymbolTable,
result: &InterproceduralResult,
) -> Option<CallSite> {
// Get callee name
let func_node = node
.child_by_field_name("function")
.or_else(|| node.child(0))?;
let callee_name = func_node.utf8_text(self.source).ok()?.to_string();
// Get arguments
let args_node = node.child_by_field_name("arguments")?;
let mut arguments = Vec::new();
let mut cursor = args_node.walk();
for (idx, arg) in args_node.named_children(&mut cursor).enumerate() {
let expr = arg.utf8_text(self.source).unwrap_or("").to_string();
// Check if it's a simple variable reference
let var_name = if self.semantics.is_identifier(arg.kind()) || arg.kind() == "identifier"
{
Some(expr.clone())
} else {
None
};
// Check if argument is tainted
let is_tainted = var_name
.as_ref()
.map(|name| {
result
.function_taint
.values()
.any(|vars| vars.contains(name))
})
.unwrap_or(false);
arguments.push(CallArg {
index: idx,
expr,
var_name,
is_tainted,
taint_kind: if is_tainted {
Some(TaintKind::Unknown)
} else {
None
},
});
}
Some(CallSite {
node_id: node.id(),
callee_name,
arguments,
line: node.start_position().row + 1,
block_id: None,
result_var: None,
})
}
/// Propagate taint through the call graph
#[allow(dead_code)]
fn propagate_taint(&self, symbols: &SymbolTable, result: &mut InterproceduralResult) {
// Initialize with locally tainted variables
for (name, info) in symbols.iter() {
if self.is_initially_tainted(&info.initializer) {
// Use empty string for file-level scope
result
.function_taint
.entry(String::new())
.or_default()
.insert(name.clone());
}
}
// Fixed-point iteration
let mut changed = true;
let mut iterations = 0;
const MAX_ITERATIONS: usize = 100;
while changed && iterations < MAX_ITERATIONS {
changed = false;
iterations += 1;
// For each call site, apply callee summary
for call_site in &result.call_sites {
if let Some(summary) = result.summaries.get(&call_site.callee_name) {
// Check if any tainted argument flows through
let mut result_tainted = summary.is_source;
for arg in &call_site.arguments {
if arg.is_tainted && summary.param_taints_return(arg.index) {
result_tainted = true;
break;
}
}
// If result is tainted and assigned to a variable, mark it
if result_tainted && let Some(ref result_var) = call_site.result_var {
let func_taint = result.function_taint.entry(String::new()).or_default();
if !func_taint.contains(result_var) {
func_taint.insert(result_var.clone());
changed = true;
}
}
}
}
}
result.iterations = iterations;
}
/// Detect source-to-sink flows
#[allow(dead_code)]
fn detect_flows(&self, symbols: &SymbolTable, _cfg: &CFG, result: &mut InterproceduralResult) {
// Find all sinks and check if their arguments are tainted
for call_site in &result.call_sites {
if let Some(summary) = result.summaries.get(&call_site.callee_name)
&& !summary.sink_params.is_empty()
{
// This is a sink
for &sink_param in &summary.sink_params {
if let Some(arg) = call_site.arguments.get(sink_param) {
// Check if this argument is tainted
let is_tainted = arg.is_tainted
|| arg.var_name.as_ref().is_some_and(|name| {
result
.function_taint
.values()
.any(|vars| vars.contains(name))
});
if is_tainted {
// Find the source of taint
if let Some(source) = self.find_taint_source(
arg.var_name.as_deref().unwrap_or(&arg.expr),
symbols,
result,
) {
let sink = TaintEndpoint {
name: call_site.callee_name.clone(),
line: call_site.line,
node_id: call_site.node_id,
function: None,
kind: TaintKind::from_source_name(&call_site.callee_name),
file: self.file_path.clone(),
};
let flow = TaintFlow::intraprocedural(source, sink);
result.flows.push(flow);
}
}
}
}
}
}
}
fn find_taint_source(
&self,
var_name: &str,
symbols: &SymbolTable,
result: &InterproceduralResult,
) -> Option<TaintEndpoint> {
// Check if it's from a known source function
if let Some(info) = symbols.get(var_name) {
if let ValueOrigin::FunctionCall(func_name) = &info.initializer
&& let Some(summary) = result.summaries.get(func_name)
&& summary.is_source
{
return Some(TaintEndpoint {
name: var_name.to_string(),
line: info.line,
node_id: info.declaration_node_id,
function: None,
kind: summary.source_kind.unwrap_or(TaintKind::Unknown),
file: self.file_path.clone(),
});
}
// Check member access sources
if let ValueOrigin::MemberAccess(path) = &info.initializer
&& self.config.is_source_member(path)
{
return Some(TaintEndpoint {
name: var_name.to_string(),
line: info.line,
node_id: info.declaration_node_id,
function: None,
kind: TaintKind::from_source_name(path),
file: self.file_path.clone(),
});
}
// Check parameter sources
if matches!(info.initializer, ValueOrigin::Parameter(_)) {
return Some(TaintEndpoint {
name: var_name.to_string(),
line: info.line,
node_id: info.declaration_node_id,
function: None,
kind: TaintKind::UserInput,
file: self.file_path.clone(),
});
}
}
None
}
fn is_initially_tainted(&self, origin: &ValueOrigin) -> bool {
match origin {
ValueOrigin::Parameter(_) => true, // Conservative: all params are tainted
ValueOrigin::FunctionCall(func) => self.config.is_source_function(func),
ValueOrigin::MemberAccess(path) => self.config.is_source_member(path),
// String concatenation: check if any operand is a source
ValueOrigin::StringConcat(variables) => variables
.iter()
.any(|var| self.config.is_source_member(var)),
// Template literals: check interpolations
ValueOrigin::TemplateLiteral(variables) => variables
.iter()
.any(|var| self.config.is_source_member(var)),
// Method calls: check receiver and arguments
ValueOrigin::MethodCall {
method,
receiver,
arguments,
} => {
if self.config.is_source_function(method) {
return true;
}
if let Some(recv) = receiver
&& self.config.is_source_member(recv)
{
return true;
}
arguments
.iter()
.any(|arg| self.config.is_source_member(arg))
}
_ => false,
}
}
}
/// Run inter-procedural taint analysis
pub fn analyze_interprocedural(
symbols: &SymbolTable,
cfg: &CFG,
config: &TaintConfig,
tree: &tree_sitter::Tree,
source: &[u8],
semantics: &'static LanguageSemantics,
) -> InterproceduralResult {
let analyzer = InterproceduralAnalyzer::new(semantics, config, source, tree);
analyzer.analyze(symbols, cfg)
}
/// Run inter-procedural taint analysis with call graph for cross-file tracking
#[allow(clippy::too_many_arguments)]
pub fn analyze_interprocedural_with_call_graph(
symbols: &SymbolTable,
cfg: &CFG,
config: &TaintConfig,
tree: &tree_sitter::Tree,
source: &[u8],
semantics: &'static LanguageSemantics,
call_graph: &CallGraph,
file_path: &Path,
) -> InterproceduralResult {
let analyzer = InterproceduralAnalyzer::with_call_graph(
semantics,
config,
source,
tree,
call_graph,
file_path.to_path_buf(),
);
analyzer.analyze(symbols, cfg)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::flow::sources::TaintConfig;
use crate::flow::symbol_table::SymbolTable;
use rma_common::Language;
use rma_parser::ParserEngine;
use std::path::Path;
fn parse_js(code: &str) -> rma_parser::ParsedFile {
let config = rma_common::RmaConfig::default();
let parser = ParserEngine::new(config);
parser
.parse_file(Path::new("test.js"), code)
.expect("parse failed")
}
#[test]
fn test_function_summary_creation() {
let summary = FunctionSummary::new("encodeURIComponent")
.as_sanitizer()
.param_to_return(0);
assert!(summary.is_sanitizer);
assert!(summary.param_taints_return(0));
assert!(!summary.param_taints_return(1));
}
#[test]
fn test_taint_kind_inference() {
assert_eq!(
TaintKind::from_source_name("req.query"),
TaintKind::UserInput
);
assert_eq!(
TaintKind::from_source_name("file_path"),
TaintKind::FilePath
);
assert_eq!(
TaintKind::from_source_name("sql_query"),
TaintKind::SqlQuery
);
assert_eq!(TaintKind::from_source_name("exec_cmd"), TaintKind::Command);
}
#[test]
fn test_basic_interprocedural() {
let code = r#"
function getInput() {
return req.query.name;
}
function processInput(data) {
return data.trim();
}
const input = getInput();
const processed = processInput(input);
console.log(processed);
"#;
let parsed = parse_js(code);
let symbols = SymbolTable::build(&parsed, Language::JavaScript);
let cfg = CFG::build(&parsed, Language::JavaScript);
let config = TaintConfig::for_language(Language::JavaScript);
let semantics = LanguageSemantics::for_language(Language::JavaScript);
let result = analyze_interprocedural(
&symbols,
&cfg,
&config,
&parsed.tree,
code.as_bytes(),
semantics,
);
// Should have detected some function summaries
assert!(!result.summaries.is_empty());
// Should have detected call sites
assert!(!result.call_sites.is_empty());
}
#[test]
fn test_known_summaries() {
let code = "const x = 1;";
let parsed = parse_js(code);
let symbols = SymbolTable::build(&parsed, Language::JavaScript);
let cfg = CFG::build(&parsed, Language::JavaScript);
let config = TaintConfig::for_language(Language::JavaScript);
let semantics = LanguageSemantics::for_language(Language::JavaScript);
let result = analyze_interprocedural(
&symbols,
&cfg,
&config,
&parsed.tree,
code.as_bytes(),
semantics,
);
// Should have passthrough function summaries
assert!(result.summaries.contains_key("toString"));
assert!(result.summaries.contains_key("trim"));
// toString should pass taint through
let to_string = result.get_summary("toString").unwrap();
assert!(to_string.param_taints_return(0));
}
#[test]
fn test_taint_flow_detection() {
let code = r#"
function handler(userInput) {
const data = userInput;
sendToServer(data);
}
"#;
let parsed = parse_js(code);
let symbols = SymbolTable::build(&parsed, Language::JavaScript);
let cfg = CFG::build(&parsed, Language::JavaScript);
let config = TaintConfig::for_language(Language::JavaScript);
let semantics = LanguageSemantics::for_language(Language::JavaScript);
let result = analyze_interprocedural(
&symbols,
&cfg,
&config,
&parsed.tree,
code.as_bytes(),
semantics,
);
// Should complete analysis
assert!(result.iterations > 0);
}
#[test]
fn test_call_site_extraction() {
let code = r#"
fetch("/api");
console.log("hello");
process(data);
"#;
let parsed = parse_js(code);
let symbols = SymbolTable::build(&parsed, Language::JavaScript);
let cfg = CFG::build(&parsed, Language::JavaScript);
let config = TaintConfig::for_language(Language::JavaScript);
let semantics = LanguageSemantics::for_language(Language::JavaScript);
let result = analyze_interprocedural(
&symbols,
&cfg,
&config,
&parsed.tree,
code.as_bytes(),
semantics,
);
// Should have extracted call sites
let call_names: Vec<_> = result.call_sites.iter().map(|c| &c.callee_name).collect();
assert!(call_names.iter().any(|n| n.contains("fetch")));
assert!(call_names.iter().any(|n| n.contains("console")));
}
#[test]
fn test_interprocedural_result_queries() {
let mut result = InterproceduralResult::default();
// Add a source summary
let source_summary = FunctionSummary::new("getInput").as_source(TaintKind::UserInput);
result
.summaries
.insert("getInput".to_string(), source_summary);
// Add a sanitizer summary
let sanitizer_summary = FunctionSummary::new("escape").as_sanitizer();
result
.summaries
.insert("escape".to_string(), sanitizer_summary);
assert!(result.is_source("getInput"));
assert!(!result.is_source("escape"));
assert!(result.is_sanitizer("escape"));
assert!(!result.is_sanitizer("getInput"));
}
#[test]
fn test_taint_summary_from_function_summary() {
let func_summary = FunctionSummary::new("getInput")
.as_source(TaintKind::UserInput)
.with_file(PathBuf::from("/project/src/utils.js"))
.as_exported();
let taint_summary = TaintSummary::from_function_summary(func_summary);
assert!(taint_summary.is_source());
assert!(!taint_summary.is_sanitizer());
assert_eq!(taint_summary.name(), "getInput");
assert!(
taint_summary
.introduced_taint_kinds
.contains(&TaintKind::UserInput)
);
}
#[test]
fn test_taint_summary_propagation() {
let func_summary = FunctionSummary::new("processData")
.param_to_return(0)
.param_to_return(1);
let taint_summary = TaintSummary::from_function_summary(func_summary);
assert!(taint_summary.propagates_taint);
assert!(taint_summary.param_taints_return(0));
assert!(taint_summary.param_taints_return(1));
assert!(!taint_summary.param_taints_return(2));
}
#[test]
fn test_cross_file_taint_flow_creation() {
let source = TaintEndpoint {
name: "userInput".to_string(),
line: 10,
node_id: 100,
function: Some("getInput".to_string()),
kind: TaintKind::UserInput,
file: Some(PathBuf::from("/project/src/input.js")),
};
let sink = TaintEndpoint {
name: "query".to_string(),
line: 20,
node_id: 200,
function: Some("runQuery".to_string()),
kind: TaintKind::SqlQuery,
file: Some(PathBuf::from("/project/src/database.js")),
};
let flow = TaintFlow::cross_file(
source,
sink,
vec!["getInput".to_string(), "runQuery".to_string()],
vec![
PathBuf::from("/project/src/input.js"),
PathBuf::from("/project/src/database.js"),
],
);
assert!(flow.is_cross_file);
assert!(flow.is_interprocedural);
assert_eq!(flow.files_involved.len(), 2);
assert_eq!(flow.functions_involved.len(), 2);
}
#[test]
fn test_interprocedural_result_merge() {
let mut result1 = InterproceduralResult::default();
let mut result2 = InterproceduralResult::default();
// Add summary to result1
let summary1 = FunctionSummary::new("func1").as_source(TaintKind::UserInput);
result1.summaries.insert("func1".to_string(), summary1);
// Add summary to result2
let summary2 = FunctionSummary::new("func2").as_sanitizer();
result2.summaries.insert("func2".to_string(), summary2);
// Merge result2 into result1
result1.merge(result2);
// Both summaries should be present
assert!(result1.summaries.contains_key("func1"));
assert!(result1.summaries.contains_key("func2"));
}
#[test]
fn test_cross_file_taint_tracking_integration() {
use crate::callgraph::CallGraphBuilder;
use crate::imports::FileImports;
use crate::imports::ImportKind;
use crate::imports::ResolvedImport;
// Simulate file1.js: exports getInput() that returns req.query
let file1_code = r#"
export function getInput() {
return req.query.name;
}
"#;
// Simulate file2.js: imports getInput and passes result to db.query()
let file2_code = r#"
import { getInput } from './utils';
function handleRequest() {
const input = getInput();
db.query(input);
}
"#;
// Parse both files
let file1_path = Path::new("/project/src/utils.js");
let file2_path = Path::new("/project/src/handler.js");
let parsed1 = ParserEngine::new(rma_common::RmaConfig::default())
.parse_file(file1_path, file1_code)
.expect("parse file1 failed");
let parsed2 = ParserEngine::new(rma_common::RmaConfig::default())
.parse_file(file2_path, file2_code)
.expect("parse file2 failed");
// Build call graph
let mut builder = CallGraphBuilder::new();
// Add file1 (utils.js) - exports getInput
builder.add_file(
file1_path,
Language::JavaScript,
vec![("getInput".to_string(), 2, true)], // exported function
vec![], // no calls
FileImports::default(),
);
// Add file2 (handler.js) - imports and calls getInput
let mut file2_imports = FileImports::default();
file2_imports.imports.push(ResolvedImport {
local_name: "getInput".to_string(),
source_file: file1_path.to_path_buf(),
exported_name: "getInput".to_string(),
kind: ImportKind::Named,
specifier: "./utils".to_string(),
line: 2,
});
builder.add_file(
file2_path,
Language::JavaScript,
vec![("handleRequest".to_string(), 4, false)],
vec![
("getInput".to_string(), 5, Some("handleRequest".to_string())),
("query".to_string(), 6, Some("handleRequest".to_string())),
],
file2_imports,
);
let call_graph = builder.build();
// Verify call graph has cross-file edge
assert!(call_graph.edge_count() >= 1);
let cross_file_edges = call_graph.cross_file_edges();
assert!(
!cross_file_edges.is_empty(),
"Should have cross-file call edge"
);
// Analyze file1 and create taint summary
let symbols1 = SymbolTable::build(&parsed1, Language::JavaScript);
let cfg1 = CFG::build(&parsed1, Language::JavaScript);
let config = TaintConfig::for_language(Language::JavaScript);
let semantics = LanguageSemantics::for_language(Language::JavaScript);
let result1 = analyze_interprocedural_with_call_graph(
&symbols1,
&cfg1,
&config,
&parsed1.tree,
file1_code.as_bytes(),
semantics,
&call_graph,
file1_path,
);
// getInput should be detected as returning tainted data
// (it accesses req.query which is a known source)
assert!(result1.summaries.contains_key("getInput"));
// Analyze file2 with the call graph
let symbols2 = SymbolTable::build(&parsed2, Language::JavaScript);
let cfg2 = CFG::build(&parsed2, Language::JavaScript);
let result2 = analyze_interprocedural_with_call_graph(
&symbols2,
&cfg2,
&config,
&parsed2.tree,
file2_code.as_bytes(),
semantics,
&call_graph,
file2_path,
);
// Verify call sites were extracted
assert!(!result2.call_sites.is_empty());
let callee_names: Vec<_> = result2.call_sites.iter().map(|c| &c.callee_name).collect();
assert!(
callee_names.iter().any(|n| *n == "getInput"),
"Should detect getInput call"
);
}
#[test]
fn test_taint_endpoint_with_file() {
let endpoint = TaintEndpoint {
name: "data".to_string(),
line: 10,
node_id: 100,
function: Some("handler".to_string()),
kind: TaintKind::UserInput,
file: Some(PathBuf::from("/project/src/main.js")),
};
assert_eq!(endpoint.name, "data");
assert_eq!(endpoint.file, Some(PathBuf::from("/project/src/main.js")));
}
#[test]
fn test_cross_file_flow_queries() {
let mut result = InterproceduralResult::default();
// Add a cross-file flow
let source = TaintEndpoint {
name: "input".to_string(),
line: 1,
node_id: 1,
function: Some("getInput".to_string()),
kind: TaintKind::UserInput,
file: Some(PathBuf::from("/file1.js")),
};
let sink = TaintEndpoint {
name: "query".to_string(),
line: 10,
node_id: 10,
function: Some("runQuery".to_string()),
kind: TaintKind::SqlQuery,
file: Some(PathBuf::from("/file2.js")),
};
let flow = TaintFlow::cross_file(
source,
sink,
vec!["getInput".to_string(), "runQuery".to_string()],
vec![PathBuf::from("/file1.js"), PathBuf::from("/file2.js")],
);
result.cross_file_flows.push(flow);
// Query cross-file flows
let cross_file = result.cross_file_flows();
assert_eq!(cross_file.len(), 1);
assert!(cross_file[0].is_cross_file);
// Total flow count should include cross-file flows
assert_eq!(result.flow_count(), 1);
}
// ==================== Context-Sensitivity Tests ====================
#[test]
fn test_call_context_creation() {
// Empty context (all safe)
let ctx = CallContext::new();
assert!(!ctx.has_tainted_params());
assert_eq!(ctx.tainted_count(), 0);
assert!(!ctx.is_param_tainted(0));
// Context with tainted param 0
let ctx = CallContext::from_tainted_params([0]);
assert!(ctx.has_tainted_params());
assert_eq!(ctx.tainted_count(), 1);
assert!(ctx.is_param_tainted(0));
assert!(!ctx.is_param_tainted(1));
// Context with taint kinds
let ctx =
CallContext::with_taint_kinds([(0, TaintKind::UserInput), (2, TaintKind::SqlQuery)]);
assert!(ctx.is_param_tainted(0));
assert!(!ctx.is_param_tainted(1));
assert!(ctx.is_param_tainted(2));
assert_eq!(ctx.get_taint_kind(0), Some(TaintKind::UserInput));
assert_eq!(ctx.get_taint_kind(2), Some(TaintKind::SqlQuery));
assert_eq!(ctx.get_taint_kind(1), None);
}
#[test]
fn test_call_context_key_generation() {
let ctx1 = CallContext::from_tainted_params([0, 2]);
let ctx2 = CallContext::from_tainted_params([2, 0]); // Same params, different order
// Keys should be the same regardless of insertion order
assert_eq!(ctx1.to_key(), ctx2.to_key());
assert_eq!(ctx1.to_key(), "ctx[0,2]");
}
#[test]
fn test_call_context_subset_superset() {
let ctx_empty = CallContext::new();
let ctx_0 = CallContext::from_tainted_params([0]);
let ctx_01 = CallContext::from_tainted_params([0, 1]);
let ctx_012 = CallContext::from_tainted_params([0, 1, 2]);
// Empty is subset of everything
assert!(ctx_empty.is_subset_of(&ctx_0));
assert!(ctx_empty.is_subset_of(&ctx_01));
// Proper subset relationship
assert!(ctx_0.is_subset_of(&ctx_01));
assert!(ctx_01.is_subset_of(&ctx_012));
assert!(!ctx_01.is_subset_of(&ctx_0));
// Superset relationships
assert!(ctx_01.is_superset_of(&ctx_0));
assert!(ctx_012.is_superset_of(&ctx_01));
}
#[test]
fn test_context_specific_result() {
// Safe result
let result = ContextSpecificResult::safe_return();
assert!(!result.return_tainted);
assert!(result.return_taint_kind.is_none());
// Tainted result
let result = ContextSpecificResult::tainted_return(TaintKind::UserInput)
.with_contributing_param(0)
.with_contributing_param(2);
assert!(result.return_tainted);
assert_eq!(result.return_taint_kind, Some(TaintKind::UserInput));
assert!(result.contributing_params.contains(&0));
assert!(result.contributing_params.contains(&2));
assert!(!result.contributing_params.contains(&1));
// Result with side effects
let result = ContextSpecificResult::tainted_return(TaintKind::Command)
.with_side_effect("receiver".to_string(), TaintKind::Command);
assert_eq!(
result.side_effect_taints.get("receiver"),
Some(&TaintKind::Command)
);
}
#[test]
fn test_context_sensitive_summary_basic() {
// Create a function that passes param 0 through but sanitizes param 1
let mut base = FunctionSummary::new("process");
base.param_effects
.entry(0)
.or_default()
.push(ParamEffect::TaintsReturn);
base.param_effects
.entry(1)
.or_default()
.push(ParamEffect::Sanitized);
let mut cs_summary = ContextSensitiveSummary::new(base);
cs_summary.mark_always_sanitizes(1);
// Query with param 0 tainted -> return tainted
let ctx0 = CallContext::from_tainted_params([0]);
let result0 = cs_summary.query(&ctx0);
assert!(result0.return_tainted, "param 0 should taint return");
assert!(result0.contributing_params.contains(&0));
// Query with param 1 tainted -> return safe (sanitized)
let ctx1 = CallContext::from_tainted_params([1]);
let result1 = cs_summary.query(&ctx1);
assert!(!result1.return_tainted, "param 1 should be sanitized");
// Query with both tainted -> return tainted (param 0 wins)
let ctx01 = CallContext::from_tainted_params([0, 1]);
let result01 = cs_summary.query(&ctx01);
assert!(
result01.return_tainted,
"param 0 should taint despite param 1 sanitizing"
);
}
#[test]
fn test_context_sensitive_summary_with_explicit_contexts() {
let base = FunctionSummary::new("transform");
let mut cs_summary = ContextSensitiveSummary::new(base);
// Add explicit context-specific summaries
let ctx0 = CallContext::from_tainted_params([0]);
cs_summary.add_context_summary(
ctx0.clone(),
ContextSpecificResult::tainted_return(TaintKind::UserInput).with_contributing_param(0),
);
let ctx1 = CallContext::from_tainted_params([1]);
cs_summary.add_context_summary(ctx1.clone(), ContextSpecificResult::safe_return());
// Query explicit contexts
let result0 = cs_summary.query(&ctx0);
assert!(result0.return_tainted);
let result1 = cs_summary.query(&ctx1);
assert!(!result1.return_tainted);
}
#[test]
fn test_context_sensitive_source_function() {
// Source functions always taint return regardless of context
let base = FunctionSummary::new("getInput").as_source(TaintKind::UserInput);
let cs_summary = ContextSensitiveSummary::new(base);
// Even with no tainted params, a source returns tainted
let ctx_empty = CallContext::new();
let result = cs_summary.query(&ctx_empty);
assert!(result.return_tainted);
assert_eq!(result.return_taint_kind, Some(TaintKind::UserInput));
}
#[test]
fn test_context_sensitive_sanitizer_function() {
// Sanitizer functions always return safe
let base = FunctionSummary::new("escape")
.as_sanitizer()
.param_to_return(0);
let cs_summary = ContextSensitiveSummary::new(base);
// Even with tainted input, sanitizer returns safe
let ctx = CallContext::from_tainted_params([0]);
let result = cs_summary.query(&ctx);
assert!(!result.return_tainted);
}
#[test]
fn test_interprocedural_result_query_with_context() {
let mut result = InterproceduralResult::default();
// Add a function that taints return from param 0 only
let summary = FunctionSummary::new("process").param_to_return(0);
result
.summaries
.insert("process".to_string(), summary.clone());
let mut cs_summary = ContextSensitiveSummary::new(summary);
cs_summary.mark_always_taints_return(0);
result
.context_sensitive_summaries
.insert("process".to_string(), cs_summary);
// Query with param 0 tainted
let ctx0 = CallContext::from_tainted_params([0]);
let query0 = result.query_with_context("process", &ctx0);
assert!(
query0.return_tainted,
"process(tainted, _) should return tainted"
);
// Query with param 1 tainted (not param 0)
let ctx1 = CallContext::from_tainted_params([1]);
let query1 = result.query_with_context("process", &ctx1);
assert!(
!query1.return_tainted,
"process(_, tainted) should return safe"
);
}
#[test]
fn test_different_contexts_produce_different_results() {
// This is the key test: func(tainted, safe) != func(safe, tainted)
let mut result = InterproceduralResult::default();
// Create a function where:
// - param 0 tainted -> return tainted
// - param 1 tainted -> return safe (it sanitizes)
let mut summary = FunctionSummary::new("processInput");
summary
.param_effects
.entry(0)
.or_default()
.push(ParamEffect::TaintsReturn);
result
.summaries
.insert("processInput".to_string(), summary.clone());
let mut cs_summary = ContextSensitiveSummary::with_param_count(summary, 2);
cs_summary.mark_always_taints_return(0);
cs_summary.mark_always_sanitizes(1);
result
.context_sensitive_summaries
.insert("processInput".to_string(), cs_summary);
// func(tainted, safe) -> tainted
let ctx_tainted_safe = CallContext::from_tainted_params([0]);
let result_ts = result.query_with_context("processInput", &ctx_tainted_safe);
assert!(
result_ts.return_tainted,
"func(tainted, safe) should return tainted"
);
// func(safe, tainted) -> safe (param 1 sanitizes)
let ctx_safe_tainted = CallContext::from_tainted_params([1]);
let result_st = result.query_with_context("processInput", &ctx_safe_tainted);
assert!(
!result_st.return_tainted,
"func(safe, tainted) should return safe"
);
// These two contexts produce DIFFERENT results!
assert_ne!(
result_ts.return_tainted, result_st.return_tainted,
"Different contexts should produce different results"
);
}
#[test]
fn test_context_sensitive_summary_merge() {
let base = FunctionSummary::new("func");
let mut summary1 = ContextSensitiveSummary::new(base.clone());
let mut summary2 = ContextSensitiveSummary::new(base);
// Add different contexts to each
let ctx0 = CallContext::from_tainted_params([0]);
summary1.add_context_summary(
ctx0.clone(),
ContextSpecificResult::tainted_return(TaintKind::UserInput),
);
let ctx1 = CallContext::from_tainted_params([1]);
summary2.add_context_summary(ctx1.clone(), ContextSpecificResult::safe_return());
// Merge
summary1.merge(&summary2);
// Both contexts should be present
assert!(summary1.context_summaries.contains_key(&ctx0));
assert!(summary1.context_summaries.contains_key(&ctx1));
}
#[test]
fn test_ensure_context_sensitive_summary() {
let mut result = InterproceduralResult::default();
// Add base summary
let summary = FunctionSummary::new("myFunc").param_to_return(0);
result.summaries.insert("myFunc".to_string(), summary);
// Ensure creates it if it doesn't exist
{
let cs = result.ensure_context_sensitive_summary("myFunc");
cs.mark_always_taints_return(0);
}
// Should now exist
assert!(result.context_sensitive_summaries.contains_key("myFunc"));
// Should preserve modifications
let cs = result.get_context_sensitive_summary("myFunc").unwrap();
assert!(cs.always_taints_return.contains(&0));
}
#[test]
fn test_unknown_function_context_query() {
let result = InterproceduralResult::default();
// Query an unknown function - should be conservative
let ctx = CallContext::from_tainted_params([0]);
let query = result.query_with_context("unknownFunc", &ctx);
// Conservative: tainted input -> tainted output for unknown functions
assert!(query.return_tainted);
assert_eq!(query.return_taint_kind, Some(TaintKind::Unknown));
}
#[test]
fn test_context_with_taint_kind_propagation() {
let mut result = InterproceduralResult::default();
// Function that passes through the taint kind
let summary = FunctionSummary::new("passthrough").param_to_return(0);
result
.summaries
.insert("passthrough".to_string(), summary.clone());
result.context_sensitive_summaries.insert(
"passthrough".to_string(),
ContextSensitiveSummary::new(summary),
);
// Query with SQL taint
let ctx = CallContext::with_taint_kinds([(0, TaintKind::SqlQuery)]);
let query = result.query_with_context("passthrough", &ctx);
assert!(query.return_tainted);
assert_eq!(query.return_taint_kind, Some(TaintKind::SqlQuery));
}
#[test]
fn test_build_context_sensitive_summaries_creates_common_contexts() {
let code = r#"
function process(a, b) {
return a.trim();
}
process(userInput, safe);
"#;
let parsed = parse_js(code);
let symbols = SymbolTable::build(&parsed, Language::JavaScript);
let cfg = CFG::build(&parsed, Language::JavaScript);
let config = TaintConfig::for_language(Language::JavaScript);
let semantics = LanguageSemantics::for_language(Language::JavaScript);
let result = analyze_interprocedural(
&symbols,
&cfg,
&config,
&parsed.tree,
code.as_bytes(),
semantics,
);
// Should have built context-sensitive summaries
// Note: "process" might be in summaries depending on analysis
// The key point is that context-sensitive infrastructure is in place
assert!(!result.context_sensitive_summaries.is_empty() || !result.summaries.is_empty());
}
}