Skip to content

Commit

Permalink
Fix opening links in desktop IDE (#6507)
Browse files Browse the repository at this point in the history
* Fix opening links

* Address review

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
somebody1234 and mergify[bot] authored May 5, 2023
1 parent b5578ec commit e1b4019
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
20 changes: 17 additions & 3 deletions app/ide-desktop/lib/client/src/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import * as electron from 'electron'
/** The list of hosts that the app can access. They are required for user authentication to work. */
const TRUSTED_HOSTS = ['accounts.google.com', 'accounts.youtube.com', 'github.com']

/** The list of hosts that the app can open external links to. */
const TRUSTED_EXTERNAL_HOSTS = ['discord.gg']

/** The list of URLs a new WebView can be pointed to. */
const WEBVIEW_URL_WHITELIST: string[] = []

Expand Down Expand Up @@ -79,7 +82,12 @@ function preventNavigation() {
electron.app.on('web-contents-created', (_event, contents) => {
contents.on('will-navigate', (event, navigationUrl) => {
const parsedUrl = new URL(navigationUrl)
if (parsedUrl.origin !== origin && !TRUSTED_HOSTS.includes(parsedUrl.host)) {
const currentWindowUrl = electron.BrowserWindow.getFocusedWindow()?.webContents.getURL()
const parsedCurrentWindowUrl = currentWindowUrl ? new URL(currentWindowUrl) : null
if (
parsedUrl.origin !== parsedCurrentWindowUrl?.origin &&
!TRUSTED_HOSTS.includes(parsedUrl.host)
) {
event.preventDefault()
console.error(`Prevented navigation to '${navigationUrl}'.`)
}
Expand All @@ -95,8 +103,14 @@ function preventNavigation() {
function disableNewWindowsCreation() {
electron.app.on('web-contents-created', (_event, contents) => {
contents.setWindowOpenHandler(({ url }) => {
console.error(`Blocking new window creation request to '${url}'.`)
return { action: 'deny' }
const parsedUrl = new URL(url)
if (TRUSTED_EXTERNAL_HOSTS.includes(parsedUrl.host)) {
void electron.shell.openExternal(url)
return { action: 'deny' }
} else {
console.error(`Blocking new window creation request to '${url}'.`)
return { action: 'deny' }
}
})
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function TopBar(props: TopBarProps) {
<div className="grow" />
<a
href="https://discord.gg/enso"
target="_blank"
className="flex items-center bg-help rounded-full px-2.5 text-white mx-2"
>
<span>help chat</span>
Expand Down

0 comments on commit e1b4019

Please sign in to comment.