Skip to main content

fetchium_core/validate/
authority.rs

1//! V1 Authority scoring — domain reputation, SSL, redirect analysis (PRD §19, §21).
2
3use crate::validate::types::*;
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7/// Domain reputation tier.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9pub enum DomainTier {
10    /// Tier 1: Highly authoritative (.gov, .edu, major outlets).
11    Authoritative,
12    /// Tier 2: Generally reliable (established tech blogs, official docs).
13    Reliable,
14    /// Tier 3: Mixed quality (forums, personal blogs).
15    Mixed,
16    /// Tier 4: Low trust (unknown, new domains).
17    Unknown,
18    /// Tier 5: Blocklisted (known spam, SEO farms).
19    Blocked,
20}
21
22/// V1 Authority Scorer.
23pub struct AuthorityScorer {
24    domain_tiers: HashMap<String, DomainTier>,
25}
26
27impl Default for AuthorityScorer {
28    fn default() -> Self {
29        let mut tiers = HashMap::new();
30        // Tier 1: Authoritative
31        for d in &[
32            "github.com",
33            "docs.rs",
34            "rust-lang.org",
35            "wikipedia.org",
36            "arxiv.org",
37            "nature.com",
38            "science.org",
39            "ieee.org",
40            "acm.org",
41            "nih.gov",
42            "cdc.gov",
43        ] {
44            tiers.insert(d.to_string(), DomainTier::Authoritative);
45        }
46        // Tier 2: Reliable
47        for d in &[
48            "stackoverflow.com",
49            "developer.mozilla.org",
50            "medium.com",
51            "dev.to",
52            "hacker-news.firebaseio.com",
53            "reddit.com",
54        ] {
55            tiers.insert(d.to_string(), DomainTier::Reliable);
56        }
57        Self {
58            domain_tiers: tiers,
59        }
60    }
61}
62
63impl AuthorityScorer {
64    /// Score a single source URL. Returns (score 0.0-1.0, domain tier).
65    pub fn score(&self, url: &str, has_ssl: bool, redirect_count: u32) -> (f64, DomainTier) {
66        let domain = Self::extract_domain(url);
67        let tier = self.domain_tiers.get(&domain).copied().unwrap_or_else(|| {
68            if domain.ends_with(".gov") || domain.ends_with(".edu") {
69                DomainTier::Authoritative
70            } else if domain.ends_with(".org") {
71                DomainTier::Reliable
72            } else {
73                DomainTier::Unknown
74            }
75        });
76
77        let mut score: f64 = match tier {
78            DomainTier::Authoritative => 0.95,
79            DomainTier::Reliable => 0.75,
80            DomainTier::Mixed => 0.50,
81            DomainTier::Unknown => 0.40,
82            DomainTier::Blocked => 0.05,
83        };
84
85        if !has_ssl {
86            score *= 0.7;
87        }
88        if redirect_count > 2 {
89            score *= 0.9;
90        }
91        if redirect_count > 5 {
92            score *= 0.8;
93        }
94
95        (score.clamp(0.0, 1.0), tier)
96    }
97
98    /// Run as V1 validation layer.
99    /// Each tuple: (url, has_ssl, redirect_count).
100    pub fn validate_sources(&self, sources: &[(String, bool, u32)]) -> LayerResult {
101        let start = std::time::Instant::now();
102        let mut issues = Vec::new();
103        let mut scores = Vec::new();
104
105        for (url, has_ssl, redirects) in sources {
106            let (score, tier) = self.score(url, *has_ssl, *redirects);
107            scores.push(score);
108
109            if tier == DomainTier::Blocked {
110                issues.push(ValidationIssue {
111                    severity: IssueSeverity::Error,
112                    code: "V1_BLOCKED_DOMAIN".into(),
113                    message: format!("Domain is blocklisted: {url}"),
114                    source_url: Some(url.clone()),
115                });
116            }
117            if !has_ssl {
118                issues.push(ValidationIssue {
119                    severity: IssueSeverity::Warning,
120                    code: "V1_NO_SSL".into(),
121                    message: format!("No SSL/TLS: {url}"),
122                    source_url: Some(url.clone()),
123                });
124            }
125        }
126
127        let avg = if scores.is_empty() {
128            0.5
129        } else {
130            scores.iter().sum::<f64>() / scores.len() as f64
131        };
132
133        LayerResult {
134            layer: ValidationLayerId::V1Source,
135            passed: avg >= 0.3,
136            score: avg,
137            issues,
138            duration_ms: start.elapsed().as_millis() as u64,
139        }
140    }
141
142    fn extract_domain(url: &str) -> String {
143        url::Url::parse(url)
144            .ok()
145            .and_then(|u| u.host_str().map(|h| h.to_string()))
146            .unwrap_or_default()
147    }
148}
149
150#[cfg(test)]
151mod tests {
152    use super::*;
153
154    #[test]
155    fn gov_domain_high_score() {
156        let s = AuthorityScorer::default();
157        let (score, tier) = s.score("https://nih.gov/article", true, 0);
158        assert!(score >= 0.9);
159        assert_eq!(tier, DomainTier::Authoritative);
160    }
161
162    #[test]
163    fn no_ssl_penalty() {
164        let s = AuthorityScorer::default();
165        let (with, _) = s.score("https://example.com/page", true, 0);
166        let (without, _) = s.score("http://example.com/page", false, 0);
167        assert!(without < with);
168    }
169
170    #[test]
171    fn redirect_penalty() {
172        let s = AuthorityScorer::default();
173        let (no_redirect, _) = s.score("https://github.com/repo", true, 0);
174        let (many_redirects, _) = s.score("https://github.com/repo", true, 6);
175        assert!(many_redirects < no_redirect);
176    }
177
178    #[test]
179    fn validate_layer_result() {
180        let s = AuthorityScorer::default();
181        let sources = vec![
182            ("https://rust-lang.org/page".to_string(), true, 0u32),
183            ("https://docs.rs/crate".to_string(), true, 0u32),
184        ];
185        let r = s.validate_sources(&sources);
186        assert!(r.passed);
187        assert!(r.score > 0.8);
188    }
189}