浏览代码

LibHTTP: Trim received data to Content-Length

Apparently servers will feel free to pad their response if they send one
that contains a content-length field.
We should not assume that the entirety of the response is valid data.
AnotherTest 5 年之前
父节点
当前提交
a63e8c4a03
共有 2 个文件被更改,包括 9 次插入3 次删除
  1. 5 2
      Libraries/LibHTTP/HttpJob.cpp
  2. 4 1
      Libraries/LibHTTP/HttpsJob.cpp

+ 5 - 2
Libraries/LibHTTP/HttpJob.cpp

@@ -158,8 +158,11 @@ void HttpJob::on_socket_connected()
         auto content_length_header = m_headers.get("Content-Length");
         auto content_length_header = m_headers.get("Content-Length");
         if (content_length_header.has_value()) {
         if (content_length_header.has_value()) {
             bool ok;
             bool ok;
-            if (m_received_size >= content_length_header.value().to_uint(ok) && ok)
-                return finish_up();
+            auto content_length = content_length_header.value().to_uint(ok);
+            if (ok && m_received_size >= content_length) {
+                m_received_size = content_length;
+                finish_up();
+            }
         }
         }
     };
     };
 }
 }

+ 4 - 1
Libraries/LibHTTP/HttpsJob.cpp

@@ -168,8 +168,11 @@ void HttpsJob::on_socket_connected()
         auto content_length_header = m_headers.get("Content-Length");
         auto content_length_header = m_headers.get("Content-Length");
         if (content_length_header.has_value()) {
         if (content_length_header.has_value()) {
             bool ok;
             bool ok;
-            if (m_received_size >= content_length_header.value().to_uint(ok) && ok)
+            auto content_length = content_length_header.value().to_uint(ok);
+            if (ok && m_received_size >= content_length) {
+                m_received_size = content_length;
                 finish_up();
                 finish_up();
+            }
         } else {
         } else {
             // no content-length, assume closed connection
             // no content-length, assume closed connection
             finish_up();
             finish_up();