gossan-hidden 0.3.3

Hidden endpoint and misconfiguration scanner for gossan (CORS, SSRF, JWT, Swagger, cache deception), part of the security research ecosystem
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
//! CORS misconfiguration detection.
//!
//! Tests the target for dangerous Cross-Origin Resource Sharing configurations:
//!
//! - **Origin reflection**: server blindly reflects any `Origin` header → full
//!   account takeover via cross-origin credential theft.
//! - **Null origin**: `Origin: null` is trusted → sandbox/data-URI exploits can
//!   read authenticated responses.
//! - **Wildcard with credentials**: `Access-Control-Allow-Origin: *` combined with
//!   `Access-Control-Allow-Credentials: true` → browsers block this, but the
//!   misconfiguration signals a confused developer who may fix it wrong.
//! - **Subdomain wildcard**: a prefix/suffix match (e.g., `evil-example.com` is
//!   accepted when `example.com` is the real origin) → attacker-controlled
//!   subdomain can steal data.
//! - **HTTP origin trusted on HTTPS site**: `http://example.com` is allowed on
//!   an `https://` endpoint → network MITM can inject JS to exfiltrate data
//!   cross-origin.
//!
//! Each probe sends a single request with a crafted `Origin` header and inspects
//! the `Access-Control-Allow-Origin` and `Access-Control-Allow-Credentials`
//! response headers.

use gossan_core::Target;
use reqwest::Client;
use secfinding::{Evidence, Finding, Severity};

/// CORS test origins (each targets a different misconfiguration class).
const EVIL_ORIGIN: &str = "https://evil.com";
const NULL_ORIGIN: &str = "null";

pub async fn probe(client: &Client, target: &Target) -> anyhow::Result<Vec<Finding>> {
    let Target::Web(asset) = target else {
        return Ok(vec![]);
    };
    let base = asset.url.as_str();
    let mut findings = Vec::new();

    // ── Test 1: arbitrary origin reflection ──────────────────────────────
    match client.get(base).header("Origin", EVIL_ORIGIN).send().await {
        Ok(resp) => {
            let acao = header_value(&resp, "access-control-allow-origin");
        let acac = header_value(&resp, "access-control-allow-credentials");
        let status = resp.status().as_u16();

        if acao.as_deref() == Some(EVIL_ORIGIN) {
            let credentials = acac.as_deref() == Some("true");
            let (severity, title, detail) = if credentials {
                (
                    Severity::Critical,
                    "CORS: arbitrary origin reflected with credentials",
                    "The server reflects any Origin header AND sends \
                     Access-Control-Allow-Credentials: true. An attacker on any domain \
                     can make authenticated cross-origin requests and read the response. \
                     this enables full account takeover via credential theft. \
                     Fix: validate Origin against an explicit allowlist.",
                )
            } else {
                (
                    Severity::High,
                    "CORS: arbitrary origin reflected",
                    "The server reflects any Origin header in Access-Control-Allow-Origin. \
                     While credentials are not explicitly allowed, this still exposes \
                     non-authenticated API responses to any origin. If the server later \
                     adds credentials support, it becomes Critical. \
                     Fix: validate Origin against an explicit allowlist.",
                )
            };

            findings.push(
                crate::misconfig_finding(target, severity, title, detail)
                    .evidence(Evidence::HttpResponse {
                        status,
                        headers: build_cors_evidence(&acao, &acac),
                        body_excerpt: None,
                    })
                    .tag("cors")
                    .tag("web")
                    .tag("misconfiguration")
                    .build()
                    .map_err(|e| anyhow::anyhow!(e))?,
            );
            }
        }
        Err(e) => {
            tracing::warn!(
                "CORS probe send failed: origin={} url={} error={}",
                EVIL_ORIGIN,
                base,
                e
            );
        }
    }

    // ── Test 2: null origin trusted ──────────────────────────────────────
    match client.get(base).header("Origin", NULL_ORIGIN).send().await {
        Ok(resp) => {
            let acao = header_value(&resp, "access-control-allow-origin");
        let acac = header_value(&resp, "access-control-allow-credentials");
        let status = resp.status().as_u16();

        if acao.as_deref() == Some("null") {
            let credentials = acac.as_deref() == Some("true");
            findings.push(
                crate::misconfig_finding(
                    target,
                    if credentials {
                        Severity::Critical
                    } else {
                        Severity::High
                    },
                    "CORS: null origin trusted",
                    format!(
                        "The server allows Origin: null{}. \
                         Sandboxed iframes, data: URIs, and file: pages send null origin. \
                         an attacker can craft a page that reads cross-origin responses. \
                         Fix: never trust null origin.",
                        if credentials { " with credentials" } else { "" }
                    ),
                )
                .evidence(Evidence::HttpResponse {
                    status,
                    headers: build_cors_evidence(&acao, &acac),
                    body_excerpt: None,
                })
                .tag("cors")
                .tag("web")
                .tag("misconfiguration")
                .build()
                .map_err(|e| anyhow::anyhow!(e))?,
            );
            }
        }
        Err(e) => {
            tracing::warn!(
                "CORS probe send failed: origin={} url={} error={}",
                NULL_ORIGIN,
                base,
                e
            );
        }
    }

    // ── Test 3: subdomain wildcard / prefix mismatch ─────────────────────
    // If the site is https://example.com, test if https://evil-example.com is accepted
    if let Some(domain) = target.domain() {
        let prefix_evil = format!("https://evil-{domain}");
        match client.get(base).header("Origin", &prefix_evil).send().await {
            Ok(resp) => {
                let acao = header_value(&resp, "access-control-allow-origin");
            let status = resp.status().as_u16();

            if acao.as_deref() == Some(prefix_evil.as_str()) {
                findings.push(
                    crate::misconfig_finding(
                        target,
                        Severity::High,
                        "CORS: prefix/suffix origin bypass",
                        format!(
                            "The server accepted '{prefix_evil}' as a trusted origin. \
                             This means the CORS validation uses a naive contains/endsWith \
                             check instead of exact matching. An attacker can register a \
                             lookalike domain to bypass CORS restrictions. \
                             Fix: use exact origin comparison, not substring matching."
                        ),
                    )
                    .evidence(Evidence::HttpResponse {
                        status,
                        headers: build_cors_evidence(&acao, &None),
                        body_excerpt: None,
                    })
                    .tag("cors")
                    .tag("web")
                    .tag("misconfiguration")
                    .build()
                    .map_err(|e| anyhow::anyhow!(e))?,
                );
                }
            }
            Err(e) => {
                tracing::warn!(
                    "CORS probe send failed: origin={} url={} error={}",
                    prefix_evil,
                    base,
                    e
                );
            }
        }
    }

    // ── Test 4: suffix origin bypass (regex wildcard) ───────────────────
    if let Some(domain) = target.domain() {
        let suffix_evil = format!("https://{}.evil.com", domain);
        match client.get(base).header("Origin", &suffix_evil).send().await {
            Ok(resp) => {
                let acao = header_value(&resp, "access-control-allow-origin");
            let status = resp.status().as_u16();
            if acao.as_deref() == Some(suffix_evil.as_str()) {
                findings.push(
                    crate::misconfig_finding(
                        target,
                        Severity::High,
                        "CORS: suffix origin bypass",
                        format!(
                            "The server accepted '{}' as a trusted origin. \
                             This means the CORS validation uses a naive contains/endsWith \
                             check instead of exact matching. An attacker can register a \
                             lookalike domain to bypass CORS restrictions. \
                             Fix: use exact origin comparison, not substring matching.",
                            suffix_evil
                        ),
                    )
                    .evidence(Evidence::HttpResponse {
                        status,
                        headers: build_cors_evidence(&acao, &None),
                        body_excerpt: None,
                    })
                    .tag("cors")
                    .tag("web")
                    .tag("misconfiguration")
                    .build()
                    .map_err(|e| anyhow::anyhow!(e))?,
                );
                }
            }
            Err(e) => {
                tracing::warn!(
                    "CORS probe send failed: origin={} url={} error={}",
                    suffix_evil,
                    base,
                    e
                );
            }
        }
    }

    // ── Test 5: HTTP origin trusted on HTTPS site ────────────────────────
    if base.starts_with("https://") {
        if let Some(domain) = target.domain() {
            let http_origin = format!("http://{domain}");
            match client.get(base).header("Origin", &http_origin).send().await {
                Ok(resp) => {
                    let acao = header_value(&resp, "access-control-allow-origin");
                let status = resp.status().as_u16();

                if acao.as_deref() == Some(http_origin.as_str()) {
                    findings.push(
                        crate::misconfig_finding(
                            target,
                            Severity::Medium,
                            "CORS: HTTP origin trusted on HTTPS endpoint",
                            format!(
                                "The HTTPS endpoint accepts 'http://{domain}' as a trusted \
                                 CORS origin. A network-level attacker (MITM) can inject \
                                 JavaScript on the HTTP origin to exfiltrate data from the \
                                 HTTPS endpoint cross-origin. \
                                 Fix: only allow HTTPS origins in CORS configuration."
                            ),
                        )
                        .evidence(Evidence::HttpResponse {
                            status,
                            headers: build_cors_evidence(&acao, &None),
                            body_excerpt: None,
                        })
                        .tag("cors")
                        .tag("web")
                        .tag("misconfiguration")
                        .build()
                        .map_err(|e| anyhow::anyhow!(e))?,
                    );
                    }
                }
                Err(e) => {
                    tracing::warn!(
                        "CORS probe send failed: origin={} url={} error={}",
                        http_origin,
                        base,
                        e
                    );
                }
            }
        }
    }

    // ── Test 6: overly permissive methods ───────────────────────────────
    match client
        .request(reqwest::Method::OPTIONS, base)
        .header("Origin", EVIL_ORIGIN)
        .header("Access-Control-Request-Method", "DELETE")
        .send()
        .await
    {
        Ok(resp) => {
            let acam = header_value(&resp, "access-control-allow-methods");
        let status = resp.status().as_u16();

        if let Some(ref methods) = acam {
            let methods_upper = methods.to_uppercase();
            let dangerous_methods: Vec<&str> = ["DELETE", "PUT", "PATCH"]
                .iter()
                .filter(|m| methods_upper.contains(**m))
                .copied()
                .collect();
            // Fire when the server exposes a wildcard method list OR allows
            // two or more high-risk methods cross-origin. The explicit
            // parentheses pin the intended precedence (`&&` binds tighter
            // than `||` without them) so a future reader cannot misread the
            // condition.
            if (!dangerous_methods.is_empty() && methods_upper.contains("*"))
                || dangerous_methods.len() >= 2
            {
                findings.push(
                    crate::misconfig_finding(
                        target,
                        Severity::Medium,
                        "CORS: dangerous methods allowed cross-origin",
                        format!(
                            "The server allows dangerous HTTP methods ({}) cross-origin.                              This may enable cross-origin data modification if credentials are also allowed.                              Fix: restrict Access-Control-Allow-Methods to only required methods.",
                            dangerous_methods.join(", ")
                        ),
                    )
                    .evidence(Evidence::HttpResponse {
                        status,
                        headers: vec![("access-control-allow-methods".into(), methods.clone().into())],
                        body_excerpt: None,
                    })
                    .tag("cors")
                    .tag("web")
                    .tag("misconfiguration")
                    .build()
                    .map_err(|e| anyhow::anyhow!(e))?,
                );
            }
            }
        }
        Err(e) => {
            tracing::warn!(
                "CORS OPTIONS probe send failed: origin={} url={} error={}",
                EVIL_ORIGIN,
                base,
                e
            );
        }
    }

    Ok(findings)
}

/// Extract a response header value as an owned string.
fn header_value(resp: &reqwest::Response, name: &str) -> Option<String> {
    resp.headers()
        .get(name)
        .and_then(|v| v.to_str().ok())
        .map(|s| s.to_string())
}

/// Build evidence headers for CORS-related findings.
fn build_cors_evidence(
    acao: &Option<String>,
    acac: &Option<String>,
) -> Vec<(std::sync::Arc<str>, std::sync::Arc<str>)> {
    let mut headers = Vec::new();
    if let Some(val) = acao {
        headers.push((
            "access-control-allow-origin".into(),
            std::sync::Arc::<str>::from(val.as_str()),
        ));
    }
    if let Some(val) = acac {
        headers.push((
            "access-control-allow-credentials".into(),
            std::sync::Arc::<str>::from(val.as_str()),
        ));
    }
    headers
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn header_value_extracts_correctly() {
        let evidence = build_cors_evidence(&Some("https://evil.com".into()), &Some("true".into()));
        assert_eq!(evidence.len(), 2);
        assert_eq!(&*evidence[0].0, "access-control-allow-origin");
        assert_eq!(&*evidence[1].0, "access-control-allow-credentials");
    }

    #[test]
    fn evidence_handles_none() {
        let evidence = build_cors_evidence(&None, &None);
        assert!(evidence.is_empty());
    }

    #[test]
    fn evil_origin_is_https() {
        assert!(EVIL_ORIGIN.starts_with("https://"));
    }

    #[test]
    fn build_cors_evidence_only_acao() {
        let evidence = build_cors_evidence(&Some("*".into()), &None);
        assert_eq!(evidence.len(), 1);
        assert_eq!(&*evidence[0].0, "access-control-allow-origin");
        assert_eq!(&*evidence[0].1, "*");
    }

    #[test]
    fn build_cors_evidence_only_acac() {
        let evidence = build_cors_evidence(&None, &Some("true".into()));
        assert_eq!(evidence.len(), 1);
        assert_eq!(&*evidence[0].0, "access-control-allow-credentials");
    }

    #[test]
    fn null_origin_is_lowercase() {
        assert_eq!(NULL_ORIGIN, "null");
    }

    #[test]
    fn evil_origin_contains_evil_com() {
        assert!(EVIL_ORIGIN.contains("evil.com"));
    }

    #[test]
    fn build_cors_evidence_preserves_values() {
        let evidence = build_cors_evidence(&Some("https://example.com".into()), &Some("false".into()));
        assert_eq!(&*evidence[0].1, "https://example.com");
        assert_eq!(&*evidence[1].1, "false");
    }

    #[test]
    fn build_cors_evidence_empty_strings_adversarial() {
        let evidence = build_cors_evidence(&Some("".into()), &Some("".into()));
        assert_eq!(evidence.len(), 2);
        assert_eq!(&*evidence[0].1, "");
        assert_eq!(&*evidence[1].1, "");
    }

    #[test]
    fn build_cors_evidence_special_chars_in_origin_adversarial() {
        let evidence = build_cors_evidence(&Some("https://evil.com?x=1&y=2".into()), &None);
        assert_eq!(evidence.len(), 1);
        assert_eq!(&*evidence[0].1, "https://evil.com?x=1&y=2");
    }

    #[test]
    fn build_cors_evidence_unicode_in_origin_adversarial() {
        let evidence = build_cors_evidence(&Some("https://日本語.example.com".into()), &None);
        assert_eq!(evidence.len(), 1);
        assert_eq!(&*evidence[0].1, "https://日本語.example.com");
    }

    // ── Dangerous-methods condition logic (anti-rig for §15 test 6) ────────

    /// Anti-rig: pin the precedence of the dangerous-methods condition.
    /// The condition must fire when any dangerous method AND wildcard appears,
    /// OR when two or more dangerous methods are present, but NOT when only
    /// one dangerous method is listed without a wildcard.
    #[test]
    fn dangerous_methods_condition_requires_wildcard_or_two_plus() {
        // Helper that mirrors the probe condition.
        fn should_fire(methods_upper: &str) -> bool {
            let dangerous: Vec<&str> = ["DELETE", "PUT", "PATCH"]
                .iter()
                .filter(|m| methods_upper.contains(**m))
                .copied()
                .collect();
            (!dangerous.is_empty() && methods_upper.contains("*"))
                || dangerous.len() >= 2
        }

        // One dangerous + wildcard → fire.
        assert!(should_fire("GET, DELETE, *"), "DELETE + wildcard must fire");

        // Two dangerous, no wildcard → fire.
        assert!(should_fire("GET, DELETE, PUT"), "DELETE + PUT must fire");

        // Three dangerous → fire.
        assert!(should_fire("DELETE, PUT, PATCH"), "all three dangerous must fire");

        // One dangerous, no wildcard → must NOT fire (would be false positive).
        assert!(!should_fire("GET, DELETE"), "single dangerous method without wildcard must NOT fire");
        assert!(!should_fire("GET, PUT"), "single dangerous method without wildcard must NOT fire");
        assert!(!should_fire("GET, PATCH"), "single dangerous method without wildcard must NOT fire");

        // No dangerous methods → must NOT fire.
        assert!(!should_fire("GET, POST"), "no dangerous methods must NOT fire");
        assert!(!should_fire("GET, *"), "wildcard alone with no dangerous method must NOT fire");
    }
}