parlov-elicit 0.5.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `AuthStripElicitation` -- probes with the `Authorization` header removed.
//!
//! Sends the probe request without an `Authorization` header while the baseline
//! uses `ctx.headers` unmodified. A server that returns 403 for authenticated
//! requests to existing resources but 404 for unauthenticated requests reveals
//! resource existence through authorization-gated responses.

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

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

static METADATA: StrategyMetadata = StrategyMetadata {
    strategy_id: "auth-strip-elicit",
    strategy_name: "Auth Strip Elicitation",
    risk: RiskLevel::Safe,
};

static TECHNIQUE: Technique = Technique {
    id: "auth-strip",
    name: "Authorization header strip",
    oracle_class: OracleClass::Existence,
    vector: Vector::StatusCodeDiff,
    strength: NormativeStrength::Should,
    normalization_weight: Some(0.25),
    inverted_signal_weight: None,
    method_relevant: false,
    parser_relevant: false,
    applicability: always_applicable,
    contradiction_surface: SignalSurface::Status,
};

/// Elicits existence differentials by stripping the `Authorization` header from probe requests.
pub struct AuthStripElicitation;

impl Strategy for AuthStripElicitation {
    fn metadata(&self) -> &'static StrategyMetadata {
        &METADATA
    }

    fn technique_def(&self) -> &'static Technique {
        &TECHNIQUE
    }

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

    fn is_applicable(&self, ctx: &ScanContext) -> bool {
        ctx.headers.contains_key(http::header::AUTHORIZATION)
    }

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let mut specs = Vec::with_capacity(2);
        for method in [Method::GET, Method::HEAD] {
            let baseline_headers = ctx.headers.clone();
            let mut probe_headers = ctx.headers.clone();
            probe_headers.remove(http::header::AUTHORIZATION);
            let pair = build_pair(
                ctx,
                method,
                baseline_headers,
                probe_headers,
                None,
                METADATA.clone(),
                TECHNIQUE,
            );
            specs.push(ProbeSpec::Pair(pair));
        }
        specs
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::{ctx_with_auth, minimal_ctx};

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

    #[test]
    fn is_applicable_false_without_auth_header() {
        assert!(!AuthStripElicitation.is_applicable(&minimal_ctx()));
    }

    #[test]
    fn is_applicable_true_with_auth_header() {
        assert!(AuthStripElicitation.is_applicable(&ctx_with_auth()));
    }

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

    #[test]
    fn probe_lacks_authorization_header() {
        let specs = AuthStripElicitation.generate(&ctx_with_auth());
        let ProbeSpec::Pair(pair) = &specs[0] else {
            panic!("expected Pair")
        };
        assert!(pair
            .probe
            .headers
            .get(http::header::AUTHORIZATION)
            .is_none());
    }

    #[test]
    fn baseline_has_authorization_header() {
        let specs = AuthStripElicitation.generate(&ctx_with_auth());
        let ProbeSpec::Pair(pair) = &specs[0] else {
            panic!("expected Pair")
        };
        assert!(pair
            .baseline
            .headers
            .get(http::header::AUTHORIZATION)
            .is_some());
    }

    #[test]
    fn technique_strength_is_should() {
        let specs = AuthStripElicitation.generate(&ctx_with_auth());
        assert_eq!(specs[0].technique().strength, NormativeStrength::Should);
    }

    #[test]
    fn normalization_weight_is_0_25() {
        assert_eq!(TECHNIQUE.normalization_weight, Some(0.25));
    }

    #[test]
    fn inverted_signal_weight_is_none() {
        assert_eq!(TECHNIQUE.inverted_signal_weight, None);
    }
}