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-state-conflict",
strategy_name: "EMG: POST State Conflict",
risk: RiskLevel::MethodDestructive,
}
}
fn technique() -> Technique {
Technique {
id: "emg-state-conflict",
name: "POST state conflict body differential",
oracle_class: OracleClass::Existence,
vector: Vector::ErrorMessageGranularity,
strength: NormativeStrength::Should,
}
}
pub struct EmgStateConflict;
impl Strategy for EmgStateConflict {
fn id(&self) -> &'static str {
"emg-state-conflict"
}
fn name(&self) -> &'static str {
"EMG: POST State Conflict"
}
fn risk(&self) -> RiskLevel {
RiskLevel::MethodDestructive
}
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_with(&ctx.headers, "content-type", "application/json");
let pair = build_pair(
ctx,
Method::POST,
headers.clone(),
headers,
None,
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/documents/{id}/comments".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: Some(r#"{"text":"test comment"}"#.to_string()),
}
}
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);
}
}