| @@ -593,6 +593,133 @@ fn http_body_limit_and_write_timeout() { |
| 593 | 593 | assert!(stderr.contains("tilrt: live blocks: 0\n"), "{stderr}"); |
| 594 | 594 | } |
| 595 | 595 | |
| 596 | +/// Malformed and smuggling-shaped requests (issue #62): each gets one |
| 597 | +/// error response (400, 413 or 501), and the connection is closed, so a |
| 598 | +/// request pipelined behind it is never served. Nothing leaks. |
| 599 | +#[test] |
| 600 | +fn http_rejects_malformed_requests() { |
| 601 | + use std::io::{BufRead, BufReader, Read, Write}; |
| 602 | + use std::time::Duration; |
| 603 | + let lib = std::path::Path::new(env!("CARGO_BIN_EXE_tilc")).with_file_name("libtilrt.a"); |
| 604 | + if !lib.is_file() { |
| 605 | + eprintln!("skipping: {} not built", lib.display()); |
| 606 | + return; |
| 607 | + } |
| 608 | + let exe = std::path::PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("http-malformed"); |
| 609 | + let (ok, _, stderr) = tilc(&[ |
| 610 | + "build", |
| 611 | + "tests/fixtures/http/malformed.til", |
| 612 | + "-o", |
| 613 | + exe.to_str().unwrap(), |
| 614 | + ]); |
| 615 | + assert!(ok, "{stderr}"); |
| 616 | + let mut server = Command::new(&exe) |
| 617 | + .env("TILRT_LEAKS", "1") |
| 618 | + .stdout(std::process::Stdio::piped()) |
| 619 | + .stderr(std::process::Stdio::piped()) |
| 620 | + .spawn() |
| 621 | + .unwrap(); |
| 622 | + let mut out = BufReader::new(server.stdout.take().unwrap()); |
| 623 | + let mut port = String::new(); |
| 624 | + out.read_line(&mut port).unwrap(); |
| 625 | + let port: u16 = port.trim().parse().expect("the server prints its port"); |
| 626 | + // Send `text` in one write, then read until the server closes. |
| 627 | + let exchange = |text: &str| { |
| 628 | + let mut conn = std::net::TcpStream::connect(("127.0.0.1", port)).unwrap(); |
| 629 | + conn.set_read_timeout(Some(Duration::from_secs(10))) |
| 630 | + .unwrap(); |
| 631 | + conn.write_all(text.as_bytes()).unwrap(); |
| 632 | + let mut response = String::new(); |
| 633 | + conn.read_to_string(&mut response).unwrap(); |
| 634 | + response |
| 635 | + }; |
| 636 | + // One response with `status` and body `message`, then the end. |
| 637 | + let rejected = |text: &str, status: &str, message: &str| { |
| 638 | + let response = exchange(text); |
| 639 | + assert!( |
| 640 | + response.starts_with(&format!("HTTP/1.1 {status} ")), |
| 641 | + "{text:?}: {response}" |
| 642 | + ); |
| 643 | + assert!(response.contains("Connection: close\r\n"), "{response}"); |
| 644 | + assert!( |
| 645 | + response.ends_with(&format!("\r\n\r\n{message}")), |
| 646 | + "{text:?}: {response}" |
| 647 | + ); |
| 648 | + assert_eq!(response.matches("HTTP/1.1 ").count(), 1, "{response}"); |
| 649 | + }; |
| 650 | + let smuggled = "GET /smuggled HTTP/1.1\r\nHost: x\r\n\r\n"; |
| 651 | + |
| 652 | + // Two lengths that disagree, or one that is not a decimal number. |
| 653 | + rejected( |
| 654 | + &format!( |
| 655 | + "POST /echo HTTP/1.1\r\nHost: x\r\nContent-Length: 5\r\nContent-Length: 6\r\n\r\nhello!{smuggled}" |
| 656 | + ), |
| 657 | + "400", |
| 658 | + "bad request: invalid Content-Length", |
| 659 | + ); |
| 660 | + rejected( |
| 661 | + &format!("POST /echo HTTP/1.1\r\nHost: x\r\nContent-Length: +5\r\n\r\nhello{smuggled}"), |
| 662 | + "400", |
| 663 | + "bad request: invalid Content-Length", |
| 664 | + ); |
| 665 | + // Both framings: neither is trusted. |
| 666 | + rejected( |
| 667 | + &format!( |
| 668 | + "POST /echo HTTP/1.1\r\nHost: x\r\nContent-Length: 5\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n{smuggled}" |
| 669 | + ), |
| 670 | + "400", |
| 671 | + "bad request: both Content-Length and Transfer-Encoding", |
| 672 | + ); |
| 673 | + rejected( |
| 674 | + &format!( |
| 675 | + "POST /echo HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: gzip, chunked\r\n\r\n0\r\n\r\n{smuggled}" |
| 676 | + ), |
| 677 | + "501", |
| 678 | + "unsupported Transfer-Encoding: gzip, chunked", |
| 679 | + ); |
| 680 | + // Whitespace before a header's colon. |
| 681 | + rejected( |
| 682 | + &format!("POST /echo HTTP/1.1\r\nHost: x\r\nContent-Length : 5\r\n\r\nhello{smuggled}"), |
| 683 | + "400", |
| 684 | + "bad request: invalid header name", |
| 685 | + ); |
| 686 | + // Malformed chunks. |
| 687 | + let chunked = "POST /echo HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\n\r\n"; |
| 688 | + rejected( |
| 689 | + &format!("{chunked}zz\r\nhello\r\n0\r\n\r\n{smuggled}"), |
| 690 | + "400", |
| 691 | + "bad request: invalid chunk size", |
| 692 | + ); |
| 693 | + rejected( |
| 694 | + &format!("{chunked}5\r\nhello!!0\r\n\r\n{smuggled}"), |
| 695 | + "400", |
| 696 | + "bad request: no CRLF after a chunk", |
| 697 | + ); |
| 698 | + rejected( |
| 699 | + &format!("{chunked}4000000000000000\r\nhello\r\n0\r\n\r\n{smuggled}"), |
| 700 | + "400", |
| 701 | + "bad request: chunk too large", |
| 702 | + ); |
| 703 | + // One chunk over the 64-byte limit: refused before its data arrives. |
| 704 | + rejected(&format!("{chunked}41\r\n"), "413", "request body too large"); |
| 705 | + // Lengths that agree are one length: the request and the one |
| 706 | + // pipelined behind it are served. |
| 707 | + let served = exchange( |
| 708 | + "POST /echo HTTP/1.1\r\nHost: x\r\nContent-Length: 5\r\ncontent-length: 5\r\n\r\nhelloGET /next HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n", |
| 709 | + ); |
| 710 | + assert_eq!(served.matches("HTTP/1.1 200 OK\r\n").count(), 2, "{served}"); |
| 711 | + assert!(served.contains("\r\n\r\n/echo hello"), "{served}"); |
| 712 | + assert!(served.ends_with("\r\n\r\n/next "), "{served}"); |
| 713 | + |
| 714 | + let mut rest = String::new(); |
| 715 | + out.read_to_string(&mut rest).unwrap(); |
| 716 | + assert_eq!(rest, "done\n"); |
| 717 | + let output = server.wait_with_output().unwrap(); |
| 718 | + assert!(output.status.success()); |
| 719 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 720 | + assert!(stderr.contains("tilrt: live blocks: 0\n"), "{stderr}"); |
| 721 | +} |
| 722 | + |
| 596 | 723 | /// Read one response: its head, and its body (`Content-Length` bytes). |
| 597 | 724 | fn read_response(conn: &mut impl std::io::BufRead) -> (String, Vec<u8>) { |
| 598 | 725 | let mut head = String::new(); |