Strip AWS auth headers from raw HTTP request bytes. Removes `Authorization`, `X-Amz-Date`, `X-Amz-Security-Token`, and `X-Amz-Content-Sha256` headers so the request can pass through the proxy's fail-closed placeholder scan before re-signing. Returns `Err` if the header block is not valid UTF-8. Failing closed prevents non-UTF-8 requests from passing through with their original AWS credentials i
(raw: &[u8])
| 69 | /// prevents non-UTF-8 requests from passing through with their original |
| 70 | /// AWS credentials intact. |
| 71 | pub fn strip_aws_headers(raw: &[u8]) -> Result<Vec<u8>> { |
| 72 | let header_end = raw |
| 73 | .windows(4) |
| 74 | .position(|w| w == b"\r\n\r\n") |
| 75 | .map_or(raw.len(), |p| p + 4); |
| 76 | |
| 77 | // The caller (rest.rs) validates UTF-8 strictly before reaching this |
| 78 | // point, so `from_utf8` should never fail here. Fail closed so that |
| 79 | // crafted non-UTF-8 bytes cannot smuggle AWS credentials through. |
| 80 | let header_str = std::str::from_utf8(&raw[..header_end]) |
| 81 | .map_err(|e| miette!("strip_aws_headers: header block is not valid UTF-8: {e}"))?; |
| 82 | let lines: Vec<&str> = header_str.split("\r\n").collect(); |
| 83 | |
| 84 | let mut output = Vec::with_capacity(raw.len()); |
| 85 | |
| 86 | for (i, line) in lines.iter().enumerate() { |
| 87 | if i == 0 { |
| 88 | output.extend_from_slice(line.as_bytes()); |
| 89 | output.extend_from_slice(b"\r\n"); |
| 90 | continue; |
| 91 | } |
| 92 | if line.is_empty() { |
| 93 | break; |
| 94 | } |
| 95 | let lower = line.to_ascii_lowercase(); |
| 96 | if lower.starts_with("authorization:") |
| 97 | || lower.starts_with("x-amz-date:") |
| 98 | || lower.starts_with("x-amz-security-token:") |
| 99 | || lower.starts_with("x-amz-content-sha256:") |
| 100 | { |
| 101 | continue; |
| 102 | } |
| 103 | output.extend_from_slice(line.as_bytes()); |
| 104 | output.extend_from_slice(b"\r\n"); |
| 105 | } |
| 106 | |
| 107 | output.extend_from_slice(b"\r\n"); |
| 108 | |
| 109 | if header_end < raw.len() { |
| 110 | output.extend_from_slice(&raw[header_end..]); |
| 111 | } |
| 112 | |
| 113 | Ok(output) |
| 114 | } |
| 115 | |
| 116 | struct RequestParts<'a> { |
| 117 | method: &'a str, |
no test coverage detected