api-subframe-spec.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { expect } from 'chai'
  2. import * as path from 'path'
  3. import * as http from 'http'
  4. import { emittedNTimes, emittedOnce } from './events-helpers'
  5. import { closeWindow } from './window-helpers'
  6. import { app, BrowserWindow, ipcMain } from 'electron'
  7. import { AddressInfo } from 'net'
  8. import { ifdescribe } from './spec-helpers';
  9. describe('renderer nodeIntegrationInSubFrames', () => {
  10. const generateTests = (description: string, webPreferences: any) => {
  11. describe(description, () => {
  12. const fixtureSuffix = webPreferences.webviewTag ? '-webview' : ''
  13. let w: BrowserWindow
  14. beforeEach(async () => {
  15. await closeWindow(w)
  16. w = new BrowserWindow({
  17. show: false,
  18. width: 400,
  19. height: 400,
  20. webPreferences
  21. })
  22. })
  23. afterEach(async () => {
  24. await closeWindow(w)
  25. w = null as unknown as BrowserWindow
  26. })
  27. it('should load preload scripts in top level iframes', async () => {
  28. const detailsPromise = emittedNTimes(ipcMain, 'preload-ran', 2)
  29. w.loadFile(path.resolve(__dirname, `fixtures/sub-frames/frame-container${fixtureSuffix}.html`))
  30. const [event1, event2] = await detailsPromise
  31. expect(event1[0].frameId).to.not.equal(event2[0].frameId)
  32. expect(event1[0].frameId).to.equal(event1[2])
  33. expect(event2[0].frameId).to.equal(event2[2])
  34. })
  35. it('should load preload scripts in nested iframes', async () => {
  36. const detailsPromise = emittedNTimes(ipcMain, 'preload-ran', 3)
  37. w.loadFile(path.resolve(__dirname, `fixtures/sub-frames/frame-with-frame-container${fixtureSuffix}.html`))
  38. const [event1, event2, event3] = await detailsPromise
  39. expect(event1[0].frameId).to.not.equal(event2[0].frameId)
  40. expect(event1[0].frameId).to.not.equal(event3[0].frameId)
  41. expect(event2[0].frameId).to.not.equal(event3[0].frameId)
  42. expect(event1[0].frameId).to.equal(event1[2])
  43. expect(event2[0].frameId).to.equal(event2[2])
  44. expect(event3[0].frameId).to.equal(event3[2])
  45. })
  46. it('should correctly reply to the main frame with using event.reply', async () => {
  47. const detailsPromise = emittedNTimes(ipcMain, 'preload-ran', 2)
  48. w.loadFile(path.resolve(__dirname, `fixtures/sub-frames/frame-container${fixtureSuffix}.html`))
  49. const [event1] = await detailsPromise
  50. const pongPromise = emittedOnce(ipcMain, 'preload-pong')
  51. event1[0].reply('preload-ping')
  52. const details = await pongPromise
  53. expect(details[1]).to.equal(event1[0].frameId)
  54. })
  55. it('should correctly reply to the sub-frames with using event.reply', async () => {
  56. const detailsPromise = emittedNTimes(ipcMain, 'preload-ran', 2)
  57. w.loadFile(path.resolve(__dirname, `fixtures/sub-frames/frame-container${fixtureSuffix}.html`))
  58. const [, event2] = await detailsPromise
  59. const pongPromise = emittedOnce(ipcMain, 'preload-pong')
  60. event2[0].reply('preload-ping')
  61. const details = await pongPromise
  62. expect(details[1]).to.equal(event2[0].frameId)
  63. })
  64. it('should correctly reply to the nested sub-frames with using event.reply', async () => {
  65. const detailsPromise = emittedNTimes(ipcMain, 'preload-ran', 3)
  66. w.loadFile(path.resolve(__dirname, `fixtures/sub-frames/frame-with-frame-container${fixtureSuffix}.html`))
  67. const [, , event3] = await detailsPromise
  68. const pongPromise = emittedOnce(ipcMain, 'preload-pong')
  69. event3[0].reply('preload-ping')
  70. const details = await pongPromise
  71. expect(details[1]).to.equal(event3[0].frameId)
  72. })
  73. it('should not expose globals in main world', async () => {
  74. const detailsPromise = emittedNTimes(ipcMain, 'preload-ran', 2)
  75. w.loadFile(path.resolve(__dirname, `fixtures/sub-frames/frame-container${fixtureSuffix}.html`))
  76. const details = await detailsPromise
  77. const senders = details.map(event => event[0].sender)
  78. const isolatedGlobals = await Promise.all(senders.map(sender => sender.webContents.executeJavaScript('window.isolatedGlobal')))
  79. for (const result of isolatedGlobals) {
  80. if (webPreferences.contextIsolation) {
  81. expect(result).to.be.undefined()
  82. } else {
  83. expect(result).to.equal(true)
  84. }
  85. }
  86. })
  87. })
  88. }
  89. const generateConfigs = (webPreferences: any, ...permutations: {name: string, webPreferences: any}[]) => {
  90. const configs = [{ webPreferences, names: [] as string[] }]
  91. for (let i = 0; i < permutations.length; i++) {
  92. const length = configs.length
  93. for (let j = 0; j < length; j++) {
  94. const newConfig = Object.assign({}, configs[j])
  95. newConfig.webPreferences = Object.assign({},
  96. newConfig.webPreferences, permutations[i].webPreferences)
  97. newConfig.names = newConfig.names.slice(0)
  98. newConfig.names.push(permutations[i].name)
  99. configs.push(newConfig)
  100. }
  101. }
  102. return configs.map((config: any) => {
  103. if (config.names.length > 0) {
  104. config.title = `with ${config.names.join(', ')} on`
  105. } else {
  106. config.title = `without anything special turned on`
  107. }
  108. delete config.names
  109. return config as {title: string, webPreferences: any}
  110. })
  111. }
  112. generateConfigs(
  113. {
  114. preload: path.resolve(__dirname, 'fixtures/sub-frames/preload.js'),
  115. nodeIntegrationInSubFrames: true
  116. },
  117. {
  118. name: 'sandbox',
  119. webPreferences: { sandbox: true }
  120. },
  121. {
  122. name: 'context isolation',
  123. webPreferences: { contextIsolation: true }
  124. },
  125. {
  126. name: 'webview',
  127. webPreferences: { webviewTag: true, preload: false }
  128. }
  129. ).forEach(config => {
  130. generateTests(config.title, config.webPreferences)
  131. })
  132. describe('internal <iframe> inside of <webview>', () => {
  133. let w: BrowserWindow
  134. beforeEach(async () => {
  135. await closeWindow(w)
  136. w = new BrowserWindow({
  137. show: false,
  138. width: 400,
  139. height: 400,
  140. webPreferences: {
  141. preload: path.resolve(__dirname, 'fixtures/sub-frames/webview-iframe-preload.js'),
  142. nodeIntegrationInSubFrames: true,
  143. webviewTag: true
  144. }
  145. })
  146. })
  147. afterEach(async () => {
  148. await closeWindow(w)
  149. w = null as unknown as BrowserWindow
  150. })
  151. it('should not load preload scripts', async () => {
  152. const promisePass = emittedOnce(ipcMain, 'webview-loaded')
  153. const promiseFail = emittedOnce(ipcMain, 'preload-in-frame').then(() => {
  154. throw new Error('preload loaded in internal frame')
  155. })
  156. await w.loadURL('about:blank')
  157. return Promise.race([promisePass, promiseFail])
  158. })
  159. })
  160. })
  161. // app.getAppMetrics() does not return sandbox information on Linux.
  162. ifdescribe(process.platform !== 'linux')('cross-site frame sandboxing', () => {
  163. let server: http.Server
  164. let crossSiteUrl: string
  165. let serverUrl: string
  166. before(function (done) {
  167. server = http.createServer((req, res) => {
  168. res.end(`<iframe name="frame" src="${crossSiteUrl}" />`)
  169. })
  170. server.listen(0, '127.0.0.1', () => {
  171. serverUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}/`
  172. crossSiteUrl = `http://localhost:${(server.address() as AddressInfo).port}/`
  173. done()
  174. })
  175. })
  176. after(() => {
  177. server.close()
  178. server = null as unknown as http.Server
  179. })
  180. let w: BrowserWindow
  181. afterEach(async () => {
  182. await closeWindow(w)
  183. w = null as unknown as BrowserWindow
  184. })
  185. const generateSpecs = (description: string, webPreferences: any) => {
  186. describe(description, () => {
  187. it('iframe process is sandboxed if possible', async () => {
  188. w = new BrowserWindow({
  189. show: false,
  190. webPreferences
  191. })
  192. await w.loadURL(serverUrl)
  193. const pidMain = w.webContents.getOSProcessId()
  194. const pidFrame = (w.webContents as any)._getOSProcessIdForFrame('frame', crossSiteUrl)
  195. const metrics = app.getAppMetrics()
  196. const isProcessSandboxed = function (pid: number) {
  197. const entry = metrics.filter(metric => metric.pid === pid)[0]
  198. return entry && entry.sandboxed
  199. }
  200. const sandboxMain = !!(webPreferences.sandbox || process.mas)
  201. const sandboxFrame = sandboxMain || !webPreferences.nodeIntegrationInSubFrames
  202. expect(isProcessSandboxed(pidMain)).to.equal(sandboxMain)
  203. expect(isProcessSandboxed(pidFrame)).to.equal(sandboxFrame)
  204. })
  205. })
  206. }
  207. generateSpecs('nodeIntegrationInSubFrames = false, sandbox = false', {
  208. nodeIntegrationInSubFrames: false,
  209. sandbox: false
  210. })
  211. generateSpecs('nodeIntegrationInSubFrames = false, sandbox = true', {
  212. nodeIntegrationInSubFrames: false,
  213. sandbox: true
  214. })
  215. generateSpecs('nodeIntegrationInSubFrames = true, sandbox = false', {
  216. nodeIntegrationInSubFrames: true,
  217. sandbox: false
  218. })
  219. generateSpecs('nodeIntegrationInSubFrames = true, sandbox = true', {
  220. nodeIntegrationInSubFrames: true,
  221. sandbox: true
  222. })
  223. })