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: "cp-if-range",
strategy_name: "Cache Probe: If-Range",
risk: RiskLevel::Safe,
}
}
fn technique() -> Technique {
Technique {
id: "cp-if-range",
name: "If-Range validator-gated range request",
oracle_class: OracleClass::Existence,
vector: Vector::CacheProbing,
strength: NormativeStrength::Should,
}
}
pub struct CpIfRange;
impl Strategy for CpIfRange {
fn id(&self) -> &'static str {
"cp-if-range"
}
fn name(&self) -> &'static str {
"Cache Probe: If-Range"
}
fn risk(&self) -> RiskLevel {
RiskLevel::Safe
}
fn methods(&self) -> &[Method] {
&[Method::GET]
}
fn is_applicable(&self, _ctx: &ScanContext) -> bool {
true
}
fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
let hdrs = clone_headers_with(&ctx.headers, "if-range", "\"nonexistent-etag\"");
let hdrs = clone_headers_with(&hdrs, "range", "bytes=0-0");
let pair = build_pair(
ctx,
Method::GET,
hdrs.clone(),
hdrs,
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/users/{id}".to_string(),
baseline_id: "1001".to_string(),
probe_id: "9999".to_string(),
headers: HeaderMap::new(),
max_risk: RiskLevel::Safe,
known_duplicate: None,
state_field: None,
alt_credential: None,
body_template: None,
}
}
#[test]
fn generates_correct_technique_vector() {
let specs = CpIfRange.generate(&make_ctx());
assert_eq!(specs[0].technique().vector, Vector::CacheProbing);
}
#[test]
fn generates_correct_technique_strength() {
let specs = CpIfRange.generate(&make_ctx());
assert_eq!(specs[0].technique().strength, NormativeStrength::Should);
}
#[test]
fn generates_probe_with_correct_headers() {
let specs = CpIfRange.generate(&make_ctx());
let ProbeSpec::Pair(pair) = &specs[0] else {
panic!("expected Pair variant")
};
assert_eq!(
pair.probe.headers.get("if-range").unwrap(),
"\"nonexistent-etag\""
);
assert_eq!(pair.probe.headers.get("range").unwrap(), "bytes=0-0");
}
#[test]
fn is_always_applicable() {
assert!(CpIfRange.is_applicable(&make_ctx()));
}
#[test]
fn risk_is_safe() {
assert_eq!(CpIfRange.risk(), RiskLevel::Safe);
}
}