use http::Method;
use parlov_core::ProbeDefinition;
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,
}
}
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(),
};
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 methods_contains_put_patch_delete() {
let methods = IfMatchElicitation.methods();
assert_eq!(methods.len(), 3);
assert!(methods.contains(&Method::PUT));
assert!(methods.contains(&Method::PATCH));
assert!(methods.contains(&Method::DELETE));
}
#[test]
fn is_applicable_always_true() {
assert!(IfMatchElicitation.is_applicable(&make_ctx()));
}
#[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 baseline_lacks_if_match_header() {
let specs = IfMatchElicitation.generate(&make_ctx());
let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
assert!(pair.baseline.headers.get("if-match").is_none());
}
#[test]
fn put_pair_probe_has_body() {
let specs = IfMatchElicitation.generate(&make_ctx());
let put_pair = specs.iter().find_map(|s| {
if let ProbeSpec::Pair(p) = s {
if p.probe.method == Method::PUT {
return Some(p);
}
}
None
});
let pair = put_pair.expect("PUT pair must exist");
assert!(pair.probe.body.is_some());
}
#[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
});
let pair = del_pair.expect("DELETE pair must exist");
assert!(pair.probe.body.is_none());
}
}