Skip to content

Commit

Permalink
removed unsafe usage of shell.openExternal
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Sep 29, 2023
1 parent 654608e commit 47aebc6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,9 @@ export class ForkRepoComponent {
externalLink(e: Event, url: string) {
e.preventDefault();

// If electron app
if (
(window as any).process &&
(window as any).process.versions['electron']
) {
const electron = (window as any).require('electron');
electron.shell.openExternal(url);
} else {
const win = window.open(url, '_blank');
if (win) {
win.focus();
}
const win = window.open(url, '_blank');
if (win) {
win.focus();
}
}
}
15 changes: 3 additions & 12 deletions packages/altair-app/src/app/modules/altair/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,9 @@ export const handleExternalLinks = () => {
}
e.preventDefault();

// If electron app
if (
(window as any).process &&
(window as any).process.versions['electron']
) {
const electron = (window as any).require('electron');
electron.shell.openExternal(url);
} else {
const win = window.open(url, '_blank');
if (win) {
win.focus();
}
const win = window.open(url, '_blank');
if (win) {
win.focus();
}
});
};
22 changes: 3 additions & 19 deletions packages/altair-app/src/app/modules/altair/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,25 +297,9 @@ export function truncateText(text: string, maxLength = 70) {
export const externalLink = (e: Event, url: string) => {
e.preventDefault();

// If electron app
if (
(window as any).process &&
(window as any).process.versions.electron &&
isElectron
) {
const electron = (window as any).require('electron');
if (electron) {
electron.shell.openExternal(url);
} else {
debug.log(
'cannot open link. Somehow electron is undefined. Please report this issue.'
);
}
} else {
const win = window.open(url, '_blank');
if (win) {
win.focus();
}
const win = window.open(url, '_blank');
if (win) {
win.focus();
}
};

Expand Down
15 changes: 13 additions & 2 deletions packages/altair-electron/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,19 @@ export class ElectronApp {

app.on('web-contents-created', (event, contents) => {
contents.setWindowOpenHandler(details => {
// Ask the operating system to open this event's url in the default browser.
shell.openExternal(details.url);
try {
log('Opening url', details.url);
// Ask the operating system to open this event's url in the default browser.
const url = new URL(details.url);
const supportedProtocols = ['http:', 'https:', 'mailto:'];
if (!supportedProtocols.includes(url.protocol)) {
log('Unsupported protocol', url.protocol);
return { action: 'deny' };
}
shell.openExternal(url.href);
} catch (err) {
log('Error opening url', err);
}
return { action: 'deny' };
});
});
Expand Down

0 comments on commit 47aebc6

Please sign in to comment.