use bytes::Bytes;
use http::Method;
use parlov_core::{NormativeStrength, OracleClass, Technique, Vector};
use crate::context::ScanContext;
use crate::strategy::Strategy;
use crate::types::{ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::{build_pair, clone_headers_with};
fn metadata() -> StrategyMetadata {
StrategyMetadata {
strategy_id: "emg-schema-validation-patch",
strategy_name: "EMG: PATCH Schema Validation",
risk: RiskLevel::MethodDestructive,
}
}
fn technique() -> Technique {
Technique {
id: "emg-schema-validation-patch",
name: "PATCH schema validation order differential",
oracle_class: OracleClass::Existence,
vector: Vector::ErrorMessageGranularity,
strength: NormativeStrength::May,
}
}
pub struct EmgSchemaValidationPatch;
impl Strategy for EmgSchemaValidationPatch {
fn id(&self) -> &'static str {
"emg-schema-validation-patch"
}
fn name(&self) -> &'static str {
"EMG: PATCH Schema Validation"
}
fn risk(&self) -> RiskLevel {
RiskLevel::MethodDestructive
}
fn methods(&self) -> &[Method] {
&[Method::PATCH]
}
fn is_applicable(&self, _ctx: &ScanContext) -> bool {
true
}
fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
let headers = clone_headers_with(&ctx.headers, "content-type", "application/json");
let body = Some(Bytes::from_static(b"{\"email\":[\"invalid_type\"]}"));
let pair = build_pair(
ctx,
Method::PATCH,
headers.clone(),
headers,
body,
metadata(),
technique(),
);
vec![ProbeSpec::Pair(pair)]
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::HeaderMap;
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::MethodDestructive,
known_duplicate: None,
state_field: None,
alt_credential: None,
body_template: None,
}
}
#[test]
fn generates_correct_technique_vector() {
let specs = EmgSchemaValidationPatch.generate(&make_ctx());
assert_eq!(specs[0].technique().vector, Vector::ErrorMessageGranularity);
}
#[test]
fn generates_correct_technique_strength() {
let specs = EmgSchemaValidationPatch.generate(&make_ctx());
assert_eq!(specs[0].technique().strength, NormativeStrength::May);
}
#[test]
fn is_always_applicable() {
assert!(EmgSchemaValidationPatch.is_applicable(&make_ctx()));
}
#[test]
fn risk_is_method_destructive() {
assert_eq!(EmgSchemaValidationPatch.risk(), RiskLevel::MethodDestructive);
}
#[test]
fn generates_patch_method() {
let specs = EmgSchemaValidationPatch.generate(&make_ctx());
let ProbeSpec::Pair(pair) = &specs[0] else {
panic!("expected Pair variant")
};
assert_eq!(pair.baseline.method, Method::PATCH);
assert_eq!(pair.probe.method, Method::PATCH);
}
#[test]
fn has_content_type_json() {
let specs = EmgSchemaValidationPatch.generate(&make_ctx());
let ProbeSpec::Pair(pair) = &specs[0] else {
panic!("expected Pair variant")
};
assert_eq!(pair.baseline.headers.get("content-type").unwrap(), "application/json");
assert_eq!(pair.probe.headers.get("content-type").unwrap(), "application/json");
}
#[test]
fn has_malformed_body() {
let specs = EmgSchemaValidationPatch.generate(&make_ctx());
let ProbeSpec::Pair(pair) = &specs[0] else {
panic!("expected Pair variant")
};
let body = pair.baseline.body.as_ref().expect("body must be present");
let text = std::str::from_utf8(body).expect("body must be valid UTF-8");
assert!(text.contains("email"), "body must contain 'email': {text}");
}
}