parlov-elicit 0.5.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `EmgStateConflict` -- POST state conflict body differential.
//!
//! Sends a POST with the body template to both baseline and probe URLs.
//! Per RFC 9110 S15.5.10, 409 Conflict SHOULD include enough information
//! for the user to recognize the source of the conflict. When the server
//! returns different error bodies for existing (conflicting) vs nonexistent
//! resources, existence is revealed through body content.

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

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

static METADATA: StrategyMetadata = StrategyMetadata {
    strategy_id: "emg-state-conflict",
    strategy_name: "EMG: POST State Conflict",
    risk: RiskLevel::MethodDestructive,
};

static TECHNIQUE: Technique = Technique {
    id: "emg-state-conflict",
    name: "POST state conflict body differential",
    oracle_class: OracleClass::Existence,
    vector: Vector::ErrorMessageGranularity,
    strength: NormativeStrength::Should,
    normalization_weight: None,
    inverted_signal_weight: None,
    method_relevant: false,
    parser_relevant: false,
    applicability: always_applicable,
    contradiction_surface: SignalSurface::Body,
};

/// Elicits existence differentials via POST state conflict body content differences.
pub struct EmgStateConflict;

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

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

    fn methods(&self) -> &[Method] {
        &[Method::POST]
    }

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

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let headers = clone_headers_static(&ctx.headers, "content-type", "application/json");
        let pair = build_pair(
            ctx,
            Method::POST,
            headers.clone(),
            headers,
            None,
            METADATA.clone(),
            TECHNIQUE,
        );
        vec![ProbeSpec::Pair(pair)]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::ctx_method_destructive;

    fn make_ctx() -> ScanContext {
        ScanContext {
            target: "https://api.example.com/documents/{id}/comments".to_owned(),
            body_template: Some(r#"{"text":"test comment"}"#.to_owned()),
            ..ctx_method_destructive()
        }
    }

    fn make_ctx_no_body() -> ScanContext {
        ScanContext {
            body_template: None,
            ..make_ctx()
        }
    }

    #[test]
    fn generates_correct_technique_vector() {
        let specs = EmgStateConflict.generate(&make_ctx());
        assert_eq!(specs[0].technique().vector, Vector::ErrorMessageGranularity);
    }

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

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

    #[test]
    fn not_applicable_without_body_template() {
        assert!(!EmgStateConflict.is_applicable(&make_ctx_no_body()));
    }

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

    #[test]
    fn generates_post_method() {
        let specs = EmgStateConflict.generate(&make_ctx());
        let ProbeSpec::Pair(pair) = &specs[0] else {
            panic!("expected Pair variant")
        };
        assert_eq!(pair.baseline.method, Method::POST);
        assert_eq!(pair.probe.method, Method::POST);
    }
}