parlov-elicit 0.3.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `CaseNormalizeElicitation` -- uppercases the path component of the probe URL.
//!
//! Sends a probe whose URL path is fully uppercased while the baseline URL is
//! unmodified. Servers with case-sensitive routing will return 404 for the
//! uppercased path only when the resource exists at the canonical lowercase
//! path -- a confirmation of existence.

use http::Method;
use parlov_core::{
    NormativeStrength, OracleClass, ProbeDefinition, Technique, Vector,
};

use crate::strategy::Strategy;
use crate::types::{ProbePair, ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::substitute_url;
use crate::ScanContext;

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "case-normalize-elicit",
        strategy_name: "Case Normalize Elicitation",
        risk: RiskLevel::Safe,
    }
}

fn technique() -> Technique {
    Technique {
        id: "case-normalize",
        name: "URL path case normalization",
        oracle_class: OracleClass::Existence,
        vector: Vector::StatusCodeDiff,
        strength: NormativeStrength::May,
    }
}

/// Uppercases the path component of a URL, leaving scheme, authority, and query intact.
fn uppercase_url_path(url: &str) -> String {
    match url.split_once('?') {
        Some((path, query)) => format!("{}?{query}", path.to_uppercase()),
        None => url.to_uppercase(),
    }
}

/// Elicits existence differentials by uppercasing the path component of the probe URL.
pub struct CaseNormalizeElicitation;

impl Strategy for CaseNormalizeElicitation {
    fn id(&self) -> &'static str {
        "case-normalize-elicit"
    }

    fn name(&self) -> &'static str {
        "Case Normalize Elicitation"
    }

    fn risk(&self) -> RiskLevel {
        RiskLevel::Safe
    }

    fn methods(&self) -> &[Method] {
        &[Method::GET, Method::HEAD]
    }

    fn is_applicable(&self, _ctx: &ScanContext) -> bool {
        true
    }

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let mut specs = Vec::with_capacity(2);
        let baseline_url = substitute_url(&ctx.target, &ctx.baseline_id);
        let probe_url_base = substitute_url(&ctx.target, &ctx.probe_id);
        let probe_url = uppercase_url_path(&probe_url_base);

        for method in [Method::GET, Method::HEAD] {
            let pair = ProbePair {
                baseline: ProbeDefinition {
                    url: baseline_url.clone(),
                    method: method.clone(),
                    headers: ctx.headers.clone(),
                    body: None,
                },
                probe: ProbeDefinition {
                    url: probe_url.clone(),
                    method,
                    headers: ctx.headers.clone(),
                    body: None,
                },
                metadata: metadata(),
                technique: technique(),
            };
            specs.push(ProbeSpec::Pair(pair));
        }
        specs
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use http::{HeaderMap, Method};

    fn make_ctx() -> ScanContext {
        ScanContext {
            target: "https://api.example.com/users/{id}".to_string(),
            baseline_id: "1001".to_string(),
            probe_id: "abc".to_string(),
            headers: HeaderMap::new(),
            max_risk: RiskLevel::Safe,
            known_duplicate: None,
            state_field: None,
            alt_credential: None,
            body_template: None,
        }
    }

    fn make_ctx_with_query() -> ScanContext {
        ScanContext {
            target: "https://api.example.com/users/{id}?foo=bar".to_string(),
            ..make_ctx()
        }
    }

    #[test]
    fn risk_is_safe() {
        assert_eq!(CaseNormalizeElicitation.risk(), RiskLevel::Safe);
    }

    #[test]
    fn generate_returns_two_items() {
        assert_eq!(CaseNormalizeElicitation.generate(&make_ctx()).len(), 2);
    }

    #[test]
    fn probe_url_path_is_uppercased() {
        let specs = CaseNormalizeElicitation.generate(&make_ctx());
        let pair = specs.iter().find_map(|s| {
            if let ProbeSpec::Pair(p) = s {
                if p.probe.method == Method::GET { return Some(p); }
            }
            None
        });
        let pair = pair.expect("GET pair must exist");
        assert!(pair.probe.url.contains("/USERS/ABC"), "got: {}", pair.probe.url);
    }

    #[test]
    fn query_string_is_not_uppercased() {
        let specs = CaseNormalizeElicitation.generate(&make_ctx_with_query());
        let pair = specs.iter().find_map(|s| {
            if let ProbeSpec::Pair(p) = s {
                if p.probe.method == Method::GET { return Some(p); }
            }
            None
        });
        let pair = pair.expect("GET pair must exist");
        assert!(pair.probe.url.contains("?foo=bar"), "got: {}", pair.probe.url);
    }

    #[test]
    fn baseline_url_is_unmodified() {
        let specs = CaseNormalizeElicitation.generate(&make_ctx());
        let pair = specs.iter().find_map(|s| {
            if let ProbeSpec::Pair(p) = s {
                if p.baseline.method == Method::GET { return Some(p); }
            }
            None
        });
        let pair = pair.expect("GET pair must exist");
        assert_eq!(pair.baseline.url, "https://api.example.com/users/1001");
    }

    #[test]
    fn technique_strength_is_may() {
        let specs = CaseNormalizeElicitation.generate(&make_ctx());
        assert_eq!(specs[0].technique().strength, NormativeStrength::May);
    }
}