From 7c98a6be17e7d17f4e0d1b81ff92fa3242d2ff0c Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Mon, 12 Apr 2021 00:47:33 +0430 Subject: [PATCH] LibHTTP: Handle running out of input between chunk body and ending CRLF Fixes an issue where LibHTTP would incorrectly detect an end of stream when it runs out of TLS application data between the chunk body and its ending CRLF. --- Userland/Libraries/LibHTTP/Job.cpp | 15 +++++++++++---- Userland/Libraries/LibHTTP/Job.h | 1 + 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibHTTP/Job.cpp b/Userland/Libraries/LibHTTP/Job.cpp index 60a24d8a2a..0a06eed32a 100644 --- a/Userland/Libraries/LibHTTP/Job.cpp +++ b/Userland/Libraries/LibHTTP/Job.cpp @@ -242,6 +242,11 @@ void Job::on_socket_connected() if (remaining == -1) { // read size auto size_data = read_line(PAGE_SIZE); + if (m_should_read_chunk_ending_line) { + VERIFY(size_data.is_empty()); + m_should_read_chunk_ending_line = false; + return IterationDecision::Continue; + } auto size_lines = size_data.view().lines(); dbgln_if(JOB_DEBUG, "Job: Received a chunk with size '{}'", size_data); if (size_lines.size() == 0) { @@ -327,10 +332,12 @@ void Job::on_socket_connected() // we've read everything, now let's get the next chunk size = -1; - [[maybe_unused]] auto line = read_line(PAGE_SIZE); - - if constexpr (JOB_DEBUG) - dbgln("Line following (should be empty): '{}'", line); + if (can_read_line()) { + auto line = read_line(PAGE_SIZE); + VERIFY(line.is_empty()); + } else { + m_should_read_chunk_ending_line = true; + } } m_current_chunk_remaining_size = size; } diff --git a/Userland/Libraries/LibHTTP/Job.h b/Userland/Libraries/LibHTTP/Job.h index b57b220396..47b560010e 100644 --- a/Userland/Libraries/LibHTTP/Job.h +++ b/Userland/Libraries/LibHTTP/Job.h @@ -82,6 +82,7 @@ protected: Optional m_current_chunk_remaining_size; Optional m_current_chunk_total_size; bool m_can_stream_response { true }; + bool m_should_read_chunk_ending_line { false }; }; }