use http::Method;
use parlov_core::{NormativeStrength, OracleClass, Technique, Vector};
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,
}
}
fn technique() -> Technique {
Technique {
id: "state-transition",
name: "Invalid state transition probe",
oracle_class: OracleClass::Existence,
vector: Vector::StatusCodeDiff,
strength: NormativeStrength::May,
}
}
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(), technique(),
);
specs.push(ProbeSpec::Pair(pair));
}
specs
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::StateField;
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,
}
}
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 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 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("valid JSON");
assert_eq!(parsed["status"], "invalid_state");
}
#[test]
fn technique_strength_is_may() {
let specs = StateTransitionElicitation.generate(&make_ctx_with_state());
assert_eq!(specs[0].technique().strength, NormativeStrength::May);
}
}