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;
use crate::ScanContext;
fn metadata() -> StrategyMetadata {
StrategyMetadata {
strategy_id: "scope-manipulation-elicit",
strategy_name: "Scope Manipulation Elicitation",
risk: RiskLevel::Safe,
}
}
fn technique() -> Technique {
Technique {
id: "scope-manipulation",
name: "Scope manipulation credential probe",
oracle_class: OracleClass::Existence,
vector: Vector::StatusCodeDiff,
strength: NormativeStrength::Should,
}
}
pub struct ScopeManipulationElicitation;
impl Strategy for ScopeManipulationElicitation {
fn id(&self) -> &'static str {
"scope-manipulation-elicit"
}
fn name(&self) -> &'static str {
"Scope Manipulation Elicitation"
}
fn risk(&self) -> RiskLevel {
RiskLevel::Safe
}
fn methods(&self) -> &[Method] {
&[Method::GET, Method::HEAD]
}
fn is_applicable(&self, ctx: &ScanContext) -> bool {
ctx.alt_credential.is_some()
}
fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
let alt = match &ctx.alt_credential {
Some(h) => h.clone(),
None => return vec![],
};
let mut specs = Vec::with_capacity(2);
for method in [Method::GET, Method::HEAD] {
let pair = build_pair(
ctx, method,
ctx.headers.clone(), alt.clone(),
None, metadata(), technique(),
);
specs.push(ProbeSpec::Pair(pair));
}
specs
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::{HeaderMap, HeaderValue};
fn make_ctx_no_alt() -> 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_alt() -> ScanContext {
let mut headers = HeaderMap::new();
headers.insert(
http::header::AUTHORIZATION,
HeaderValue::from_static("Bearer token123"),
);
let mut alt = HeaderMap::new();
alt.insert(
http::header::AUTHORIZATION,
HeaderValue::from_static("Bearer under-scoped-token"),
);
ScanContext {
headers,
alt_credential: Some(alt),
..make_ctx_no_alt()
}
}
#[test]
fn risk_is_safe() {
assert_eq!(ScopeManipulationElicitation.risk(), RiskLevel::Safe);
}
#[test]
fn is_applicable_false_when_alt_credential_none() {
assert!(!ScopeManipulationElicitation.is_applicable(&make_ctx_no_alt()));
}
#[test]
fn is_applicable_true_when_alt_credential_some() {
assert!(ScopeManipulationElicitation.is_applicable(&make_ctx_with_alt()));
}
#[test]
fn generate_returns_two_items() {
assert_eq!(ScopeManipulationElicitation.generate(&make_ctx_with_alt()).len(), 2);
}
#[test]
fn probe_uses_alt_credential_headers() {
let ctx = make_ctx_with_alt();
let specs = ScopeManipulationElicitation.generate(&ctx);
let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
assert_eq!(
pair.probe.headers.get(http::header::AUTHORIZATION).unwrap(),
"Bearer under-scoped-token"
);
}
#[test]
fn technique_strength_is_should() {
let specs = ScopeManipulationElicitation.generate(&make_ctx_with_alt());
assert_eq!(specs[0].technique().strength, NormativeStrength::Should);
}
}