parlov-elicit 0.5.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `EmgFkViolation` -- DELETE foreign key violation body differential.
//!
//! Sends a DELETE 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 resources (with FK constraints) vs nonexistent ones, 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;

static METADATA: StrategyMetadata = StrategyMetadata {
    strategy_id: "emg-fk-violation",
    strategy_name: "EMG: DELETE FK Violation",
    risk: RiskLevel::MethodDestructive,
};

static TECHNIQUE: Technique = Technique {
    id: "emg-fk-violation",
    name: "DELETE foreign key violation 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 DELETE FK constraint body differences.
pub struct EmgFkViolation;

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

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

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

    fn is_applicable(&self, _ctx: &ScanContext) -> bool {
        true
    }

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let pair = build_pair(
            ctx,
            Method::DELETE,
            ctx.headers.clone(),
            ctx.headers.clone(),
            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/organizations/{id}".to_owned(),
            ..ctx_method_destructive()
        }
    }

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

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

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

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

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