Skip to content

Commit eff85a5

Browse files
authored
Validate sender in all IPC event handlers (#8248)
1 parent d584ad3 commit eff85a5

File tree

1 file changed

+126
-37
lines changed

1 file changed

+126
-37
lines changed

src/main/index.js

Lines changed: 126 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,19 @@ function runApp() {
351351
replaceMainWindow: false,
352352
showWindowNow: true,
353353
})
354-
ipcMain.once(IpcChannels.APP_READY, () => {
355-
newWindow.webContents.send(IpcChannels.OPEN_URL, newStartupUrl)
356-
})
354+
355+
/**
356+
* @param {import('electron').IpcMainEvent} event
357+
*/
358+
const readyHandler = (event) => {
359+
if (isFreeTubeUrl(event.senderFrame.url)) {
360+
newWindow.webContents.ipc.off(IpcChannels.APP_READY, readyHandler)
361+
362+
event.reply(IpcChannels.OPEN_URL, newStartupUrl)
363+
}
364+
}
365+
366+
newWindow.webContents.ipc.on(IpcChannels.APP_READY, readyHandler)
357367
}
358368
})
359369
}
@@ -1100,9 +1110,18 @@ function runApp() {
11001110
}
11011111

11021112
if (typeof searchQueryText === 'string' && searchQueryText.length > 0) {
1103-
ipcMain.once(IpcChannels.SEARCH_INPUT_HANDLING_READY, () => {
1104-
newWindow.webContents.send(IpcChannels.UPDATE_SEARCH_INPUT_TEXT, searchQueryText)
1105-
})
1113+
/**
1114+
* @param {import('electron').IpcMainEvent} event
1115+
*/
1116+
const searchInputReadyHandler = (event) => {
1117+
if (isFreeTubeUrl(event.senderFrame.url)) {
1118+
newWindow.webContents.ipc.off(IpcChannels.SEARCH_INPUT_HANDLING_READY, searchInputReadyHandler)
1119+
1120+
event.reply(IpcChannels.UPDATE_SEARCH_INPUT_TEXT, searchQueryText)
1121+
}
1122+
}
1123+
1124+
newWindow.webContents.ipc.on(IpcChannels.SEARCH_INPUT_HANDLING_READY, searchInputReadyHandler)
11061125
}
11071126

11081127
// Show when loaded
@@ -1155,11 +1174,13 @@ function runApp() {
11551174
return newWindow
11561175
}
11571176

1158-
ipcMain.on(IpcChannels.APP_READY, () => {
1159-
if (startupUrl) {
1160-
mainWindow.webContents.send(IpcChannels.OPEN_URL, startupUrl)
1177+
ipcMain.on(IpcChannels.APP_READY, (event) => {
1178+
if (isFreeTubeUrl(event.senderFrame.url)) {
1179+
if (startupUrl) {
1180+
mainWindow.webContents.send(IpcChannels.OPEN_URL, startupUrl)
1181+
}
1182+
startupUrl = null
11611183
}
1162-
startupUrl = null
11631184
})
11641185

11651186
function relaunch() {
@@ -1203,23 +1224,35 @@ function runApp() {
12031224
const allWindows = BrowserWindow.getAllWindows()
12041225

12051226
allWindows.forEach((window) => {
1206-
window.webContents.send(IpcChannels.NATIVE_THEME_UPDATE, nativeTheme.shouldUseDarkColors)
1227+
if (isFreeTubeUrl(window.webContents.getURL())) {
1228+
window.webContents.send(IpcChannels.NATIVE_THEME_UPDATE, nativeTheme.shouldUseDarkColors)
1229+
}
12071230
})
12081231
})
12091232

1210-
ipcMain.handle(IpcChannels.GENERATE_PO_TOKEN, (_, videoId, context) => {
1211-
return generatePoToken(videoId, context, proxyUrl)
1233+
ipcMain.handle(IpcChannels.GENERATE_PO_TOKEN, (event, videoId, context) => {
1234+
if (isFreeTubeUrl(event.senderFrame.url)) {
1235+
return generatePoToken(videoId, context, proxyUrl)
1236+
}
12121237
})
12131238

1214-
ipcMain.on(IpcChannels.ENABLE_PROXY, (_, url) => {
1239+
ipcMain.on(IpcChannels.ENABLE_PROXY, (event, url) => {
1240+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1241+
return
1242+
}
1243+
12151244
session.defaultSession.setProxy({
12161245
proxyRules: url
12171246
})
12181247
proxyUrl = url
12191248
session.defaultSession.closeAllConnections()
12201249
})
12211250

1222-
ipcMain.on(IpcChannels.DISABLE_PROXY, () => {
1251+
ipcMain.on(IpcChannels.DISABLE_PROXY, (event) => {
1252+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1253+
return
1254+
}
1255+
12231256
session.defaultSession.setProxy({})
12241257
proxyUrl = undefined
12251258
session.defaultSession.closeAllConnections()
@@ -1231,7 +1264,11 @@ function runApp() {
12311264
// Math.trunc but with a bitwise OR so that it can be calcuated at build time and the number inlined
12321265
const HALF_OF_NAV_HISTORY_DISPLAY_LIMIT = (NAV_HISTORY_DISPLAY_LIMIT / 2) | 0
12331266

1234-
ipcMain.handle(IpcChannels.GET_NAVIGATION_HISTORY, ({ sender }) => {
1267+
ipcMain.handle(IpcChannels.GET_NAVIGATION_HISTORY, ({ senderFrame, sender }) => {
1268+
if (!isFreeTubeUrl(senderFrame.url)) {
1269+
return
1270+
}
1271+
12351272
const activeIndex = sender.navigationHistory.getActiveIndex()
12361273
const length = sender.navigationHistory.length()
12371274

@@ -1262,17 +1299,17 @@ function runApp() {
12621299

12631300
// #endregion navigation history
12641301

1265-
ipcMain.handle(IpcChannels.GET_SYSTEM_LOCALE, () => {
1266-
// we should switch to getPreferredSystemLanguages at some point and iterate through until we find a supported locale
1267-
return app.getSystemLocale()
1302+
ipcMain.handle(IpcChannels.GET_SYSTEM_LOCALE, (event) => {
1303+
if (isFreeTubeUrl(event.senderFrame.url)) {
1304+
// we should switch to getPreferredSystemLanguages at some point and iterate through until we find a supported locale
1305+
return app.getSystemLocale()
1306+
}
12681307
})
12691308

12701309
ipcMain.handle(IpcChannels.GET_SCREENSHOT_FALLBACK_FOLDER, (event) => {
1271-
if (!isFreeTubeUrl(event.senderFrame.url)) {
1272-
return
1310+
if (isFreeTubeUrl(event.senderFrame.url)) {
1311+
return path.join(app.getPath('pictures'), 'Freetube')
12731312
}
1274-
1275-
return path.join(app.getPath('pictures'), 'Freetube')
12761313
})
12771314

12781315
ipcMain.on(IpcChannels.CHOOSE_DEFAULT_FOLDER, async (event, kind) => {
@@ -1317,7 +1354,9 @@ function runApp() {
13171354
}
13181355

13191356
BrowserWindow.getAllWindows().forEach((window) => {
1320-
window.webContents.send(IpcChannels.SYNC_SETTINGS, syncPayload)
1357+
if (isFreeTubeUrl(window.webContents.getURL())) {
1358+
window.webContents.send(IpcChannels.SYNC_SETTINGS, syncPayload)
1359+
}
13211360
})
13221361
})
13231362

@@ -1434,16 +1473,24 @@ function runApp() {
14341473
})
14351474
})
14361475

1437-
ipcMain.on(IpcChannels.OPEN_IN_EXTERNAL_PLAYER, (_, executable, args) => {
1438-
const child = cp.spawn(executable, args, { detached: true, stdio: 'ignore' })
1439-
child.unref()
1476+
ipcMain.on(IpcChannels.OPEN_IN_EXTERNAL_PLAYER, (event, executable, args) => {
1477+
if (isFreeTubeUrl(event.senderFrame.url)) {
1478+
const child = cp.spawn(executable, args, { detached: true, stdio: 'ignore' })
1479+
child.unref()
1480+
}
14401481
})
14411482

1442-
ipcMain.handle(IpcChannels.GET_REPLACE_HTTP_CACHE, () => {
1443-
return replaceHttpCache
1483+
ipcMain.handle(IpcChannels.GET_REPLACE_HTTP_CACHE, (event) => {
1484+
if (isFreeTubeUrl(event.senderFrame.url)) {
1485+
return replaceHttpCache
1486+
}
14441487
})
14451488

1446-
ipcMain.once(IpcChannels.TOGGLE_REPLACE_HTTP_CACHE, async () => {
1489+
ipcMain.once(IpcChannels.TOGGLE_REPLACE_HTTP_CACHE, async (event) => {
1490+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1491+
return
1492+
}
1493+
14471494
if (replaceHttpCache) {
14481495
await asyncFs.rm(REPLACE_HTTP_CACHE_PATH)
14491496
} else {
@@ -1464,7 +1511,11 @@ function runApp() {
14641511
return path.join(PLAYER_CACHE_PATH, sanitizedKey)
14651512
}
14661513

1467-
ipcMain.handle(IpcChannels.PLAYER_CACHE_GET, async (_, key) => {
1514+
ipcMain.handle(IpcChannels.PLAYER_CACHE_GET, async (event, key) => {
1515+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1516+
return
1517+
}
1518+
14681519
const filePath = playerCachePathForKey(key)
14691520

14701521
try {
@@ -1482,7 +1533,11 @@ function runApp() {
14821533
}
14831534
})
14841535

1485-
ipcMain.handle(IpcChannels.PLAYER_CACHE_SET, async (_, key, value) => {
1536+
ipcMain.handle(IpcChannels.PLAYER_CACHE_SET, async (event, key, value) => {
1537+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1538+
return
1539+
}
1540+
14861541
const filePath = playerCachePathForKey(key)
14871542

14881543
await asyncFs.mkdir(PLAYER_CACHE_PATH, { recursive: true })
@@ -1511,6 +1566,10 @@ function runApp() {
15111566

15121567
// Settings
15131568
ipcMain.handle(IpcChannels.DB_SETTINGS, async (event, { action, data }) => {
1569+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1570+
return
1571+
}
1572+
15141573
try {
15151574
switch (action) {
15161575
case DBActions.GENERAL.FIND:
@@ -1569,6 +1628,10 @@ function runApp() {
15691628
// *********** //
15701629
// History
15711630
ipcMain.handle(IpcChannels.DB_HISTORY, async (event, { action, data }) => {
1631+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1632+
return
1633+
}
1634+
15721635
try {
15731636
switch (action) {
15741637
case DBActions.GENERAL.FIND:
@@ -1641,6 +1704,10 @@ function runApp() {
16411704
// *********** //
16421705
// Profiles
16431706
ipcMain.handle(IpcChannels.DB_PROFILES, async (event, { action, data }) => {
1707+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1708+
return
1709+
}
1710+
16441711
try {
16451712
switch (action) {
16461713
case DBActions.GENERAL.CREATE: {
@@ -1709,6 +1776,10 @@ function runApp() {
17091776
// The remaining should have it implemented only when playlists
17101777
// get fully implemented into the app
17111778
ipcMain.handle(IpcChannels.DB_PLAYLISTS, async (event, { action, data }) => {
1779+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1780+
return
1781+
}
1782+
17121783
try {
17131784
switch (action) {
17141785
case DBActions.GENERAL.CREATE:
@@ -1810,6 +1881,10 @@ function runApp() {
18101881
// ************** //
18111882
// Search History
18121883
ipcMain.handle(IpcChannels.DB_SEARCH_HISTORY, async (event, { action, data }) => {
1884+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1885+
return
1886+
}
1887+
18131888
try {
18141889
switch (action) {
18151890
case DBActions.GENERAL.FIND:
@@ -1855,6 +1930,10 @@ function runApp() {
18551930
// *********** //
18561931
// Profiles
18571932
ipcMain.handle(IpcChannels.DB_SUBSCRIPTION_CACHE, async (event, { action, data }) => {
1933+
if (!isFreeTubeUrl(event.senderFrame.url)) {
1934+
return
1935+
}
1936+
18581937
try {
18591938
switch (action) {
18601939
case DBActions.GENERAL.FIND:
@@ -1937,7 +2016,7 @@ function runApp() {
19372016

19382017
function syncOtherWindows(channel, event, payload) {
19392018
const otherWindows = BrowserWindow.getAllWindows().filter((window) => {
1940-
return window.webContents.id !== event.sender.id
2019+
return window.webContents.id !== event.sender.id && isFreeTubeUrl(window.webContents.getURL())
19412020
})
19422021

19432022
for (const window of otherWindows) {
@@ -2047,9 +2126,19 @@ function runApp() {
20472126
replaceMainWindow: false,
20482127
showWindowNow: true,
20492128
})
2050-
ipcMain.once(IpcChannels.APP_READY, () => {
2051-
newWindow.webContents.send(IpcChannels.OPEN_URL, newStartupUrl)
2052-
})
2129+
2130+
/**
2131+
* @param {import('electron').IpcMainEvent} event
2132+
*/
2133+
const readyHandler = (event) => {
2134+
if (isFreeTubeUrl(event.senderFrame.url)) {
2135+
newWindow.webContents.ipc.off(IpcChannels.APP_READY, readyHandler)
2136+
2137+
event.reply(IpcChannels.OPEN_URL, newStartupUrl)
2138+
}
2139+
}
2140+
2141+
newWindow.webContents.ipc.on(IpcChannels.APP_READY, readyHandler)
20532142
})
20542143

20552144
app.on('web-contents-created', (_, webContents) => {
@@ -2108,7 +2197,7 @@ function runApp() {
21082197
*/
21092198

21102199
function navigateTo(path, browserWindow) {
2111-
if (browserWindow == null) {
2200+
if (browserWindow == null || !isFreeTubeUrl(browserWindow.webContents.getURL())) {
21122201
return
21132202
}
21142203

0 commit comments

Comments
 (0)