Skip to content

Commit 06ee0ca

Browse files
flotwigbrian-mann
andcommitted
Fix erroneous socket connections to undefinedCHANGE_ME_PATH (#4799)
* only connect to socket if path has been replaced, fixes #4776 * upgrade sinon, do not instantiate sandbox * add tests to ensure connect is not called when not in browser mode * refactor once again to push connect to a separate module to make testable * move initializing extension connection to init.js - prevents situation where connect(…) is called when module is required - simplifies handling automatic connection only when in the browser without needing to modify globals Co-authored-by: Brian Mann <brian.mann86@gmail.com>
1 parent 6be24f6 commit 06ee0ca

File tree

9 files changed

+64
-50
lines changed

9 files changed

+64
-50
lines changed

packages/extension/app/background.coffee

+3-13
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ map = require("lodash/map")
22
pick = require("lodash/pick")
33
once = require("lodash/once")
44
Promise = require("bluebird")
5-
{ client, circularParser } = require("@packages/socket/lib/browser")
6-
7-
HOST = "CHANGE_ME_HOST"
8-
PATH = "CHANGE_ME_PATH"
5+
client = require("./client")
96

107
httpRe = /^http/
118

@@ -35,11 +32,7 @@ connect = (host, path) ->
3532
.catch (err) ->
3633
fail(id, err)
3734

38-
ws = client.connect(host, {
39-
path: path,
40-
transports: ["websocket"]
41-
parser: circularParser
42-
})
35+
ws = client.connect(host, path)
4336

4437
ws.on "automation:request", (id, msg, data) ->
4538
switch msg
@@ -68,10 +61,7 @@ connect = (host, path) ->
6861
ws.emit("automation:client:connected")
6962

7063
return ws
71-
72-
## initially connect
73-
connect(HOST, PATH)
74-
64+
7565
automation = {
7666
connect
7767

packages/extension/app/client.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { client, circularParser } = require('@packages/socket/lib/browser')
2+
3+
const connect = (host, path) => {
4+
return client.connect(host, {
5+
path,
6+
transports: ['websocket'],
7+
// @ts-ignore
8+
parser: circularParser,
9+
})
10+
}
11+
12+
module.exports = {
13+
connect,
14+
15+
socketIoClient: client,
16+
17+
socketIoParser: circularParser,
18+
}

packages/extension/app/init.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const background = require('./background.coffee')
2+
3+
const HOST = 'CHANGE_ME_HOST'
4+
const PATH = 'CHANGE_ME_PATH'
5+
6+
// immediately connect
7+
background.connect(HOST, PATH)

packages/extension/gulpfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ gulp.task('backup', () => {
3737

3838
gulp.task('background', () => {
3939
return browserify({
40-
entries: 'app/background.coffee',
40+
entries: 'app/init.js',
4141
transform: coffeeify,
4242
})
4343
.bundle()

packages/extension/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"gulp": "4.0.2",
3636
"gulp-clean": "0.4.0",
3737
"gulp-rename": "1.4.0",
38-
"sinon": "1.17.7",
38+
"run-sequence": "1.2.2",
39+
"sinon": "7.3.2",
3940
"sinon-chai": "3.3.0",
4041
"vinyl-source-stream": "2.0.0"
4142
},

packages/extension/test/integration/background_spec.coffee

+25-25
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ describe "app/background", ->
112112
it "emits 'automation:client:connected'", (done) ->
113113
client = background.connect("http://localhost:#{PORT}", "/__socket.io")
114114

115-
@sandbox.spy(client, "emit")
115+
sinon.spy(client, "emit")
116116

117117
client.on "connect", _.once ->
118118
expect(client.emit).to.be.calledWith("automation:client:connected")
119119
done()
120120

121121
it "listens to cookie changes", (done) ->
122-
addListener = @sandbox.stub(chrome.cookies.onChanged, "addListener")
122+
addListener = sinon.stub(chrome.cookies.onChanged, "addListener")
123123
client = background.connect("http://localhost:#{PORT}", "/__socket.io")
124124

125125
client.on "connect", _.once ->
@@ -128,10 +128,10 @@ describe "app/background", ->
128128

129129
context "onChanged", ->
130130
it "does not emit when cause is overwrite", (done) ->
131-
addListener = @sandbox.stub(chrome.cookies.onChanged, "addListener")
131+
addListener = sinon.stub(chrome.cookies.onChanged, "addListener")
132132
client = background.connect("http://localhost:#{PORT}", "/__socket.io")
133133

134-
@sandbox.spy(client, "emit")
134+
sinon.spy(client, "emit")
135135

136136
client.on "connect", _.once ->
137137
fn = addListener.getCall(0).args[0]
@@ -144,7 +144,7 @@ describe "app/background", ->
144144
it "emits 'automation:push:request'", (done) ->
145145
info = { cause: "explicit", cookie: {name: "foo", value: "bar"} }
146146

147-
addListener = @sandbox.stub(chrome.cookies.onChanged, "addListener").yieldsAsync(info)
147+
addListener = sinon.stub(chrome.cookies.onChanged, "addListener").yieldsAsync(info)
148148
client = background.connect("http://localhost:#{PORT}", "/__socket.io")
149149

150150
client.on "connect", ->
@@ -156,7 +156,7 @@ describe "app/background", ->
156156

157157
context ".getAll", ->
158158
it "resolves with specific cookie properties", ->
159-
@sandbox.stub(chrome.cookies, "getAll")
159+
sinon.stub(chrome.cookies, "getAll")
160160
.withArgs({domain: "localhost"})
161161
.yieldsAsync([
162162
{name: "foo", value: "f", path: "/", domain: "localhost", secure: true, httpOnly: true, expirationDate: 123}
@@ -175,11 +175,11 @@ describe "app/background", ->
175175
@code = "var s; (s = document.getElementById('__cypress-string')) && s.textContent"
176176

177177
it "resolves on the 1st tab", ->
178-
@sandbox.stub(chrome.tabs, "query")
178+
sinon.stub(chrome.tabs, "query")
179179
.withArgs({windowType: "normal"})
180180
.yieldsAsync([tab1])
181181

182-
@sandbox.stub(chrome.tabs, "executeScript")
182+
sinon.stub(chrome.tabs, "executeScript")
183183
.withArgs(tab1.id, {code: @code})
184184
.yieldsAsync(["1234"])
185185

@@ -189,11 +189,11 @@ describe "app/background", ->
189189
})
190190

191191
it "resolves on the 2nd tab", ->
192-
@sandbox.stub(chrome.tabs, "query")
192+
sinon.stub(chrome.tabs, "query")
193193
.withArgs({windowType: "normal"})
194194
.yieldsAsync([tab1, tab2])
195195

196-
@sandbox.stub(chrome.tabs, "executeScript")
196+
sinon.stub(chrome.tabs, "executeScript")
197197
.withArgs(tab1.id, {code: @code})
198198
.yieldsAsync(["foobarbaz"])
199199
.withArgs(tab2.id, {code: @code})
@@ -205,7 +205,7 @@ describe "app/background", ->
205205
})
206206

207207
it "filters out tabs that don't start with http", ->
208-
@sandbox.stub(chrome.tabs, "query")
208+
sinon.stub(chrome.tabs, "query")
209209
.yieldsAsync([tab3])
210210

211211
background.query({
@@ -219,11 +219,11 @@ describe "app/background", ->
219219
expect(err).to.be.instanceof(Promise.RangeError)
220220

221221
it "rejects if no tab matches", ->
222-
@sandbox.stub(chrome.tabs, "query")
222+
sinon.stub(chrome.tabs, "query")
223223
.withArgs({windowType: "normal"})
224224
.yieldsAsync([tab1, tab2])
225225

226-
@sandbox.stub(chrome.tabs, "executeScript")
226+
sinon.stub(chrome.tabs, "executeScript")
227227
.withArgs(tab1.id, {code: @code})
228228
.yieldsAsync(["foobarbaz"])
229229
.withArgs(tab2.id, {code: @code})
@@ -241,7 +241,7 @@ describe "app/background", ->
241241
expect(err).to.be.instanceof(Promise.AggregateError)
242242

243243
it "rejects if no tabs were found", ->
244-
@sandbox.stub(chrome.tabs, "query")
244+
sinon.stub(chrome.tabs, "query")
245245
.yieldsAsync([])
246246

247247
background.query({
@@ -263,7 +263,7 @@ describe "app/background", ->
263263

264264
describe "get:cookies", ->
265265
beforeEach ->
266-
@sandbox.stub(chrome.cookies, "getAll")
266+
sinon.stub(chrome.cookies, "getAll")
267267
.withArgs({domain: "google.com"})
268268
.yieldsAsync([{}, {}])
269269

@@ -277,7 +277,7 @@ describe "app/background", ->
277277

278278
describe "get:cookie", ->
279279
beforeEach ->
280-
@sandbox.stub(chrome.cookies, "getAll")
280+
sinon.stub(chrome.cookies, "getAll")
281281
.withArgs({domain: "google.com", name: "session"})
282282
.yieldsAsync([
283283
{name: "session", value: "key", path: "/login", domain: "google", secure: true, httpOnly: true, expirationDate: 123}
@@ -305,7 +305,7 @@ describe "app/background", ->
305305
beforeEach ->
306306
chrome.runtime.lastError = {message: "some error"}
307307

308-
@sandbox.stub(chrome.cookies, "set")
308+
sinon.stub(chrome.cookies, "set")
309309
.withArgs({domain: "google.com", name: "session", value: "key", path: "/", secure: false, url: "http://google.com/"})
310310
.yieldsAsync(
311311
{name: "session", value: "key", path: "/", domain: "google", secure: false, httpOnly: false}
@@ -358,7 +358,7 @@ describe "app/background", ->
358358
beforeEach ->
359359
chrome.runtime.lastError = {message: "some error"}
360360

361-
@sandbox.stub(chrome.cookies, "getAll")
361+
sinon.stub(chrome.cookies, "getAll")
362362
.withArgs({domain: "google.com"})
363363
.yieldsAsync([
364364
{name: "session", value: "key", path: "/", domain: "google.com", secure: true, httpOnly: true, expirationDate: 123}
@@ -369,7 +369,7 @@ describe "app/background", ->
369369
{name: "shouldThrow", value: "key", path: "/assets", domain: "cdn.github.com", secure: false, httpOnly: true, expirationDate: 123}
370370
])
371371

372-
@sandbox.stub(chrome.cookies, "remove")
372+
sinon.stub(chrome.cookies, "remove")
373373
.withArgs({name: "session", url: "https://google.com/"})
374374
.yieldsAsync(
375375
{name: "session", url: "https://google.com/", storeId: "123"}
@@ -417,7 +417,7 @@ describe "app/background", ->
417417
beforeEach ->
418418
chrome.runtime.lastError = {message: "some error"}
419419

420-
@sandbox.stub(chrome.cookies, "getAll")
420+
sinon.stub(chrome.cookies, "getAll")
421421
.withArgs({domain: "google.com", name: "session"})
422422
.yieldsAsync([
423423
{name: "session", value: "key", path: "/", domain: "google.com", secure: true, httpOnly: true, expirationDate: 123}
@@ -429,7 +429,7 @@ describe "app/background", ->
429429
{name: "shouldThrow", value: "key", path: "/assets", domain: "cdn.github.com", secure: false, httpOnly: true, expirationDate: 123}
430430
])
431431

432-
@sandbox.stub(chrome.cookies, "remove")
432+
sinon.stub(chrome.cookies, "remove")
433433
.withArgs({name: "session", url: "https://google.com/"})
434434
.yieldsAsync(
435435
{name: "session", url: "https://google.com/", storeId: "123"}
@@ -468,7 +468,7 @@ describe "app/background", ->
468468

469469
describe "is:automation:client:connected", ->
470470
beforeEach ->
471-
@sandbox.stub(chrome.tabs, "query")
471+
sinon.stub(chrome.tabs, "query")
472472
.withArgs({url: "CHANGE_ME_HOST/*", windowType: "normal"})
473473
.yieldsAsync([])
474474

@@ -482,13 +482,13 @@ describe "app/background", ->
482482

483483
describe "take:screenshot", ->
484484
beforeEach ->
485-
@sandbox.stub(chrome.windows, "getLastFocused").yieldsAsync({id: 1})
485+
sinon.stub(chrome.windows, "getLastFocused").yieldsAsync({id: 1})
486486

487487
afterEach ->
488488
delete chrome.runtime.lastError
489489

490490
it "resolves with screenshot", (done) ->
491-
@sandbox.stub(chrome.tabs, "captureVisibleTab").withArgs(1, {format: "png"}).yieldsAsync("foobarbaz")
491+
sinon.stub(chrome.tabs, "captureVisibleTab").withArgs(1, {format: "png"}).yieldsAsync("foobarbaz")
492492

493493
@socket.on "automation:response", (id, obj = {}) ->
494494
expect(id).to.eq(123)
@@ -499,7 +499,7 @@ describe "app/background", ->
499499

500500
it "rejects with chrome.runtime.lastError", (done) ->
501501
chrome.runtime.lastError = {message: "some error"}
502-
@sandbox.stub(chrome.tabs, "captureVisibleTab").withArgs(1, {format: "png"}).yieldsAsync(undefined)
502+
sinon.stub(chrome.tabs, "captureVisibleTab").withArgs(1, {format: "png"}).yieldsAsync(undefined)
503503

504504
@socket.on "automation:response", (id, obj = {}) ->
505505
expect(id).to.eq(123)

packages/extension/test/spec_helper.coffee

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ sinonChai = require("sinon-chai")
44

55
chai.use(sinonChai)
66

7+
global.sinon = sinon
78
global.expect = chai.expect
89

9-
beforeEach ->
10-
@sandbox = sinon.sandbox.create()
11-
1210
afterEach ->
13-
@sandbox.restore()
11+
sinon.restore()

packages/extension/test/unit/extension_spec.coffee

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ require("../spec_helper")
22

33
exec = require("child_process").exec
44
fs = require("fs-extra")
5+
EE = require("events")
6+
eol = require("eol")
57
path = require("path")
68
Promise = require("bluebird")
7-
eol = require("eol")
89
extension = require("../../index")
9-
cwd = process.cwd()
1010

11+
cwd = process.cwd()
1112
fs = Promise.promisifyAll(fs)
1213
exec = Promise.promisify(exec)
1314

@@ -47,7 +48,7 @@ describe "Extension", ->
4748
beforeEach ->
4849
@src = path.join(cwd, "test", "helpers", "background.js")
4950

50-
@sandbox.stub(extension, "getPathToExtension")
51+
sinon.stub(extension, "getPathToExtension")
5152
.withArgs("background.js").returns(@src)
5253

5354
it "rewrites the background.js source", ->
@@ -98,4 +99,3 @@ describe "Extension", ->
9899
exec(cmd)
99100
.then (stdout) ->
100101
expect(stdout).to.eq("caljajdfkjjjdehjdoimjkkakekklcck")
101-

packages/server/test/e2e/2_browser_path_spec.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ describe "e2e launching browsers by path", ->
3333
launcher.detect().then (browsers) =>
3434
browsers.find (browser) =>
3535
browser.family == "chrome"
36-
.then (browser) =>
36+
.tap (browser) =>
3737
if !browser
3838
throw new Error("A 'chrome' family browser must be installed for this test")
39-
browser.path
39+
.get("path")
4040
## turn binary browser names ("google-chrome") into their absolute paths
4141
## so that server recognizes them as a path, not as a browser name
4242
.then (absPath)

0 commit comments

Comments
 (0)