api-subframe-spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. const configs = 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. );
  162. for (const config of configs) {
  163. generateTests(config.title, config.webPreferences);
  164. }
  165. describe('internal <iframe> inside of <webview>', () => {
  166. let w: BrowserWindow;
  167. beforeEach(async () => {
  168. await closeWindow(w);
  169. w = new BrowserWindow({
  170. show: false,
  171. width: 400,
  172. height: 400,
  173. webPreferences: {
  174. preload: path.resolve(__dirname, 'fixtures/sub-frames/webview-iframe-preload.js'),
  175. nodeIntegrationInSubFrames: true,
  176. webviewTag: true,
  177. contextIsolation: false
  178. }
  179. });
  180. });
  181. afterEach(async () => {
  182. await closeWindow(w);
  183. w = null as unknown as BrowserWindow;
  184. });
  185. it('should not load preload scripts', async () => {
  186. const promisePass = once(ipcMain, 'webview-loaded');
  187. const promiseFail = once(ipcMain, 'preload-in-frame').then(() => {
  188. throw new Error('preload loaded in internal frame');
  189. });
  190. await w.loadURL('about:blank');
  191. return Promise.race([promisePass, promiseFail]);
  192. });
  193. });
  194. });
  195. // app.getAppMetrics() does not return sandbox information on Linux.
  196. ifdescribe(process.platform !== 'linux')('cross-site frame sandboxing', () => {
  197. let server: http.Server;
  198. let crossSiteUrl: string;
  199. let serverUrl: string;
  200. before(async function () {
  201. server = http.createServer((req, res) => {
  202. res.end(`<iframe name="frame" src="${crossSiteUrl}" />`);
  203. });
  204. serverUrl = (await listen(server)).url;
  205. crossSiteUrl = serverUrl.replace('127.0.0.1', 'localhost');
  206. });
  207. after(() => {
  208. server.close();
  209. server = null as unknown as http.Server;
  210. });
  211. let w: BrowserWindow;
  212. afterEach(async () => {
  213. await closeWindow(w);
  214. w = null as unknown as BrowserWindow;
  215. });
  216. const generateSpecs = (description: string, webPreferences: any) => {
  217. describe(description, () => {
  218. it('iframe process is sandboxed if possible', async () => {
  219. w = new BrowserWindow({
  220. show: false,
  221. webPreferences
  222. });
  223. await w.loadURL(serverUrl);
  224. const pidMain = w.webContents.getOSProcessId();
  225. const pidFrame = w.webContents.mainFrame.frames.find(f => f.name === 'frame')!.osProcessId;
  226. const metrics = app.getAppMetrics();
  227. const isProcessSandboxed = function (pid: number) {
  228. const entry = metrics.find(metric => metric.pid === pid);
  229. return entry && entry.sandboxed;
  230. };
  231. const sandboxMain = !!(webPreferences.sandbox || process.mas);
  232. const sandboxFrame = sandboxMain || !webPreferences.nodeIntegrationInSubFrames;
  233. expect(isProcessSandboxed(pidMain)).to.equal(sandboxMain);
  234. expect(isProcessSandboxed(pidFrame)).to.equal(sandboxFrame);
  235. });
  236. });
  237. };
  238. generateSpecs('nodeIntegrationInSubFrames = false, sandbox = false', {
  239. nodeIntegrationInSubFrames: false,
  240. sandbox: false
  241. });
  242. generateSpecs('nodeIntegrationInSubFrames = false, sandbox = true', {
  243. nodeIntegrationInSubFrames: false,
  244. sandbox: true
  245. });
  246. generateSpecs('nodeIntegrationInSubFrames = true, sandbox = false', {
  247. nodeIntegrationInSubFrames: true,
  248. sandbox: false
  249. });
  250. generateSpecs('nodeIntegrationInSubFrames = true, sandbox = true', {
  251. nodeIntegrationInSubFrames: true,
  252. sandbox: true
  253. });
  254. });