Blame SOURCES/squid-4.11-CVE-2020-15811.patch

88e69d
diff --git a/src/HttpHeader.cc b/src/HttpHeader.cc
818ced
index dc6e0ff..67543a4 100644
88e69d
--- a/src/HttpHeader.cc
88e69d
+++ b/src/HttpHeader.cc
88e69d
@@ -174,6 +174,7 @@ HttpHeader::operator =(const HttpHeader &other)
88e69d
         update(&other); // will update the mask as well
88e69d
         len = other.len;
88e69d
         conflictingContentLength_ = other.conflictingContentLength_;
88e69d
+        teUnsupported_ = other.teUnsupported_;
88e69d
     }
88e69d
     return *this;
88e69d
 }
88e69d
@@ -222,6 +223,7 @@ HttpHeader::clean()
88e69d
     httpHeaderMaskInit(&mask, 0);
88e69d
     len = 0;
88e69d
     conflictingContentLength_ = false;
88e69d
+    teUnsupported_ = false;
88e69d
     PROF_stop(HttpHeaderClean);
88e69d
 }
88e69d
 
818ced
@@ -464,11 +466,23 @@ HttpHeader::parse(const char *header_start, size_t hdrLen)
88e69d
                Raw("header", header_start, hdrLen));
88e69d
     }
88e69d
 
88e69d
-    if (chunked()) {
88e69d
+    String rawTe;
88e69d
+    if (getByIdIfPresent(Http::HdrType::TRANSFER_ENCODING, &rawTe)) {
88e69d
         // RFC 2616 section 4.4: ignore Content-Length with Transfer-Encoding
88e69d
         // RFC 7230 section 3.3.3 #3: Transfer-Encoding overwrites Content-Length
88e69d
         delById(Http::HdrType::CONTENT_LENGTH);
88e69d
         // and clen state becomes irrelevant
88e69d
+
88e69d
+        if (rawTe == "chunked") {
88e69d
+            ; // leave header present for chunked() method
88e69d
+        } else if (rawTe == "identity") { // deprecated. no coding
88e69d
+            delById(Http::HdrType::TRANSFER_ENCODING);
88e69d
+        } else {
88e69d
+            // This also rejects multiple encodings until we support them properly.
88e69d
+            debugs(55, warnOnError, "WARNING: unsupported Transfer-Encoding used by client: " << rawTe);
88e69d
+            teUnsupported_ = true;
88e69d
+        }
88e69d
+
88e69d
     } else if (clen.sawBad) {
88e69d
         // ensure our callers do not accidentally see bad Content-Length values
88e69d
         delById(Http::HdrType::CONTENT_LENGTH);
88e69d
diff --git a/src/HttpHeader.h b/src/HttpHeader.h
818ced
index e3553a4..64f294a 100644
88e69d
--- a/src/HttpHeader.h
88e69d
+++ b/src/HttpHeader.h
88e69d
@@ -140,7 +140,13 @@ public:
88e69d
     int hasListMember(Http::HdrType id, const char *member, const char separator) const;
88e69d
     int hasByNameListMember(const char *name, const char *member, const char separator) const;
88e69d
     void removeHopByHopEntries();
88e69d
-    inline bool chunked() const; ///< whether message uses chunked Transfer-Encoding
88e69d
+
88e69d
+    /// whether the message uses chunked Transfer-Encoding
88e69d
+    /// optimized implementation relies on us rejecting/removing other codings
88e69d
+    bool chunked() const { return has(Http::HdrType::TRANSFER_ENCODING); }
88e69d
+
88e69d
+    /// whether message used an unsupported and/or invalid Transfer-Encoding
88e69d
+    bool unsupportedTe() const { return teUnsupported_; }
88e69d
 
88e69d
     /* protected, do not use these, use interface functions instead */
88e69d
     std::vector<HttpHeaderEntry *> entries;     /**< parsed fields in raw format */
88e69d
@@ -158,6 +164,9 @@ protected:
88e69d
 private:
88e69d
     HttpHeaderEntry *findLastEntry(Http::HdrType id) const;
88e69d
     bool conflictingContentLength_; ///< found different Content-Length fields
88e69d
+    /// unsupported encoding, unnecessary syntax characters, and/or
88e69d
+    /// invalid field-value found in Transfer-Encoding header
88e69d
+    bool teUnsupported_ = false;
88e69d
 };
88e69d
 
88e69d
 int httpHeaderParseQuotedString(const char *start, const int len, String *val);
88e69d
@@ -167,13 +176,6 @@ SBuf httpHeaderQuoteString(const char *raw);
88e69d
 
88e69d
 void httpHeaderCalcMask(HttpHeaderMask * mask, Http::HdrType http_hdr_type_enums[], size_t count);
88e69d
 
88e69d
-inline bool
88e69d
-HttpHeader::chunked() const
88e69d
-{
88e69d
-    return has(Http::HdrType::TRANSFER_ENCODING) &&
88e69d
-           hasListMember(Http::HdrType::TRANSFER_ENCODING, "chunked", ',');
88e69d
-}
88e69d
-
88e69d
 void httpHeaderInitModule(void);
88e69d
 
88e69d
 #endif /* SQUID_HTTPHEADER_H */
88e69d
diff --git a/src/client_side.cc b/src/client_side.cc
818ced
index 5f5a79e..000a00b 100644
88e69d
--- a/src/client_side.cc
88e69d
+++ b/src/client_side.cc
818ced
@@ -1600,9 +1600,7 @@ void
88e69d
 clientProcessRequest(ConnStateData *conn, const Http1::RequestParserPointer &hp, Http::Stream *context)
88e69d
 {
88e69d
     ClientHttpRequest *http = context->http;
88e69d
-    bool chunked = false;
88e69d
     bool mustReplyToOptions = false;
88e69d
-    bool unsupportedTe = false;
88e69d
     bool expectBody = false;
88e69d
 
88e69d
     // We already have the request parsed and checked, so we
818ced
@@ -1659,13 +1657,7 @@ clientProcessRequest(ConnStateData *conn, const Http1::RequestParserPointer &hp,
88e69d
         request->http_ver.minor = http_ver.minor;
88e69d
     }
88e69d
 
88e69d
-    if (request->header.chunked()) {
88e69d
-        chunked = true;
88e69d
-    } else if (request->header.has(Http::HdrType::TRANSFER_ENCODING)) {
88e69d
-        const String te = request->header.getList(Http::HdrType::TRANSFER_ENCODING);
88e69d
-        // HTTP/1.1 requires chunking to be the last encoding if there is one
88e69d
-        unsupportedTe = te.size() && te != "identity";
88e69d
-    } // else implied identity coding
88e69d
+    const auto unsupportedTe = request->header.unsupportedTe();
88e69d
 
88e69d
     mustReplyToOptions = (request->method == Http::METHOD_OPTIONS) &&
88e69d
                          (request->header.getInt64(Http::HdrType::MAX_FORWARDS) == 0);
818ced
@@ -1682,6 +1674,7 @@ clientProcessRequest(ConnStateData *conn, const Http1::RequestParserPointer &hp,
88e69d
         return;
88e69d
     }
88e69d
 
88e69d
+    const auto chunked = request->header.chunked();
88e69d
     if (!chunked && !clientIsContentLengthValid(request.getRaw())) {
88e69d
         clientStreamNode *node = context->getClientReplyContext();
88e69d
         clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
88e69d
diff --git a/src/http.cc b/src/http.cc
818ced
index 9654c4a..6f4d3b2 100644
88e69d
--- a/src/http.cc
88e69d
+++ b/src/http.cc
88e69d
@@ -1292,6 +1292,9 @@ HttpStateData::continueAfterParsingHeader()
88e69d
             } else if (vrep->header.conflictingContentLength()) {
88e69d
                 fwd->dontRetry(true);
88e69d
                 error = ERR_INVALID_RESP;
88e69d
+            } else if (vrep->header.unsupportedTe()) {
88e69d
+                fwd->dontRetry(true);
88e69d
+                error = ERR_INVALID_RESP;
88e69d
             } else {
88e69d
                 return true; // done parsing, got reply, and no error
88e69d
             }