wafrift-oracle 0.2.5

Payload validity oracles for SQL, SSRF, path traversal, command injection, and template injection.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! WAF response oracle.
//!
//! Classifies HTTP responses into [`Verdict`](wafrift_types::Verdict) values using multiple
//! signals: status code, body markers, response time, connection
//! behavior, and HTTP/2 GOAWAY frames.

use crate::calibration::CalibrationSession;
use crate::signal_body_marker::{extract_block_reason, extract_body_signals};
use crate::signal_connection::classify_connection;
use crate::signal_h2_goaway::classify_h2_goaway;
use crate::signal_response_time::classify_response_time;
use crate::signal_status_code::classify_status_code;
use wafrift_types::{BlockReason, ConnectionBehavior, Signal, Verdict};

/// Input context for response classification.
#[derive(Debug, Clone, Default)]
pub struct ResponseContext {
    /// HTTP status code.
    pub status: u16,
    /// Response headers.
    pub headers: Vec<(String, String)>,
    /// Response body bytes.
    pub body: Vec<u8>,
    /// Response time in milliseconds.
    pub response_time_ms: u64,
    /// Connection behavior anomaly.
    pub connection_behavior: Option<ConnectionBehavior>,
    /// HTTP/2 GOAWAY reason (if any).
    pub h2_goaway: Option<String>,
    /// Whether the body is gzip-compressed.
    pub is_gzipped: bool,
}

/// A response oracle that classifies HTTP responses.
#[derive(Debug, Clone, Default)]
pub struct ResponseOracle {
    /// Optional per-target calibration session.
    pub calibration: Option<CalibrationSession>,
}

impl ResponseOracle {
    /// Create a new response oracle with no calibration.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Attach a calibration session.
    pub fn with_calibration(mut self, calibration: CalibrationSession) -> Self {
        self.calibration = Some(calibration);
        self
    }

    /// Classify a response into a [`Verdict`].
    ///
    /// This method is **deterministic**: given identical `ctx` and
    /// `calibration`, it always returns the same verdict.
    pub fn classify(&self, ctx: &ResponseContext) -> Verdict {
        let mut signals = Vec::new();
        let mut competing = Vec::new();

        // ── Status code ──
        let (status_verdict, status_signal) = classify_status_code(ctx.status);
        signals.push(status_signal);

        // ── Body markers ──
        let body_signals = extract_body_signals(&ctx.body, ctx.is_gzipped);
        signals.extend(body_signals.clone());

        // ── Response headers ──
        let header_signals = crate::signal_headers::classify_headers(&ctx.headers);
        signals.extend(header_signals);

        // ── Response time ──
        // Prefer measured benign latency; otherwise default 200ms.
        let baseline_ms = self
            .calibration
            .as_ref()
            .map_or(200, |c| c.benign_latency_ms.unwrap_or(200));
        if let Some(s) = classify_response_time(baseline_ms, ctx.response_time_ms) {
            signals.push(s);
        }

        // ── Connection behavior ──
        if let Some(ref behavior) = ctx.connection_behavior {
            signals.push(classify_connection(behavior.clone()));
        }

        // ── H2 GOAWAY ──
        if let Some(ref reason) = ctx.h2_goaway
            && let Some(s) = classify_h2_goaway(reason)
        {
            signals.push(s);
        }

        // ── Calibration drift ──
        if let Some(ref cal) = self.calibration
            && cal.is_complete()
        {
            let benign_drift = cal.drift_from_benign(ctx.status, ctx.body.len());
            let blocked_drift = cal.drift_from_blocked(ctx.status, ctx.body.len());

            match (benign_drift, blocked_drift) {
                (Some(b), Some(bl)) => {
                    if b.is_closer_than(&bl) {
                        signals.push(Signal::FingerprintDrift("closer to benign baseline".into()));
                    } else if bl.is_closer_than(&b) {
                        signals.push(Signal::FingerprintDrift(
                            "closer to blocked baseline".into(),
                        ));
                    } else {
                        signals.push(Signal::FingerprintDrift(
                            "equidistant from baselines".into(),
                        ));
                    }
                }
                (Some(_), None) => {
                    signals.push(Signal::FingerprintDrift("closer to benign baseline".into()));
                }
                (None, Some(_)) => {
                    signals.push(Signal::FingerprintDrift(
                        "closer to blocked baseline".into(),
                    ));
                }
                _ => {}
            }
        }

        // ── Resolve challenge vs block vs rate-limit from body ──
        let has_challenge = signals.iter().any(|s| {
            matches!(s, Signal::ChallengePlatform(_))
                || matches!(s, Signal::BodyMarker(m) if m.contains("challenge"))
        });
        let has_rate_limit = signals
            .iter()
            .any(|s| matches!(s, Signal::BodyMarker(m) if m.contains("rate-limit")));
        let has_block_marker = body_signals
            .iter()
            .any(|s| matches!(s, Signal::BodyMarker(_)));
        let has_success_marker = body_signals
            .iter()
            .any(|s| matches!(s, Signal::SuccessMarker(_)));

        // If status says allowed but body has block markers, that's a conflict.
        if status_verdict.is_allowed() && has_block_marker {
            competing.push((
                status_verdict.clone(),
                signals
                    .iter()
                    .filter(|s| matches!(s, Signal::StatusCode { .. }))
                    .cloned()
                    .collect(),
            ));
            let reason = extract_block_reason(&ctx.body, ctx.is_gzipped);
            competing.push((
                Verdict::blocked_with_reason(
                    reason.unwrap_or(BlockReason::Unknown),
                    body_signals.clone(),
                ),
                body_signals.clone(),
            ));
        }

        // If status says blocked but body has success markers, that's a conflict.
        if status_verdict.is_blocked() && has_success_marker {
            competing.push((
                status_verdict.clone(),
                signals
                    .iter()
                    .filter(|s| matches!(s, Signal::StatusCode { .. }))
                    .cloned()
                    .collect(),
            ));
            competing.push((
                Verdict::allowed(
                    body_signals
                        .iter()
                        .filter(|s| matches!(s, Signal::SuccessMarker(_)))
                        .cloned()
                        .collect(),
                ),
                body_signals
                    .iter()
                    .filter(|s| matches!(s, Signal::SuccessMarker(_)))
                    .cloned()
                    .collect(),
            ));
        }

        // If 503 with challenge markers → ChallengeRequired
        if ctx.status == 503 && has_challenge {
            let platform = signals.iter().find_map(|s| match s {
                Signal::ChallengePlatform(p) => Some(p.clone()),
                _ => None,
            });
            return Verdict::challenge_required(platform, signals);
        }

        // If 429 or has rate-limit markers → RateLimited
        if ctx.status == 429 || has_rate_limit {
            return Verdict::rate_limited(signals);
        }

        // If conflicting signals were collected, return Ambiguous.
        if !competing.is_empty() {
            return Verdict::Ambiguous {
                competing,
                explanation: format!("status {} conflicts with body markers", ctx.status),
            };
        }

        // Default: attach the full collected signal set (status, body, calibration, …).
        match status_verdict {
            Verdict::Blocked { .. } => {
                let reason = extract_block_reason(&ctx.body, ctx.is_gzipped);
                Verdict::Blocked { reason, signals }
            }
            Verdict::Partial { .. } => {
                let reason = extract_block_reason(&ctx.body, ctx.is_gzipped);
                Verdict::Partial { reason, signals }
            }
            Verdict::Allowed { .. } => Verdict::allowed(signals),
            Verdict::RateLimited { .. } => Verdict::rate_limited(signals),
            Verdict::ServerError { .. } => Verdict::server_error(signals),
            Verdict::ChallengeRequired { platform, .. } => {
                Verdict::challenge_required(platform, signals)
            }
            Verdict::Ambiguous { .. } => status_verdict,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn classify_allowed() {
        let oracle = ResponseOracle::new();
        let ctx = ResponseContext {
            status: 200,
            body: b"welcome".to_vec(),
            ..Default::default()
        };
        let v = oracle.classify(&ctx);
        assert!(v.is_allowed());
    }

    #[test]
    fn classify_blocked() {
        let oracle = ResponseOracle::new();
        let ctx = ResponseContext {
            status: 403,
            body: b"access denied".to_vec(),
            ..Default::default()
        };
        let v = oracle.classify(&ctx);
        assert!(v.is_blocked());
    }

    #[test]
    fn classify_challenge() {
        let oracle = ResponseOracle::new();
        let ctx = ResponseContext {
            status: 503,
            body: b"challenge-platform".to_vec(),
            ..Default::default()
        };
        let v = oracle.classify(&ctx);
        assert!(v.is_challenge());
    }

    #[test]
    fn classify_ambiguous() {
        let oracle = ResponseOracle::new();
        let ctx = ResponseContext {
            status: 200,
            body: b"access denied".to_vec(),
            ..Default::default()
        };
        let v = oracle.classify(&ctx);
        assert!(v.is_ambiguous());
    }

    #[test]
    fn deterministic_classify() {
        let oracle = ResponseOracle::new();
        let ctx = ResponseContext {
            status: 403,
            body: b"blocked".to_vec(),
            ..Default::default()
        };
        let v1 = oracle.classify(&ctx);
        let v2 = oracle.classify(&ctx);
        assert_eq!(v1, v2);
    }

    #[test]
    fn adversarial_empty_body() {
        let oracle = ResponseOracle::new();
        let ctx = ResponseContext {
            status: 403,
            body: vec![],
            ..Default::default()
        };
        let v = oracle.classify(&ctx);
        assert!(v.is_blocked());
    }

    #[test]
    fn adversarial_body_with_both_markers() {
        let oracle = ResponseOracle::new();
        let ctx = ResponseContext {
            status: 200,
            body: b"access denied but login successful".to_vec(),
            ..Default::default()
        };
        let v = oracle.classify(&ctx);
        assert!(v.is_ambiguous());
    }

    #[test]
    fn adversarial_200_with_rst() {
        let oracle = ResponseOracle::new();
        let ctx = ResponseContext {
            status: 200,
            body: b"ok".to_vec(),
            connection_behavior: Some(ConnectionBehavior::TcpReset),
            ..Default::default()
        };
        let v = oracle.classify(&ctx);
        // 200 with RST should still be allowed by status, but connection signal is present
        assert!(v.is_allowed());
        let signals = v.signals();
        assert!(
            signals
                .iter()
                .any(|s| matches!(s, Signal::ConnectionBehavior(ConnectionBehavior::TcpReset)))
        );
    }

    #[test]
    fn adversarial_gzipped_block_page() {
        use flate2::Compression;
        use flate2::write::GzEncoder;
        use std::io::Write;

        let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
        encoder.write_all(b"access denied").unwrap();
        let gzipped = encoder.finish().unwrap();

        let oracle = ResponseOracle::new();
        let ctx = ResponseContext {
            status: 403,
            body: gzipped,
            is_gzipped: true,
            ..Default::default()
        };
        let v = oracle.classify(&ctx);
        assert!(v.is_blocked());
        let signals = v.signals();
        assert!(
            signals
                .iter()
                .any(|s| matches!(s, Signal::BodyMarker(m) if m == "access denied"))
        );
    }

    #[test]
    fn calibration_drift_used() {
        let mut cal = CalibrationSession::default();
        cal.record_benign(200, &[], b"x".repeat(100).as_slice());
        cal.record_blocked(403, &[], b"y".repeat(200).as_slice());

        let oracle = ResponseOracle::new().with_calibration(cal);
        let ctx = ResponseContext {
            status: 200,
            body: b"x".repeat(100),
            ..Default::default()
        };
        let v = oracle.classify(&ctx);
        assert!(v.is_allowed());
        let signals = v.signals();
        assert!(
            signals
                .iter()
                .any(|s| matches!(s, Signal::FingerprintDrift(_)))
        );
    }

    #[test]
    fn calibration_latency_used_for_response_time_signal() {
        let mut cal = CalibrationSession::default();
        cal.record_benign_with_latency(200, &[], b"ok", 50);
        cal.record_blocked(403, &[], b"blocked");

        let oracle = ResponseOracle::new().with_calibration(cal);
        let ctx = ResponseContext {
            status: 200,
            response_time_ms: 500,
            ..Default::default()
        };
        let v = oracle.classify(&ctx);
        let signals = v.signals();
        assert!(signals.iter().any(|s| matches!(
            s,
            Signal::ResponseTimeAnomaly {
                baseline_ms: 50,
                actual_ms: 500
            }
        )));
    }
}