From db6097ecec5b4d6f8d6c04070d4c6039d5b0dde9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 28 Apr 2017 09:28:11 -0700 Subject: [PATCH 1/3] Add failing spec for invalid debugger message --- spec/api-debugger-spec.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/spec/api-debugger-spec.js b/spec/api-debugger-spec.js index f8597c03409b..9224d643184c 100644 --- a/spec/api-debugger-spec.js +++ b/spec/api-debugger-spec.js @@ -1,4 +1,6 @@ const assert = require('assert') +const fs = require('fs') +const http = require('http') const path = require('path') const {closeWindow} = require('./window-helpers') const BrowserWindow = require('electron').remote.BrowserWindow @@ -70,6 +72,15 @@ describe('debugger module', function () { }) describe('debugger.sendCommand', function () { + let server + + afterEach(function () { + if (server != null) { + server.close() + server = null + } + }) + it('retuns response', function (done) { w.webContents.loadURL('about:blank') try { @@ -125,5 +136,33 @@ describe('debugger module', function () { done() }) }) + + it('handles invalid unicode characters in message', function (done) { + try { + w.webContents.debugger.attach() + } catch (err) { + done('unexpected error : ' + err) + } + + w.webContents.debugger.on('message', (event, method, params) => { + if (method === 'Network.loadingFinished') { + w.webContents.debugger.sendCommand('Network.getResponseBody', { + requestId: params.requestId + }, () => { + done() + }) + } + }) + + server = http.createServer((req, res) => { + res.setHeader('Content-Type', 'text/plain; charset=utf-8') + res.end('\uFFFF') + }) + + server.listen(0, '127.0.0.1', () => { + w.webContents.debugger.sendCommand('Network.enable') + w.loadURL(`http://127.0.0.1:${server.address().port}`) + }) + }) }) }) From f2f64155436554b12ca8fbb00dfcab542c825cbc Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 28 Apr 2017 10:42:01 -0700 Subject: [PATCH 2/3] Use v8 to parse message as JSON --- atom/browser/api/atom_api_debugger.cc | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/atom/browser/api/atom_api_debugger.cc b/atom/browser/api/atom_api_debugger.cc index 90999a4f602a..912d5da3683e 100644 --- a/atom/browser/api/atom_api_debugger.cc +++ b/atom/browser/api/atom_api_debugger.cc @@ -45,12 +45,23 @@ void Debugger::DispatchProtocolMessage(DevToolsAgentHost* agent_host, const std::string& message) { DCHECK(agent_host == agent_host_.get()); - std::unique_ptr parsed_message(base::JSONReader::Read(message)); - if (!parsed_message->IsType(base::Value::TYPE_DICTIONARY)) - return; + v8::Locker locker(isolate()); + v8::HandleScope handle_scope(isolate()); + + v8::Local local_message = + v8::String::NewFromUtf8(isolate(), message.data()); + v8::MaybeLocal parsed_message = v8::JSON::Parse( + isolate()->GetCurrentContext(), local_message); + if (parsed_message.IsEmpty()) { + return; + } + + std::unique_ptr dict(new base::DictionaryValue()); + if (!mate::ConvertFromV8(isolate(), parsed_message.ToLocalChecked(), + dict.get())) { + return; + } - base::DictionaryValue* dict = - static_cast(parsed_message.get()); int id; if (!dict->GetInteger("id", &id)) { std::string method; From b639dd0c81e882aa9e68d01f3ee23f3000b63002 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 28 Apr 2017 10:46:14 -0700 Subject: [PATCH 3/3] Remove unused include/require statement --- atom/browser/api/atom_api_debugger.cc | 1 - spec/api-debugger-spec.js | 1 - 2 files changed, 2 deletions(-) diff --git a/atom/browser/api/atom_api_debugger.cc b/atom/browser/api/atom_api_debugger.cc index 912d5da3683e..075823185259 100644 --- a/atom/browser/api/atom_api_debugger.cc +++ b/atom/browser/api/atom_api_debugger.cc @@ -9,7 +9,6 @@ #include "atom/browser/atom_browser_main_parts.h" #include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/value_converter.h" -#include "base/json/json_reader.h" #include "base/json/json_writer.h" #include "content/public/browser/devtools_agent_host.h" #include "content/public/browser/web_contents.h" diff --git a/spec/api-debugger-spec.js b/spec/api-debugger-spec.js index 9224d643184c..83b114f72380 100644 --- a/spec/api-debugger-spec.js +++ b/spec/api-debugger-spec.js @@ -1,5 +1,4 @@ const assert = require('assert') -const fs = require('fs') const http = require('http') const path = require('path') const {closeWindow} = require('./window-helpers')