Bug 1774683, use ValidateFilenameForSaving to validate dropped links on Windows, r=mhowell

Differential Revision: https://phabricator.services.mozilla.com/D149746
This commit is contained in:
Neil Deakin 2022-06-22 08:27:07 +00:00
Родитель fd5663f3e8
Коммит b17459dd7a
1 изменённых файлов: 20 добавлений и 10 удалений

Просмотреть файл

@ -41,6 +41,7 @@
#include "nsIPrincipal.h" #include "nsIPrincipal.h"
#include "nsNativeCharsetUtils.h" #include "nsNativeCharsetUtils.h"
#include "nsMimeTypes.h" #include "nsMimeTypes.h"
#include "nsIMIMEService.h"
#include "imgIEncoder.h" #include "imgIEncoder.h"
#include "imgITools.h" #include "imgITools.h"
#include "WinUtils.h" #include "WinUtils.h"
@ -1088,6 +1089,21 @@ nsDataObj ::GetFileContents(FORMATETC& aFE, STGMEDIUM& aSTG) {
} // GetFileContents } // GetFileContents
// Ensure that the supplied name doesn't have invalid characters.
static void ValidateFilename(nsString& aFilename) {
nsCOMPtr<nsIMIMEService> mimeService = do_GetService("@mozilla.org/mime;1");
if (NS_WARN_IF(!mimeService)) {
aFilename.Truncate();
return;
}
nsAutoString outFilename;
mimeService->ValidateFileNameForSaving(aFilename, EmptyCString(),
nsIMIMEService::VALIDATE_SANITIZE_ONLY,
outFilename);
aFilename = outFilename;
}
// //
// Given a unicode string, convert it down to a valid local charset filename // Given a unicode string, convert it down to a valid local charset filename
// with the supplied extension. This ensures that we do not cut MBCS characters // with the supplied extension. This ensures that we do not cut MBCS characters
@ -1097,10 +1113,7 @@ nsDataObj ::GetFileContents(FORMATETC& aFE, STGMEDIUM& aSTG) {
// //
static bool CreateFilenameFromTextA(nsString& aText, const char* aExtension, static bool CreateFilenameFromTextA(nsString& aText, const char* aExtension,
char* aFilename, uint32_t aFilenameLen) { char* aFilename, uint32_t aFilenameLen) {
// ensure that the supplied name doesn't have invalid characters. If ValidateFilename(aText);
// a valid mangled filename couldn't be created then it will leave the
// text empty.
nsLocalFile::CheckForReservedFileName(aText);
if (aText.IsEmpty()) return false; if (aText.IsEmpty()) return false;
// repeatably call WideCharToMultiByte as long as the title doesn't fit in the // repeatably call WideCharToMultiByte as long as the title doesn't fit in the
@ -1129,10 +1142,7 @@ static bool CreateFilenameFromTextA(nsString& aText, const char* aExtension,
static bool CreateFilenameFromTextW(nsString& aText, const wchar_t* aExtension, static bool CreateFilenameFromTextW(nsString& aText, const wchar_t* aExtension,
wchar_t* aFilename, uint32_t aFilenameLen) { wchar_t* aFilename, uint32_t aFilenameLen) {
// ensure that the supplied name doesn't have invalid characters. If ValidateFilename(aText);
// a valid mangled filename couldn't be created then it will leave the
// text empty.
nsLocalFile::CheckForReservedFileName(aText);
if (aText.IsEmpty()) return false; if (aText.IsEmpty()) return false;
const int extensionLen = wcslen(aExtension); const int extensionLen = wcslen(aExtension);
@ -2151,10 +2161,10 @@ HRESULT nsDataObj::GetDownloadDetails(nsIURI** aSourceURI,
NS_UnescapeURL(urlFileName); NS_UnescapeURL(urlFileName);
CopyUTF8toUTF16(urlFileName, srcFileName); CopyUTF8toUTF16(urlFileName, srcFileName);
} }
if (srcFileName.IsEmpty()) return E_FAIL;
// make the name safe for the filesystem // make the name safe for the filesystem
nsLocalFile::CheckForReservedFileName(srcFileName); ValidateFilename(srcFileName);
if (srcFileName.IsEmpty()) return E_FAIL;
sourceURI.swap(*aSourceURI); sourceURI.swap(*aSourceURI);
aFilename = srcFileName; aFilename = srcFileName;