oxedyne/fe2o3/fe2o3_net/tests/http_framing_strict.rs
4.8 KiB, 2 runs
created by r1435043967:77593, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #![cfg(feature = "async")] |
| 2 | //! An inbound message whose length can be read two ways is refused, never guessed at. |
| 3 | //! |
| 4 | //! A front proxy and the server behind it that frame one request differently let a second |
| 5 | //! request ride inside the first (RFC 9112 §6.3, §6.1, §7.1; RFC 9110 §5.5 on obs-fold). Every |
| 6 | //! refusal carries `Invalid`, which the server answers with 400 and a close. |
| 7 | |
| 8 | use oxedyne_fe2o3_core::prelude::*; |
| 9 | use oxedyne_fe2o3_net::{ |
| 10 | constant, |
| 11 | http::msg::HttpMessage, |
| 12 | }; |
| 13 | |
| 14 | use std::pin::Pin; |
| 15 | |
| 16 | |
| 17 | async fn read(wire: &str) -> Outcome<(Option<HttpMessage>, Vec<u8>)> { |
| 18 | let mut src = wire.as_bytes(); |
| 19 | HttpMessage::read::< |
| 20 | { constant::HTTP_DEFAULT_HEADER_CHUNK_SIZE }, |
| 21 | { constant::HTTP_DEFAULT_BODY_CHUNK_SIZE }, |
| 22 | _, |
| 23 | >(Pin::new(&mut src), &Vec::new(), None, None).await |
| 24 | } |
| 25 | |
| 26 | async fn refused(wire: &str) { |
| 27 | match read(wire).await { |
| 28 | Ok((msg, _)) => panic!("accepted {:?}, read as {:?}", wire, msg.map(|m| m.body)), |
| 29 | Err(e) => assert!(e.tags().contains(&ErrTag::Invalid), |
| 30 | "refused {:?} without Invalid, so not as a bad request: {}", wire, e), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | async fn body(wire: &str) -> Vec<u8> { |
| 35 | match read(wire).await { |
| 36 | Ok((Some(msg), _)) => msg.body, |
| 37 | Ok((None, _)) => panic!("no message in {:?}", wire), |
| 38 | Err(e) => panic!("refused {:?}: {}", wire, e), |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | #[tokio::test] |
| 43 | async fn two_differing_content_lengths_are_refused() { |
| 44 | refused("POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 3\r\nContent-Length: 5\r\n\r\nabcde").await; |
| 45 | } |
| 46 | |
| 47 | #[tokio::test] |
| 48 | async fn identical_repeated_content_lengths_are_one() { |
| 49 | assert_eq!(body("POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 3\r\nContent-Length: 3\r\n\r\nabc").await, b"abc"); |
| 50 | } |
| 51 | |
| 52 | #[tokio::test] |
| 53 | async fn signed_content_length_is_refused() { |
| 54 | refused("POST / HTTP/1.1\r\nHost: a\r\nContent-Length: +3\r\n\r\nabc").await; |
| 55 | } |
| 56 | |
| 57 | #[tokio::test] |
| 58 | async fn content_length_with_transfer_encoding_is_refused() { |
| 59 | refused("POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 3\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await; |
| 60 | refused("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\nContent-Length: 3\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await; |
| 61 | } |
| 62 | |
| 63 | #[tokio::test] |
| 64 | async fn repeated_transfer_encoding_is_refused() { |
| 65 | refused("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\nTransfer-Encoding: gzip\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await; |
| 66 | } |
| 67 | |
| 68 | #[tokio::test] |
| 69 | async fn transfer_encoding_not_ending_in_chunked_is_refused() { |
| 70 | refused("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked, gzip\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await; |
| 71 | refused("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: xchunked\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await; |
| 72 | } |
| 73 | |
| 74 | #[tokio::test] |
| 75 | async fn chunked_as_final_coding_is_decoded() { |
| 76 | assert_eq!(body("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: gzip, Chunked\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await, b"abc"); |
| 77 | } |
| 78 | |
| 79 | #[tokio::test] |
| 80 | async fn non_hex_chunk_size_is_refused() { |
| 81 | refused("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\n+3\r\nabc\r\n0\r\n\r\n").await; |
| 82 | refused("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\n 3\r\nabc\r\n0\r\n\r\n").await; |
| 83 | refused("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\n10000000000000003\r\nabc\r\n0\r\n\r\n").await; |
| 84 | } |
| 85 | |
| 86 | #[tokio::test] |
| 87 | async fn folded_header_line_is_refused() { |
| 88 | refused("GET / HTTP/1.1\r\nHost: a\r\nX-Note: one\r\n two\r\n\r\n").await; |
| 89 | refused("POST / HTTP/1.1\r\nHost: a\r\nX-Note: one\r\n\tContent-Length: 3\r\n\r\nabc").await; |
| 90 | } |
| 91 | |
| 92 | // The head grammar (RFC 9112 §2.2, §5.1): a field a front hop would read as another name, or a |
| 93 | // line end it would not see, is refused. |
| 94 | |
| 95 | #[tokio::test] |
| 96 | async fn space_before_colon_is_refused() { |
| 97 | refused("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding : chunked\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await; |
| 98 | } |
| 99 | |
| 100 | #[tokio::test] |
| 101 | async fn bare_lf_is_refused() { |
| 102 | refused("POST / HTTP/1.1\r\nHost: a\nTransfer-Encoding: chunked\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await; |
| 103 | } |
| 104 | |
| 105 | #[tokio::test] |
| 106 | async fn bare_cr_is_refused() { |
| 107 | refused("POST / HTTP/1.1\r\nHost: a\r\nX-A: 1\rContent-Length: 9\r\nContent-Length: 3\r\n\r\nabc").await; |
| 108 | } |
| 109 | |
| 110 | #[tokio::test] |
| 111 | async fn non_tchar_field_name_is_refused() { |
| 112 | refused("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding\x0c: chunked\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await; |
| 113 | refused("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding\u{a0}: chunked\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await; |
| 114 | } |
| 115 | |
| 116 | #[tokio::test] |
| 117 | async fn empty_list_element_is_ignored() { |
| 118 | assert_eq!(body("POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked,\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await, b"abc"); |
| 119 | } |
| 120 | |
| 121 | #[tokio::test] |
| 122 | async fn transfer_encoding_in_http_1_0_is_refused() { |
| 123 | refused("POST / HTTP/1.0\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nabc\r\n0\r\n\r\n").await; |
| 124 | } |