api-subframe-spec.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. const { expect } = require('chai');
  2. const { remote } = require('electron');
  3. const path = require('path');
  4. const http = require('http');
  5. const { emittedNTimes, emittedOnce } = require('./events-helpers');
  6. const { closeWindow } = require('./window-helpers');
  7. const { app, BrowserWindow, ipcMain } = remote;
  8. describe('renderer nodeIntegrationInSubFrames', () => {
  9. const generateTests = (description, webPreferences) => {
  10. describe(description, () => {
  11. const fixtureSuffix = webPreferences.webviewTag ? '-webview' : '';
  12. let w;
  13. beforeEach(async () => {
  14. await closeWindow(w);
  15. w = new BrowserWindow({
  16. show: false,
  17. width: 400,
  18. height: 400,
  19. webPreferences
  20. });
  21. });
  22. afterEach(() => {
  23. return closeWindow(w).then(() => {
  24. w = null;
  25. });
  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. await new Promise(async resolve => {
  79. let resultCount = 0;
  80. senders.forEach(async sender => {
  81. const result = await sender.webContents.executeJavaScript('window.isolatedGlobal');
  82. if (webPreferences.contextIsolation) {
  83. expect(result).to.be.null();
  84. } else {
  85. expect(result).to.equal(true);
  86. }
  87. resultCount++;
  88. if (resultCount === senders.length) resolve();
  89. });
  90. });
  91. });
  92. });
  93. };
  94. const generateConfigs = (webPreferences, ...permutations) => {
  95. const configs = [{ webPreferences, names: [] }];
  96. for (let i = 0; i < permutations.length; i++) {
  97. const length = configs.length;
  98. for (let j = 0; j < length; j++) {
  99. const newConfig = Object.assign({}, configs[j]);
  100. newConfig.webPreferences = Object.assign({},
  101. newConfig.webPreferences, permutations[i].webPreferences);
  102. newConfig.names = newConfig.names.slice(0);
  103. newConfig.names.push(permutations[i].name);
  104. configs.push(newConfig);
  105. }
  106. }
  107. return configs.map(config => {
  108. if (config.names.length > 0) {
  109. config.title = `with ${config.names.join(', ')} on`;
  110. } else {
  111. config.title = `without anything special turned on`;
  112. }
  113. delete config.names;
  114. return config;
  115. });
  116. };
  117. generateConfigs(
  118. {
  119. preload: path.resolve(__dirname, 'fixtures/sub-frames/preload.js'),
  120. nodeIntegrationInSubFrames: true
  121. },
  122. {
  123. name: 'sandbox',
  124. webPreferences: { sandbox: true }
  125. },
  126. {
  127. name: 'context isolation',
  128. webPreferences: { contextIsolation: true }
  129. },
  130. {
  131. name: 'webview',
  132. webPreferences: { webviewTag: true, preload: false }
  133. }
  134. ).forEach(config => {
  135. generateTests(config.title, config.webPreferences);
  136. });
  137. describe('internal <iframe> inside of <webview>', () => {
  138. let w;
  139. beforeEach(async () => {
  140. await closeWindow(w);
  141. w = new BrowserWindow({
  142. show: false,
  143. width: 400,
  144. height: 400,
  145. webPreferences: {
  146. preload: path.resolve(__dirname, 'fixtures/sub-frames/webview-iframe-preload.js'),
  147. nodeIntegrationInSubFrames: true,
  148. webviewTag: true
  149. }
  150. });
  151. });
  152. afterEach(() => {
  153. return closeWindow(w).then(() => {
  154. w = null;
  155. });
  156. });
  157. it('should not load preload scripts', async () => {
  158. const promisePass = emittedOnce(ipcMain, 'webview-loaded');
  159. const promiseFail = emittedOnce(ipcMain, 'preload-in-frame').then(() => {
  160. throw new Error('preload loaded in internal frame');
  161. });
  162. await w.loadURL('about:blank');
  163. return Promise.race([promisePass, promiseFail]);
  164. });
  165. });
  166. });
  167. describe('cross-site frame sandboxing', () => {
  168. let server = null;
  169. beforeEach(function () {
  170. if (process.platform === 'linux') {
  171. this.skip();
  172. }
  173. });
  174. before(function (done) {
  175. server = http.createServer((req, res) => {
  176. res.end(`<iframe name="frame" src="${server.cross_site_url}" />`);
  177. });
  178. server.listen(0, '127.0.0.1', () => {
  179. server.url = `http://127.0.0.1:${server.address().port}/`;
  180. server.cross_site_url = `http://localhost:${server.address().port}/`;
  181. done();
  182. });
  183. });
  184. after(() => {
  185. server.close();
  186. server = null;
  187. });
  188. let w;
  189. afterEach(() => {
  190. return closeWindow(w).then(() => {
  191. w = null;
  192. });
  193. });
  194. const generateSpecs = (description, webPreferences) => {
  195. describe(description, () => {
  196. it('iframe process is sandboxed if possible', async () => {
  197. w = new BrowserWindow({
  198. show: false,
  199. webPreferences
  200. });
  201. await w.loadURL(server.url);
  202. const pidMain = w.webContents.getOSProcessId();
  203. const pidFrame = w.webContents._getOSProcessIdForFrame('frame', server.cross_site_url);
  204. const metrics = app.getAppMetrics();
  205. const isProcessSandboxed = function (pid) {
  206. const entry = metrics.filter(metric => metric.pid === pid)[0];
  207. return entry && entry.sandboxed;
  208. };
  209. const sandboxMain = !!(webPreferences.sandbox || process.mas);
  210. const sandboxFrame = sandboxMain || !webPreferences.nodeIntegrationInSubFrames;
  211. expect(isProcessSandboxed(pidMain)).to.equal(sandboxMain);
  212. expect(isProcessSandboxed(pidFrame)).to.equal(sandboxFrame);
  213. });
  214. });
  215. };
  216. generateSpecs('nodeIntegrationInSubFrames = false, sandbox = false', {
  217. nodeIntegrationInSubFrames: false,
  218. sandbox: false
  219. });
  220. generateSpecs('nodeIntegrationInSubFrames = false, sandbox = true', {
  221. nodeIntegrationInSubFrames: false,
  222. sandbox: true
  223. });
  224. generateSpecs('nodeIntegrationInSubFrames = true, sandbox = false', {
  225. nodeIntegrationInSubFrames: true,
  226. sandbox: false
  227. });
  228. generateSpecs('nodeIntegrationInSubFrames = true, sandbox = true', {
  229. nodeIntegrationInSubFrames: true,
  230. sandbox: true
  231. });
  232. });