parlov-elicit 0.4.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `EmgSchemaValidationPut` -- PUT schema validation order differential.
//!
//! Sends a malformed JSON body (`{"email":["invalid_type"]}`) via PUT.
//! If the server resolves the resource before validating the schema, an
//! existing resource returns a schema error while a nonexistent one returns
//! 404 or 201, revealing existence through validation order.

use bytes::Bytes;
use http::Method;
use parlov_core::{NormativeStrength, OracleClass, Technique, Vector};

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

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "emg-schema-validation-put",
        strategy_name: "EMG: PUT Schema Validation",
        risk: RiskLevel::MethodDestructive,
    }
}

fn technique() -> Technique {
    Technique {
        id: "emg-schema-validation-put",
        name: "PUT schema validation order differential",
        oracle_class: OracleClass::Existence,
        vector: Vector::ErrorMessageGranularity,
        strength: NormativeStrength::May,
    }
}

/// Elicits existence differentials via PUT schema validation ordering.
pub struct EmgSchemaValidationPut;

impl Strategy for EmgSchemaValidationPut {
    fn id(&self) -> &'static str {
        "emg-schema-validation-put"
    }

    fn name(&self) -> &'static str {
        "EMG: PUT Schema Validation"
    }

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

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

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

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let headers = clone_headers_with(&ctx.headers, "content-type", "application/json");
        let body = Some(Bytes::from_static(b"{\"email\":[\"invalid_type\"]}"));
        let pair = build_pair(
            ctx,
            Method::PUT,
            headers.clone(),
            headers,
            body,
            metadata(),
            technique(),
        );
        vec![ProbeSpec::Pair(pair)]
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use http::HeaderMap;

    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,
        }
    }

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

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

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

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

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

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

    #[test]
    fn has_malformed_body() {
        let specs = EmgSchemaValidationPut.generate(&make_ctx());
        let ProbeSpec::Pair(pair) = &specs[0] else {
            panic!("expected Pair variant")
        };
        let body = pair.baseline.body.as_ref().expect("body must be present");
        let text = std::str::from_utf8(body).expect("body must be valid UTF-8");
        assert!(text.contains("email"), "body must contain 'email': {text}");
    }
}