diff --git a/Http.lean b/Http.lean index d510264..e99ffe1 100644 --- a/Http.lean +++ b/Http.lean @@ -4,3 +4,5 @@ import Http.Match import Http.Grammar import Http.Head import Http.Framing +import Http.Bytes +import Http.Parse diff --git a/Http/Bytes.lean b/Http/Bytes.lean new file mode 100644 index 0000000..eb70edd --- /dev/null +++ b/Http/Bytes.lean @@ -0,0 +1,446 @@ +import Http.Types + +namespace Http + +/-- The largest integer that fits in 64 bits. A chunk size above this is rejected. -/ +def maxUInt64 : Nat := 2 ^ 64 - 1 + +def lowerByte (b : UInt8) : UInt8 := + if 65 ≤ b && b ≤ 90 then b + 32 else b + +def mapBytes (f : UInt8 → UInt8) : List UInt8 → List UInt8 + | [] => [] + | b :: bs => f b :: mapBytes f bs + +def lowerBytes (bs : List UInt8) : List UInt8 := + mapBytes lowerByte bs + +def isOWS (b : UInt8) : Bool := + b == 32 || b == 9 + +def dropWhileBytes (p : UInt8 → Bool) : List UInt8 → List UInt8 + | [] => [] + | b :: bs => if p b then dropWhileBytes p bs else b :: bs + +def revBytes : List UInt8 → List UInt8 → List UInt8 + | [], acc => acc + | b :: bs, acc => revBytes bs (b :: acc) + +def trimOWS (bs : List UInt8) : List UInt8 := + revBytes (dropWhileBytes isOWS (revBytes (dropWhileBytes isOWS bs) [])) [] + +def allBytes (p : UInt8 → Bool) : List UInt8 → Bool + | [] => true + | b :: bs => p b && allBytes p bs + +def anyBytes (p : UInt8 → Bool) : List UInt8 → Bool + | [] => false + | b :: bs => p b || anyBytes p bs + +def takeBytes : Nat → List UInt8 → List UInt8 + | 0, _ => [] + | _, [] => [] + | n + 1, b :: bs => b :: takeBytes n bs + +def dropBytes : Nat → List UInt8 → List UInt8 + | 0, bs => bs + | _, [] => [] + | n + 1, _ :: bs => dropBytes n bs + +def takeWhileBytes (p : UInt8 → Bool) : List UInt8 → List UInt8 + | [] => [] + | b :: bs => if p b then b :: takeWhileBytes p bs else [] + +def splitByte (sep : UInt8) : List UInt8 → List (List UInt8) + | [] => [[]] + | b :: bs => + let parts := splitByte sep bs + if b == sep then + [] :: parts + else + match parts with + | [] => [[b]] + | p :: ps => (b :: p) :: ps + +/-- Split at the first CRLF. A bare CR or a bare LF makes the split fail, +and neither byte is kept in the line. -/ +def splitCRLF : List UInt8 → Option (List UInt8 × List UInt8) + | [] => none + | c :: bs => + if c = 0x0D then + match bs with + | [] => none + | d :: rest => if d = 0x0A then some ([], rest) else none + else if c = 0x0A then + none + else + match splitCRLF bs with + | none => none + | some (line, rest) => some (c :: line, rest) + +def noBareLineBreak (line : List UInt8) : Prop := + ∀ b, b ∈ line → b ≠ 0x0D ∧ b ≠ 0x0A + +theorem splitCRLF_of_line (line rest : List UInt8) (h : noBareLineBreak line) : + splitCRLF (line ++ [0x0D, 0x0A] ++ rest) = some (line, rest) := by + induction line with + | nil => + simp [splitCRLF] + | cons c line ih => + have hc : c ≠ 0x0D ∧ c ≠ 0x0A := h c (by simp) + have hline : noBareLineBreak line := fun b hb => h b (by simp [hb]) + have ih' := ih hline + rcases hc with ⟨hcr, hlf⟩ + have ih'' : splitCRLF (line ++ 0x0D :: 0x0A :: rest) = some (line, rest) := by + simpa [List.append_assoc] using ih' + simp [splitCRLF, hcr, hlf, ih''] + +theorem splitCRLF_sound_left (bs line rest : List UInt8) + (h : splitCRLF bs = some (line, rest)) : + noBareLineBreak line ∧ bs = line ++ [0x0D, 0x0A] ++ rest := by + induction bs generalizing line rest with + | nil => + simp [splitCRLF] at h + | cons c bs ih => + by_cases hcr : c = 0x0D + · subst hcr + cases bs with + | nil => + simp [splitCRLF] at h + | cons d ds => + by_cases hlf : d = 0x0A + · subst hlf + simp [splitCRLF] at h + rcases h with ⟨rfl, rfl⟩ + simp [noBareLineBreak] + · simp [splitCRLF, hlf] at h + · by_cases hlf : c = 0x0A + · simp [splitCRLF, hlf] at h + · simp only [splitCRLF, hcr, hlf] at h + cases hsp : splitCRLF bs with + | none => + simp [hsp] at h + | some pair => + obtain ⟨line', rest'⟩ := pair + simp [hsp] at h + rcases h with ⟨rfl, rfl⟩ + obtain ⟨hn, heq⟩ := ih line' rest' hsp + refine ⟨?_, ?_⟩ + · intro b hb + simp at hb + cases hb with + | inl hb => + subst hb + exact ⟨hcr, hlf⟩ + | inr hb => + exact hn b hb + · simp [heq] + +theorem splitCRLF_sound (bs line rest : List UInt8) : + splitCRLF bs = some (line, rest) ↔ + noBareLineBreak line ∧ bs = line ++ [0x0D, 0x0A] ++ rest := by + constructor + · exact splitCRLF_sound_left bs line rest + · intro h + rcases h with ⟨hn, heq⟩ + subst heq + exact splitCRLF_of_line line rest hn + +theorem splitCRLF_prefix_free (bs line rest extra : List UInt8) + (h : splitCRLF bs = some (line, rest)) : + splitCRLF (bs ++ extra) = some (line, rest ++ extra) := by + obtain ⟨hn, heq⟩ := splitCRLF_sound_left bs line rest h + have h' := splitCRLF_of_line line (rest ++ extra) hn + simpa [heq, List.append_assoc] using h' + +def decVal (b : UInt8) : Option Nat := + if 48 ≤ b.toNat ∧ b.toNat ≤ 57 then some (b.toNat - 48) else none + +def hexVal (b : UInt8) : Option Nat := + if 48 ≤ b.toNat ∧ b.toNat ≤ 57 then some (b.toNat - 48) + else if 97 ≤ b.toNat ∧ b.toNat ≤ 102 then some (b.toNat - 87) + else if 65 ≤ b.toNat ∧ b.toNat ≤ 70 then some (b.toNat - 55) + else none + +def parseAcc (base : Nat) (val : UInt8 → Option Nat) (acc : Nat) : List UInt8 → Option Nat + | [] => if acc ≤ maxUInt64 then some acc else none + | b :: bs => + match val b with + | none => none + | some d => + let n := acc * base + d + if n ≤ maxUInt64 then parseAcc base val n bs else none + +def parseDec (ds : List UInt8) : Option Nat := + match ds with + | [] => none + | _ => parseAcc 10 decVal 0 ds + +def parseHex (ds : List UInt8) : Option Nat := + match ds with + | [] => none + | _ => parseAcc 16 hexVal 0 ds + +def natToDecChars (n : Nat) : List UInt8 := + if n < 10 then [UInt8.ofNat (48 + n)] + else natToDecChars (n / 10) ++ [UInt8.ofNat (48 + n % 10)] +termination_by n +decreasing_by + apply Nat.div_lt_self + · omega + · decide + +def nibble (d : Nat) : UInt8 := + if d < 10 then UInt8.ofNat (48 + d) else UInt8.ofNat (87 + d) + +def natToHexLower (n : Nat) : List UInt8 := + let digit := nibble (n % 16) + if n < 16 then [digit] + else natToHexLower (n / 16) ++ [digit] +termination_by n +decreasing_by + apply Nat.div_lt_self + · omega + · decide + +def decDigits (n : Nat) : Nat := + if n < 10 then 1 else decDigits (n / 10) + 1 +termination_by n +decreasing_by + apply Nat.div_lt_self + · omega + · decide + +def hexDigits (n : Nat) : Nat := + if n < 16 then 1 else hexDigits (n / 16) + 1 +termination_by n +decreasing_by + apply Nat.div_lt_self + · omega + · decide + +theorem toNat_ofNat_small (n : Nat) (h : n < 256) : (UInt8.ofNat n).toNat = n := by + simp [UInt8.ofNat, UInt8.toNat, Nat.mod_eq_of_lt h] + +theorem decVal_digit (d : Nat) (h : d < 10) : + decVal (UInt8.ofNat (48 + d)) = some d := by + have hlt : 48 + d < 256 := by omega + unfold decVal + rw [toNat_ofNat_small _ hlt] + have hlo : 48 ≤ 48 + d := Nat.le_add_right _ _ + have hhi : 48 + d ≤ 57 := by omega + simp only [hlo, hhi, and_self, ite_true] + exact congrArg some (Nat.add_sub_cancel_left 48 d) + +theorem hexVal_digit (d : Nat) (h : d < 16) : + hexVal (nibble d) = some d := by + unfold nibble hexVal + by_cases h10 : d < 10 + · have hlt : 48 + d < 256 := by omega + simp only [h10, ite_true] + rw [toNat_ofNat_small _ hlt] + have hlo : 48 ≤ 48 + d := Nat.le_add_right _ _ + have hhi : 48 + d ≤ 57 := by omega + simp only [hlo, hhi, and_self, ite_true] + exact congrArg some (Nat.add_sub_cancel_left 48 d) + · have hlt : 87 + d < 256 := by omega + simp only [h10, ite_false] + rw [toNat_ofNat_small _ hlt] + have hnot : ¬(48 ≤ 87 + d ∧ 87 + d ≤ 57) := by omega + have hlo : 97 ≤ 87 + d := by omega + have hhi : 87 + d ≤ 102 := by omega + simp only [hnot, ite_false, hlo, hhi, and_self, ite_true] + exact congrArg some (Nat.add_sub_cancel_left 87 d) + +theorem parseAcc_append (base : Nat) (val : UInt8 → Option Nat) (acc : Nat) + (xs ys : List UInt8) (hacc : acc ≤ maxUInt64) : + parseAcc base val acc (xs ++ ys) = + match parseAcc base val acc xs with + | none => none + | some n => parseAcc base val n ys := by + induction xs generalizing acc with + | nil => + simp [parseAcc, hacc] + | cons b xs ih => + simp only [List.cons_append, parseAcc] + cases hval : val b with + | none => + simp [hval] + | some d => + simp only [hval] + by_cases hn : acc * base + d ≤ maxUInt64 + · simp only [hn, ite_true] + have hacc' : acc * base + d ≤ maxUInt64 := hn + simpa using ih (acc * base + d) hacc' + · simp [hn] + +theorem parseAcc_dec (acc n : Nat) (hacc : acc ≤ maxUInt64) + (h : acc * 10 ^ decDigits n + n ≤ maxUInt64) : + parseAcc 10 decVal acc (natToDecChars n) = some (acc * 10 ^ decDigits n + n) := by + induction n using Nat.strongRecOn generalizing acc with + | ind n ih => + by_cases h10 : n < 10 + · have hchars : natToDecChars n = [UInt8.ofNat (48 + n)] := by + rw [natToDecChars] + split + · rfl + · contradiction + have hdig : decDigits n = 1 := by + rw [decDigits] + split + · rfl + · contradiction + rw [hdig, Nat.pow_one] at h + rw [hchars, hdig, Nat.pow_one] + unfold parseAcc + rw [decVal_digit n h10] + simp only [h, ite_true] + unfold parseAcc + simp only [h, ite_true] + · have hdiv : n / 10 < n := Nat.div_lt_self (by omega) (by decide) + have hdigits : decDigits n = decDigits (n / 10) + 1 := by + rw [decDigits] + split + · contradiction + · rfl + have hpow : 10 ^ decDigits n = 10 ^ decDigits (n / 10) * 10 := by + rw [hdigits, Nat.pow_succ, Nat.mul_comm] + have hassoc : acc * (10 ^ decDigits (n / 10) * 10) = acc * 10 ^ decDigits (n / 10) * 10 := by + simp [Nat.mul_assoc] + have hbound : acc * 10 ^ decDigits (n / 10) + n / 10 ≤ maxUInt64 := by + have hle : acc * 10 ^ decDigits (n / 10) + n / 10 ≤ acc * 10 ^ decDigits n + n := by + rw [hpow, hassoc] + have hmul : acc * 10 ^ decDigits (n / 10) ≤ acc * 10 ^ decDigits (n / 10) * 10 := + Nat.le_mul_of_pos_right _ (by decide) + have hdivle : n / 10 ≤ n := Nat.div_le_self _ _ + omega + exact Nat.le_trans hle h + have ih' := ih (n / 10) hdiv acc hacc hbound + have hdigit : n % 10 < 10 := Nat.mod_lt _ (by decide) + have hd := decVal_digit (n % 10) hdigit + have hstep : (acc * 10 ^ decDigits (n / 10) + n / 10) * 10 + n % 10 = + acc * 10 ^ decDigits n + n := by + have hdivmod : n / 10 * 10 + n % 10 = n := by + simpa [Nat.mul_comm] using Nat.div_add_mod n 10 + have hmul : (acc * 10 ^ decDigits (n / 10) + n / 10) * 10 = + acc * 10 ^ decDigits (n / 10) * 10 + n / 10 * 10 := by + rw [Nat.add_mul] + rw [hmul, Nat.add_assoc, hdivmod, hpow, hassoc] + have hsum : (acc * 10 ^ decDigits (n / 10) + n / 10) * 10 + n % 10 ≤ maxUInt64 := by + rw [hstep]; exact h + have hchars : natToDecChars n = + natToDecChars (n / 10) ++ [UInt8.ofNat (48 + n % 10)] := by + rw [natToDecChars] + split + · contradiction + · rfl + have happ := parseAcc_append 10 decVal acc (natToDecChars (n / 10)) + [UInt8.ofNat (48 + n % 10)] hacc + rw [hchars, happ, ih'] + unfold parseAcc + rw [hd] + simp only [hsum, ite_true, hstep] + unfold parseAcc + simp only [h, ite_true] + +theorem natToDecChars_ne_nil (n : Nat) : natToDecChars n ≠ [] := by + rw [natToDecChars] + split + · simp + · simp [List.append_eq_nil_iff] + +theorem parseDec_printDec (n : Nat) (h : n ≤ maxUInt64) : + parseDec (natToDecChars n) = some n := by + have hparse := parseAcc_dec 0 n (by decide) (by simpa using h) + have hne := natToDecChars_ne_nil n + have hzero : 0 * 10 ^ decDigits n + n = n := by simp + rw [hzero] at hparse + cases hchars : natToDecChars n with + | nil => exact absurd hchars hne + | cons b bs => + rw [hchars] at hparse + unfold parseDec + simp only [hchars] + exact hparse + +theorem parseAcc_hex (acc n : Nat) (hacc : acc ≤ maxUInt64) + (h : acc * 16 ^ hexDigits n + n ≤ maxUInt64) : + parseAcc 16 hexVal acc (natToHexLower n) = some (acc * 16 ^ hexDigits n + n) := by + induction n using Nat.strongRecOn generalizing acc with + | ind n ih => + by_cases h16 : n < 16 + · have hmod : n % 16 = n := Nat.mod_eq_of_lt h16 + have hchars : natToHexLower n = [nibble n] := by + rw [natToHexLower] + simp [h16, hmod] + have hdig : hexDigits n = 1 := by + rw [hexDigits] + simp [h16] + rw [hdig, Nat.pow_one] at h + rw [hchars, hdig, Nat.pow_one] + unfold parseAcc + rw [hexVal_digit n h16] + simp only [h, ite_true] + unfold parseAcc + simp only [h, ite_true] + · have hdiv : n / 16 < n := Nat.div_lt_self (by omega) (by decide) + have hdigits : hexDigits n = hexDigits (n / 16) + 1 := by + rw [hexDigits] + simp [h16] + have hpow : 16 ^ hexDigits n = 16 ^ hexDigits (n / 16) * 16 := by + rw [hdigits, Nat.pow_succ, Nat.mul_comm] + have hassoc : acc * (16 ^ hexDigits (n / 16) * 16) = acc * 16 ^ hexDigits (n / 16) * 16 := by + simp [Nat.mul_assoc] + have hbound : acc * 16 ^ hexDigits (n / 16) + n / 16 ≤ maxUInt64 := by + have hle : acc * 16 ^ hexDigits (n / 16) + n / 16 ≤ acc * 16 ^ hexDigits n + n := by + rw [hpow, hassoc] + have hmul : acc * 16 ^ hexDigits (n / 16) ≤ acc * 16 ^ hexDigits (n / 16) * 16 := + Nat.le_mul_of_pos_right _ (by decide) + have hdivle : n / 16 ≤ n := Nat.div_le_self _ _ + omega + exact Nat.le_trans hle h + have ih' := ih (n / 16) hdiv acc hacc hbound + have hdigit : n % 16 < 16 := Nat.mod_lt _ (by decide) + have hd := hexVal_digit (n % 16) hdigit + have hstep : (acc * 16 ^ hexDigits (n / 16) + n / 16) * 16 + n % 16 = + acc * 16 ^ hexDigits n + n := by + have hdivmod : n / 16 * 16 + n % 16 = n := by + simpa [Nat.mul_comm] using Nat.div_add_mod n 16 + have hmul : (acc * 16 ^ hexDigits (n / 16) + n / 16) * 16 = + acc * 16 ^ hexDigits (n / 16) * 16 + n / 16 * 16 := by + rw [Nat.add_mul] + rw [hmul, Nat.add_assoc, hdivmod, hpow, hassoc] + have hsum : (acc * 16 ^ hexDigits (n / 16) + n / 16) * 16 + n % 16 ≤ maxUInt64 := by + rw [hstep]; exact h + have hchars : natToHexLower n = natToHexLower (n / 16) ++ [nibble (n % 16)] := by + rw [natToHexLower] + simp [h16] + have happ := parseAcc_append 16 hexVal acc (natToHexLower (n / 16)) [nibble (n % 16)] hacc + rw [hchars, happ, ih'] + unfold parseAcc + rw [hd] + simp only [hsum, ite_true, hstep] + unfold parseAcc + simp only [h, ite_true] + +theorem natToHexLower_ne_nil (n : Nat) : natToHexLower n ≠ [] := by + rw [natToHexLower] + by_cases h16 : n < 16 + · simp [h16] + · simp [h16, List.append_eq_nil_iff] + +theorem parseHex_printHex (n : Nat) (h : n ≤ maxUInt64) : + parseHex (natToHexLower n) = some n := by + have hparse := parseAcc_hex 0 n (by decide) (by simpa using h) + have hne := natToHexLower_ne_nil n + have hzero : 0 * 16 ^ hexDigits n + n = n := by simp + rw [hzero] at hparse + cases hchars : natToHexLower n with + | nil => exact absurd hchars hne + | cons b bs => + rw [hchars] at hparse + unfold parseHex + simp only [hchars] + exact hparse + +end Http diff --git a/Http/ChunkProofs.lean b/Http/ChunkProofs.lean new file mode 100644 index 0000000..d2cbb67 --- /dev/null +++ b/Http/ChunkProofs.lean @@ -0,0 +1,267 @@ +import Http.Parse + +namespace Http + +theorem takeBytes_append (bs rest : List UInt8) : + takeBytes bs.length (bs ++ rest) = bs := by + induction bs with + | nil => simp [takeBytes] + | cons b bs ih => simp [takeBytes, List.length_cons, ih] + +theorem dropBytes_append (bs rest : List UInt8) : + dropBytes bs.length (bs ++ rest) = rest := by + induction bs with + | nil => simp [dropBytes] + | cons b bs ih => simp [dropBytes, List.length_cons, ih] + +theorem allBytes_append (p : UInt8 → Bool) (xs ys : List UInt8) : + allBytes p (xs ++ ys) = (allBytes p xs && allBytes p ys) := by + induction xs with + | nil => simp [allBytes] + | cons x xs ih => simp [allBytes, ih, Bool.and_assoc] + +theorem nibble_ok (d : Nat) (hd : d < 16) : + isHex (nibble d) = true ∧ nibble d ≠ 13 ∧ nibble d ≠ 10 := by + revert hd + match d with + | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 => + intro _ + decide + | n + 16 => + intro h + omega + +theorem allBytes_hex (n : Nat) : allBytes isHex (natToHexLower n) = true := by + induction n using Nat.strongRecOn with + | ind n ih => + by_cases h16 : n < 16 + · have hmod : n % 16 = n := Nat.mod_eq_of_lt h16 + simp [natToHexLower, h16, hmod, allBytes, (nibble_ok n h16).1] + · have hdiv : n / 16 < n := Nat.div_lt_self (by omega) (by decide) + have hdigit : n % 16 < 16 := Nat.mod_lt _ (by decide) + rw [natToHexLower] + simp only [h16, ite_false] + rw [allBytes_append] + simp [allBytes, ih (n / 16) hdiv, (nibble_ok _ hdigit).1] + +theorem natToHex_no_break (n : Nat) : noBareLineBreak (natToHexLower n) := by + induction n using Nat.strongRecOn with + | ind n ih => + by_cases h16 : n < 16 + · have hmod : n % 16 = n := Nat.mod_eq_of_lt h16 + simp [natToHexLower, h16, hmod, noBareLineBreak] + exact ⟨(nibble_ok n h16).2.1, (nibble_ok n h16).2.2⟩ + · have hdiv : n / 16 < n := Nat.div_lt_self (by omega) (by decide) + have hdigit : n % 16 < 16 := Nat.mod_lt _ (by decide) + intro b hb + rw [natToHexLower] at hb + simp only [h16, ite_false, List.mem_append] at hb + cases hb with + | inl hb => exact ih _ hdiv b hb + | inr hb => + simp at hb + subst hb + exact ⟨(nibble_ok _ hdigit).2.1, (nibble_ok _ hdigit).2.2⟩ + +theorem takeWhile_of_all (p : UInt8 → Bool) (bs : List UInt8) (h : allBytes p bs = true) : + takeWhileBytes p bs = bs := by + induction bs with + | nil => simp [takeWhileBytes] + | cons b bs ih => + simp [allBytes] at h + rcases h with ⟨hb, hbs⟩ + simp [takeWhileBytes, hb, ih hbs] + +theorem parseChunkSize_hex (n : Nat) (h : n ≤ maxUInt64) : + parseChunkSize (natToHexLower n) = some n := by + have htake := takeWhile_of_all isHex (natToHexLower n) (allBytes_hex n) + have hdrop : dropBytes (natToHexLower n).length (natToHexLower n) = [] := by + simpa using dropBytes_append (natToHexLower n) [] + have hne := natToHexLower_ne_nil n + simp [parseChunkSize, htake, hdrop, parseHex_printHex n h, parseExt, hne] + +theorem decode_last (fuel : Nat) (acc : List UInt8) (h : 0 < fuel) : + decodeLoop fuel acc ([48] ++ crlf ++ crlf) = some (acc, [], []) := by + cases fuel with + | zero => omega + | succ fuel => + simp [decodeLoop, crlf, splitCRLF, parseChunkSize, parseHex, parseAcc, hexVal, parseExt, + parseTrailers, takeWhileBytes, dropBytes, isHex, isDigit] + +theorem decodeChunked_encodeChunked (body : List UInt8) (h : body.length ≤ maxUInt64) : + decodeChunked (encodeChunked body) = some (body, [], []) := by + cases body with + | nil => + simp [encodeChunked, decodeChunked, decodeLoop, crlf, splitCRLF, parseChunkSize, parseHex, + parseAcc, hexVal, parseExt, parseTrailers, takeWhileBytes, dropBytes, isHex, isDigit] + | cons b bs => + have hline := natToHex_no_break (b :: bs).length + have hhex := parseChunkSize_hex (b :: bs).length h + have hsplit := splitCRLF_of_line (natToHexLower (b :: bs).length) + ((b :: bs) ++ crlf ++ [48] ++ crlf ++ crlf) hline + have htake := takeBytes_append (b :: bs) (crlf ++ [48] ++ crlf ++ crlf) + have hdrop := dropBytes_append (b :: bs) (crlf ++ [48] ++ crlf ++ crlf) + simp [crlf, List.append_assoc, List.cons_append, List.length_cons] at hsplit htake hdrop hhex + simp [encodeChunked, decodeChunked, List.isEmpty, crlf, decodeLoop, hsplit] + rw [hhex] + simp [List.length_cons, List.length_append] + rw [hdrop, htake] + simp [crlf, splitCRLF, parseChunkSize, parseHex, parseAcc, hexVal, parseExt, parseTrailers, + takeWhileBytes, dropBytes, isHex, isDigit, decode_last] + +/-- Chunked framing is HTTP/1.1 or a later 1.x, with no Content-Length, and + the combined Transfer-Encoding value is a list of the one coding + `chunked` (empty elements allowed). The method is not `CONNECT`, and + the target does not begin with `:`. -/ +theorem determineFraming_rejects_nonfinal_chunked (h : Head) + (hf : determineFraming h = some .chunked) : + h.major = 1 ∧ 1 ≤ h.minor ∧ + (∀ f, f ∈ h.fields → f.isNamed contentLengthName = false) ∧ + Grammar.chunkedList.isMatch + (List.intercalate [0x2C] + ((h.fields.filter (·.isNamed transferEncodingName)).map (·.value))) = true ∧ + h.method ≠ connectName ∧ h.target.head? ≠ some 0x3A := by + simp only [determineFraming] at hf + revert hf + simp only [Head.framing] + intro hf + split at hf + · simp at hf + · rename_i fr hcore + split at hf + · simp at hf + · rename_i hnot + cases hf + simp only [Head.framingCore] at hcore + -- Host and version guards refuse before the field match. + split at hcore + · simp at hcore + · split at hcore + · simp at hcore + · rename_i hhosts hconnect + -- `hhosts` / `hconnect` are the false guards. The match is on the two filters. + split at hcore + · simp at hcore + · simp at hcore + · next _ _ ts heq => + split at hcore + · next hok => + injection hcore with hfr + cases hfr + simp only [Bool.and_eq_true] at hok + rcases hok with ⟨hminor, hmatch⟩ + have hminor' : 1 ≤ h.minor := by + simpa using hminor + refine ⟨?major, hminor', ?nocl, ?te, ?method, ?colon⟩ + case major => + have hhosts' := hhosts + simp only [Bool.not_eq_true, Bool.or_eq_false_iff, bne, beq_iff_eq] at hhosts' + rcases hhosts' with ⟨hmaj, _⟩ + cases hbeq : h.major == 1 with + | false => simp [hbeq] at hmaj + | true => exact beq_iff_eq.mp hbeq + case nocl => + intro f hf' + by_cases hnamed : f.isNamed contentLengthName = true + · have hmem : f ∈ h.fields.filter (·.isNamed contentLengthName) := + List.mem_filter.mpr ⟨hf', hnamed⟩ + simp [heq] at hmem + · simpa [Bool.not_eq_true] using hnamed + case te => + simpa [ts] using hmatch + case method => + have hnot' := hnot + simp only [Bool.not_eq_true, Bool.or_eq_false_iff] at hnot' + rcases hnot' with ⟨_, hconn⟩ + simp only [Bool.and_eq_false_iff] at hconn + cases hconn with + | inl hne => + simpa [beq_eq_false_iff_ne] using hne + | inr hlen => + simp at hlen + case colon => + have hnot' := hnot + simp only [Bool.not_eq_true, Bool.or_eq_false_iff] at hnot' + rcases hnot' with ⟨hcol, _⟩ + simpa [beq_eq_false_iff_ne] using hcol + · injection hcore + · injection hcore + +/-- The weak form: chunked framing needs HTTP/1.1 or a later 1.x and no + Content-Length. The combined Transfer-Encoding value is not part of + this statement. -/ +theorem chunked_requires_http11_without_cl (h : Head) + (hf : determineFraming h = some .chunked) : + h.major = 1 ∧ 1 ≤ h.minor ∧ + (∀ f, f ∈ h.fields → f.isNamed contentLengthName = false) := by + rcases determineFraming_rejects_nonfinal_chunked h hf with ⟨hmaj, hmin, hcl, _, _, _⟩ + exact ⟨hmaj, hmin, hcl⟩ + +/-- A Transfer-Encoding field whose combined value is not a list of the + one coding `chunked` is not framed as chunked. -/ +theorem determineFraming_rejects_bad_te (h : Head) + (hte : ∃ f, f ∈ h.fields ∧ f.isNamed transferEncodingName = true) + (hbad : Grammar.chunkedList.isMatch + (List.intercalate [0x2C] + ((h.fields.filter (·.isNamed transferEncodingName)).map (·.value))) = false) : + determineFraming h ≠ some .chunked := by + have _ := hte + intro hf + rcases determineFraming_rejects_nonfinal_chunked h hf with ⟨_, _, _, hmatch, _, _⟩ + exact Bool.noConfusion (hmatch.symm.trans hbad) + +/-- HTTP/1.0 with a Transfer-Encoding field is not framed as chunked. -/ +theorem determineFraming_rejects_http10_te (h : Head) + (hminor : h.minor = 0) + (_hte : ∃ f, f ∈ h.fields ∧ f.isNamed transferEncodingName = true) : + determineFraming h ≠ some .chunked := by + intro hf + rcases determineFraming_rejects_nonfinal_chunked h hf with ⟨_, hmin, _⟩ + omega + +theorem determineFraming_disjoint (h : Head) (f1 f2 : Framing) + (h1 : determineFraming h = some f1) (h2 : determineFraming h = some f2) : + f1 = f2 := + Option.some.inj (h1.symm.trans h2) + +theorem takeBytes_length (bs : List UInt8) : takeBytes bs.length bs = bs := by + simpa using takeBytes_append bs [] + +theorem dropBytes_length (bs : List UInt8) : dropBytes bs.length bs = [] := by + simpa using dropBytes_append bs [] + +theorem parseRequest_serializeFixed (h : Head) (body : List UInt8) + (hf : h.framing = some (.length body.length)) + (hp : parseHead (h.print ++ body) = some (h, body)) : + parseRequest (h.print ++ body) = some { head := h, body := body, rest := [] } := by + simp [parseRequest, hp, hf, takeBytes_length, dropBytes_length] + +theorem parseRequest_serializeChunked (h : Head) (body : List UInt8) + (hlen : body.length ≤ maxUInt64) + (hf : h.framing = some .chunked) + (hp : parseHead (h.print ++ encodeChunked body) = some (h, encodeChunked body)) : + parseRequest (h.print ++ encodeChunked body) = + some { head := h, body := body, rest := [] } := by + simp [parseRequest, hp, hf, decodeChunked_encodeChunked body hlen] + +theorem parseRequest_stream_unique (stream tail1 tail2 : List UInt8) + (msgs1 msgs2 : List (Head × List UInt8)) + (h1 : StreamFrames msgs1 tail1 stream) (h2 : StreamFrames msgs2 tail2 stream) + (hlen : msgs1.length = msgs2.length) : + msgs1 = msgs2 ∧ tail1 = tail2 := by + induction h1 generalizing msgs2 tail2 with + | nil => + cases h2 with + | nil => exact ⟨rfl, rfl⟩ + | cons _ _ => simp at hlen + | cons hp hrest ih => + cases h2 with + | nil => simp at hlen + | cons hp2 hrest2 => + have hr : _ = _ := hp.symm.trans hp2 + cases hr + simp at hlen + rcases ih _ _ hrest2 hlen with ⟨rfl, rfl⟩ + exact ⟨rfl, rfl⟩ + +end Http diff --git a/Http/Framing.lean b/Http/Framing.lean index f0346c0..a1b219d 100644 --- a/Http/Framing.lean +++ b/Http/Framing.lean @@ -117,4 +117,7 @@ def Head.framing (h : Head) : Option Framing := if h.target.head? == some 0x3A || (h.method == connectName && fr != .length 0) then none else some fr +/-- `Head.framing`, named for the chunked-framing statements. -/ +def determineFraming (h : Head) : Option Framing := h.framing + end Http diff --git a/Http/Parse.lean b/Http/Parse.lean new file mode 100644 index 0000000..5cd03a6 --- /dev/null +++ b/Http/Parse.lean @@ -0,0 +1,162 @@ +/- + The body after a head `parseHead` has read. A length framing takes that + many octets. A chunked framing is decoded here, including chunk + extensions and the trailer section, which is parsed and then dropped. +-/ + +import Http.Head +import Http.Framing +import Http.Bytes + +namespace Http + +def crlf : List UInt8 := [13, 10] + +def isHex (b : UInt8) : Bool := + isDigit b || (97 ≤ b && b ≤ 102) || (65 ≤ b && b ≤ 70) + +/-- Chunk extensions: a token name and an optional token or quoted-string + (RFC 9112, section 7.1.1), not a whitelist of field characters. -/ + +def skipOWS : List UInt8 → List UInt8 + | 32 :: bs => skipOWS bs + | 9 :: bs => skipOWS bs + | bs => bs + +def takeToken (bs : List UInt8) : Option (List UInt8 × List UInt8) := + let tok := takeWhileBytes isTchar bs + if tok.isEmpty then none else some (tok, dropBytes tok.length bs) + +def isQdtext (b : UInt8) : Bool := + b == 9 || b == 32 || b == 33 || (35 ≤ b && b ≤ 91) || (93 ≤ b && b ≤ 126) || b ≥ 128 + +def isQuotedChar (b : UInt8) : Bool := + b == 9 || b == 32 || isVchar b + +def parseQuoted : List UInt8 → Option (List UInt8) + | [] => none + | 34 :: rest => some rest + | 92 :: c :: rest => if isQuotedChar c then parseQuoted rest else none + | b :: rest => if isQdtext b then parseQuoted rest else none + +/-- One extension, including the leading semicolon. Returns the unread suffix. -/ +def parseOneExt (bs : List UInt8) : Option (List UInt8) := + match skipOWS bs with + | 59 :: rest => + match takeToken (skipOWS rest) with + | none => none + | some (_, after) => + match skipOWS after with + | 61 :: restEq => + match skipOWS restEq with + | 34 :: quoted => parseQuoted quoted + | other => + match takeToken other with + | some (_, r) => some r + | none => none + | other => some other + | _ => none + +def parseExt (fuel : Nat) : List UInt8 → Bool + | [] => true + | bs => + match fuel with + | 0 => false + | fuel + 1 => + match parseOneExt bs with + | none => false + | some rest => + if rest.length < bs.length then parseExt fuel rest else false + +def parseChunkSize (line : List UInt8) : Option Nat := + let digits := takeWhileBytes isHex line + if digits.isEmpty then + none + else + match parseHex digits with + | none => none + | some n => + if parseExt line.length (dropBytes digits.length line) then some n else none + +def parseTrailers (fuel : Nat) (bs : List UInt8) : Option (List Field × List UInt8) := + match fuel with + | 0 => none + | fuel + 1 => + match splitCRLF bs with + | none => none + | some ([], rest) => some ([], rest) + | some (line, rest) => + match parseField line with + | none => none + | some h => + match parseTrailers fuel rest with + | none => none + | some (hs, rest') => some (h :: hs, rest') + +def decodeLoop (fuel : Nat) (acc : List UInt8) (buf : List UInt8) : + Option (List UInt8 × List Field × List UInt8) := + match fuel with + | 0 => none + | fuel + 1 => + match splitCRLF buf with + | none => none + | some (line, rest) => + match parseChunkSize line with + | none => none + | some 0 => + match parseTrailers rest.length rest with + | none => none + | some (ts, rest') => some (acc, ts, rest') + | some n => + if rest.length < n + 2 then + none + else + let data := takeBytes n rest + match dropBytes n rest with + | 13 :: 10 :: rest' => decodeLoop fuel (acc ++ data) rest' + | _ => none + +def decodeChunked (buf : List UInt8) : Option (List UInt8 × List Field × List UInt8) := + decodeLoop (buf.length + 1) [] buf + +def encodeChunked (body : List UInt8) : List UInt8 := + if body.isEmpty then + [48] ++ crlf ++ crlf + else + natToHexLower body.length ++ crlf ++ body ++ crlf ++ [48] ++ crlf ++ crlf + +/-- A request `parseHead` accepted, together with its body and the octets + that follow the body. -/ +structure FramedRequest where + head : Head + body : List UInt8 + rest : List UInt8 + deriving Repr + +/-- Read one request. The head is `parseHead`, and the body is framed by + `Head.framing`: a length takes that many octets, and chunked decoding + reads the chunk stream. -/ +def parseRequest (bs : List UInt8) : Option FramedRequest := + match parseHead bs with + | none => none + | some (hd, rest) => + match hd.framing with + | none => none + | some (.length n) => + if rest.length < n then none + else some { head := hd, body := takeBytes n rest, rest := dropBytes n rest } + | some .chunked => + match decodeChunked rest with + | none => none + | some (body, _, rest') => some { head := hd, body, rest := rest' } + +/-- Two readings of one byte string into the same number of requests. -/ +inductive StreamFrames : List (Head × List UInt8) → List UInt8 → List UInt8 → Prop where + | nil (tail : List UInt8) : StreamFrames [] tail tail + | cons {stream : List UInt8} {r : FramedRequest} {msgs : List (Head × List UInt8)} + {tail : List UInt8} : + parseRequest stream = some r → + StreamFrames msgs tail r.rest → + StreamFrames ((r.head, r.body) :: msgs) tail stream + +end Http diff --git a/Main.lean b/Main.lean index 613a9ac..258b09d 100644 --- a/Main.lean +++ b/Main.lean @@ -18,14 +18,17 @@ `Head.print` writes it, in hex, the octets after the head, in hex, and the framing (`length N`, `chunked` or `reject`); or REJECT + chunk HEX prints OK, then tab-separated: the body length, + the body in hex, and the octets after the body, + in hex; or REJECT A malformed request prints `error`: an unknown operation, or hex digits that are not lower case or not in pairs. A request line may end in LF or CR LF. - Every answer comes from the proved functions in `Http/`: `parseHead`, - `Head.print` and `Head.framing`. The hex coding and the request - handling here are covered by the tests, not by a proof. + A `head` answer comes from `parseHead`, `Head.print` and `Head.framing`. + A `chunk` answer comes from `parseRequest`. The hex coding and the + request handling here are covered by the tests, not by a proof. -/ import Http @@ -89,6 +92,12 @@ def request (line : String) : Option String := pure <| match parseHead bs with | some (hd, rest) => s!"OK\t{toHex hd.print}\t{toHex rest}\t{framingText hd.framing}" | none => "REJECT" + | ["chunk", h] => do + let bs ← hexBytes h.toList + pure <| match parseRequest bs with + | some r => + s!"OK\t{decimalText 64 r.body.length 0}\t{toHex r.body}\t{toHex r.rest}" + | none => "REJECT" | _ => none /-- The line without its line ending: LF, or CR LF. -/ diff --git a/Proofs.lean b/Proofs.lean index 6a6e36c..000bd02 100644 --- a/Proofs.lean +++ b/Proofs.lean @@ -1 +1,2 @@ import Proofs.Main +import Proofs.Chunked diff --git a/Proofs/Chunked.lean b/Proofs/Chunked.lean new file mode 100644 index 0000000..ab99a91 --- /dev/null +++ b/Proofs/Chunked.lean @@ -0,0 +1,61 @@ +import Http.ChunkProofs + +open Http + +theorem Spec.determineFraming_rejects_nonfinal_chunked (h : Head) + (hf : determineFraming h = some .chunked) : + h.major = 1 ∧ 1 ≤ h.minor ∧ + (∀ f, f ∈ h.fields → f.isNamed contentLengthName = false) ∧ + Grammar.chunkedList.isMatch + (List.intercalate [0x2C] + ((h.fields.filter (·.isNamed transferEncodingName)).map (·.value))) = true ∧ + h.method ≠ connectName ∧ h.target.head? ≠ some 0x3A := + Http.determineFraming_rejects_nonfinal_chunked h hf + +theorem Spec.chunked_requires_http11_without_cl (h : Head) + (hf : determineFraming h = some .chunked) : + h.major = 1 ∧ 1 ≤ h.minor ∧ + (∀ f, f ∈ h.fields → f.isNamed contentLengthName = false) := + Http.chunked_requires_http11_without_cl h hf + +theorem Spec.determineFraming_rejects_bad_te (h : Head) + (hte : ∃ f, f ∈ h.fields ∧ f.isNamed transferEncodingName = true) + (hbad : Grammar.chunkedList.isMatch + (List.intercalate [0x2C] + ((h.fields.filter (·.isNamed transferEncodingName)).map (·.value))) = false) : + determineFraming h ≠ some .chunked := + Http.determineFraming_rejects_bad_te h hte hbad + +theorem Spec.determineFraming_rejects_http10_te (h : Head) (hminor : h.minor = 0) + (hte : ∃ f, f ∈ h.fields ∧ f.isNamed transferEncodingName = true) : + determineFraming h ≠ some .chunked := + Http.determineFraming_rejects_http10_te h hminor hte + +theorem Spec.determineFraming_disjoint (h : Head) (f1 f2 : Framing) + (h1 : determineFraming h = some f1) (h2 : determineFraming h = some f2) : + f1 = f2 := + Http.determineFraming_disjoint h f1 f2 h1 h2 + +theorem Spec.decodeChunked_encodeChunked (body : List UInt8) (h : body.length ≤ maxUInt64) : + decodeChunked (encodeChunked body) = some (body, [], []) := + Http.decodeChunked_encodeChunked body h + +theorem Spec.parseRequest_serializeFixed (h : Head) (body : List UInt8) + (hf : h.framing = some (.length body.length)) + (hp : parseHead (h.print ++ body) = some (h, body)) : + parseRequest (h.print ++ body) = some { head := h, body := body, rest := [] } := + Http.parseRequest_serializeFixed h body hf hp + +theorem Spec.parseRequest_serializeChunked (h : Head) (body : List UInt8) + (hlen : body.length ≤ maxUInt64) (hf : h.framing = some .chunked) + (hp : parseHead (h.print ++ encodeChunked body) = some (h, encodeChunked body)) : + parseRequest (h.print ++ encodeChunked body) = + some { head := h, body := body, rest := [] } := + Http.parseRequest_serializeChunked h body hlen hf hp + +theorem Spec.parseRequest_stream_unique (stream tail1 tail2 : List UInt8) + (msgs1 msgs2 : List (Head × List UInt8)) + (h1 : StreamFrames msgs1 tail1 stream) (h2 : StreamFrames msgs2 tail2 stream) + (hlen : msgs1.length = msgs2.length) : + msgs1 = msgs2 ∧ tail1 = tail2 := + Http.parseRequest_stream_unique stream tail1 tail2 msgs1 msgs2 h1 h2 hlen diff --git a/README.md b/README.md index 0424f61..613124a 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,8 @@ It prints the request line and each field, then the framing, here `length 5`. It - `Http.parseHead : List UInt8 → Option (Head × List UInt8)` reads a head and gives the octets after it. - `Http.Head.print` writes a head. -- `Http.Head.framing : Head → Option Framing` gives the framing, or `none` when this library refuses the request. +- `Http.Head.framing : Head → Option Framing` gives the framing, or `none` when this library refuses the request. `Http.determineFraming` is that function. +- `Http.parseRequest` reads one request: the head, then the body that framing names. `Http.decodeChunked` reads a chunked body, and `Http.encodeChunked` writes one. - `Http.Re.isMatch` matches a string against a rule of the grammar. `SPEC.md` says what each one does. @@ -51,13 +52,23 @@ The statements are in `Spec.lean`, and the proofs are in `Proofs/`. The statemen | A request with both Transfer-Encoding and Content-Length is rejected. | `Spec.framing_te_cl` | | A request with two Content-Length field lines is rejected, even when they agree. | `Spec.framing_two_cl` | | Test vectors, each with the answer `framing` gives. Accepted with no body: the examples of RFC 9112 sections 3.2.1 to 3.2.4, an HTTP/1.0 request with no Host, `CONNECT [::1]:65535`, `CONNECT http:80`, whose host is `http`, and `GET HTTP://a:/x`, with the scheme in upper case and an empty port. Accepted with a body: `Content-Length: 5` frames the five octets after the head, and `Transfer-Encoding: Chunked` is chunked. So are Transfer-Encoding lists with empty elements: `chunked,`, `, chunked`, and a `chunked` line with an empty Transfer-Encoding line, and `chunked` after a tab or two spaces. An empty Host value and `CONNECT a:443` with `Content-Length: 0` are accepted with no body. The classic shapes of request smuggling are rejected: two Content-Length values, Content-Length with Transfer-Encoding, `xchunked`, `chunked, identity`, `chunked, chunked`, `chunked;a=b`, a `chunked` line with a line of another coding, whitespace before a colon, a folded line, a bare LF, a bare CR, and Transfer-Encoding in HTTP/1.0. So are a missing Host and a repeated Host in HTTP/1.1, the Host values `:80` and `:`, a `CONNECT` with an empty port, port 0, port 65536, the target `/` or `:80`, or with content by `Content-Length: 5` or `Transfer-Encoding: chunked`, `GET :80` and `OPTIONS :`, and three `GET` targets: `http:` (no authority), `http:///a` (an empty host) and `https://u@a/` (userinfo). | `Spec.head_vectors` | +| Chunked framing implies major version 1, a minor version of at least 1, no Content-Length field, and a combined Transfer-Encoding value that matches a list of the one coding `chunked`, empty elements allowed. The method is not `CONNECT`, and the target does not begin with `:`. `determineFraming` is `Head.framing`. | `Spec.determineFraming_rejects_nonfinal_chunked` | +| The same hypothesis implies HTTP/1.1 or a later 1.x and no Content-Length, and this statement does not mention the combined Transfer-Encoding value. | `Spec.chunked_requires_http11_without_cl` | +| A Transfer-Encoding field whose combined value is not that list is not framed as chunked. | `Spec.determineFraming_rejects_bad_te` | +| HTTP/1.0 with a Transfer-Encoding field is not framed as chunked. | `Spec.determineFraming_rejects_http10_te` | +| A head has at most one framing. | `Spec.determineFraming_disjoint` | +| `decodeChunked` reads the body `encodeChunked` wrote, for a body of at most 2^64 − 1 octets. An empty body is the chunk `0`. | `Spec.decodeChunked_encodeChunked` | +| When `parseHead` returns a printed head and the octets of a length-framed body, `parseRequest` returns that head and body. | `Spec.parseRequest_serializeFixed` | +| When `parseHead` returns a printed head framed as chunked, followed by `encodeChunked` of a body of at most 2^64 − 1 octets, `parseRequest` returns that head and body. | `Spec.parseRequest_serializeChunked` | +| Two readings of one byte string into the same number of requests agree on the requests and on the octets left over. | `Spec.parseRequest_stream_unique` | The command-line tool in `Main.lean` calls these functions directly. ## What is not proved - Reading requests and printing answers in `Main.lean`: the hex coding in `serve` mode, reading standard input in `check` mode, and the output and exit codes. The differential tests cover them, with malformed requests and CR LF line endings. -- The body. This library frames the body but does not read it. It does not decode chunked data, chunk extensions or trailers, and does not count the octets of a length-framed body. +- The command `chunk` in `Main.lean`, which prints the body `parseRequest` read. The differential tests cover it, including `GET foo:`, `CONNECT []:443`, `GET http://a/<`, a quoted chunk extension, an unknown extension, `1;=x`, an unclosed quote, and a chunk terminator that is not CRLF. +- Which chunk streams `decodeChunked` accepts, beyond the round trip with `encodeChunked`. The extension grammar is the one in `SPEC.md`. The proofs do not say that every extension the RFC allows is accepted, or that every one it rejects is rejected. The tests check the cases named above. - What a server does with a request it accepts: the meaning of the method, the target and the fields, routing by Host, and the response. - Completeness against the RFCs. No theorem says that this library refuses every request the RFCs say a server must refuse. The statements are about its own rules, written from the RFCs, and `SPEC.md` says which of its refusals the RFCs require. What a server does after a refusal is also outside this library. For example, for an invalid Content-Length, RFC 9112 section 6.3 says "the server MUST respond with a 400 (Bad Request) status code and then close the connection". This library only decides that the request is refused. - Repairs the RFC allows. Where RFC 9112 or RFC 9110 lets a recipient either reject or repair, this library always rejects. That covers a bare LF as a line end, a bare CR, whitespace before the first field line, a folded line, whitespace other than one SP in the request line, a NUL in a value, Transfer-Encoding with Content-Length, two Content-Length lines that agree, and a Content-Length list of equal values such as `5, 5`. A server that repairs these accepts requests that this one refuses. @@ -129,7 +140,7 @@ The test script compares the tool with the rules of RFC 9112, RFC 9110 and RFC 3 - `Http/Types.lean`: the head, field and framing types. It is part of the specification. - `Http/Abnf.lean`: the grammar notation, a small language of regular expressions over octets. It is part of the specification. -- `Http/Match.lean`, `Http/Grammar.lean`, `Http/Head.lean`, `Http/Framing.lean`: the code under proof. `Http/Grammar.lean` is the code's copy of the rules for the target, an http or https target, the Host value and a Transfer-Encoding list, which the proofs show equal to the spec's. +- `Http/Match.lean`, `Http/Grammar.lean`, `Http/Head.lean`, `Http/Framing.lean`, `Http/Bytes.lean`, `Http/Parse.lean`: the code under proof. `Http/Grammar.lean` is the code's copy of the rules for the target, an http or https target, the Host value and a Transfer-Encoding list, which the proofs show equal to the spec's. `Http/Parse.lean` reads the body. `Http/ChunkProofs.lean` holds the proofs of the chunked statements, which `Spec.lean` and `Proofs/` both use. - `Spec.lean`: the statements, with `sorry` for proofs. A reviewer approves this file. - `Spec/Abnf.lean`: what a rule of the notation matches. - `Spec/Rules.lean`: the ABNF rules, from RFC 5234, RFC 9110, RFC 3986 and RFC 9112. diff --git a/SPEC.md b/SPEC.md index a33a2b5..d873099 100644 --- a/SPEC.md +++ b/SPEC.md @@ -49,7 +49,7 @@ A field's value does not include the optional whitespace around it (section 5.1) - `authority-form`, a host and port; - `asterisk-form`, `*`. - Each form is in its RFC 3986 grammar, with percent-escapes of two hex digits, and IPv6 and IPvFuture literals. A fragment (`#`) is not part of any form. The head keeps percent-escapes as written and does not decode them. + Each form is in its RFC 3986 grammar, with percent-escapes of two hex digits, and IPv6 and IPvFuture literals. A fragment (`#`) is not part of any form. The head keeps percent-escapes as written and does not decode them. `absolute-form` is an `absolute-URI`: a scheme, `:`, a hier-part, and an optional query. The hier-part may be empty (`path-empty`), so `GET foo:` is absolute-form, with the scheme `foo` and nothing after the colon. With `Host: a` and neither Transfer-Encoding nor Content-Length, that request is accepted and its body has length 0. RFC 9112 allows a recipient to repair some of these instead of rejecting them. Section 2.2 allows a single LF as a line end, and a bare CR replaced by SP. Section 3 allows parsing the request line on other whitespace. Section 5.2 allows a folded line to be replaced by SP. This library takes none of those choices: it rejects. Section 5.1 leaves no choice for whitespace before a colon: "A server MUST reject ... any received request message that contains whitespace between a header field name and colon". @@ -62,7 +62,7 @@ RFC 9112 allows a recipient to repair some of these instead of rejecting them. S - **The version:** only major version 1 is accepted. RFC 9110 section 6.2 lets a server refuse other major versions with 505. A minor version of 1 or more is handled as 1.1, as section 6.2 asks of a version higher than the recipient implements. - **Host:** a request with more than one Host field line is refused. So is a request with a minor version of 1 or more and no Host line (RFC 9112, section 3.2). A Host value must be empty, or a host that is not empty with an optional port: `[ hostNonEmpty [ ":" port ] ]` (`Spec.hostField`). So `Host: :80` and `Host: :` are refused, though `uri-host [ ":" port ]` allows them, since `reg-name` may be empty. The reason is RFC 9112 section 3.3: for a target that is not in absolute-form, the server builds the target URI from the scheme, "://", and the Host value as its authority. `GET /` with `Host: :80` has the target URI `http://:80/`, with an empty host, and RFC 9110 section 4.2.1 says a recipient that processes such a URI "MUST reject it as invalid". An empty Host value is different. RFC 9112 section 3.2 tells a client to send one when the target URI has no authority, and section 3.3 says the server "can reject the request or determine whether a configured default applies". This library accepts it and leaves that choice to the server. - **An empty host in authority-form:** a target that begins with `:` is refused, whatever the method. In a `request-target` that is exactly an authority-form target with an empty host, such as `:80` or `:`: origin-form begins with `/`, absolute-form with a scheme, which begins with a letter, and asterisk-form is `*`. RFC 9112 section 3.3 says "If the request-target is in authority-form, the target URI's authority component is the request-target", for any method. So `GET :80` has the target URI `http://:80`, with an empty host, which RFC 9110 section 4.2.1 says a recipient "MUST reject". `Spec.AuthorityHostOK` states this. -- **CONNECT:** when the method is `CONNECT`, the target must be `uri-host ":" port` with a host that is not empty (by the rule above), and the port, read as a decimal number, must be from 1 to 65535. Leading zeros are allowed, so `a:00080` is port 80. An empty port is refused, and so is a target that is not a host and port, such as `/` or `*`. RFC 9110 section 9.3.6 says "A server MUST reject a CONNECT request that targets an empty or invalid port number". It does not say which port numbers are invalid. Here a valid one is from 1 to 65535: a TCP port number is 16 bits (RFC 9293, section 3.1), and 0 is a Reserved port number (RFC 6335, section 6). A server need not accept CONNECT at all ("An origin server MAY accept a CONNECT request"), so refusing port 0 is allowed however "invalid" is read. The method is compared as written, since "The method token is case-sensitive" (section 9.1). So `connect` is another method, and this rule does not apply to it. +- **CONNECT:** when the method is `CONNECT`, the target must be `uri-host ":" port` with a host that is not empty (by the rule above), and the port, read as a decimal number, must be from 1 to 65535. Leading zeros are allowed, so `a:00080` is port 80. An empty port is refused, and so is a target that is not a host and port, such as `/` or `*`. `CONNECT []:443` is refused: `[]` is not an IP-literal, which is `[` then an IPv6address or an IPvFuture then `]`, and it is not a reg-name. `Host: []` is refused for the same reason. RFC 9110 section 9.3.6 says "A server MUST reject a CONNECT request that targets an empty or invalid port number". It does not say which port numbers are invalid. Here a valid one is from 1 to 65535: a TCP port number is 16 bits (RFC 9293, section 3.1), and 0 is a Reserved port number (RFC 6335, section 6). A server need not accept CONNECT at all ("An origin server MAY accept a CONNECT request"), so refusing port 0 is allowed however "invalid" is read. The method is compared as written, since "The method token is case-sensitive" (section 9.1). So `connect` is another method, and this rule does not apply to it. - **CONNECT has no content:** a `CONNECT` with Transfer-Encoding, or with a Content-Length that is not 0, is refused (`Spec.ConnectNoContent`). RFC 9110 section 9.3.6 says "A CONNECT request message does not have content". Read alone, RFC 9112 section 6.3 would frame a body after such a head, and parsers differ on those octets: Go reads them as a body, and Node passes them to the tunnel. Refusing the request removes that difference. A server need not accept CONNECT at all (section 9.3.6: "An origin server MAY accept a CONNECT request"). `Content-Length: 0` is accepted, since it frames no content. - **http and https targets:** when the method is not exactly `CONNECT` and the target starts with `http:` or `https:`, in any case, the target must match @@ -74,6 +74,7 @@ RFC 9112 allows a recipient to repair some of these instead of rejecting them. S - an empty host, such as `http://`, `http:///a` or `https://:443/`; - userinfo, such as `https://u@a/`, or the empty userinfo of `http://@a/`; - no authority, such as `http:` or `http:/a`. That is not an `http-URI` or `https-URI` at all, since both need `"://" authority`. + - a path that is not `path-abempty`. `path-abempty` is `*( "/" segment )` and `segment` is `*pchar`, so `GET http://a/<` is refused: `<` is a VCHAR and is not a `pchar`. An empty port is allowed, since "If the port subcomponent is empty or not given, TCP port 80 (the reserved port for WWW services) is the default" (section 4.2.1), and section 4.2.2 says the same of port 443. So `HTTP://a:/x` is accepted. A CONNECT target is not a URI. It consists "of only the host and port number of the tunnel destination" (section 9.3.6), so this rule does not apply to it: `CONNECT http:80` names the host `http` and the port 80. The method is compared as written here too, so `connect http:80` is held to this rule, and refused. With any method other than `CONNECT`, `http:80` is refused, though it also fits the authority-form. - **Transfer-Encoding:** all its field lines are read as one value. That combined field value is their values in order, joined with commas (RFC 9110, section 5.2). Transfer-Encoding is accepted only when the combined value is a list of the one coding `chunked`, in any case, in a request with a minor version of 1 or more and no Content-Length. Then the body is chunked. The list may have empty elements, which "do not contribute to the count of elements present", and RFC 9110 section 5.6.1.2 says a recipient MUST accept such lists. So the value must match `*( OWS "," OWS ) "chunked" *( OWS "," OWS )`. For example, `chunked,` and `, chunked` are chunked, and so is a `chunked` line together with an empty Transfer-Encoding line. Every other use is refused: @@ -83,7 +84,18 @@ RFC 9112 allows a recipient to repair some of these instead of rejecting them. S - Chunked twice, such as `chunked, chunked`, or `chunked` on two lines. Section 6.1 says "A sender MUST NOT apply the chunked transfer coding more than once", and RFC 9110 section 15.5.1 lets a server answer "invalid request message framing" with 400. - Another coding before chunked, such as `gzip, chunked`. This library does not understand that coding, and section 6.1 says a server that receives "a transfer coding it does not understand SHOULD respond with 501". - **Content-Length:** with no Transfer-Encoding, it must be one field line whose value is `1*DIGIT` (RFC 9110, section 8.6). Then the body has that many octets. Section 6.3 lets a recipient accept a list of equal values such as `5, 5`. This library refuses it, and refuses two Content-Length lines even when they agree. -- **Neither field:** the body has length 0, by section 6.3: "If this is a request message and none of the above are true, then the message body length is zero". +- **Neither field:** the body has length 0, by section 6.3: "If this is a request message and none of the above are true, then the message body length is zero". So `GET foo: HTTP/1.1` with `Host: a` and no Transfer-Encoding or Content-Length is accepted with a body of length 0. + +## Decoding a chunked body + +When the framing is chunked, `decodeChunked` reads the body (RFC 9112, section 7.1). `parseRequest` is that reading for one whole request: `parseHead`, then the body `Head.framing` names. A length takes that many octets. Chunked uses `decodeChunked`, and the trailer section is parsed and then discarded. + +- A chunk-size is `1*HEXDIG`. This library refuses a size above 2^64 − 1. +- A chunk extension is a token name and an optional `=` followed by a token or a quoted-string (section 7.1.1). It is not a whitelist of characters. `1;=x` is refused, because the name is missing. An unclosed quoted-string is refused. `1;a="x"` is accepted, and so is an unknown well-formed extension such as `1;foo`. +- The octets of a chunk are followed by CRLF. Any other terminator is refused. +- A chunk of size 0 ends the body. `encodeChunked` of an empty body is the two lines `0` and the empty line. + +`determineFraming` is `Head.framing`. Chunked framing implies major version 1, a minor version of at least 1, no Content-Length field, and a combined Transfer-Encoding value that matches the one-coding list above. It also implies that the method is not `CONNECT` and that the target does not begin with `:`. ## Which refusals the RFCs require @@ -118,7 +130,7 @@ No theorem says that this library refuses every request the RFCs require a serve ## Errors -`parseHead` returns `none` for any input that does not start with a complete head of the grammar. That includes input that ends before the empty line. `Head.framing` returns `none` for a request this library refuses. No function can fail any other way. +`parseHead` returns `none` for any input that does not start with a complete head of the grammar. That includes input that ends before the empty line, `CONNECT []:443`, and `GET http://a/<`. `Head.framing` returns `none` for a request this library refuses. `parseRequest` returns `none` when the head is refused or is not of the grammar, when a length-framed body is shorter than its Content-Length, or when the chunk stream is not of the grammar above. No function can fail any other way. ## The command-line tool @@ -127,7 +139,7 @@ The command-line tool in `Main.lean` has two modes: - `httphead check` reads a request on standard input. It prints the request line, each field and the framing: `length N`, `chunked` or `reject`. It prints `invalid` for input with no head of the grammar. It exits with 0 only for a request this library accepts. - `httphead serve` answers requests from the differential tests, one per line, with octets sent as hex digits. The header comment of `Main.lean` gives the protocol. -Every answer comes from `parseHead`, `Head.print` and `Head.framing`. +Every `head` answer comes from `parseHead`, `Head.print` and `Head.framing`. A `chunk` answer comes from `parseRequest`. ## What must always hold @@ -143,6 +155,14 @@ Each property is a theorem in `Spec.lean`, under the name given. | Transfer-Encoding with Content-Length is refused. | `Spec.framing_te_cl` | | Two Content-Length field lines are refused. | `Spec.framing_two_cl` | | The examples of RFC 9112 sections 3.2.1 to 3.2.4 are accepted, with no body, and so are an HTTP/1.0 request with no Host, `CONNECT [::1]:65535`, `CONNECT http:80` and `GET HTTP://a:/x`. So are an empty Host value and `CONNECT a:443` with `Content-Length: 0`. A length and a chunked body are framed. Transfer-Encoding lists with empty elements, on one line or two, are chunked, and so is `chunked` after a tab or two spaces. The shapes of request smuggling are refused, and so is `chunked;a=b`. Also refused: a missing or repeated Host in HTTP/1.1; the Host values `:80` and `:`; a `CONNECT` with an empty port, port 0, port 65536, the target `/` or `:80`, or with content by `Content-Length: 5` or `Transfer-Encoding: chunked`; `GET :80` and `OPTIONS :`; and the targets `http:`, `http:///a` and `https://u@a/`. | `Spec.head_vectors` | +| Chunked framing implies HTTP/1.1 or a later 1.x, no Content-Length, a combined Transfer-Encoding value that is a list of the one coding `chunked`, a method other than `CONNECT`, and a target that does not begin with `:`. | `Spec.determineFraming_rejects_nonfinal_chunked` | +| The same, without the claim about the combined Transfer-Encoding value. | `Spec.chunked_requires_http11_without_cl` | +| A Transfer-Encoding value that is not that list is not framed as chunked. | `Spec.determineFraming_rejects_bad_te` | +| Transfer-Encoding in HTTP/1.0 is not framed as chunked. | `Spec.determineFraming_rejects_http10_te` | +| A head has at most one framing. | `Spec.determineFraming_disjoint` | +| `decodeChunked` reads the body `encodeChunked` wrote, when the body has at most 2^64 − 1 octets. | `Spec.decodeChunked_encodeChunked` | +| After a printed head that `parseHead` returns, a length-framed body and a chunked body written by `encodeChunked` are the bodies `parseRequest` returns. | `Spec.parseRequest_serializeFixed`, `Spec.parseRequest_serializeChunked` | +| Two readings of one byte string into the same number of requests agree. | `Spec.parseRequest_stream_unique` | Together, these fix every output. diff --git a/Spec.lean b/Spec.lean index b968266..712bc12 100644 --- a/Spec.lean +++ b/Spec.lean @@ -7,6 +7,7 @@ -/ import Http +import Http.ChunkProofs import Spec.Abnf import Spec.Rules import Spec.Defs @@ -170,3 +171,82 @@ theorem Spec.head_vectors : (·.1.framing) = some (.length 0) ∧ (parseHead (Spec.ascii "CONNECT http:80 HTTP/1.1\r\nHost: http\r\n\r\n")).bind (·.1.framing) = some (.length 0) := sorry + +/-! ## Chunked framing and the body -/ + +/-- Chunked framing is HTTP/1.1 or a later 1.x, with no Content-Length + field, and the combined Transfer-Encoding value matches a list of the + one coding `chunked` (empty elements allowed). The method is not + `CONNECT`, and the target does not begin with `:`. -/ +theorem Spec.determineFraming_rejects_nonfinal_chunked (h : Head) + (hf : determineFraming h = some .chunked) : + h.major = 1 ∧ 1 ≤ h.minor ∧ + (∀ f, f ∈ h.fields → f.isNamed contentLengthName = false) ∧ + Grammar.chunkedList.isMatch + (List.intercalate [0x2C] + ((h.fields.filter (·.isNamed transferEncodingName)).map (·.value))) = true ∧ + h.method ≠ connectName ∧ h.target.head? ≠ some 0x3A := + Http.determineFraming_rejects_nonfinal_chunked h hf + +/-- The same hypothesis, without the claim about the combined + Transfer-Encoding value. -/ +theorem Spec.chunked_requires_http11_without_cl (h : Head) + (hf : determineFraming h = some .chunked) : + h.major = 1 ∧ 1 ≤ h.minor ∧ + (∀ f, f ∈ h.fields → f.isNamed contentLengthName = false) := + Http.chunked_requires_http11_without_cl h hf + +/-- A Transfer-Encoding field whose combined value is not that list is + not framed as chunked. -/ +theorem Spec.determineFraming_rejects_bad_te (h : Head) + (hte : ∃ f, f ∈ h.fields ∧ f.isNamed transferEncodingName = true) + (hbad : Grammar.chunkedList.isMatch + (List.intercalate [0x2C] + ((h.fields.filter (·.isNamed transferEncodingName)).map (·.value))) = false) : + determineFraming h ≠ some .chunked := + Http.determineFraming_rejects_bad_te h hte hbad + +/-- HTTP/1.0 with a Transfer-Encoding field is not framed as chunked. -/ +theorem Spec.determineFraming_rejects_http10_te (h : Head) (hminor : h.minor = 0) + (hte : ∃ f, f ∈ h.fields ∧ f.isNamed transferEncodingName = true) : + determineFraming h ≠ some .chunked := + Http.determineFraming_rejects_http10_te h hminor hte + +/-- A head has at most one framing. -/ +theorem Spec.determineFraming_disjoint (h : Head) (f1 f2 : Framing) + (h1 : determineFraming h = some f1) (h2 : determineFraming h = some f2) : + f1 = f2 := + Http.determineFraming_disjoint h f1 f2 h1 h2 + +/-- `decodeChunked` reads what `encodeChunked` wrote, for a body of at + most 2^64 − 1 octets. An empty body is the chunk `0`. -/ +theorem Spec.decodeChunked_encodeChunked (body : List UInt8) (h : body.length ≤ maxUInt64) : + decodeChunked (encodeChunked body) = some (body, [], []) := + Http.decodeChunked_encodeChunked body h + +/-- A length-framed body that `parseHead` returns after the printed head + is the body `parseRequest` returns, with nothing after it. -/ +theorem Spec.parseRequest_serializeFixed (h : Head) (body : List UInt8) + (hf : h.framing = some (.length body.length)) + (hp : parseHead (h.print ++ body) = some (h, body)) : + parseRequest (h.print ++ body) = some { head := h, body := body, rest := [] } := + Http.parseRequest_serializeFixed h body hf hp + +/-- A chunked body that `encodeChunked` wrote, after a printed head that + `parseHead` returns and that is framed as chunked, is the body + `parseRequest` returns. -/ +theorem Spec.parseRequest_serializeChunked (h : Head) (body : List UInt8) + (hlen : body.length ≤ maxUInt64) (hf : h.framing = some .chunked) + (hp : parseHead (h.print ++ encodeChunked body) = some (h, encodeChunked body)) : + parseRequest (h.print ++ encodeChunked body) = + some { head := h, body := body, rest := [] } := + Http.parseRequest_serializeChunked h body hlen hf hp + +/-- Two readings of one byte string into the same number of requests + agree on the requests and on the octets left over. -/ +theorem Spec.parseRequest_stream_unique (stream tail1 tail2 : List UInt8) + (msgs1 msgs2 : List (Head × List UInt8)) + (h1 : StreamFrames msgs1 tail1 stream) (h2 : StreamFrames msgs2 tail2 stream) + (hlen : msgs1.length = msgs2.length) : + msgs1 = msgs2 ∧ tail1 = tail2 := + Http.parseRequest_stream_unique stream tail1 tail2 msgs1 msgs2 h1 h2 hlen diff --git a/bar.toml b/bar.toml index 4d4f16c..72a1f06 100644 --- a/bar.toml +++ b/bar.toml @@ -22,7 +22,13 @@ files = [ "Http/Types.lean", "Http/Abnf.lean", ] # The code under proof: the functions the statements are about. -code = ["Http.Re.isMatch", "Http.parseHead", "Http.Head.print", "Http.Head.framing"] +code = [ + "Http.Re.isMatch", "Http.parseHead", "Http.Head.print", "Http.Head.framing", + "Http.determineFraming", "Http.Field.isNamed", "Http.contentLengthName", + "Http.transferEncodingName", "Http.connectName", "Http.Grammar.chunkedList", + "Http.decodeChunked", "Http.encodeChunked", "Http.parseRequest", "Http.maxUInt64", + "Http.FramedRequest", "Http.FramedRequest.mk", "Http.StreamFrames", +] # One entry per approval, added in a pull request the reviewer approves. # [[spec.approval]] diff --git a/comparator.json b/comparator.json index 9cef432..2e4eafb 100644 --- a/comparator.json +++ b/comparator.json @@ -9,7 +9,16 @@ "Spec.framing_iff", "Spec.framing_te_cl", "Spec.framing_two_cl", - "Spec.head_vectors" + "Spec.head_vectors", + "Spec.determineFraming_rejects_nonfinal_chunked", + "Spec.chunked_requires_http11_without_cl", + "Spec.determineFraming_rejects_bad_te", + "Spec.determineFraming_rejects_http10_te", + "Spec.determineFraming_disjoint", + "Spec.decodeChunked_encodeChunked", + "Spec.parseRequest_serializeFixed", + "Spec.parseRequest_serializeChunked", + "Spec.parseRequest_stream_unique" ], "permitted_axioms": [ "propext", diff --git a/tests/CROSS-CHECK.md b/tests/CROSS-CHECK.md index 3efeb63..de3c305 100644 --- a/tests/CROSS-CHECK.md +++ b/tests/CROSS-CHECK.md @@ -3,7 +3,7 @@ Written by `tests/cross_check.py`, a one-off comparison that is not part of the verified bar. Its docstring says how the heads are sent and judged, and how a difference gets its category. RFC 9112 lets a recipient repair some malformed heads instead of rejecting them, so a difference here is a different choice, not always an error. - Heads: the first 5991 inputs of the differential tests (seed 20261025, `--count 6000`, less the malformed requests to the tool). Every parser is sent all 5991. A head whose verdicts differ is also sent again with features changed, to find its category. -- This project rejects 4195 of them. It accepts 1083 with no body, 481 with a Content-Length from 1 to 1,000,000, 223 as chunked, and 9 with a Content-Length above 1,000,000. +- This project rejects 4196 of them. It accepts 1083 with no body, 480 with a Content-Length from 1 to 1,000,000, 223 as chunked, and 9 with a Content-Length above 1,000,000. - Sent after the head: nothing after a head this project rejects; the whole body for a Content-Length up to 1,000,000; `5\r\nhello\r\n0\r\n\r\n` (5 octets of data) for chunked; nothing for a Content-Length above 1,000,000, so those heads are judged on the head alone. - A parser accepts a head when it reads it and passes the request on, even when it then waits for a body that is never sent. - In the tables, "accepts" means the other parser accepts a head this project rejects, and "rejects" means it rejects a head this project accepts. The examples show the head as sent, cut at 100 octets. @@ -12,10 +12,10 @@ Written by `tests/cross_check.py`, a one-off comparison that is not part of the ## Node v22.23.4 (llhttp) - Heads sent: 5991 -- Same verdict: 4422 (920 both accept, 3502 both reject) -- Different verdict: 1563 (687 it accepts and this project rejects, 876 it rejects and this project accepts) +- Same verdict: 4418 (918 both accept, 3500 both reject) +- Different verdict: 1567 (690 it accepts and this project rejects, 877 it rejects and this project accepts) - No answer: 6 -- Body lengths compared, where both accept: 875. Of those, 581 have no body and 294 a non-zero length (261 by Content-Length, 33 chunked). 875 read the same length and 0 another. +- Body lengths compared, where both accept: 873. Of those, 580 have no body and 293 a non-zero length (260 by Content-Length, 33 chunked). 873 read the same length and 0 another. - Not compared, where both accept: 0 with a Content-Length above 1,000,000, and 45 whose answer has no count. The heads it rejects and this project accepts, by the feature whose change makes it accept them: alone, or together with other changes. @@ -24,7 +24,7 @@ The heads it rejects and this project accepts, by the feature whose change makes | --- | --- | --- | | a method other than the registered ones | 225 | 138 | | the authority-form with a method other than CONNECT | 122 | 76 | -| an absolute-form target with no authority | 123 | 61 | +| an absolute-form target with no authority | 124 | 61 | | HTTP/1.2 to 1.9 | 93 | 80 | | a Transfer-Encoding list with empty elements | 62 | 69 | | a tab after a Content-Length or Transfer-Encoding value | 15 | 12 | @@ -36,9 +36,9 @@ Where it differs from this project (the two shortest examples): | It | Category | Heads | Examples | | --- | --- | --- | --- | | rejects | a method other than the registered ones | 225 | `b'Connect / HTTP/1.1\r\nHost: a\r\n\r\n'`, `b'IyUqp * HTTP/1.1\r\nhOsT: [::]:80 \r\n\r\n'` | +| accepts | a Host value with a bad IP literal | 130 | `b'GET / HTTP/1.1\r\nHost: []\r\n\r\n'`, `b'GET / HTTP/1.1\r\nHost: ::1\r\n\r\n'` | | accepts | a major version other than 1 | 130 | `b'GET / HTTP/2.0\r\nHost: a\r\n\r\n'`, `b'DELETE * HTTP/2.0\r\nhost::80 \r\n\r\n'` | -| accepts | a Host value with a bad IP literal | 129 | `b'GET / HTTP/1.1\r\nHost: ::1\r\n\r\n'`, `b'GET / HTTP/1.1\r\nHost: [v.a]\r\n\r\n'` | -| rejects | an absolute-form target with no authority | 123 | `b'GET a:80 HTTP/1.1\r\nHost: a\r\n\r\n'`, `b'GET htt:a HTTP/1.1\r\nHost: a\r\n\r\n'` | +| rejects | an absolute-form target with no authority | 124 | `b'GET a:80 HTTP/1.1\r\nHost: a\r\n\r\n'`, `b'GET foo: HTTP/1.1\r\nHost: a\r\n\r\n'` | | rejects | the authority-form with a method other than CONNECT | 122 | `b'POST 200.9.99.250:443 HTTP/1.1\r\nHOST: \t\r\n\r\n'`, `b'OPTIONS [::]:0 HTTP/1.1\r\neqg: \r\nhOsT: ~x \r\n\r\n'` | | rejects | HTTP/1.2 to 1.9 | 93 | `b'GET / HTTP/1.2\r\nHost: a\r\n\r\n'`, `b'HEAD /RUXBl HTTP/1.2\r\nhost: [::]:80 \r\n\r\n'` | | accepts | two Host lines | 70 | `b'GET / HTTP/1.0\r\nHost: a\r\nHost: b\r\n\r\n'`, `b'GET / HTTP/1.1\r\nHost: a\r\nHost: a\r\n\r\n'` | @@ -57,8 +57,8 @@ Where it differs from this project (the two shortest examples): | accepts | CONNECT with an empty host | 18 | `b'CONNECT :443 HTTP/1.0\r\n\r\n'`, `b'CONNECT : HTTP/1.1\r\nHost: a\r\n\r\n'` | | accepts | Transfer-Encoding in HTTP/1.0 | 18 | `b'POST /a HTTP/1.0\r\nTransfer-Encoding: chunked\r\n\r\n'`, `b'OPTIONS /S1/@ HTTP/1.0\r\nm+PTgQe:_ \t\t\xbfwK \r\nHost:[::D5a:ed32] \r\nTRANSFER-ENCODING: gzip, chunked \r\n\r\n'` | | rejects | together: a method other than the registered ones; a Transfer-Encoding list with empty elements | 18 | `b'OZxN!T. * HTTP/1.1\r\nHOST:10.255.255.1\r\n7S: h zQo:!Je \r\nTransfer-Encoding: \t CHUNKED ,\r\n*2u3: \tZw \t \r'`, `b'\r\n\r\nj /=nLs@/%47?Fu HTTP/1.1\r\nHost:[vFA.v&nb]\r\ntransfer-encoding:\t , ,\t \t , ChUNKed , \t , \r\nsW:'` | +| accepts | a target with a bad IP literal | 17 | `b'CONNECT []:443 HTTP/1.1\r\nHost: a\r\n\r\n'`, `b'GET http://[v1.]/ HTTP/1.1\r\nHost: a\r\n\r\n'` | | rejects | together: HTTP/1.2 to 1.9; a Transfer-Encoding list with empty elements | 17 | `b"OPTIONS /U?XPY HTTP/1.9\r\nko3R69KF: \x88Y'f\t@$\r\nHost:\t[D02d:C4:F:A:fe58:a7:8:e30]\t\r\nTransfer-Encoding:,"`, `b"GET /'~XD/:P?9nh1s/+o HTTP/1.9\r\nTRANSFER-ENCODING: Chunked \r\nTransfer-Encoding: ,\t\r\nHOST: 199.9.249"` | -| accepts | a target with a bad IP literal | 16 | `b'GET http://[v1.]/ HTTP/1.1\r\nHost: a\r\n\r\n'`, `b'GET http://[1::2::3]/ HTTP/1.1\r\nHost: x\r\n\r\n'` | | accepts | other whitespace in the request line | 16 | `b'GET /a HTTP/1.1\r\nHost: x\r\n\r\n'`, `b'PATCH * HTTP/1.1\r\nHost: \r\no8s: \xa9\r\n\r\n'` | | rejects | a tab after a Content-Length or Transfer-Encoding value | 15 | `b'PUT /n HTTP/1.1\r\nhost: %DCT%Ee7tj-.GL:80 \r\nTransfer-Encoding:\tCHUNKED\t\r\n\r\n'`, `b'TRACE /!:wEp) HTTP/1.1\r\nTransfer-Encoding:\tchunked \t \r\n*`: P\t-GW:I\t\xe0K\tyO:\r\nHost: %26c%cDJ*$_s:8080\t'` | | rejects | together: the authority-form with a method other than CONNECT; a Transfer-Encoding list with empty elements | 14 | `b'POST DW~g:443 HTTP/1.1\r\nTRANSFER-ENCODING:\tchunked,\t\r\nhOsT:\t[7::A3A5:0:Ae5]\t\r\n\r\n'`, `b'GET JOg.Nt%BBysUBr.d5T:443 HTTP/1.1\r\ntransfer-encoding: chunked , , \r\nhost:5.I.Yc%CA&d: \r\nSOsR: l~b'` | @@ -67,8 +67,8 @@ Where it differs from this project (the two shortest examples): | rejects | together: HTTP/1.2 to 1.9; the authority-form with a method other than CONNECT | 11 | `b"POST [9:ABA:DB::C79:Aa55:f3Ea:2]:8080 HTTP/1.2\r\nHost: %91.qw'zr~:443 \t \r\n\r\n"`, `b'GET 0.250.99.213:0 HTTP/1.2\r\nHOST: 6oeD.2UR%7DbLh7 \r\nContent-Length: 5 \r\n\r\n'` | | accepts | CONNECT with an empty port | 10 | `b'CONNECT a: HTTP/1.1\r\nHost: a\r\n\r\n'`, `b'CONNECT http: HTTP/1.1\r\nHost: a\r\n\r\n'` | | accepts | a target with a bad percent escape | 9 | `b'GET /%4g HTTP/1.1\r\nHost: x\r\n\r\n'`, `b'GET /%zz HTTP/1.1\r\nHost: x\r\n\r\n'` | +| accepts | another target outside RFC 3986 | 7 | `b'GET /a#frag HTTP/1.1\r\nHost: x\r\n\r\n'`, `b'CONNECT ::1:80 HTTP/1.1\r\nHost: a\r\n\r\n'` | | accepts | a bare CR | 6 | `b'\r\r\nHEAD / HTTP/1.1\r\nContent-Length: \t 996606 \r\nhOsT: \t :80 \r\n\r\n'`, `b"\rCONNECT /i%f7%7cGy%78/j*QCyl//1S; HTTP/1.1\r\n'5:]!