Skip to content

Commit 660d3b8

Browse files
authored
fix(aot) validate frame name
1 parent a87361c commit 660d3b8

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

alwaysontop/main/index.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
const crypto = require('crypto');
12
const electron = require('electron');
23
const os = require('os');
3-
const { ipcMain } = electron;
4+
const { BrowserWindow, ipcMain } = electron;
45

56
const { windowsEnableScreenProtection } = require('../../helpers/functions');
67
const { EVENTS, STATES, AOT_WINDOW_NAME, EVENTS_CHANNEL } = require('../constants');
@@ -13,11 +14,15 @@ const {
1314
savePosition,
1415
setAspectRatioToResizeableWindow,
1516
setLogger,
16-
windowExists,
17-
getAotWindow
17+
windowExists
1818
} = require('./utils');
1919
const aotConfig = require('./config');
2020

21+
/**
22+
* Token for matching window open requests.
23+
*/
24+
let aotMagic;
25+
2126
/**
2227
* The main window instance
2328
*/
@@ -34,14 +39,23 @@ let isIntersecting;
3439
*/
3540
let _existingWindowOpenHandler;
3641

42+
/**
43+
* The aot window instance
44+
*/
45+
const getAotWindow = () => BrowserWindow.getAllWindows().find(win => {
46+
if (!win || win.isDestroyed() || win.webContents.isCrashed()) return false;
47+
const frameName = win.webContents.mainFrame.name || '';
48+
return frameName === `${AOT_WINDOW_NAME}-${aotMagic}`;
49+
});
50+
3751
/**
3852
* Sends an update state event to renderer process
3953
* @param {string} value the updated aot window state
4054
*/
41-
const sendStateUpdate = state => {
55+
const sendStateUpdate = (state, data = {}) => {
4256
logInfo(`sending ${state} state update to renderer process`);
4357

44-
mainWindow.webContents.send(EVENTS_CHANNEL, { name: EVENTS.UPDATE_STATE, state });
58+
mainWindow.webContents.send(EVENTS_CHANNEL, { name: EVENTS.UPDATE_STATE, state, data });
4559
};
4660

4761
/**
@@ -87,9 +101,17 @@ const handleWindowCreated = window => {
87101
const windowOpenHandler = args => {
88102
const { frameName } = args;
89103

90-
if (frameName === AOT_WINDOW_NAME) {
104+
if (frameName.startsWith(AOT_WINDOW_NAME)) {
91105
logInfo('handling new aot window event');
92106

107+
const magic = frameName.split('-')[1];
108+
109+
if (magic !== aotMagic) {
110+
logInfo('bad AoT window magic');
111+
112+
return { action: 'deny' };
113+
}
114+
93115
return {
94116
action: 'allow',
95117
overrideBrowserWindowOptions: {
@@ -114,16 +136,20 @@ const showAot = () => {
114136
logInfo('show aot handler');
115137

116138
let state;
139+
let data = {};
140+
117141
const aotWindow = getAotWindow();
118142

119143
if (windowExists(aotWindow)) {
120144
state = STATES.SHOW;
121145
aotWindow.showInactive();
122146
} else {
123147
state = STATES.OPEN;
148+
aotMagic = crypto.randomUUID().replaceAll('-', '');
149+
data.aotMagic = aotMagic;
124150
}
125151

126-
sendStateUpdate(state);
152+
sendStateUpdate(state, data);
127153
};
128154

129155
/**

alwaysontop/main/utils.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ const Store = require('electron-store');
22
const electron = require('electron');
33
const os = require('os');
44
const log = require('@jitsi/logger');
5-
const { SIZE, ASPECT_RATIO, STORAGE, AOT_WINDOW_NAME } = require('../constants');
6-
const { BrowserWindow } = electron;
5+
const { SIZE, ASPECT_RATIO, STORAGE } = require('../constants');
76

87
/**
98
* Stores the current size of the AOT during the conference
@@ -27,15 +26,6 @@ let logger;
2726
const store = new Store();
2827

2928

30-
/**
31-
* The aot window instance
32-
*/
33-
const getAotWindow = () => BrowserWindow.getAllWindows().find(win => {
34-
if (!win || win.isDestroyed() || win.webContents.isCrashed()) return false;
35-
const frameName = win.webContents.mainFrame.name || '';
36-
return frameName === AOT_WINDOW_NAME;
37-
});
38-
3929
/**
4030
* Changes the window resize functionality to respect the passed aspect ratio.
4131
*
@@ -244,7 +234,6 @@ const windowExists = browserWindow => {
244234
};
245235

246236
module.exports = {
247-
getAotWindow,
248237
getPosition,
249238
getSize,
250239
logError,

alwaysontop/render/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ class AlwaysOnTop extends EventEmitter {
172172
/**
173173
* Opens a new window
174174
*/
175-
_openNewWindow() {
175+
_openNewWindow(magic) {
176176
logInfo('new window');
177177
this._api.on('largeVideoChanged', this._updateLargeVideoSrc);
178178
this._api.on('prejoinVideoChanged', this._updateLargeVideoSrc);
179179
this._api.on('videoMuteStatusChanged', this._updateLargeVideoSrc);
180180

181-
this._aotWindow = window.open('', AOT_WINDOW_NAME);
181+
this._aotWindow = window.open('', `${AOT_WINDOW_NAME}-${magic}`);
182182
this._aotWindow.alwaysOnTop = {
183183
api: this._api,
184184
dismiss: this._dismiss,
@@ -291,7 +291,7 @@ class AlwaysOnTop extends EventEmitter {
291291
_onAotEvent (event, { name, ...rest }) {
292292
switch (name) {
293293
case EVENTS.UPDATE_STATE:
294-
this._handleStateChange(rest.state);
294+
this._handleStateChange(rest.state, rest.data);
295295
break;
296296
}
297297
}
@@ -300,16 +300,17 @@ class AlwaysOnTop extends EventEmitter {
300300
* Handler for state updates
301301
*
302302
* @param {string} state updated state
303+
* @param {Object} data ancillary data to the event
303304
*/
304-
_handleStateChange (state) {
305+
_handleStateChange (state, data) {
305306
logInfo(`handling ${state} state update from main process`);
306307

307308
switch (state) {
308309
case STATES.HIDE:
309310
this._hideWindow();
310311
break;
311312
case STATES.OPEN:
312-
this._openNewWindow();
313+
this._openNewWindow(data.aotMagic);
313314
break;
314315
case STATES.SHOW:
315316
this._showWindow();

0 commit comments

Comments
 (0)