fix: prevent print crash on bad deviceName (#21946)
* fix: prevent print crash on bad deviceName * address review feedback
This commit is contained in:
parent
662b94f46e
commit
2955c67c4e
3 changed files with 44 additions and 3 deletions
|
@ -116,6 +116,10 @@
|
|||
#if BUILDFLAG(ENABLE_PRINTING)
|
||||
#include "chrome/browser/printing/print_view_manager_basic.h"
|
||||
#include "components/printing/common/print_messages.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
#include "printing/backend/win_helper.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||
|
@ -346,6 +350,26 @@ base::Optional<base::TimeDelta> GetCursorBlinkInterval() {
|
|||
return base::nullopt;
|
||||
}
|
||||
|
||||
#if BUILDFLAG(ENABLE_PRINTING)
|
||||
// This will return false if no printer with the provided device_name can be
|
||||
// found on the network. We need to check this because Chromium does not do
|
||||
// sanity checking of device_name validity and so will crash on invalid names.
|
||||
bool IsDeviceNameValid(const base::string16& device_name) {
|
||||
#if defined(OS_MACOSX)
|
||||
base::ScopedCFTypeRef<CFStringRef> new_printer_id(
|
||||
base::SysUTF16ToCFStringRef(device_name));
|
||||
PMPrinter new_printer = PMPrinterCreateFromPrinterID(new_printer_id.get());
|
||||
bool printer_exists = new_printer != nullptr;
|
||||
PMRelease(new_printer);
|
||||
return printer_exists;
|
||||
#elif defined(OS_WIN)
|
||||
printing::ScopedPrinterHandle printer;
|
||||
return printer.OpenPrinterWithName(device_name.c_str());
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
WebContents::WebContents(v8::Isolate* isolate,
|
||||
|
@ -1782,6 +1806,10 @@ void WebContents::Print(gin_helper::Arguments* args) {
|
|||
// Printer device name as opened by the OS.
|
||||
base::string16 device_name;
|
||||
options.Get("deviceName", &device_name);
|
||||
if (!device_name.empty() && !IsDeviceNameValid(device_name)) {
|
||||
args->ThrowError("webContents.print(): Invalid deviceName provided.");
|
||||
return;
|
||||
}
|
||||
settings.SetStringKey(printing::kSettingDeviceName, device_name);
|
||||
|
||||
int scale_factor = 100;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue