api-subframe-spec.ts 11 KB

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