use http::Method;
use crate::strategy::Strategy;
use crate::types::{ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::build_pair;
use crate::ScanContext;
fn metadata() -> StrategyMetadata {
StrategyMetadata {
strategy_id: "low-privilege-elicit",
strategy_name: "Low Privilege Elicitation",
risk: RiskLevel::Safe,
}
}
pub struct LowPrivilegeElicitation;
impl Strategy for LowPrivilegeElicitation {
fn id(&self) -> &'static str {
"low-privilege-elicit"
}
fn name(&self) -> &'static str {
"Low Privilege Elicitation"
}
fn risk(&self) -> RiskLevel {
RiskLevel::Safe
}
fn methods(&self) -> &[Method] {
&[Method::GET, Method::HEAD]
}
fn is_applicable(&self, ctx: &ScanContext) -> bool {
ctx.headers.contains_key(http::header::AUTHORIZATION)
}
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::Pair(pair));
}
specs
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::{HeaderMap, HeaderValue, Method};
fn make_ctx_no_auth() -> 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,
}
}
fn make_ctx_with_auth() -> ScanContext {
let mut headers = HeaderMap::new();
headers.insert(
http::header::AUTHORIZATION,
HeaderValue::from_static("Bearer token123"),
);
ScanContext { headers, ..make_ctx_no_auth() }
}
#[test]
fn risk_is_safe() {
assert_eq!(LowPrivilegeElicitation.risk(), RiskLevel::Safe);
}
#[test]
fn is_applicable_false_without_auth_header() {
assert!(!LowPrivilegeElicitation.is_applicable(&make_ctx_no_auth()));
}
#[test]
fn is_applicable_true_with_auth_header() {
assert!(LowPrivilegeElicitation.is_applicable(&make_ctx_with_auth()));
}
#[test]
fn generate_returns_two_items() {
let specs = LowPrivilegeElicitation.generate(&make_ctx_with_auth());
assert_eq!(specs.len(), 2);
}
#[test]
fn all_items_are_pair_variants() {
let specs = LowPrivilegeElicitation.generate(&make_ctx_with_auth());
for spec in &specs {
assert!(matches!(spec, ProbeSpec::Pair(_)));
}
}
#[test]
fn both_sides_carry_authorization_header() {
let specs = LowPrivilegeElicitation.generate(&make_ctx_with_auth());
let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
assert!(pair.baseline.headers.get(http::header::AUTHORIZATION).is_some());
assert!(pair.probe.headers.get(http::header::AUTHORIZATION).is_some());
}
#[test]
fn methods_contains_get_and_head() {
let methods = LowPrivilegeElicitation.methods();
assert_eq!(methods.len(), 2);
assert!(methods.contains(&Method::GET));
assert!(methods.contains(&Method::HEAD));
}
}