use http::Method;
use parlov_core::{
NormativeStrength, OracleClass, ProbeDefinition, Technique, Vector,
};
use crate::strategy::Strategy;
use crate::types::{ProbePair, ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::substitute_url;
use crate::ScanContext;
const TARGET_LEN: usize = 16_384;
fn metadata() -> StrategyMetadata {
StrategyMetadata {
strategy_id: "long-uri-elicit",
strategy_name: "Long URI Elicitation",
risk: RiskLevel::Safe,
}
}
fn technique() -> Technique {
Technique {
id: "long-uri",
name: "Long URI padding",
oracle_class: OracleClass::Existence,
vector: Vector::StatusCodeDiff,
strength: NormativeStrength::Should,
}
}
fn pad_url(url: &str) -> String {
let separator = if url.contains('?') { '&' } else { '?' };
let prefix = format!("{url}{separator}_pad=");
let pad_needed = TARGET_LEN.saturating_sub(prefix.len());
let pad_len = pad_needed.max(1);
let padding = "A".repeat(pad_len);
format!("{prefix}{padding}")
}
pub struct LongUriElicitation;
impl Strategy for LongUriElicitation {
fn id(&self) -> &'static str {
"long-uri-elicit"
}
fn name(&self) -> &'static str {
"Long URI Elicitation"
}
fn risk(&self) -> RiskLevel {
RiskLevel::Safe
}
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);
let baseline_url = substitute_url(&ctx.target, &ctx.baseline_id);
let probe_url_base = substitute_url(&ctx.target, &ctx.probe_id);
let probe_url = pad_url(&probe_url_base);
for method in [Method::GET, Method::HEAD] {
let pair = ProbePair {
baseline: ProbeDefinition {
url: baseline_url.clone(),
method: method.clone(),
headers: ctx.headers.clone(),
body: None,
},
probe: ProbeDefinition {
url: probe_url.clone(),
method,
headers: ctx.headers.clone(),
body: None,
},
metadata: metadata(),
technique: technique(),
};
specs.push(ProbeSpec::Pair(pair));
}
specs
}
}
#[cfg(test)]
mod tests {
use super::*;
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::Safe,
known_duplicate: None,
state_field: None,
alt_credential: None,
body_template: None,
}
}
#[test]
fn risk_is_safe() {
assert_eq!(LongUriElicitation.risk(), RiskLevel::Safe);
}
#[test]
fn generate_returns_two_items() {
assert_eq!(LongUriElicitation.generate(&make_ctx()).len(), 2);
}
#[test]
fn probe_url_length_meets_target() {
let specs = LongUriElicitation.generate(&make_ctx());
let pair = specs.iter().find_map(|s| {
if let ProbeSpec::Pair(p) = s {
if p.probe.method == Method::GET { return Some(p); }
}
None
});
let pair = pair.expect("GET pair must exist");
assert!(pair.probe.url.len() >= TARGET_LEN);
}
#[test]
fn baseline_url_is_unmodified() {
let specs = LongUriElicitation.generate(&make_ctx());
let pair = specs.iter().find_map(|s| {
if let ProbeSpec::Pair(p) = s {
if p.baseline.method == Method::GET { return Some(p); }
}
None
});
let pair = pair.expect("GET pair must exist");
assert_eq!(pair.baseline.url, "https://api.example.com/users/1001");
}
#[test]
fn technique_strength_is_should() {
let specs = LongUriElicitation.generate(&make_ctx());
assert_eq!(specs[0].technique().strength, NormativeStrength::Should);
}
}