MM-14446: consider subpath when evaluating if url is internal (#946)

* MM-14446: consider subpath when evaluating if url is internal

When clicking on an URL with `target=_blank`, the webview decides if it should launch an external browser or a new window within the Electron application. Update this logic to consider the application's configured subpath so as to treat links outside the subpath but on the same domain as external.

* fix licensing on new file

* fix .eslintrc.json indentation

* tweak header eslint rules for specific files
This commit is contained in:
Jesse Hallam
2019-03-15 15:20:41 -04:00
committed by William Gathoye
parent 6e2b3d7fab
commit 79e020ba43
5 changed files with 182 additions and 30 deletions

View File

@@ -12,6 +12,7 @@ import PropTypes from 'prop-types';
import {ipcRenderer, remote, shell} from 'electron';
import contextMenu from '../js/contextMenu';
import Utils from '../../utils/util';
import {protocols} from '../../../electron-builder.json';
const scheme = protocols[0].schemes[0];
@@ -31,6 +32,7 @@ export default class MattermostView extends React.Component {
isContextMenuAdded: false,
reloadTimeoutID: null,
isLoaded: false,
basename: '/',
};
this.handleUnreadCountChange = this.handleUnreadCountChange.bind(this);
@@ -94,7 +96,7 @@ export default class MattermostView extends React.Component {
return;
}
if (currentURL.host === destURL.host) {
if (Utils.isInternalURL(destURL, currentURL, this.state.basename)) {
if (destURL.path.match(/^\/api\/v[3-4]\/public\/files\//)) {
ipcRenderer.send('download-url', e.url);
} else {
@@ -137,6 +139,7 @@ export default class MattermostView extends React.Component {
case 'onGuestInitialized':
self.setState({
isLoaded: true,
basename: event.args[0] || '/',
});
break;
case 'onBadgeChange': {

View File

@@ -46,7 +46,7 @@ window.addEventListener('load', () => {
return;
}
watchReactAppUntilInitialized(() => {
ipcRenderer.sendToHost('onGuestInitialized');
ipcRenderer.sendToHost('onGuestInitialized', window.basename);
});
});

View File

@@ -8,4 +8,22 @@ function getDomain(inputURL) {
return `${parsedURL.protocol}//${parsedURL.host}`;
}
export default {getDomain};
// isInternalURL determines if the target url is internal to the application.
// - currentURL is the current url inside the webview
// - basename is the global export from the Mattermost application defining the subpath, if any
function isInternalURL(targetURL, currentURL, basename = '/') {
if (targetURL.host !== currentURL.host) {
return false;
}
if (!(targetURL.pathname || '/').startsWith(basename)) {
return false;
}
return true;
}
export default {
getDomain,
isInternalURL,
};