Skip to content

Commit 8b72b62

Browse files
authored
feat: prevent local files from opening. (#777)
1 parent d58b3c3 commit 8b72b62

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

src/components/Repository.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
const { shell } = require('electron');
2-
31
import React, { useCallback, useContext } from 'react';
42
import { ReadIcon } from '@primer/octicons-react';
53
import { CSSTransition, TransitionGroup } from 'react-transition-group';
64

75
import { AppContext } from '../context/App';
86
import { Notification } from '../typesGithub';
97
import { NotificationRow } from './NotificationRow';
8+
import { openExternalLink } from '../utils/comms';
109

1110
interface IProps {
1211
hostname: string;
@@ -23,7 +22,7 @@ export const RepositoryNotifications: React.FC<IProps> = ({
2322

2423
const openBrowser = useCallback(() => {
2524
const url = repoNotifications[0].repository.html_url;
26-
shell.openExternal(url);
25+
openExternalLink(url);
2726
}, [repoNotifications]);
2827

2928
const markRepoAsRead = useCallback(() => {

src/components/Sidebar.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BellIcon } from '@primer/octicons-react';
2-
import { ipcRenderer, shell } from 'electron';
2+
import { ipcRenderer } from 'electron';
33
import React, { useCallback, useContext, useMemo } from 'react';
44
import { useNavigate, useLocation } from 'react-router-dom';
55

@@ -9,6 +9,7 @@ import { IconCog } from '../icons/Cog';
99
import { IconQuit } from '../icons/Quit';
1010
import { IconRefresh } from '../icons/Refresh';
1111
import { Constants } from '../utils/constants';
12+
import { openExternalLink } from '../utils/comms';
1213

1314
export const Sidebar: React.FC = () => {
1415
const navigate = useNavigate();
@@ -18,11 +19,11 @@ export const Sidebar: React.FC = () => {
1819
const { notifications, fetchNotifications } = useContext(AppContext);
1920

2021
const onOpenBrowser = useCallback(() => {
21-
shell.openExternal(`https://github.com/${Constants.REPO_SLUG}`);
22+
openExternalLink(`https://github.com/${Constants.REPO_SLUG}`);
2223
}, []);
2324

2425
const onOpenGitHubNotifications = useCallback(() => {
25-
shell.openExternal(`https://github.com/notifications`);
26+
openExternalLink(`https://github.com/notifications`);
2627
}, []);
2728

2829
const quitApp = useCallback(() => {

src/routes/LoginWithToken.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import React, { useCallback, useContext, useState } from 'react';
22
import { Form, FormRenderProps } from 'react-final-form';
33
import { ArrowLeftIcon } from '@primer/octicons-react';
44
import { useNavigate } from 'react-router-dom';
5-
import { shell } from 'electron';
65

76
import { AppContext } from '../context/App';
87
import { AuthTokenOptions } from '../types';
98
import { Constants } from '../utils/constants';
109
import { FieldInput } from '../components/fields/FieldInput';
10+
import { openExternalLink } from '../utils/comms';
1111

1212
interface IValues {
1313
token?: string;
@@ -46,7 +46,7 @@ export const LoginWithToken: React.FC = () => {
4646
const [isValidToken, setIsValidToken] = useState<boolean>(true);
4747

4848
const openLink = useCallback((url: string) => {
49-
shell.openExternal(url);
49+
openExternalLink(url);
5050
}, []);
5151

5252
const renderForm = (formProps: FormRenderProps) => {

src/utils/comms.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ describe('utils/comms.ts', () => {
4141
expect(shell.openExternal).toHaveBeenCalledWith('http://www.gitify.io/');
4242
});
4343

44+
it('should ignore opening external local links file:///', () => {
45+
openExternalLink('file:///Applications/SomeApp.app');
46+
expect(shell.openExternal).toHaveBeenCalledTimes(0);
47+
});
48+
4449
it('should setAutoLaunch (true)', () => {
4550
setAutoLaunch(true);
4651

src/utils/comms.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { ipcRenderer, shell } from 'electron';
22

33
export function openExternalLink(url: string): void {
4-
shell.openExternal(url);
4+
if (!url.toLowerCase().startsWith('file:///')) {
5+
shell.openExternal(url);
6+
}
57
}
68

79
export function setAutoLaunch(value: boolean): void {

0 commit comments

Comments
 (0)