diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index be516d479..d11002859 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -4855,6 +4855,12 @@ nsContentUtils::TriggerLink(nsIContent *aContent, fileName.SetIsVoid(true); // No actionable download attribute was found. } + // Sanitize fileNames containing null characters by replacing them with + // underscores. + if (!fileName.IsVoid()) { + fileName.ReplaceChar(char16_t(0), '_'); + } + nsDocShell::Cast(docShell)->OnLinkClick(aContent, aLinkURI, fileName.IsVoid() ? aTargetSpec.get() : EmptyString().get(), fileName, nullptr, nullptr, aIsTrusted); diff --git a/dom/bindings/BindingUtils.cpp b/dom/bindings/BindingUtils.cpp index 517dae97f..f0e223128 100644 --- a/dom/bindings/BindingUtils.cpp +++ b/dom/bindings/BindingUtils.cpp @@ -2290,6 +2290,12 @@ ConstructJSImplementation(JSContext* aCx, const char* aContractId, { AutoNoJSAPI nojsapi; + nsCOMPtr window = do_QueryInterface(aGlobal); + if (!window->IsCurrentInnerWindow()) { + aRv.Throw(NS_ERROR_FAILURE); + return; + } + // Get the XPCOM component containing the JS implementation. nsresult rv; nsCOMPtr implISupports = do_CreateInstance(aContractId, &rv); @@ -2304,7 +2310,6 @@ ConstructJSImplementation(JSContext* aCx, const char* aContractId, // and our global is a window. nsCOMPtr gpi = do_QueryInterface(implISupports); - nsCOMPtr window = do_QueryInterface(aGlobal); if (gpi) { JS::Rooted initReturn(aCx); rv = gpi->Init(window, &initReturn); diff --git a/dom/html/nsTextEditorState.cpp b/dom/html/nsTextEditorState.cpp index 008535ac4..3f4cf09bc 100644 --- a/dom/html/nsTextEditorState.cpp +++ b/dom/html/nsTextEditorState.cpp @@ -397,6 +397,7 @@ NS_IMETHODIMP nsTextInputSelectionImpl::SetCaretReadOnly(bool aReadOnly) { if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED; + if (!mFrameSelection) return NS_ERROR_FAILURE; nsresult result; nsCOMPtr shell = do_QueryReferent(mPresShellWeak, &result); if (shell) diff --git a/netwerk/base/nsBaseChannel.cpp b/netwerk/base/nsBaseChannel.cpp index e8ca7ccdc..dba5dafdd 100644 --- a/netwerk/base/nsBaseChannel.cpp +++ b/netwerk/base/nsBaseChannel.cpp @@ -575,6 +575,12 @@ NS_IMETHODIMP nsBaseChannel::SetContentDispositionFilename(const nsAString &aContentDispositionFilename) { mContentDispositionFilename = new nsString(aContentDispositionFilename); + + // For safety reasons ensure the filename doesn't contain null characters and + // replace them with underscores. We may later pass the extension to system + // MIME APIs that expect null terminated strings. + mContentDispositionFilename->ReplaceChar(char16_t(0), '_'); + return NS_OK; } diff --git a/netwerk/protocol/http/HttpBaseChannel.cpp b/netwerk/protocol/http/HttpBaseChannel.cpp index 89784819e..7ea31cb5b 100644 --- a/netwerk/protocol/http/HttpBaseChannel.cpp +++ b/netwerk/protocol/http/HttpBaseChannel.cpp @@ -509,6 +509,12 @@ NS_IMETHODIMP HttpBaseChannel::SetContentDispositionFilename(const nsAString& aContentDispositionFilename) { mContentDispositionFilename = new nsString(aContentDispositionFilename); + + // For safety reasons ensure the filename doesn't contain null characters and + // replace them with underscores. We may later pass the extension to system + // MIME APIs that expect null terminated strings. + mContentDispositionFilename->ReplaceChar(char16_t(0), '_'); + return NS_OK; } diff --git a/uriloader/exthandler/nsExternalHelperAppService.cpp b/uriloader/exthandler/nsExternalHelperAppService.cpp index 18a14b87b..35ed2513f 100644 --- a/uriloader/exthandler/nsExternalHelperAppService.cpp +++ b/uriloader/exthandler/nsExternalHelperAppService.cpp @@ -1288,6 +1288,8 @@ nsExternalAppHandler::nsExternalAppHandler(nsIMIMEInfo * aMIMEInfo, // replace platform specific path separator and illegal characters to avoid any confusion mSuggestedFileName.ReplaceChar(KNOWN_PATH_SEPARATORS FILE_ILLEGAL_CHARACTERS, '_'); + // If null is in an extension, we should assert (see bug 1637745). + mSuggestedFileName.ReplaceChar(char16_t(0), '_'); mTempFileExtension.ReplaceChar(KNOWN_PATH_SEPARATORS FILE_ILLEGAL_CHARACTERS, '_'); // Remove unsafe bidi characters which might have spoofing implications (bug 511521). @@ -2614,6 +2616,8 @@ NS_IMETHODIMP nsExternalHelperAppService::GetFromTypeAndExtension(const nsACStri NS_PRECONDITION(!aMIMEType.IsEmpty() || !aFileExt.IsEmpty(), "Give me something to work with"); + MOZ_DIAGNOSTIC_ASSERT(aFileExt.FindChar('\0') == kNotFound, + "The extension should never contain null characters"); LOG(("Getting mimeinfo from type '%s' ext '%s'\n", PromiseFlatCString(aMIMEType).get(), PromiseFlatCString(aFileExt).get()));