api-subframe-spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import { app, BrowserWindow, ipcMain } from 'electron/main';
  2. import { expect } from 'chai';
  3. import { once } from 'node:events';
  4. import * as http from 'node:http';
  5. import * as path from 'node:path';
  6. import { emittedNTimes } from './lib/events-helpers';
  7. import { ifdescribe, listen } from './lib/spec-helpers';
  8. import { closeWindow } from './lib/window-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. 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. describe('subframe with non-standard schemes', () => {
  196. it('should not crash when changing subframe src to about:blank and back', async () => {
  197. const w = new BrowserWindow({ show: false, width: 400, height: 400 });
  198. const fwfPath = path.resolve(__dirname, 'fixtures/sub-frames/frame-with-frame.html');
  199. await w.loadFile(fwfPath);
  200. const originalSrc = await w.webContents.executeJavaScript(`
  201. const iframe = document.querySelector('iframe');
  202. iframe.src;
  203. `);
  204. const updatedSrc = await w.webContents.executeJavaScript(`
  205. new Promise((resolve, reject) => {
  206. const iframe = document.querySelector('iframe');
  207. iframe.src = 'about:blank';
  208. resolve(iframe.src);
  209. })
  210. `);
  211. expect(updatedSrc).to.equal('about:blank');
  212. const restoredSrc = await w.webContents.executeJavaScript(`
  213. new Promise((resolve, reject) => {
  214. const iframe = document.querySelector('iframe');
  215. iframe.src = '${originalSrc}';
  216. resolve(iframe.src);
  217. })
  218. `);
  219. expect(restoredSrc).to.equal(originalSrc);
  220. });
  221. });
  222. // app.getAppMetrics() does not return sandbox information on Linux.
  223. ifdescribe(process.platform !== 'linux')('cross-site frame sandboxing', () => {
  224. let server: http.Server;
  225. let crossSiteUrl: string;
  226. let serverUrl: string;
  227. before(async function () {
  228. server = http.createServer((req, res) => {
  229. res.end(`<iframe name="frame" src="${crossSiteUrl}" />`);
  230. });
  231. serverUrl = (await listen(server)).url;
  232. crossSiteUrl = serverUrl.replace('127.0.0.1', 'localhost');
  233. });
  234. after(() => {
  235. server.close();
  236. server = null as unknown as http.Server;
  237. });
  238. let w: BrowserWindow;
  239. afterEach(async () => {
  240. await closeWindow(w);
  241. w = null as unknown as BrowserWindow;
  242. });
  243. const generateSpecs = (description: string, webPreferences: any) => {
  244. describe(description, () => {
  245. it('iframe process is sandboxed if possible', async () => {
  246. w = new BrowserWindow({
  247. show: false,
  248. webPreferences
  249. });
  250. await w.loadURL(serverUrl);
  251. const pidMain = w.webContents.getOSProcessId();
  252. const pidFrame = w.webContents.mainFrame.frames.find(f => f.name === 'frame')!.osProcessId;
  253. const metrics = app.getAppMetrics();
  254. const isProcessSandboxed = function (pid: number) {
  255. const entry = metrics.find(metric => metric.pid === pid);
  256. return entry && entry.sandboxed;
  257. };
  258. const sandboxMain = !!(webPreferences.sandbox || process.mas);
  259. const sandboxFrame = sandboxMain || !webPreferences.nodeIntegrationInSubFrames;
  260. expect(isProcessSandboxed(pidMain)).to.equal(sandboxMain);
  261. expect(isProcessSandboxed(pidFrame)).to.equal(sandboxFrame);
  262. });
  263. });
  264. };
  265. generateSpecs('nodeIntegrationInSubFrames = false, sandbox = false', {
  266. nodeIntegrationInSubFrames: false,
  267. sandbox: false
  268. });
  269. generateSpecs('nodeIntegrationInSubFrames = false, sandbox = true', {
  270. nodeIntegrationInSubFrames: false,
  271. sandbox: true
  272. });
  273. generateSpecs('nodeIntegrationInSubFrames = true, sandbox = false', {
  274. nodeIntegrationInSubFrames: true,
  275. sandbox: false
  276. });
  277. generateSpecs('nodeIntegrationInSubFrames = true, sandbox = true', {
  278. nodeIntegrationInSubFrames: true,
  279. sandbox: true
  280. });
  281. });