domain-status 0.2.1

Concurrent URL status checker that captures comprehensive metadata in SQLite.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Body-based technology detection (HTML, script sources, meta tags, inline scripts).
//!
//! This module matches technologies based on HTML body content,
//! following wappalyzergo's `checkBody()` logic which checks:
//! 1. HTML patterns (via `matchString(body, htmlPart)`)
//! 2. Script sources (via `matchString(scriptSrc, scriptPart)`)
//! 3. Meta tags (via `matchKeyValueString(name, content, metaPart)`)
//! 4. Script source text (Wappalyzer `scripts` field — static text only;
//!    shared with post-join fetched-body matching via [`check_scripts_with_ruleset`])

use std::collections::HashMap;

use crate::fingerprint::models::FingerprintRuleset;
use crate::fingerprint::patterns::{check_meta_patterns, matches_pattern};

use super::source::DetectionSource;

/// Result of body matching for a single technology
#[derive(Debug, Clone)]
pub struct BodyMatchResult {
    pub tech_name: String,
    pub version: Option<String>,
    pub source: DetectionSource,
}

/// Tries patterns against `text`, updating `matched` / `version`.
/// Returns `true` when a version was captured (caller may stop early).
fn match_patterns_against_text(
    patterns: &[String],
    text: &str,
    matched: &mut bool,
    version: &mut Option<String>,
) -> bool {
    for pattern in patterns {
        let result = matches_pattern(pattern, text);
        if result.matched {
            *matched = true;
            if version.is_none() && result.version.is_some() {
                *version = result.version;
            }
            if version.is_some() {
                return true;
            }
        }
    }
    false
}

fn match_meta(
    tech_meta: &HashMap<String, Vec<String>>,
    meta_tags: &HashMap<String, Vec<String>>,
    matched: &mut bool,
    version: &mut Option<String>,
) {
    for (meta_key, patterns) in tech_meta {
        let result = check_meta_patterns(meta_key, patterns, meta_tags);
        if result.matched {
            *matched = true;
            if version.is_none() && result.version.is_some() {
                *version = result.version;
            }
            if version.is_some() {
                break;
            }
        }
    }
}

/// Checks all technologies against HTML body content and returns matches.
///
/// Synchronous body check using a pre-fetched ruleset (for use on blocking threads).
#[allow(clippy::too_many_arguments)] // Distinct body-side signal channels
pub(crate) fn check_body_with_ruleset(
    ruleset: &FingerprintRuleset,
    html_body: &str,
    script_sources: &[String],
    meta_tags: &HashMap<String, Vec<String>>,
    url: &str,
    inline_script_text: &str,
) -> Vec<BodyMatchResult> {
    let mut results = Vec::new();
    for (tech_name, tech) in &ruleset.technologies {
        if tech.html.is_empty()
            && tech.script.is_empty()
            && tech.scripts.is_empty()
            && tech.meta.is_empty()
            && tech.url.is_empty()
        {
            continue;
        }
        let mut matched = false;
        let mut version: Option<String> = None;
        let mut source: Option<DetectionSource> = None;

        let previously = matched;
        let _ = match_patterns_against_text(&tech.html, html_body, &mut matched, &mut version);
        if matched && !previously {
            source = Some(DetectionSource::Html);
        }
        if version.is_none() {
            for script_src in script_sources {
                let previously = matched;
                if match_patterns_against_text(&tech.script, script_src, &mut matched, &mut version)
                {
                    if source.is_none() {
                        source = Some(DetectionSource::ScriptSrc);
                    }
                    break;
                }
                if matched && !previously && source.is_none() {
                    source = Some(DetectionSource::ScriptSrc);
                }
            }
        }
        if version.is_none() && !inline_script_text.is_empty() {
            let previously = matched;
            let _ = match_patterns_against_text(
                &tech.scripts,
                inline_script_text,
                &mut matched,
                &mut version,
            );
            if matched && !previously && source.is_none() {
                source = Some(DetectionSource::Scripts);
            }
        }
        if version.is_none() {
            let previously = matched;
            match_meta(&tech.meta, meta_tags, &mut matched, &mut version);
            if matched && !previously && source.is_none() {
                source = Some(DetectionSource::Html);
            }
        }
        if version.is_none() {
            let previously = matched;
            let _ = match_patterns_against_text(&tech.url, url, &mut matched, &mut version);
            if matched && !previously && source.is_none() {
                source = Some(DetectionSource::Url);
            }
        }

        if matched {
            results.push(BodyMatchResult {
                tech_name: tech_name.clone(),
                version,
                source: source.unwrap_or(DetectionSource::Html),
            });
        }
    }
    results
}

/// Matches only Wappalyzer `scripts` patterns against script source text.
///
/// Shared by the first-pass body matcher (inline `<script>` text) and the
/// post-join supplement for fetched first-party bodies — one field, one path.
pub(crate) fn check_scripts_with_ruleset(
    ruleset: &FingerprintRuleset,
    script_text: &str,
) -> Vec<BodyMatchResult> {
    if script_text.is_empty() {
        return Vec::new();
    }
    let mut results = Vec::new();
    for (tech_name, tech) in &ruleset.technologies {
        if tech.scripts.is_empty() {
            continue;
        }
        let mut matched = false;
        let mut version: Option<String> = None;
        let _ = match_patterns_against_text(&tech.scripts, script_text, &mut matched, &mut version);
        if matched {
            results.push(BodyMatchResult {
                tech_name: tech_name.clone(),
                version,
                source: DetectionSource::Scripts,
            });
        }
    }
    results
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fingerprint::models::{FingerprintMetadata, Technology};

    fn empty_tech() -> Technology {
        Technology::default()
    }

    fn ruleset_with(technologies: HashMap<String, Technology>) -> FingerprintRuleset {
        FingerprintRuleset {
            technologies,
            categories: HashMap::new(),
            metadata: FingerprintMetadata {
                source: "test".into(),
                version: "0".into(),
                last_updated: std::time::SystemTime::now(),
            },
        }
    }

    /// Meta tag detection: `tech.meta` key `"generator"` matches `meta_tags` key
    /// `"name:generator"` (see `check_meta_patterns`).
    #[test]
    fn test_body_meta_generator_match() {
        let mut tech = empty_tech();
        tech.meta
            .insert("generator".to_string(), vec!["mura cms 1".to_string()]);
        let mut technologies = HashMap::new();
        technologies.insert("Mura CMS".to_string(), tech);
        let ruleset = ruleset_with(technologies);

        let html_body = r#"<html>
<head>
<meta name="generator" content="mura cms 1">
</head>
</html>"#;

        let mut meta_tags = HashMap::new();
        meta_tags.insert("name:generator".to_string(), vec!["mura cms 1".to_string()]);
        let script_sources = vec![];
        let url = "https://example.com";

        let results =
            check_body_with_ruleset(&ruleset, html_body, &script_sources, &meta_tags, url, "");

        let tech_names: Vec<String> = results.iter().map(|r| r.tech_name.clone()).collect();
        assert_eq!(
            tech_names,
            vec!["Mura CMS".to_string()],
            "Could not get correct match for Mura CMS via generator meta tag"
        );
    }

    /// HTML pattern detection via `data-ng-app` attribute (`AngularJS`).
    #[test]
    fn test_body_html_pattern_angularjs() {
        let mut tech = empty_tech();
        tech.html.push("data-ng-app".to_string());
        let mut technologies = HashMap::new();
        technologies.insert("AngularJS".to_string(), tech);
        let ruleset = ruleset_with(technologies);

        let html_body = r#"<html data-ng-app="rbschangeapp">
<head>
</head>
<body>
</body>
</html>"#;

        let meta_tags = HashMap::new();
        let script_sources = vec![];
        let url = "https://example.com";

        let results =
            check_body_with_ruleset(&ruleset, html_body, &script_sources, &meta_tags, url, "");

        let tech_names: Vec<String> = results.iter().map(|r| r.tech_name.clone()).collect();
        assert!(
            tech_names.contains(&"AngularJS".to_string()),
            "Could not get correct match for AngularJS via data-ng-app"
        );
    }

    /// Script source detection with version extraction (jQuery CDN URL).
    #[test]
    fn test_body_script_src_jquery() {
        let mut tech = empty_tech();
        tech.script
            .push(r"jquery(?:-(\d+\.\d+\.\d+))[/.-]\;version:\1".to_string());
        let mut technologies = HashMap::new();
        technologies.insert("jQuery".to_string(), tech);
        let ruleset = ruleset_with(technologies);

        let html_body = "";
        let script_sources = vec!["https://cdn.example.com/jquery-3.6.0.min.js".to_string()];
        let meta_tags = HashMap::new();
        let url = "https://example.com";

        let results =
            check_body_with_ruleset(&ruleset, html_body, &script_sources, &meta_tags, url, "");

        let result = results
            .iter()
            .find(|r| r.tech_name == "jQuery")
            .expect("Could not detect jQuery via script src");
        assert_eq!(result.version.as_deref(), Some("3.6.0"));
    }

    /// Inline `scripts` pattern detection (static text, no JS execution).
    #[test]
    fn test_body_inline_scripts_pattern() {
        let mut tech = empty_tech();
        tech.scripts
            .push(r"function webpackJsonpCallback\(data\) \{".to_string());
        let mut technologies = HashMap::new();
        technologies.insert("Webpack".to_string(), tech);
        let ruleset = ruleset_with(technologies);

        let inline = "function webpackJsonpCallback(data) { return data; }";
        let results = check_body_with_ruleset(
            &ruleset,
            "",
            &[],
            &HashMap::new(),
            "https://example.com",
            inline,
        );
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].tech_name, "Webpack");
    }

    /// Shared `scripts` matcher used by the post-join external-body supplement.
    #[test]
    fn test_check_scripts_with_ruleset_matches() {
        let mut tech = empty_tech();
        tech.scripts
            .push(r"function webpackJsonpCallback\(data\) \{".to_string());
        let ruleset = ruleset_with(HashMap::from([("Webpack".into(), tech)]));
        let results = check_scripts_with_ruleset(
            &ruleset,
            "function webpackJsonpCallback(data) { return data; }",
        );
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].tech_name, "Webpack");
        assert!(check_scripts_with_ruleset(&ruleset, "").is_empty());
    }

    /// HTML pattern detection (`WordPress` via `/wp-content/`).
    #[test]
    fn test_body_html_pattern_wordpress() {
        let mut tech = empty_tech();
        tech.html.push("wp-content".to_string());
        let mut technologies = HashMap::new();
        technologies.insert("WordPress".to_string(), tech);
        let ruleset = ruleset_with(technologies);

        let html_body = r#"<html>
<head>
</head>
<body>
<link rel="stylesheet" href="/wp-content/themes/twenty-twenty-one/style.css">
</body>
</html>"#;

        let script_sources = vec![];
        let meta_tags = HashMap::new();
        let url = "https://example.com";

        let results =
            check_body_with_ruleset(&ruleset, html_body, &script_sources, &meta_tags, url, "");

        let tech_names: Vec<String> = results.iter().map(|r| r.tech_name.clone()).collect();
        assert!(
            tech_names.contains(&"WordPress".to_string()),
            "Could not get correct match for WordPress via wp-content"
        );
    }

    #[test]
    fn test_check_body_with_empty_ruleset() {
        let ruleset = FingerprintRuleset::empty_for_tests();
        let html_body = "<html></html>";
        let script_sources = vec![];
        let meta_tags = HashMap::new();
        let url = "https://example.com";

        let results =
            check_body_with_ruleset(&ruleset, html_body, &script_sources, &meta_tags, url, "");
        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn test_check_body_empty_inputs() {
        // Empty inputs must not panic; no remote ruleset required.
        let ruleset = FingerprintRuleset::empty_for_tests();

        let html_body = "";
        let script_sources = vec![];
        let meta_tags = HashMap::new();
        let url = "";

        let result =
            check_body_with_ruleset(&ruleset, html_body, &script_sources, &meta_tags, url, "");
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn test_check_body_very_large_html() {
        // Large HTML must not panic the matcher; no remote ruleset required.
        let ruleset = FingerprintRuleset::empty_for_tests();

        let large_html = format!("<html><body>{}</body></html>", "A".repeat(1_000_000));
        let script_sources = vec![];
        let meta_tags = HashMap::new();
        let url = "https://example.com";

        let _result =
            check_body_with_ruleset(&ruleset, &large_html, &script_sources, &meta_tags, url, "");
    }
}