Bug 1571742 - Move Nosniff-Check into Sniffers r=ckerschb,dragana
authorSebastian Streich <sstreich@mozilla.com>
Wed, 14 Aug 2019 13:59:06 +0000
changeset 487908 b3ee5fed44ec711f79202150223cda9397c6ed80
parent 487907 5ed6b561f0eac353ca492fdc9017673f3b94ccd1
child 487909 15846b81170a0c7cdeee4f72695e6543fbfe8362
push id36433
push userbtara@mozilla.com
push dateWed, 14 Aug 2019 21:57:52 +0000
treeherdermozilla-central@7d9a2196d313 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersckerschb, dragana
bugs1571742
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1571742 - Move Nosniff-Check into Sniffers r=ckerschb,dragana Differential Revision: https://phabricator.services.mozilla.com/D41504
image/imgLoader.cpp
netwerk/base/nsNetUtil.cpp
netwerk/protocol/http/nsHttpChannel.cpp
netwerk/streamconv/converters/nsUnknownDecoder.cpp
toolkit/components/mediasniffer/nsMediaSniffer.cpp
--- a/image/imgLoader.cpp
+++ b/image/imgLoader.cpp
@@ -2565,16 +2565,23 @@ bool imgLoader::SupportImageWithMimeType
   DecoderType type = DecoderFactory::GetDecoderType(mimeType.get());
   return type != DecoderType::UNKNOWN;
 }
 
 NS_IMETHODIMP
 imgLoader::GetMIMETypeFromContent(nsIRequest* aRequest,
                                   const uint8_t* aContents, uint32_t aLength,
                                   nsACString& aContentType) {
+  nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
+  if (channel) {
+    nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
+    if (loadInfo->GetSkipContentSniffing()) {
+      return NS_ERROR_NOT_AVAILABLE;
+    }
+  }
   return GetMimeTypeFromContent((const char*)aContents, aLength, aContentType);
 }
 
 /* static */
 nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
                                            uint32_t aLength,
                                            nsACString& aContentType) {
   /* Is it a GIF? */
--- a/netwerk/base/nsNetUtil.cpp
+++ b/netwerk/base/nsNetUtil.cpp
@@ -2689,25 +2689,16 @@ nsresult NS_GenerateHostPort(const nsCSt
     hostLine.AppendInt(port);
   }
   return NS_OK;
 }
 
 void NS_SniffContent(const char* aSnifferType, nsIRequest* aRequest,
                      const uint8_t* aData, uint32_t aLength,
                      nsACString& aSniffedType) {
-  // In case XCTO nosniff was present, we could just skip sniffing here
-  nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
-  if (channel) {
-    nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
-    if (loadInfo->GetSkipContentSniffing()) {
-      aSniffedType.Truncate();
-      return;
-    }
-  }
   typedef nsCategoryCache<nsIContentSniffer> ContentSnifferCache;
   extern ContentSnifferCache* gNetSniffers;
   extern ContentSnifferCache* gDataSniffers;
   ContentSnifferCache* cache = nullptr;
   if (!strcmp(aSnifferType, NS_CONTENT_SNIFFER_CATEGORY)) {
     if (!gNetSniffers) {
       gNetSniffers = new ContentSnifferCache(NS_CONTENT_SNIFFER_CATEGORY);
     }
--- a/netwerk/protocol/http/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/nsHttpChannel.cpp
@@ -1443,17 +1443,17 @@ nsresult ProcessXCTO(nsHttpChannel* aCha
   }
 
   auto policyType = aLoadInfo->GetExternalContentPolicyType();
   if ((policyType == nsIContentPolicy::TYPE_DOCUMENT ||
        policyType == nsIContentPolicy::TYPE_SUBDOCUMENT) &&
       gHttpHandler->IsDocumentNosniffEnabled()) {
     // If the header XCTO nosniff is set for any browsing context, then
     // we set the skipContentSniffing flag on the Loadinfo. Within
-    // NS_SniffContent we then bail early and do not do any sniffing.
+    // GetMIMETypeFromContent we then bail early and do not do any sniffing.
     aLoadInfo->SetSkipContentSniffing(true);
     return NS_OK;
   }
 
   return NS_OK;
 }
 
 // Ensure that a load of type script has correct MIME type
--- a/netwerk/streamconv/converters/nsUnknownDecoder.cpp
+++ b/netwerk/streamconv/converters/nsUnknownDecoder.cpp
@@ -322,19 +322,21 @@ nsUnknownDecoder::OnStopRequest(nsIReque
 // ----
 NS_IMETHODIMP
 nsUnknownDecoder::GetMIMETypeFromContent(nsIRequest* aRequest,
                                          const uint8_t* aData, uint32_t aLength,
                                          nsACString& type) {
   // This is only used by sniffer, therefore we do not need to lock anything
   // here.
   nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
-  nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
-  if (loadInfo->GetSkipContentSniffing()) {
-    return NS_OK;
+  if (channel) {
+    nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
+    if (loadInfo->GetSkipContentSniffing()) {
+      return NS_ERROR_NOT_AVAILABLE;
+    }
   }
 
   mBuffer = const_cast<char*>(reinterpret_cast<const char*>(aData));
   mBufferLen = aLength;
   DetermineContentType(aRequest);
   mBuffer = nullptr;
   mBufferLen = 0;
   type.Assign(mContentType);
--- a/toolkit/components/mediasniffer/nsMediaSniffer.cpp
+++ b/toolkit/components/mediasniffer/nsMediaSniffer.cpp
@@ -135,16 +135,20 @@ static bool MatchesADTS(const uint8_t* a
 
 NS_IMETHODIMP
 nsMediaSniffer::GetMIMETypeFromContent(nsIRequest* aRequest,
                                        const uint8_t* aData,
                                        const uint32_t aLength,
                                        nsACString& aSniffedType) {
   nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
   if (channel) {
+    nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
+    if (loadInfo->GetSkipContentSniffing()) {
+      return NS_ERROR_NOT_AVAILABLE;
+    }
     nsLoadFlags loadFlags = 0;
     channel->GetLoadFlags(&loadFlags);
     if (!(loadFlags & nsIChannel::LOAD_MEDIA_SNIFFER_OVERRIDES_CONTENT_TYPE)) {
       // For media, we want to sniff only if the Content-Type is unknown, or if
       // it is application/octet-stream.
       nsAutoCString contentType;
       nsresult rv = channel->GetContentType(contentType);
       NS_ENSURE_SUCCESS(rv, rv);