use http::Method;
use crate::strategy::Strategy;
use crate::types::{BurstSpec, ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::build_pair;
use crate::ScanContext;
const BURST_COUNT: usize = 100;
fn metadata() -> StrategyMetadata {
StrategyMetadata {
strategy_id: "rate-limit-burst-elicit",
strategy_name: "Rate Limit Burst Elicitation",
risk: RiskLevel::OperationDestructive,
}
}
pub struct RateLimitBurstElicitation;
impl Strategy for RateLimitBurstElicitation {
fn id(&self) -> &'static str {
"rate-limit-burst-elicit"
}
fn name(&self) -> &'static str {
"Rate Limit Burst Elicitation"
}
fn risk(&self) -> RiskLevel {
RiskLevel::OperationDestructive
}
fn methods(&self) -> &[Method] {
&[Method::GET, Method::HEAD]
}
fn is_applicable(&self, _ctx: &ScanContext) -> bool {
true
}
fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
let mut specs = Vec::with_capacity(2);
for method in [Method::GET, Method::HEAD] {
let pair = build_pair(
ctx,
method,
ctx.headers.clone(),
ctx.headers.clone(),
None,
metadata(),
);
specs.push(ProbeSpec::Burst(BurstSpec {
baseline: pair.baseline,
probe: pair.probe,
burst_count: BURST_COUNT,
metadata: pair.metadata,
}));
}
specs
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::substitute_url;
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,
}
}
#[test]
fn risk_is_operation_destructive() {
assert_eq!(
RateLimitBurstElicitation.risk(),
RiskLevel::OperationDestructive
);
}
#[test]
fn methods_contains_get_and_head() {
let methods = RateLimitBurstElicitation.methods();
assert_eq!(methods.len(), 2);
assert!(methods.contains(&Method::GET));
assert!(methods.contains(&Method::HEAD));
}
#[test]
fn is_applicable_always_true() {
assert!(RateLimitBurstElicitation.is_applicable(&make_ctx()));
}
#[test]
fn generate_returns_two_items() {
assert_eq!(RateLimitBurstElicitation.generate(&make_ctx()).len(), 2);
}
#[test]
fn all_items_are_burst_variants() {
for spec in &RateLimitBurstElicitation.generate(&make_ctx()) {
assert!(matches!(spec, ProbeSpec::Burst(_)));
}
}
#[test]
fn burst_count_is_100() {
let specs = RateLimitBurstElicitation.generate(&make_ctx());
for spec in &specs {
let ProbeSpec::Burst(burst) = spec else { panic!("expected Burst") };
assert_eq!(burst.burst_count, 100);
}
}
#[test]
fn baseline_url_uses_baseline_id() {
let ctx = make_ctx();
let specs = RateLimitBurstElicitation.generate(&ctx);
let ProbeSpec::Burst(burst) = &specs[0] else { panic!("expected Burst") };
assert_eq!(
burst.baseline.url,
substitute_url(&ctx.target, &ctx.baseline_id)
);
}
#[test]
fn probe_url_uses_probe_id() {
let ctx = make_ctx();
let specs = RateLimitBurstElicitation.generate(&ctx);
let ProbeSpec::Burst(burst) = &specs[0] else { panic!("expected Burst") };
assert_eq!(
burst.probe.url,
substitute_url(&ctx.target, &ctx.probe_id)
);
}
}