From 84eadbe3cf6f0a9f167c8a95038e2556bb08b3bb Mon Sep 17 00:00:00 2001 From: Martok Date: Thu, 4 May 2023 21:03:16 +0200 Subject: [PATCH] No Issue - Do not parse or return body for XHRs with HEAD/CONNECT method or content-length=0 Based-on: m-c 1392220, 1437360 --- dom/xhr/XMLHttpRequestMainThread.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/dom/xhr/XMLHttpRequestMainThread.cpp b/dom/xhr/XMLHttpRequestMainThread.cpp index c5b174b5cb..aa1fc4e908 100644 --- a/dom/xhr/XMLHttpRequestMainThread.cpp +++ b/dom/xhr/XMLHttpRequestMainThread.cpp @@ -602,6 +602,12 @@ XMLHttpRequestMainThread::GetResponseText(XMLHttpRequestStringSnapshot& aSnapsho return; } + // Main Fetch step 18 requires to ignore body for head/connect methods. + if (mRequestMethod.EqualsLiteral("HEAD") || + mRequestMethod.EqualsLiteral("CONNECT")) { + return; + } + // We only decode text lazily if we're also parsing to a doc. // Also, if we've decoded all current data already, then no need to decode // more. @@ -1915,13 +1921,19 @@ XMLHttpRequestMainThread::OnStartRequest(nsIRequest *request, nsISupports *ctxt) } // Set up responseXML - bool parseBody = mResponseType == XMLHttpRequestResponseType::_empty || - mResponseType == XMLHttpRequestResponseType::Document; - nsCOMPtr httpChannel(do_QueryInterface(mChannel)); - if (parseBody && httpChannel) { - nsAutoCString method; - httpChannel->GetRequestMethod(method); - parseBody = !method.EqualsLiteral("HEAD"); + // Note: Main Fetch step 18 requires to ignore body for head/connect methods. + bool parseBody = (mResponseType == XMLHttpRequestResponseType::_empty || + mResponseType == XMLHttpRequestResponseType::Document) && + !(mRequestMethod.EqualsLiteral("HEAD") || + mRequestMethod.EqualsLiteral("CONNECT")); + + if (parseBody) { + // Do not try to parse documents if content-length = 0 + int64_t contentLength; + if (NS_SUCCEEDED(mChannel->GetContentLength(&contentLength)) && + contentLength == 0) { + parseBody = false; + } } mIsHtml = false;