use http::Method;
use parlov_core::{
always_applicable, NormativeStrength, OracleClass, SignalSurface, Technique, Vector,
};
use crate::strategy::Strategy;
use crate::types::{ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::build_pair;
use crate::ScanContext;
static METADATA: StrategyMetadata = StrategyMetadata {
strategy_id: "low-privilege-elicit",
strategy_name: "Low Privilege Elicitation",
risk: RiskLevel::Safe,
};
static TECHNIQUE: Technique = Technique {
id: "low-privilege",
name: "Low-privilege credential probe",
oracle_class: OracleClass::Existence,
vector: Vector::StatusCodeDiff,
strength: NormativeStrength::Should,
normalization_weight: None,
inverted_signal_weight: None,
method_relevant: false,
parser_relevant: false,
applicability: always_applicable,
contradiction_surface: SignalSurface::Status,
};
pub struct LowPrivilegeElicitation;
impl Strategy for LowPrivilegeElicitation {
fn metadata(&self) -> &'static StrategyMetadata {
&METADATA
}
fn technique_def(&self) -> &'static Technique {
&TECHNIQUE
}
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.clone(),
TECHNIQUE,
);
specs.push(ProbeSpec::Pair(pair));
}
specs
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::{ctx_with_auth, minimal_ctx};
#[test]
fn risk_is_safe() {
assert_eq!(LowPrivilegeElicitation.risk(), RiskLevel::Safe);
}
#[test]
fn is_applicable_false_without_auth() {
assert!(!LowPrivilegeElicitation.is_applicable(&minimal_ctx()));
}
#[test]
fn is_applicable_true_with_auth() {
assert!(LowPrivilegeElicitation.is_applicable(&ctx_with_auth()));
}
#[test]
fn generate_returns_two_items() {
assert_eq!(LowPrivilegeElicitation.generate(&ctx_with_auth()).len(), 2);
}
#[test]
fn both_sides_carry_authorization_header() {
let specs = LowPrivilegeElicitation.generate(&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 technique_strength_is_should() {
let specs = LowPrivilegeElicitation.generate(&ctx_with_auth());
assert_eq!(specs[0].technique().strength, NormativeStrength::Should);
}
#[test]
fn normalization_weight_is_none() {
assert_eq!(TECHNIQUE.normalization_weight, None);
}
#[test]
fn inverted_signal_weight_is_none() {
assert_eq!(TECHNIQUE.inverted_signal_weight, None);
}
}