parlov-elicit 0.5.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `ScopeManipulationElicitation` -- probes using an under-scoped alternate credential.
//!
//! Sends the probe request with `ctx.alt_credential` headers while the baseline
//! uses `ctx.headers` unmodified. A server that returns different status codes
//! for the under-scoped credential (e.g. 403 vs 404) reveals resource existence.

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: "scope-manipulation-elicit",
    strategy_name: "Scope Manipulation Elicitation",
    risk: RiskLevel::Safe,
};

static TECHNIQUE: Technique = Technique {
    id: "scope-manipulation",
    name: "Scope manipulation credential probe",
    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 probing with an under-scoped alternate credential.
pub struct ScopeManipulationElicitation;

impl Strategy for ScopeManipulationElicitation {
    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.alt_credential.is_some()
    }

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let alt = match &ctx.alt_credential {
            Some(h) => h.clone(),
            None => return vec![],
        };
        let mut specs = Vec::with_capacity(2);
        for method in [Method::GET, Method::HEAD] {
            let pair = build_pair(
                ctx,
                method,
                ctx.headers.clone(),
                alt.clone(),
                None,
                METADATA.clone(),
                TECHNIQUE,
            );
            specs.push(ProbeSpec::Pair(pair));
        }
        specs
    }
}

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

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

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

    #[test]
    fn is_applicable_true_when_alt_credential_some() {
        assert!(ScopeManipulationElicitation.is_applicable(&ctx_with_alt_credential()));
    }

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

    #[test]
    fn probe_uses_alt_credential_headers() {
        let ctx = ctx_with_alt_credential();
        let specs = ScopeManipulationElicitation.generate(&ctx);
        let ProbeSpec::Pair(pair) = &specs[0] else {
            panic!("expected Pair")
        };
        assert_eq!(
            pair.probe.headers.get(http::header::AUTHORIZATION).unwrap(),
            "Bearer under-scoped-token"
        );
    }

    #[test]
    fn technique_strength_is_should() {
        let specs = ScopeManipulationElicitation.generate(&ctx_with_alt_credential());
        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);
    }
}