parlov-elicit 0.1.2

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `LongUriElicitation` — pads the probe URL to ~16 KB.
//!
//! Appends `?_pad=AAAA…` (or `&_pad=AAAA…` when a query string already exists)
//! to the resolved probe URL until the total length reaches 16 384 bytes. Servers
//! with different URI length limits for existing vs nonexistent resources will
//! return different error codes (e.g. 414 vs 404), leaking existence through
//! the error path.

use http::Method;
use parlov_core::ProbeDefinition;

use crate::strategy::Strategy;
use crate::types::{ProbePair, ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::substitute_url;
use crate::ScanContext;

/// Target padded URL length in bytes.
const TARGET_LEN: usize = 16_384;

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "long-uri-elicit",
        strategy_name: "Long URI Elicitation",
        risk: RiskLevel::Safe,
    }
}

/// Pads `url` to at least `TARGET_LEN` bytes by appending `?_pad=AAA…` or `&_pad=AAA…`.
///
/// Uses `?` when no query string exists, `&` otherwise. The pad value is `A`
/// repeated until the total URL length reaches the target.
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());
    // Always pad at least one `A` so the parameter is never empty.
    let pad_len = pad_needed.max(1);
    let padding = "A".repeat(pad_len);
    format!("{prefix}{padding}")
}

/// Elicits existence differentials by padding the probe URL to ~16 KB.
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(),
            };
            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,
        }
    }

    fn make_ctx_with_query() -> ScanContext {
        ScanContext {
            target: "https://api.example.com/users/{id}?existing=yes".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() {
        let specs = LongUriElicitation.generate(&make_ctx());
        assert_eq!(specs.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,
            "probe URL length {} is below target {}",
            pair.probe.url.len(),
            TARGET_LEN
        );
    }

    #[test]
    fn pad_uses_question_mark_without_existing_query() {
        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.contains("?_pad="),
            "must use ?_pad= when no existing query string, got: {}",
            &pair.probe.url[..60.min(pair.probe.url.len())]
        );
    }

    #[test]
    fn pad_uses_ampersand_with_existing_query() {
        let specs = LongUriElicitation.generate(&make_ctx_with_query());
        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.contains("&_pad="),
            "must use &_pad= when query string exists, got: {}",
            &pair.probe.url[..60.min(pair.probe.url.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"
        );
    }
}