diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index c4762c10b9..afb6f4ce9d 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -861,7 +861,7 @@ bool WebContents::Equal(const WebContents* web_contents) const { } void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) { - if (!url.is_valid()) { + if (!url.is_valid() || url.spec().size() > url::kMaxURLChars) { Emit("did-fail-load", static_cast(net::ERR_INVALID_URL), net::ErrorToShortString(net::ERR_INVALID_URL), @@ -900,14 +900,16 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) { // We have to call it right after LoadURL because the RenderViewHost is only // created after loading a page. const auto view = web_contents()->GetRenderWidgetHostView(); - WebContentsPreferences* web_preferences = - WebContentsPreferences::FromWebContents(web_contents()); - std::string color_name; - if (web_preferences->web_preferences()->GetString(options::kBackgroundColor, - &color_name)) { - view->SetBackgroundColor(ParseHexColor(color_name)); - } else { - view->SetBackgroundColor(SK_ColorTRANSPARENT); + if (view) { + WebContentsPreferences* web_preferences = + WebContentsPreferences::FromWebContents(web_contents()); + std::string color_name; + if (web_preferences->web_preferences()->GetString(options::kBackgroundColor, + &color_name)) { + view->SetBackgroundColor(ParseHexColor(color_name)); + } else { + view->SetBackgroundColor(SK_ColorTRANSPARENT); + } } } diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 5253d890ef..3942f81836 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -218,6 +218,17 @@ describe('BrowserWindow module', function () { w.loadURL('http://127.0.0.1:11111') }) + it('should emit did-fail-load event for URL exceeding character limit', function (done) { + w.webContents.on('did-fail-load', function (event, code, desc, url, isMainFrame) { + assert.equal(desc, 'ERR_INVALID_URL') + assert.equal(code, -300) + assert.equal(isMainFrame, true) + done() + }) + const data = new Buffer(2 * 1024 * 1024).toString('base64') + w.loadURL(`data:image/png;base64,${data}`) + }) + describe('POST navigations', function () { afterEach(() => { w.webContents.session.webRequest.onBeforeSendHeaders(null)