|
| 1 | +//! Cross-crate authority propagation. |
| 2 | +//! |
| 3 | +//! Converts export maps from dependency crates into [`CustomAuthority`] values |
| 4 | +//! that can be injected into the detector. This bridges dependency analysis |
| 5 | +//! (Phase 1) with workspace crate analysis (Phase 2). |
| 6 | +
|
| 7 | +use crate::authorities::CustomAuthority; |
| 8 | +use crate::export_map::CrateExportMap; |
| 9 | + |
| 10 | +/// Converts a collection of export maps into [`CustomAuthority`] values for |
| 11 | +/// injection into the detector. |
| 12 | +/// |
| 13 | +/// For each entry in each export map, creates a `CustomAuthority` with the |
| 14 | +/// module-qualified path split into segments. The suffix matching in |
| 15 | +/// [`Detector::matches_custom_path`](crate::detector) handles both |
| 16 | +/// fully-qualified calls and imported calls. |
| 17 | +#[must_use] |
| 18 | +pub fn export_map_to_custom_authorities(export_maps: &[CrateExportMap]) -> Vec<CustomAuthority> { |
| 19 | + let mut customs = Vec::new(); |
| 20 | + |
| 21 | + for map in export_maps { |
| 22 | + for (key, authorities) in &map.exports { |
| 23 | + let path: Vec<String> = key.split("::").map(String::from).collect(); |
| 24 | + |
| 25 | + for auth in authorities { |
| 26 | + customs.push(CustomAuthority { |
| 27 | + path: path.clone(), |
| 28 | + category: auth.category.clone(), |
| 29 | + risk: auth.risk, |
| 30 | + description: format!( |
| 31 | + "Cross-crate: {}() → {} [{}]", |
| 32 | + key, |
| 33 | + auth.leaf_call, |
| 34 | + auth.category.label(), |
| 35 | + ), |
| 36 | + }); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + customs |
| 42 | +} |
| 43 | + |
| 44 | +#[cfg(test)] |
| 45 | +mod tests { |
| 46 | + use super::*; |
| 47 | + use crate::authorities::{Category, Risk}; |
| 48 | + use crate::export_map::{CrateExportMap, ExportedAuthority}; |
| 49 | + use std::collections::HashMap; |
| 50 | + |
| 51 | + fn make_export_map( |
| 52 | + crate_name: &str, |
| 53 | + entries: Vec<(&str, Category, Risk, &str)>, |
| 54 | + ) -> CrateExportMap { |
| 55 | + let mut exports = HashMap::new(); |
| 56 | + for (key, category, risk, leaf_call) in entries { |
| 57 | + exports |
| 58 | + .entry(key.to_string()) |
| 59 | + .or_insert_with(Vec::new) |
| 60 | + .push(ExportedAuthority { |
| 61 | + category, |
| 62 | + risk, |
| 63 | + leaf_call: leaf_call.to_string(), |
| 64 | + is_transitive: false, |
| 65 | + }); |
| 66 | + } |
| 67 | + CrateExportMap { |
| 68 | + crate_name: crate_name.to_string(), |
| 69 | + crate_version: "1.0.0".to_string(), |
| 70 | + exports, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn single_export_map() { |
| 76 | + let map = make_export_map( |
| 77 | + "reqwest", |
| 78 | + vec![( |
| 79 | + "reqwest::get", |
| 80 | + Category::Net, |
| 81 | + Risk::High, |
| 82 | + "TcpStream::connect", |
| 83 | + )], |
| 84 | + ); |
| 85 | + let customs = export_map_to_custom_authorities(&[map]); |
| 86 | + assert_eq!(customs.len(), 1); |
| 87 | + assert_eq!(customs[0].path, vec!["reqwest", "get"]); |
| 88 | + assert_eq!(customs[0].category, Category::Net); |
| 89 | + assert!(customs[0].description.contains("Cross-crate")); |
| 90 | + assert!(customs[0].description.contains("reqwest::get")); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn multiple_exports_per_crate() { |
| 95 | + let map = make_export_map( |
| 96 | + "tokio", |
| 97 | + vec![ |
| 98 | + ( |
| 99 | + "tokio::fs::read", |
| 100 | + Category::Fs, |
| 101 | + Risk::Medium, |
| 102 | + "std::fs::read", |
| 103 | + ), |
| 104 | + ( |
| 105 | + "tokio::net::connect", |
| 106 | + Category::Net, |
| 107 | + Risk::High, |
| 108 | + "TcpStream::connect", |
| 109 | + ), |
| 110 | + ], |
| 111 | + ); |
| 112 | + let customs = export_map_to_custom_authorities(&[map]); |
| 113 | + assert_eq!(customs.len(), 2); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn multiple_crates() { |
| 118 | + let map1 = make_export_map( |
| 119 | + "reqwest", |
| 120 | + vec![( |
| 121 | + "reqwest::get", |
| 122 | + Category::Net, |
| 123 | + Risk::High, |
| 124 | + "TcpStream::connect", |
| 125 | + )], |
| 126 | + ); |
| 127 | + let map2 = make_export_map( |
| 128 | + "rusqlite", |
| 129 | + vec![( |
| 130 | + "rusqlite::execute", |
| 131 | + Category::Ffi, |
| 132 | + Risk::High, |
| 133 | + "extern sqlite3_exec", |
| 134 | + )], |
| 135 | + ); |
| 136 | + let customs = export_map_to_custom_authorities(&[map1, map2]); |
| 137 | + assert_eq!(customs.len(), 2); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn empty_export_maps() { |
| 142 | + let customs = export_map_to_custom_authorities(&[]); |
| 143 | + assert!(customs.is_empty()); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn empty_exports_in_map() { |
| 148 | + let map = CrateExportMap { |
| 149 | + crate_name: "empty".to_string(), |
| 150 | + crate_version: "1.0.0".to_string(), |
| 151 | + exports: HashMap::new(), |
| 152 | + }; |
| 153 | + let customs = export_map_to_custom_authorities(&[map]); |
| 154 | + assert!(customs.is_empty()); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn path_segments_split_correctly() { |
| 159 | + let map = make_export_map( |
| 160 | + "reqwest", |
| 161 | + vec![( |
| 162 | + "reqwest::blocking::client::get", |
| 163 | + Category::Net, |
| 164 | + Risk::High, |
| 165 | + "connect", |
| 166 | + )], |
| 167 | + ); |
| 168 | + let customs = export_map_to_custom_authorities(&[map]); |
| 169 | + assert_eq!( |
| 170 | + customs[0].path, |
| 171 | + vec!["reqwest", "blocking", "client", "get"] |
| 172 | + ); |
| 173 | + } |
| 174 | +} |
0 commit comments