use http::Method;
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: "uniqueness-elicit",
strategy_name: "Uniqueness Elicitation",
risk: RiskLevel::OperationDestructive,
}
}
pub struct UniquenessElicitation;
impl Strategy for UniquenessElicitation {
fn id(&self) -> &'static str {
"uniqueness-elicit"
}
fn name(&self) -> &'static str {
"Uniqueness Elicitation"
}
fn risk(&self) -> RiskLevel {
RiskLevel::OperationDestructive
}
fn methods(&self) -> &[Method] {
&[Method::POST, Method::PUT]
}
fn is_applicable(&self, ctx: &ScanContext) -> bool {
ctx.known_duplicate.is_some()
}
fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
let Some(kd) = &ctx.known_duplicate else { return vec![] };
let body = Some(json_body(&[(&kd.field, &kd.value)]));
let headers = clone_headers_with(&ctx.headers, "content-type", "application/json");
let mut specs = Vec::with_capacity(2);
for method in [Method::POST, Method::PUT] {
let pair = build_pair(
ctx,
method,
headers.clone(),
headers.clone(),
body.clone(),
metadata(),
);
specs.push(ProbeSpec::Pair(pair));
}
specs
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::KnownDuplicate;
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::OperationDestructive,
known_duplicate: None,
state_field: None,
alt_credential: None,
body_template: None,
}
}
fn make_ctx_with_duplicate() -> ScanContext {
ScanContext {
known_duplicate: Some(KnownDuplicate {
field: "email".to_string(),
value: "alice@example.com".to_string(),
}),
..make_ctx()
}
}
#[test]
fn risk_is_operation_destructive() {
assert_eq!(UniquenessElicitation.risk(), RiskLevel::OperationDestructive);
}
#[test]
fn methods_contains_post_and_put() {
let methods = UniquenessElicitation.methods();
assert_eq!(methods.len(), 2);
assert!(methods.contains(&Method::POST));
assert!(methods.contains(&Method::PUT));
}
#[test]
fn is_applicable_false_when_known_duplicate_none() {
assert!(!UniquenessElicitation.is_applicable(&make_ctx()));
}
#[test]
fn is_applicable_true_when_known_duplicate_some() {
assert!(UniquenessElicitation.is_applicable(&make_ctx_with_duplicate()));
}
#[test]
fn generate_returns_two_items_when_applicable() {
assert_eq!(
UniquenessElicitation.generate(&make_ctx_with_duplicate()).len(),
2
);
}
#[test]
fn all_items_are_pair_variants() {
for spec in &UniquenessElicitation.generate(&make_ctx_with_duplicate()) {
assert!(matches!(spec, ProbeSpec::Pair(_)));
}
}
#[test]
fn probe_body_contains_duplicate_field_as_valid_json() {
let specs = UniquenessElicitation.generate(&make_ctx_with_duplicate());
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("body must be valid JSON");
assert_eq!(parsed["email"], "alice@example.com");
}
#[test]
fn both_have_application_json_content_type() {
let specs = UniquenessElicitation.generate(&make_ctx_with_duplicate());
let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
assert_eq!(
pair.baseline.headers.get("content-type").unwrap(),
"application/json"
);
assert_eq!(
pair.probe.headers.get("content-type").unwrap(),
"application/json"
);
}
}