parlov-elicit 0.1.2

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `StateTransitionElicitation` — probes with a body that triggers an invalid state transition.
//!
//! Sends a JSON body containing a known-invalid field value (from `ctx.state_field`)
//! on both baseline and probe requests. The oracle signal is the server returning
//! 409 (conflict — transition rejected on an existing resource) versus 404 for a
//! nonexistent one. Only applicable when `ctx.state_field` is supplied.

use http::Method;

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

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "state-transition-elicit",
        strategy_name: "State Transition Elicitation",
        risk: RiskLevel::MethodDestructive,
    }
}

/// Elicits existence differentials via an invalid state-transition body payload.
pub struct StateTransitionElicitation;

impl Strategy for StateTransitionElicitation {
    fn id(&self) -> &'static str {
        "state-transition-elicit"
    }

    fn name(&self) -> &'static str {
        "State Transition Elicitation"
    }

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

    fn methods(&self) -> &[Method] {
        &[Method::PATCH, Method::PUT]
    }

    fn is_applicable(&self, ctx: &ScanContext) -> bool {
        ctx.state_field.is_some()
    }

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let Some(sf) = &ctx.state_field else { return vec![] };
        let body = Some(json_body(&[(&sf.field, &sf.value)]));
        let mut specs = Vec::with_capacity(2);
        for method in [Method::PATCH, Method::PUT] {
            let headers =
                clone_headers_with(&ctx.headers, "content-type", "application/json");
            let pair = build_pair(
                ctx,
                method,
                headers.clone(),
                headers,
                body.clone(),
                metadata(),
            );
            specs.push(ProbeSpec::Pair(pair));
        }
        specs
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{StateField};
    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::MethodDestructive,
            known_duplicate: None,
            state_field: None,
            alt_credential: None,
            body_template: None,
        }
    }

    fn make_ctx_with_state() -> ScanContext {
        ScanContext {
            state_field: Some(StateField {
                field: "status".to_string(),
                value: "invalid_state".to_string(),
            }),
            ..make_ctx()
        }
    }

    #[test]
    fn risk_is_method_destructive() {
        assert_eq!(
            StateTransitionElicitation.risk(),
            RiskLevel::MethodDestructive
        );
    }

    #[test]
    fn methods_contains_patch_and_put() {
        let methods = StateTransitionElicitation.methods();
        assert_eq!(methods.len(), 2);
        assert!(methods.contains(&Method::PATCH));
        assert!(methods.contains(&Method::PUT));
    }

    #[test]
    fn is_applicable_false_when_state_field_none() {
        assert!(!StateTransitionElicitation.is_applicable(&make_ctx()));
    }

    #[test]
    fn is_applicable_true_when_state_field_some() {
        assert!(StateTransitionElicitation.is_applicable(&make_ctx_with_state()));
    }

    #[test]
    fn generate_returns_two_items_when_applicable() {
        assert_eq!(
            StateTransitionElicitation.generate(&make_ctx_with_state()).len(),
            2
        );
    }

    #[test]
    fn all_items_are_pair_variants() {
        for spec in &StateTransitionElicitation.generate(&make_ctx_with_state()) {
            assert!(matches!(spec, ProbeSpec::Pair(_)));
        }
    }

    #[test]
    fn probe_body_contains_state_field_as_valid_json() {
        let specs = StateTransitionElicitation.generate(&make_ctx_with_state());
        let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
        let body = pair.probe.body.as_deref().expect("body must be present");
        let parsed: serde_json::Value =
            serde_json::from_slice(body).expect("body must be valid JSON");
        assert_eq!(parsed["status"], "invalid_state");
    }

    #[test]
    fn both_have_application_json_content_type() {
        let specs = StateTransitionElicitation.generate(&make_ctx_with_state());
        let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
        assert_eq!(
            pair.baseline.headers.get("content-type").unwrap(),
            "application/json"
        );
        assert_eq!(
            pair.probe.headers.get("content-type").unwrap(),
            "application/json"
        );
    }
}