use http::Method;
use parlov_core::{
NormativeStrength, OracleClass, ProbeDefinition, Technique, Vector,
};
use crate::strategy::Strategy;
use crate::types::{ProbePair, ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::{clone_headers_with, json_body, substitute_url};
use crate::ScanContext;
fn metadata() -> StrategyMetadata {
StrategyMetadata {
strategy_id: "if-match-elicit",
strategy_name: "If-Match Elicitation",
risk: RiskLevel::MethodDestructive,
}
}
fn technique() -> Technique {
Technique {
id: "if-match",
name: "If-Match precondition probe",
oracle_class: OracleClass::Existence,
vector: Vector::StatusCodeDiff,
strength: NormativeStrength::Must,
}
}
pub struct IfMatchElicitation;
impl Strategy for IfMatchElicitation {
fn id(&self) -> &'static str {
"if-match-elicit"
}
fn name(&self) -> &'static str {
"If-Match Elicitation"
}
fn risk(&self) -> RiskLevel {
RiskLevel::MethodDestructive
}
fn methods(&self) -> &[Method] {
&[Method::PUT, Method::PATCH, Method::DELETE]
}
fn is_applicable(&self, _ctx: &ScanContext) -> bool {
true
}
fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
let mut specs = Vec::with_capacity(3);
let baseline_url = substitute_url(&ctx.target, &ctx.baseline_id);
let probe_url = substitute_url(&ctx.target, &ctx.probe_id);
let probe_headers = clone_headers_with(&ctx.headers, "if-match", "\"bogus-etag\"");
for method in [Method::PUT, Method::PATCH, Method::DELETE] {
let body = if method == Method::DELETE {
None
} else {
Some(json_body(&[]))
};
let pair = ProbePair {
baseline: ProbeDefinition {
url: baseline_url.clone(),
method: method.clone(),
headers: ctx.headers.clone(),
body: body.clone(),
},
probe: ProbeDefinition {
url: probe_url.clone(),
method,
headers: probe_headers.clone(),
body,
},
metadata: metadata(),
technique: technique(),
};
specs.push(ProbeSpec::Pair(pair));
}
specs
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::{HeaderMap, Method};
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 risk_is_method_destructive() {
assert_eq!(IfMatchElicitation.risk(), RiskLevel::MethodDestructive);
}
#[test]
fn generate_returns_three_items() {
assert_eq!(IfMatchElicitation.generate(&make_ctx()).len(), 3);
}
#[test]
fn probe_has_if_match_bogus_etag() {
let specs = IfMatchElicitation.generate(&make_ctx());
let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
assert_eq!(pair.probe.headers.get("if-match").unwrap(), "\"bogus-etag\"");
}
#[test]
fn delete_pair_probe_has_no_body() {
let specs = IfMatchElicitation.generate(&make_ctx());
let del_pair = specs.iter().find_map(|s| {
if let ProbeSpec::Pair(p) = s {
if p.probe.method == Method::DELETE { return Some(p); }
}
None
});
assert!(del_pair.expect("DELETE pair must exist").probe.body.is_none());
}
#[test]
fn technique_strength_is_must() {
let specs = IfMatchElicitation.generate(&make_ctx());
assert_eq!(specs[0].technique().strength, NormativeStrength::Must);
}
}