parlov-elicit 0.3.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `LongUriElicitation` -- pads the probe URL to ~16 KB.
//!
//! Appends `?_pad=AAAA...` (or `&_pad=AAAA...` when a query string already exists)
//! to the resolved probe URL until the total length reaches 16 384 bytes. Servers
//! with different URI length limits for existing vs nonexistent resources will
//! return different error codes (e.g. 414 vs 404), leaking 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;

/// Target padded URL length in bytes.
const TARGET_LEN: usize = 16_384;

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "long-uri-elicit",
        strategy_name: "Long URI Elicitation",
        risk: RiskLevel::Safe,
    }
}

fn technique() -> Technique {
    Technique {
        id: "long-uri",
        name: "Long URI padding",
        oracle_class: OracleClass::Existence,
        vector: Vector::StatusCodeDiff,
        strength: NormativeStrength::Should,
    }
}

/// Pads `url` to at least `TARGET_LEN` bytes by appending `?_pad=AAA...` or `&_pad=AAA...`.
fn pad_url(url: &str) -> String {
    let separator = if url.contains('?') { '&' } else { '?' };
    let prefix = format!("{url}{separator}_pad=");
    let pad_needed = TARGET_LEN.saturating_sub(prefix.len());
    let pad_len = pad_needed.max(1);
    let padding = "A".repeat(pad_len);
    format!("{prefix}{padding}")
}

/// Elicits existence differentials by padding the probe URL to ~16 KB.
pub struct LongUriElicitation;

impl Strategy for LongUriElicitation {
    fn id(&self) -> &'static str {
        "long-uri-elicit"
    }

    fn name(&self) -> &'static str {
        "Long URI 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 = pad_url(&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: "9999".to_string(),
            headers: HeaderMap::new(),
            max_risk: RiskLevel::Safe,
            known_duplicate: None,
            state_field: None,
            alt_credential: None,
            body_template: None,
        }
    }

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

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

    #[test]
    fn probe_url_length_meets_target() {
        let specs = LongUriElicitation.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.len() >= TARGET_LEN);
    }

    #[test]
    fn baseline_url_is_unmodified() {
        let specs = LongUriElicitation.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_should() {
        let specs = LongUriElicitation.generate(&make_ctx());
        assert_eq!(specs[0].technique().strength, NormativeStrength::Should);
    }
}