/
chromium-spec.ts
353 lines (308 loc) · 12.6 KB
/
chromium-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import { BrowserWindow, WebContents, session, ipcMain } from 'electron'
import { emittedOnce } from './events-helpers'
import { closeAllWindows } from './window-helpers'
import * as https from 'https'
import * as http from 'http'
import * as path from 'path'
import * as fs from 'fs'
import { EventEmitter } from 'events'
import { promisify } from 'util'
const { expect } = chai
chai.use(chaiAsPromised)
const fixturesPath = path.resolve(__dirname, '..', 'spec', 'fixtures')
describe('reporting api', () => {
it('sends a report for a deprecation', async () => {
const reports = new EventEmitter
// The Reporting API only works on https with valid certs. To dodge having
// to set up a trusted certificate, hack the validator.
session.defaultSession.setCertificateVerifyProc((req, cb) => {
cb(0)
})
const certPath = path.join(fixturesPath, 'certificates')
const options = {
key: fs.readFileSync(path.join(certPath, 'server.key')),
cert: fs.readFileSync(path.join(certPath, 'server.pem')),
ca: [
fs.readFileSync(path.join(certPath, 'rootCA.pem')),
fs.readFileSync(path.join(certPath, 'intermediateCA.pem'))
],
requestCert: true,
rejectUnauthorized: false
}
const server = https.createServer(options, (req, res) => {
if (req.url === '/report') {
let data = ''
req.on('data', (d) => data += d.toString('utf-8'))
req.on('end', () => {
reports.emit('report', JSON.parse(data))
})
}
res.setHeader('Report-To', JSON.stringify({
group: 'default',
max_age: 120,
endpoints: [ {url: `https://localhost:${(server.address() as any).port}/report`} ],
}))
res.setHeader('Content-Type', 'text/html')
// using the deprecated `webkitRequestAnimationFrame` will trigger a
// "deprecation" report.
res.end('<script>webkitRequestAnimationFrame(() => {})</script>')
})
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
const bw = new BrowserWindow({
show: false,
})
try {
const reportGenerated = emittedOnce(reports, 'report')
const url = `https://localhost:${(server.address() as any).port}/a`
await bw.loadURL(url)
const [report] = await reportGenerated
expect(report).to.be.an('array')
expect(report[0].type).to.equal('deprecation')
expect(report[0].url).to.equal(url)
expect(report[0].body.id).to.equal('PrefixedRequestAnimationFrame')
} finally {
bw.destroy()
server.close()
}
})
describe('window.open', () => {
it('denies custom open when nativeWindowOpen: true', async () => {
const w = new BrowserWindow({
show: false,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
nativeWindowOpen: true
}
});
w.loadURL('about:blank');
const previousListeners = process.listeners('uncaughtException');
process.removeAllListeners('uncaughtException');
try {
const uncaughtException = new Promise<Error>(resolve => {
process.once('uncaughtException', resolve);
});
expect(await w.webContents.executeJavaScript(`(${function () {
const ipc = process.electronBinding('ipc').ipc;
return ipc.sendSync(true, 'ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', ['', '', ''])[0];
}})()`)).to.be.null('null');
const exception = await uncaughtException;
expect(exception.message).to.match(/denied: expected native window\.open/);
} finally {
previousListeners.forEach(l => process.on('uncaughtException', l));
}
});
});
})
describe('window.postMessage', () => {
afterEach(async () => {
await closeAllWindows()
})
it('sets the source and origin correctly', async () => {
const w = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true}})
w.loadURL(`file://${fixturesPath}/pages/window-open-postMessage-driver.html`)
const [, message] = await emittedOnce(ipcMain, 'complete')
expect(message.data).to.equal('testing')
expect(message.origin).to.equal('file://')
expect(message.sourceEqualsOpener).to.equal(true)
expect(message.eventOrigin).to.equal('file://')
})
})
describe('focus handling', () => {
let webviewContents: WebContents = null as unknown as WebContents
let w: BrowserWindow = null as unknown as BrowserWindow
beforeEach(async () => {
w = new BrowserWindow({
show: true,
webPreferences: {
nodeIntegration: true,
webviewTag: true
}
})
const webviewReady = emittedOnce(w.webContents, 'did-attach-webview')
await w.loadFile(path.join(fixturesPath, 'pages', 'tab-focus-loop-elements.html'))
const [, wvContents] = await webviewReady
webviewContents = wvContents
await emittedOnce(webviewContents, 'did-finish-load')
w.focus()
})
afterEach(() => {
webviewContents = null as unknown as WebContents
w.destroy()
w = null as unknown as BrowserWindow
})
const expectFocusChange = async () => {
const [, focusedElementId] = await emittedOnce(ipcMain, 'focus-changed')
return focusedElementId
}
describe('a TAB press', () => {
const tabPressEvent: any = {
type: 'keyDown',
keyCode: 'Tab'
}
it('moves focus to the next focusable item', async () => {
let focusChange = expectFocusChange()
w.webContents.sendInputEvent(tabPressEvent)
let focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-element-1', `should start focused in element-1, it's instead in ${focusedElementId}`)
focusChange = expectFocusChange()
w.webContents.sendInputEvent(tabPressEvent)
focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-element-2', `focus should've moved to element-2, it's instead in ${focusedElementId}`)
focusChange = expectFocusChange()
w.webContents.sendInputEvent(tabPressEvent)
focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-wv-element-1', `focus should've moved to the webview's element-1, it's instead in ${focusedElementId}`)
focusChange = expectFocusChange()
webviewContents.sendInputEvent(tabPressEvent)
focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-wv-element-2', `focus should've moved to the webview's element-2, it's instead in ${focusedElementId}`)
focusChange = expectFocusChange()
webviewContents.sendInputEvent(tabPressEvent)
focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-element-3', `focus should've moved to element-3, it's instead in ${focusedElementId}`)
focusChange = expectFocusChange()
w.webContents.sendInputEvent(tabPressEvent)
focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-element-1', `focus should've looped back to element-1, it's instead in ${focusedElementId}`)
})
})
describe('a SHIFT + TAB press', () => {
const shiftTabPressEvent: any = {
type: 'keyDown',
modifiers: ['Shift'],
keyCode: 'Tab'
}
it('moves focus to the previous focusable item', async () => {
let focusChange = expectFocusChange()
w.webContents.sendInputEvent(shiftTabPressEvent)
let focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-element-3', `should start focused in element-3, it's instead in ${focusedElementId}`)
focusChange = expectFocusChange()
w.webContents.sendInputEvent(shiftTabPressEvent)
focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-wv-element-2', `focus should've moved to the webview's element-2, it's instead in ${focusedElementId}`)
focusChange = expectFocusChange()
webviewContents.sendInputEvent(shiftTabPressEvent)
focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-wv-element-1', `focus should've moved to the webview's element-1, it's instead in ${focusedElementId}`)
focusChange = expectFocusChange()
webviewContents.sendInputEvent(shiftTabPressEvent)
focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-element-2', `focus should've moved to element-2, it's instead in ${focusedElementId}`)
focusChange = expectFocusChange()
w.webContents.sendInputEvent(shiftTabPressEvent)
focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-element-1', `focus should've moved to element-1, it's instead in ${focusedElementId}`)
focusChange = expectFocusChange()
w.webContents.sendInputEvent(shiftTabPressEvent)
focusedElementId = await focusChange
expect(focusedElementId).to.equal('BUTTON-element-3', `focus should've looped back to element-3, it's instead in ${focusedElementId}`)
})
})
})
describe('web security', () => {
afterEach(closeAllWindows)
let server: http.Server
let serverUrl: string
before(async () => {
server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/html')
res.end('<body>')
})
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve))
serverUrl = `http://localhost:${(server.address() as any).port}`
})
after(() => {
server.close()
})
it('engages CORB when web security is not disabled', async () => {
const w = new BrowserWindow({ show: true, webPreferences: { webSecurity: true, nodeIntegration: true } })
const p = emittedOnce(ipcMain, 'success')
await w.loadURL(`data:text/html,<script>
const s = document.createElement('script')
s.src = "${serverUrl}"
// The script will load successfully but its body will be emptied out
// by CORB, so we don't expect a syntax error.
s.onload = () => { require('electron').ipcRenderer.send('success') }
document.documentElement.appendChild(s)
</script>`)
await p
})
it('bypasses CORB when web security is disabled', async () => {
const w = new BrowserWindow({ show: true, webPreferences: { webSecurity: false, nodeIntegration: true } })
const p = emittedOnce(ipcMain, 'success')
await w.loadURL(`data:text/html,
<script>
window.onerror = (e) => { require('electron').ipcRenderer.send('success', e) }
</script>
<script src="${serverUrl}"></script>`)
await p
})
})
describe('iframe using HTML fullscreen API while window is OS-fullscreened', () => {
const fullscreenChildHtml = promisify(fs.readFile)(
path.join(fixturesPath, 'pages', 'fullscreen-oopif.html')
)
let w: BrowserWindow, server: http.Server
before(() => {
server = http.createServer(async (_req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.write(await fullscreenChildHtml)
res.end()
})
server.listen(8989, '127.0.0.1')
})
beforeEach(() => {
w = new BrowserWindow({
show: true,
fullscreen: true,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInSubFrames: true
}
})
})
afterEach(async () => {
await closeAllWindows()
;(w as any) = null
server.close()
})
it('can fullscreen from out-of-process iframes (OOPIFs)', done => {
ipcMain.once('fullscreenChange', async () => {
const fullscreenWidth = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
)
expect(fullscreenWidth > 0).to.be.true
await w.webContents.executeJavaScript(
"document.querySelector('iframe').contentWindow.postMessage('exitFullscreen', '*')"
)
await new Promise(resolve => setTimeout(resolve, 500))
const width = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
)
expect(width).to.equal(0)
done()
})
const html =
'<iframe style="width: 0" frameborder=0 src="http://localhost:8989" allowfullscreen></iframe>'
w.loadURL(`data:text/html,${html}`)
})
it('can fullscreen from in-process iframes', done => {
ipcMain.once('fullscreenChange', async () => {
const fullscreenWidth = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
)
expect(fullscreenWidth > 0).to.true
await w.webContents.executeJavaScript('document.exitFullscreen()')
const width = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
)
expect(width).to.equal(0)
done()
})
w.loadFile(path.join(fixturesPath, 'pages', 'fullscreen-ipif.html'))
})
})