Skip to content

Commit e9fb897

Browse files
Bug 1863636 - adding filename validation to prevent callers of new functions from creating bad file paths r=nshukla,gstoll
As per comments on this diff about getTaskbarTabShortcutPath () I added validation on the C++ side to ensure that the shortcut name passed in can be used as a filename. Tested the changes in the browser console window and confirmed that regular names "abcd" work and confirmed that bad names ("..\\something", ".\\s", ".s", "some?") failed and generated an exception in Javascript. Differential Revision: https://phabricator.services.mozilla.com/D199590
1 parent f3e6713 commit e9fb897

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

browser/components/shell/nsWindowsShellService.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "nsIOutputStream.h"
1818
#include "nsIPrefService.h"
1919
#include "nsIStringBundle.h"
20+
#include "nsIMIMEService.h"
2021
#include "nsNetUtil.h"
2122
#include "nsServiceManagerUtils.h"
2223
#include "nsShellService.h"
@@ -1459,9 +1460,31 @@ nsWindowsShellService::UnpinShortcutFromTaskbar(
14591460
return ManageShortcutTaskbarPins(runInTestMode, pinType, aShortcutPath);
14601461
}
14611462

1463+
// Ensure that the supplied name doesn't have invalid characters.
1464+
static void ValidateFilename(nsAString& aFilename) {
1465+
nsCOMPtr<nsIMIMEService> mimeService = do_GetService("@mozilla.org/mime;1");
1466+
if (NS_WARN_IF(!mimeService)) {
1467+
aFilename.Truncate();
1468+
return;
1469+
}
1470+
1471+
uint32_t flags = nsIMIMEService::VALIDATE_SANITIZE_ONLY;
1472+
1473+
nsAutoString outFilename;
1474+
mimeService->ValidateFileNameForSaving(aFilename, EmptyCString(), flags,
1475+
outFilename);
1476+
aFilename = outFilename;
1477+
}
1478+
14621479
NS_IMETHODIMP
14631480
nsWindowsShellService::GetTaskbarTabShortcutPath(const nsAString& aShortcutName,
14641481
nsAString& aRetPath) {
1482+
nsAutoString sanitizedShortcutName(aShortcutName);
1483+
ValidateFilename(sanitizedShortcutName);
1484+
if (sanitizedShortcutName != aShortcutName) {
1485+
return NS_ERROR_FILE_INVALID_PATH;
1486+
}
1487+
14651488
// The taskbar tab shortcut will always be in
14661489
// %APPDATA%\Microsoft\Windows\Start Menu\Programs
14671490
RefPtr<IKnownFolderManager> fManager;

0 commit comments

Comments
 (0)