From eddb05cf738387f76f4aef20538e7e9cc292cc5f Mon Sep 17 00:00:00 2001 From: "jst%mozilla.jstenback.com" Date: Thu, 3 Feb 2005 17:23:35 +0000 Subject: [PATCH] Fixing bug 279945. Prevent drag n' drop of images whose extensions don't map to image mimetypes. r=caillon@gmail.com, sr=dveditz@cruzio.com git-svn-id: svn://10.0.0.236/trunk@168753 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/content/base/public/nsContentUtils.h | 11 +++++-- .../base/src/nsContentAreaDragDrop.cpp | 5 +-- mozilla/content/base/src/nsContentUtils.cpp | 31 ++++++++++++++++++- mozilla/content/base/src/nsCopySupport.cpp | 2 +- .../base/src/nsImageLoadingContent.cpp | 31 +++---------------- .../content/base/src/nsImageLoadingContent.h | 8 ----- mozilla/layout/build/nsLayoutModule.cpp | 4 --- 7 files changed, 47 insertions(+), 45 deletions(-) diff --git a/mozilla/content/base/public/nsContentUtils.h b/mozilla/content/base/public/nsContentUtils.h index 572e0e34483..d89211e3e22 100644 --- a/mozilla/content/base/public/nsContentUtils.h +++ b/mozilla/content/base/public/nsContentUtils.h @@ -250,12 +250,17 @@ public: static nsINameSpaceManager* GetNSManagerWeakRef() { return sNameSpaceManager; - }; + } static nsIIOService* GetIOServiceWeakRef() { return sIOService; - }; + } + + static imgILoader* GetImgLoader() + { + return sImgLoader; + } #ifdef MOZ_XTF static nsIXTFService* GetXTFServiceWeakRef(); @@ -365,7 +370,7 @@ public: * @param aContent The image loading content. Must not be null. * @return the nsIImage corresponding to the first frame of the image */ - static already_AddRefed GetImageFromContent(nsIImageLoadingContent* aContent); + static already_AddRefed GetImageFromContent(nsIImageLoadingContent* aContent, PRBool aDoMimeCheck); /** * Method that decides whether a content node is draggable diff --git a/mozilla/content/base/src/nsContentAreaDragDrop.cpp b/mozilla/content/base/src/nsContentAreaDragDrop.cpp index 9681b89863e..0beaa0bce2c 100644 --- a/mozilla/content/base/src/nsContentAreaDragDrop.cpp +++ b/mozilla/content/base/src/nsContentAreaDragDrop.cpp @@ -1108,8 +1108,9 @@ nsTransferableFactory::Produce(nsITransferable** outTrans) // pass out the image source string mImageSourceString = mUrlString; - // also grab the image data - mImage = nsContentUtils::GetImageFromContent(image); + // also grab the image data, assuming the image extension in + // the image URI maps to a known image mimetype. + mImage = nsContentUtils::GetImageFromContent(image, PR_TRUE); if (parentLink) { diff --git a/mozilla/content/base/src/nsContentUtils.cpp b/mozilla/content/base/src/nsContentUtils.cpp index 1d6463350e1..dc7fbcb6e7c 100644 --- a/mozilla/content/base/src/nsContentUtils.cpp +++ b/mozilla/content/base/src/nsContentUtils.cpp @@ -108,6 +108,7 @@ #include "nsIXTFService.h" static NS_DEFINE_CID(kXTFServiceCID, NS_XTFSERVICE_CID); #endif +#include "nsIMIMEService.h" // for ReportToConsole #include "nsIStringBundle.h" @@ -1882,7 +1883,8 @@ nsContentUtils::LoadImage(nsIURI* aURI, nsIDocument* aLoadingDocument, // static already_AddRefed -nsContentUtils::GetImageFromContent(nsIImageLoadingContent* aContent) +nsContentUtils::GetImageFromContent(nsIImageLoadingContent* aContent, + PRBool aDoMimeCheck) { NS_ENSURE_TRUE(aContent, nsnull); @@ -1893,6 +1895,33 @@ nsContentUtils::GetImageFromContent(nsIImageLoadingContent* aContent) return nsnull; } + if (aDoMimeCheck) { + nsCOMPtr mimeService = + do_GetService("@mozilla.org/mime;1"); + + imgILoader *imgLoader = nsContentUtils::GetImgLoader(); + + if (mimeService) { + nsCAutoString mimeType; + nsCOMPtr uri; + + imgRequest->GetURI(getter_AddRefs(uri)); + mimeService->GetTypeFromURI(uri, mimeType); + + PRBool supportedType; + if (NS_FAILED(imgLoader->SupportImageWithMimeType(mimeType.get(), + &supportedType)) || + !supportedType) { + // The extension of the image we're about to drag doesn't + // match an extension that we recognize as an image. Disallow + // dragging this image as an image to prevent dropping images + // with extensions that the OS won't recognize as images. + + return nsnull; + } + } + } + nsCOMPtr imgContainer; imgRequest->GetImage(getter_AddRefs(imgContainer)); diff --git a/mozilla/content/base/src/nsCopySupport.cpp b/mozilla/content/base/src/nsCopySupport.cpp index 431cb300dae..bced49f2c3a 100644 --- a/mozilla/content/base/src/nsCopySupport.cpp +++ b/mozilla/content/base/src/nsCopySupport.cpp @@ -462,7 +462,7 @@ nsCopySupport::ImageCopy(nsIImageLoadingContent* aImageElement, if (aCopyImageData) { // get the image data from the element nsCOMPtr image = - nsContentUtils::GetImageFromContent(aImageElement); + nsContentUtils::GetImageFromContent(aImageElement, PR_FALSE); NS_ENSURE_TRUE(image, NS_ERROR_FAILURE); nsCOMPtr diff --git a/mozilla/content/base/src/nsImageLoadingContent.cpp b/mozilla/content/base/src/nsImageLoadingContent.cpp index b7b972efb43..6c4d5dae501 100644 --- a/mozilla/content/base/src/nsImageLoadingContent.cpp +++ b/mozilla/content/base/src/nsImageLoadingContent.cpp @@ -90,10 +90,6 @@ static void PrintReqURL(imgIRequest* req) { } #endif /* DEBUG_chb */ -// Statics -imgILoader* nsImageLoadingContent::sImgLoader = nsnull; -nsIIOService* nsImageLoadingContent::sIOService = nsnull; - nsImageLoadingContent::nsImageLoadingContent() : mObserverList(nsnull), @@ -101,7 +97,7 @@ nsImageLoadingContent::nsImageLoadingContent() mImageIsBlocked(PR_FALSE), mHaveHadObserver(PR_FALSE) { - if (!sImgLoader) + if (!nsContentUtils::GetImgLoader()) mLoadingEnabled = PR_FALSE; } @@ -118,23 +114,6 @@ nsImageLoadingContent::~nsImageLoadingContent() "Observers still registered?"); } -void -nsImageLoadingContent::Initialize() -{ - // If this fails, NS_NewURI will try to get the service itself - CallGetService("@mozilla.org/network/io-service;1", &sIOService); - - // Ignore failure and just don't load images - CallGetService("@mozilla.org/image/loader;1", &sImgLoader); -} - -void -nsImageLoadingContent::Shutdown() -{ - NS_IF_RELEASE(sImgLoader); - NS_IF_RELEASE(sIOService); -} - // Macro to call some func on each observer. This handles observers // removing themselves. #define LOOP_OVER_OBSERVERS(func_) \ @@ -250,7 +229,7 @@ nsImageLoadingContent::GetLoadingEnabled(PRBool *aLoadingEnabled) NS_IMETHODIMP nsImageLoadingContent::SetLoadingEnabled(PRBool aLoadingEnabled) { - if (sImgLoader) + if (nsContentUtils::GetImgLoader()) mLoadingEnabled = aLoadingEnabled; return NS_OK; } @@ -386,7 +365,7 @@ nsImageLoadingContent::LoadImageWithChannel(nsIChannel* aChannel, NS_ENSURE_ARG_POINTER(aChannel); - if (!sImgLoader) + if (!nsContentUtils::GetImgLoader()) return NS_ERROR_NULL_POINTER; // XXX what should we do with content policies here, if anything? @@ -402,7 +381,7 @@ nsImageLoadingContent::LoadImageWithChannel(nsIChannel* aChannel, nsCOMPtr & req = mCurrentRequest ? mPendingRequest : mCurrentRequest; - return sImgLoader->LoadImageWithChannel(aChannel, this, doc, aListener, getter_AddRefs(req)); + return nsContentUtils::GetImgLoader()->LoadImageWithChannel(aChannel, this, doc, aListener, getter_AddRefs(req)); } // XXX This should be a protected method, not an interface method!!! @@ -580,7 +559,7 @@ nsImageLoadingContent::StringToURI(const nsACString& aSpec, aSpec, charset.IsEmpty() ? nsnull : PromiseFlatCString(charset).get(), baseURL, - sIOService); + nsContentUtils::GetIOServiceWeakRef()); } diff --git a/mozilla/content/base/src/nsImageLoadingContent.h b/mozilla/content/base/src/nsImageLoadingContent.h index 54e31cc93f7..2d64e2aa2ad 100644 --- a/mozilla/content/base/src/nsImageLoadingContent.h +++ b/mozilla/content/base/src/nsImageLoadingContent.h @@ -64,11 +64,6 @@ public: nsImageLoadingContent(); virtual ~nsImageLoadingContent(); - /** Called on layout startup to initialize statics */ - static void Initialize(); - /** Called on shutdown to free statics */ - static void Shutdown(); - NS_DECL_IMGICONTAINEROBSERVER NS_DECL_IMGIDECODEROBSERVER NS_DECL_NSIIMAGELOADINGCONTENT @@ -158,9 +153,6 @@ protected: nsCOMPtr mPendingRequest; nsCOMPtr mCurrentURI; - // Cache for the io service and image loader - static imgILoader* sImgLoader; - static nsIIOService* sIOService; private: /** * Typically we will have only one observer (our frame in the screen diff --git a/mozilla/layout/build/nsLayoutModule.cpp b/mozilla/layout/build/nsLayoutModule.cpp index 06145c61d9a..fe1474283bb 100644 --- a/mozilla/layout/build/nsLayoutModule.cpp +++ b/mozilla/layout/build/nsLayoutModule.cpp @@ -123,7 +123,6 @@ #include "nsCSSLoader.h" #include "nsXULAtoms.h" #include "nsLayoutCID.h" -#include "nsImageLoadingContent.h" #include "nsStyleSet.h" #include "nsImageFrame.h" #include "nsILanguageAtomService.h" @@ -336,8 +335,6 @@ Initialize(nsIModule* aSelf) return rv; } - nsImageLoadingContent::Initialize(); - // Add our shutdown observer. nsCOMPtr observerService = do_GetService("@mozilla.org/observer-service;1"); @@ -429,7 +426,6 @@ Shutdown() nsContentUtils::Shutdown(); nsLayoutStylesheetCache::Shutdown(); NS_NameSpaceManagerShutdown(); - nsImageLoadingContent::Shutdown(); nsStyleSet::FreeGlobals(); nsGlobalWindow::ShutDown();