From a6776de0c70d23ac197682c7bef603450cb8b03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Fri, 3 Mar 2023 19:08:23 +0100 Subject: [PATCH] QNetwork[http]: Discard or ignore corrupted cache entries In an attempt to avoid situations like the linked bug-report from happening again, discard cached entries with no headers and ignore entries where the payload is smaller than what the header specified (if it specified anything). In a future revision we might want to add a length to the cache's metadata, potentially with a checksum trailing the content. Task-number: QTBUG-111397 Pick-to: 6.5 6.4 6.2 Change-Id: Ie40149ffdaff7886bcd44cbd45605bdb7918e105 Reviewed-by: Konrad Kujawa Reviewed-by: Timur Pocheptsov --- src/network/access/qnetworkdiskcache.cpp | 2 +- src/network/access/qnetworkreplyhttpimpl.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/network/access/qnetworkdiskcache.cpp b/src/network/access/qnetworkdiskcache.cpp index 3c44da5b69..0b0db2d3a1 100644 --- a/src/network/access/qnetworkdiskcache.cpp +++ b/src/network/access/qnetworkdiskcache.cpp @@ -677,7 +677,7 @@ bool QCacheItem::read(QFileDevice *device, bool readData) if (!device->fileName().endsWith(expectedFilename)) return false; - return metaData.isValid(); + return metaData.isValid() && !metaData.rawHeaders().isEmpty(); } QT_END_NAMESPACE diff --git a/src/network/access/qnetworkreplyhttpimpl.cpp b/src/network/access/qnetworkreplyhttpimpl.cpp index e0fc272b61..54e70fdcf3 100644 --- a/src/network/access/qnetworkreplyhttpimpl.cpp +++ b/src/network/access/qnetworkreplyhttpimpl.cpp @@ -501,6 +501,12 @@ bool QNetworkReplyHttpImplPrivate::loadFromCacheIfAllowed(QHttpNetworkRequest &h QNetworkHeadersPrivate::RawHeadersList::ConstIterator it; cacheHeaders.setAllRawHeaders(metaData.rawHeaders()); + it = cacheHeaders.findRawHeader("content-length"); + if (it != cacheHeaders.rawHeaders.constEnd()) { + if (nc->data(httpRequest.url())->size() < it->second.toLongLong()) + return false; // The data is smaller than the content-length specified + } + it = cacheHeaders.findRawHeader("etag"); if (it != cacheHeaders.rawHeaders.constEnd()) httpRequest.setHeaderField("If-None-Match", it->second);