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
//! Pipeline composition grammar.
//!
//! Defines valid partial orderings of evasion techniques:
//! - Encoding happens at the payload layer.
//! - Grammar mutations happen at the payload-semantic layer.
//! - Content-Type switching happens at the HTTP representation layer.
//! - Header obfuscation happens at the HTTP transport layer.
//! - Smuggling and H2 evasion happen at the HTTP framing layer.
//!
/// A layer in the evasion stack.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EvasionLayer {
/// Payload encoding (URL, unicode, etc.).
Encoding,
/// Grammar-aware payload mutation.
Grammar,
/// Content-Type representation switch.
ContentType,
/// HTTP header obfuscation.
Header,
/// Request smuggling.
Smuggling,
/// HTTP/2 frame manipulation.
H2,
/// Body-size inspection bypass: pre-pend N bytes of inert filler so
/// the malicious payload sits past the cloud-WAF inspection window
/// (Cloudflare Pro 8 KB, AWS WAF 16 KB, Akamai 8 KB). Runs LAST in
/// the pipeline because it operates on the assembled body bytes —
/// any layer that re-builds the body (Encoding, `ContentType`,
/// Smuggling) must complete first.
BodyPadding,
}
impl EvasionLayer {
/// Returns the set of layers that must come *before* this layer.
#[must_use]
pub fn prerequisites(&self) -> &'static [EvasionLayer] {
match self {
Self::Encoding => &[Self::Grammar],
Self::Grammar => &[],
Self::ContentType => &[Self::Encoding, Self::Grammar],
Self::Header => &[],
Self::Smuggling => &[Self::ContentType, Self::Header],
Self::H2 => &[Self::Header],
// BodyPadding mutates the assembled body bytes, so any
// layer that re-builds the body must be done first.
Self::BodyPadding => &[Self::Encoding, Self::ContentType, Self::Grammar],
}
}
/// Returns true if `other` is allowed to appear before `self`.
#[must_use]
pub fn can_follow(&self, other: EvasionLayer) -> bool {
self.prerequisites().contains(&other) || *self == other
}
}
/// Validate that a sequence of layers respects the composition grammar.
#[must_use]
pub fn is_valid_sequence(layers: &[EvasionLayer]) -> bool {
for (i, layer) in layers.iter().enumerate() {
let prereqs = layer.prerequisites();
for prereq in prereqs {
if !layers[..i].contains(prereq) {
return false;
}
}
}
true
}
/// Build a corrected sequence by topologically sorting layers.
pub fn canonicalize(layers: &mut [EvasionLayer]) {
// Simple bubble-sort-like reordering that respects prerequisites.
let mut changed = true;
while changed {
changed = false;
for i in 0..layers.len().saturating_sub(1) {
let a = layers[i];
let b = layers[i + 1];
// If b requires a and a is not before b, swap
if b.prerequisites().contains(&a) {
continue; // already valid
}
// If a requires b but b is after a, swap
if a.prerequisites().contains(&b) {
layers.swap(i, i + 1);
changed = true;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn valid_grammar_then_encoding_then_content_type() {
let seq = vec![
EvasionLayer::Grammar,
EvasionLayer::Encoding,
EvasionLayer::ContentType,
EvasionLayer::Header,
EvasionLayer::Smuggling,
];
assert!(is_valid_sequence(&seq));
}
#[test]
fn invalid_encoding_before_grammar() {
let seq = vec![EvasionLayer::Encoding, EvasionLayer::Grammar];
assert!(!is_valid_sequence(&seq));
}
#[test]
fn canonicalize_fixes_order() {
let mut seq = vec![
EvasionLayer::Encoding,
EvasionLayer::Grammar,
EvasionLayer::ContentType,
];
canonicalize(&mut seq);
assert!(is_valid_sequence(&seq));
assert_eq!(seq[0], EvasionLayer::Grammar);
assert_eq!(seq[1], EvasionLayer::Encoding);
}
#[test]
fn smuggling_requires_content_type() {
let seq = vec![EvasionLayer::Header, EvasionLayer::Smuggling];
assert!(!is_valid_sequence(&seq));
}
#[test]
fn body_padding_must_come_after_body_mutators() {
let valid = vec![
EvasionLayer::Grammar,
EvasionLayer::Encoding,
EvasionLayer::ContentType,
EvasionLayer::BodyPadding,
];
assert!(is_valid_sequence(&valid));
// Wrong order: padding before content-type would be wiped out
// by the content-type rebuild.
let invalid = vec![
EvasionLayer::Grammar,
EvasionLayer::BodyPadding,
EvasionLayer::ContentType,
];
assert!(!is_valid_sequence(&invalid));
}
#[test]
fn body_padding_canonicalizes_to_end() {
// is_valid_sequence requires ALL listed prereqs to exist, so
// for BodyPadding we need all three (Encoding, ContentType,
// Grammar) in the sequence.
let mut seq = vec![
EvasionLayer::ContentType,
EvasionLayer::Grammar,
EvasionLayer::BodyPadding,
EvasionLayer::Encoding,
];
canonicalize(&mut seq);
assert!(
is_valid_sequence(&seq),
"canonicalize did not produce a valid sequence: {seq:?}"
);
assert_eq!(seq.last(), Some(&EvasionLayer::BodyPadding));
}
// ── Density ramp ────────────────────────────────────
#[test]
fn empty_sequence_is_valid() {
// Edge case: empty composition has no prereqs to violate.
assert!(is_valid_sequence(&[]));
}
#[test]
fn single_layer_with_no_prereqs_is_valid() {
assert!(is_valid_sequence(&[EvasionLayer::Grammar]));
assert!(is_valid_sequence(&[EvasionLayer::Header]));
}
#[test]
fn single_layer_with_unmet_prereq_is_invalid() {
// Encoding requires Grammar; alone, it fails.
assert!(!is_valid_sequence(&[EvasionLayer::Encoding]));
// ContentType requires both Encoding AND Grammar.
assert!(!is_valid_sequence(&[EvasionLayer::ContentType]));
}
#[test]
fn h2_requires_header_prereq() {
assert!(!is_valid_sequence(&[EvasionLayer::H2]));
assert!(is_valid_sequence(&[EvasionLayer::Header, EvasionLayer::H2]));
}
#[test]
fn smuggling_requires_both_content_type_and_header() {
// Both prereqs (ContentType, Header) must appear before Smuggling.
assert!(!is_valid_sequence(&[
EvasionLayer::Header,
EvasionLayer::Smuggling
]));
assert!(!is_valid_sequence(&[
EvasionLayer::ContentType,
EvasionLayer::Smuggling
]));
// With both prereqs satisfied (and ContentType's own
// prereqs of Encoding+Grammar), the sequence is valid.
assert!(is_valid_sequence(&[
EvasionLayer::Grammar,
EvasionLayer::Encoding,
EvasionLayer::ContentType,
EvasionLayer::Header,
EvasionLayer::Smuggling,
]));
}
#[test]
fn duplicate_layers_in_sequence_remain_valid() {
// The grammar doesn't forbid using the same layer twice
// (encoding the payload, then encoding again).
let seq = vec![
EvasionLayer::Grammar,
EvasionLayer::Encoding,
EvasionLayer::Encoding,
];
assert!(is_valid_sequence(&seq));
}
#[test]
fn can_follow_self_returns_true() {
// A layer can follow itself (e.g. two encoding passes).
for layer in [
EvasionLayer::Encoding,
EvasionLayer::Header,
EvasionLayer::Grammar,
] {
assert!(layer.can_follow(layer), "{layer:?} should follow itself");
}
}
#[test]
fn can_follow_unrelated_layer_returns_false() {
// Encoding's prereq is Grammar; Header isn't related.
assert!(!EvasionLayer::Encoding.can_follow(EvasionLayer::Header));
}
#[test]
fn canonicalize_empty_sequence_does_not_panic() {
let mut seq: Vec<EvasionLayer> = vec![];
canonicalize(&mut seq);
assert!(seq.is_empty());
}
#[test]
fn canonicalize_single_layer_unchanged() {
let mut seq = vec![EvasionLayer::Grammar];
canonicalize(&mut seq);
assert_eq!(seq, vec![EvasionLayer::Grammar]);
}
#[test]
fn canonicalize_already_sorted_unchanged() {
let original = vec![
EvasionLayer::Grammar,
EvasionLayer::Encoding,
EvasionLayer::ContentType,
EvasionLayer::Header,
EvasionLayer::Smuggling,
];
let mut seq = original.clone();
canonicalize(&mut seq);
assert_eq!(seq, original);
}
#[test]
fn canonicalize_terminates_on_reverse_sorted_input() {
// Defensive: the bubble-sort loop must TERMINATE even on
// a reverse-sorted sequence (a stress test for the
// `changed` flag). We don't assert full validity here —
// canonicalize is best-effort over partial orderings; some
// multi-prereq layers (Smuggling needs BOTH Header AND
// ContentType) may not land in a fully-valid position from
// pairwise swaps alone. The non-termination case is the
// legendary-bar concern; that's what this test guards.
let mut seq = vec![
EvasionLayer::Smuggling,
EvasionLayer::Header,
EvasionLayer::ContentType,
EvasionLayer::Encoding,
EvasionLayer::Grammar,
];
canonicalize(&mut seq);
// Must have terminated (test wouldn't return otherwise).
assert_eq!(seq.len(), 5);
}
#[test]
fn evasion_layer_implements_copy_and_eq() {
// Marker test: derived traits matter for HashMap keys + Vec
// membership checks elsewhere in the strategy crate.
let a = EvasionLayer::Grammar;
let b = a;
assert_eq!(a, b);
let _ = a;
let _ = b;
}
#[test]
fn body_padding_after_encoding_only_does_not_fully_validate() {
// BodyPadding lists Encoding, ContentType, Grammar as prereqs.
// Encoding alone (even with Grammar before it) isn't enough.
let seq = vec![
EvasionLayer::Grammar,
EvasionLayer::Encoding,
EvasionLayer::BodyPadding,
];
assert!(
!is_valid_sequence(&seq),
"BodyPadding requires Encoding, ContentType, AND Grammar all present"
);
}
#[test]
fn prereqs_for_each_layer_are_a_subset_of_all_layers() {
// Sanity-check the static prereq table: no prereq names a
// layer that doesn't exist.
let all = [
EvasionLayer::Encoding,
EvasionLayer::Grammar,
EvasionLayer::ContentType,
EvasionLayer::Header,
EvasionLayer::Smuggling,
EvasionLayer::H2,
EvasionLayer::BodyPadding,
];
for layer in all {
for &prereq in layer.prerequisites() {
assert!(
all.contains(&prereq),
"{layer:?} has prereq {prereq:?} not in the layer enum"
);
}
}
}
#[test]
fn grammar_has_no_prerequisites() {
// Grammar is the canonical "first" layer — everything else
// either depends on it or runs in parallel.
assert!(EvasionLayer::Grammar.prerequisites().is_empty());
}
#[test]
fn header_has_no_prerequisites() {
// Header obfuscation runs independently of payload mutation.
assert!(EvasionLayer::Header.prerequisites().is_empty());
}
}