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;
const SPOOFED_CONTENT_LENGTH: &str = "10485760";
fn metadata() -> StrategyMetadata {
StrategyMetadata {
strategy_id: "oversized-body-elicit",
strategy_name: "Oversized Body Elicitation",
risk: RiskLevel::MethodDestructive,
}
}
fn technique() -> Technique {
Technique {
id: "oversized-body",
name: "Oversized Content-Length probe",
oracle_class: OracleClass::Existence,
vector: Vector::StatusCodeDiff,
strength: NormativeStrength::Should,
}
}
pub struct OversizedBodyElicitation;
impl Strategy for OversizedBodyElicitation {
fn id(&self) -> &'static str {
"oversized-body-elicit"
}
fn name(&self) -> &'static str {
"Oversized Body Elicitation"
}
fn risk(&self) -> RiskLevel {
RiskLevel::MethodDestructive
}
fn methods(&self) -> &[Method] {
&[Method::POST, Method::PUT, Method::PATCH]
}
fn is_applicable(&self, _ctx: &ScanContext) -> bool {
true
}
fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
let body = Some(json_body(&[]));
let mut specs = Vec::with_capacity(3);
for method in [Method::POST, Method::PUT, Method::PATCH] {
let baseline_headers =
clone_headers_with(&ctx.headers, "content-type", "application/json");
let probe_headers = clone_headers_with(
&clone_headers_with(&ctx.headers, "content-type", "application/json"),
"content-length",
SPOOFED_CONTENT_LENGTH,
);
let pair = build_pair(
ctx, method, baseline_headers, probe_headers,
body.clone(), metadata(), technique(),
);
specs.push(ProbeSpec::Pair(pair));
}
specs
}
}
#[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::MethodDestructive,
known_duplicate: None,
state_field: None,
alt_credential: None,
body_template: None,
}
}
#[test]
fn risk_is_method_destructive() {
assert_eq!(OversizedBodyElicitation.risk(), RiskLevel::MethodDestructive);
}
#[test]
fn generate_returns_three_items() {
assert_eq!(OversizedBodyElicitation.generate(&make_ctx()).len(), 3);
}
#[test]
fn probe_has_spoofed_content_length() {
let specs = OversizedBodyElicitation.generate(&make_ctx());
let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
assert_eq!(pair.probe.headers.get("content-length").unwrap(), "10485760");
}
#[test]
fn technique_strength_is_should() {
let specs = OversizedBodyElicitation.generate(&make_ctx());
assert_eq!(specs[0].technique().strength, NormativeStrength::Should);
}
}