interplane.Get Omega
Read in your language

readings-window

aien-dev/readings-window

Brought by drakestapleton

fix: reject completed HTTP heads beyond MAX_HEAD

merged 9c5d44efc277b0d0 #10 · opened 2026-10-24 09:15 UTC · closed 2026-10-24 22:12 UTC · merged 2026-10-24 22:12 UTC

Imported from GitHub aien-dev/readings-window#10, by drakestapleton

Labels: ready-to-merge

Base 3197d3b2851a60008302b5b5c9c0d82e8ae93cbb · Head 6a473d6e3efba591072afe409a51e8fd27c1f22f

Merged on GitHub as 55ff64b75853085b696bdb44b7497b1d669b4b14; not reachable from main 246b9530f25e933dfdb2c59d29e712577c54de93 (observed 2026-11-18T16:40:27Z)

drakestapleton wrote on 2026-10-24 09:15 UTC

Verification

PASS: git diff --check and independent source review. NOT_RUN: cargo test, release build, clippy, formatting and old-source regression execution. No Rust toolchain is installed in the editing environment. Keep this draft until required checks run.

Correction

read_head returned as soon as it found CRLFCRLF, before checking MAX_HEAD. A completed oversized header arriving in the next read could therefore pass the cap. Check delimiter endpoint i + 4 before returning; do not reject based on entire buffered length because a valid head may arrive with body bytes.

Add three regressions: exact boundary/oversize cases, unterminated input, and valid head plus trailing body crossing the read-buffer cap. Negative control: remove only the four-line production guard while retaining tests; boundary regression must fail, then pass after restore.

Stacked on existing #9 at 3197d3b2851a60008302b5b5c9c0d82e8ae93cbb, preserving that PR's ownership and dependency repair. Source finding: https://github.com/aien-dev/readings-window/pull/9#issuecomment-6382142934 .

Separate absolute-deadline concern remains unmodified and unqualified. No live serve/collect command, service restart, SSH, deployment or visibility change. This patch does not claim to finish #9's TLS dependency/runtime acceptance.

Candidate 6bd33067d93b78faf2deaa1d0bc61219d1c3fdaf; tree 71ce14f836af57443965bd8f5c57da4eb300471a.

Apply it

git checkout -b patch-9c5d44efc277b0d0 3197d3b2851a60008302b5b5c9c0d82e8ae93cbb
curl -fsSL https://interplane.io/drakestapleton/readings-window/patches/9c5d44efc277b0d0.patch | git am

Raw patch (mbox) · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
aien-dev/readings-window
10
6a473d6e3efba591072afe409a51e8fd27c1f22f
{"kind":"github-import","owner":"aien-dev","repo":"readings-window","number":10,"url":"https://github.com/aien-dev/readings-window/pull/10","export_sha256":"cbdaf0586076eab30e67b6ea55086509660c2ec3a44f6f5fefd734139405f67c","imported_utc":"2026-11-18T16:40:27Z"}

Changes

diff --git a/Cargo.lock b/Cargo.lock
index 181016c..bc72201 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -326,9 +326,9 @@ dependencies = [
 
 [[package]]
 name = "rustls-webpki"
-version = "0.103.15"
+version = "0.103.16"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2"
+checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281"
 dependencies = [
  "ring",
  "rustls-pki-types",
diff --git a/src/tls.rs b/src/tls.rs
index e8da76f..5c48e2d 100644
--- a/src/tls.rs
+++ b/src/tls.rs
@@ -81,6 +81,10 @@ fn read_head<S: Read>(s: &mut S) -> Option<Vec<u8>> {
         }
         buf.extend_from_slice(&chunk[..n]);
         if let Some(i) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
+            // Bound the complete wire head, not body bytes read in the same chunk.
+            if i + 4 > MAX_HEAD {
+                return None;
+            }
             buf.truncate(i + 2);
             return Some(buf);
         }
@@ -216,6 +220,45 @@ mod tests {
         }
     }
 
+    #[test]
+    fn head_size_limit_includes_terminator() {
+        for size in [MAX_HEAD - 1, MAX_HEAD, MAX_HEAD + 1, MAX_HEAD + 2048] {
+            let mut head = b"GET /health HTTP/1.1\r\nX-Pad: ".to_vec();
+            head.resize(size - 4, b'a');
+            head.extend_from_slice(b"\r\n\r\n");
+            let result = read_head(&mut std::io::Cursor::new(&head));
+            if size <= MAX_HEAD {
+                assert_eq!(result.as_deref(), Some(&head[..size - 2]), "size {size}");
+            } else {
+                assert!(result.is_none(), "accepted oversized head: {size}");
+            }
+        }
+    }
+
+    #[test]
+    fn head_size_limit_ignores_trailing_body() {
+        struct Chunked<'a>(&'a [u8]);
+        impl Read for Chunked<'_> {
+            fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
+                let limit = buf.len().min(1000);
+                self.0.read(&mut buf[..limit])
+            }
+        }
+        let mut input = b"GET /health HTTP/1.1\r\nX-Pad: ".to_vec();
+        input.resize(MAX_HEAD - 104, b'a');
+        input.extend_from_slice(b"\r\n\r\n");
+        let head_end = input.len();
+        input.extend_from_slice(&[b'b'; 4096]);
+        // The last read crosses MAX_HEAD, but the terminator precedes that limit.
+        let result = read_head(&mut Chunked(&input));
+        assert_eq!(result.as_deref(), Some(&input[..head_end - 2]));
+    }
+
+    #[test]
+    fn head_size_limit_rejects_unterminated_input() {
+        assert!(read_head(&mut std::io::Cursor::new(vec![b'a'; MAX_HEAD + 1])).is_none());
+    }
+
     #[test]
     fn head_parsing() {
         let h = b"GET /x?y=1 HTTP/1.1\r\nHost: a\r\nauthorization:  Bearer t \r\n";