Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

do not generally allow file:// to be opened in new tab #14973

Merged
merged 1 commit into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/browser/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const createFileSubmenu = () => {
appActions.createTabRequested({
url: fileUrl(path),
windowId: focusedWindow.id
})
}, false, false, false, true)
})
}
})
Expand Down
5 changes: 5 additions & 0 deletions app/browser/reducers/tabsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {getFlashResourceId} = require('../../../js/flash')
const {l10nErrorText} = require('../../common/lib/httpUtil')
const flash = require('../../../js/flash')
const {isSourceAboutUrl, isTargetAboutUrl, isNavigatableAboutPage} = require('../../../js/lib/appUrlUtil')
const {isFileScheme} = require('../../../js/lib/urlutil')
const {shouldDebugTabEvents} = require('../../cmdLine')

const getWebRTCPolicy = (state, tabId) => {
Expand Down Expand Up @@ -249,6 +250,10 @@ const tabsReducer = (state, action, immutableAction) => {
windows.focus(windowId)
}
const url = action.getIn(['createProperties', 'url'])
if (isFileScheme(url) && !action.get('allowFile')) {
// Don't allow 'open in new tab' to open file:// URLs for security
action = action.setIn(['createProperties', 'url'], 'about:blank')
}
setImmediate(() => {
if (action.get('activateIfOpen') ||
((isSourceAboutUrl(url) || isTargetAboutUrl(url)) && isNavigatableAboutPage(url))) {
Expand Down
6 changes: 6 additions & 0 deletions app/browser/reducers/windowsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {makeImmutable, isImmutable} = require('../../common/state/immutableUtil')
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const firstDefinedValue = require('../../../js/lib/functional').firstDefinedValue
const {isFileScheme} = require('../../../js/lib/urlutil')
const settings = require('../../../js/constants/settings')
const getSetting = require('../../../js/settings').getSetting

Expand Down Expand Up @@ -266,6 +267,11 @@ const handleCreateWindowAction = (state, action = Immutable.Map()) => {
if (Array.isArray(frameOpts)) {
frames = frameOpts
} else {
// Don't allow 'open in new window' to open a file:// URL for
// security reasons
if (isFileScheme(frameOpts.location)) {
frameOpts.location = 'about:blank'
}
frames = [ frameOpts ]
}
} else {
Expand Down
5 changes: 4 additions & 1 deletion js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,16 @@ const appActions = {
* @param {Boolean} activateIfOpen if the tab is already open with the same properties,
* switch to it instead of creating a new one
* @param {Boolean} isRestore when true, won't try to activate the new tab, even if the user preference indicates to
* @param {Boolean} focusWindow
* @param {Boolean} allowFile - When true, allows file:// URLs to be opened
*/
createTabRequested: function (createProperties, activateIfOpen = false, isRestore = false, focusWindow = false) {
createTabRequested: function (createProperties, activateIfOpen = false, isRestore = false, focusWindow = false, allowFile = false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style comment (not blocking by any means 😛 I know it was already like this) - when functions start to have this many params, it might be better to accept a map with named params

dispatch({
actionType: appConstants.APP_CREATE_TAB_REQUESTED,
createProperties,
activateIfOpen,
isRestore,
allowFile,
focusWindow
})
},
Expand Down
5 changes: 4 additions & 1 deletion js/lib/urlutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,10 @@ const UrlUtil = {
* @return {boolean}
*/
isFileScheme: function (url) {
return this.getScheme(url) === fileScheme
if (!url) {
return false
}
return urlParse(url).protocol === 'file:'
},

/**
Expand Down
5 changes: 5 additions & 0 deletions test/unit/lib/urlutilTestComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ module.exports = {
}
},
'returns false when input:': {
'is falsey': (test) => {
test.equal(urlUtil().isFileScheme(''), false)
test.equal(urlUtil().isFileScheme(), false)
test.equal(urlUtil().isFileScheme(null), false)
},
'is an absolute file path without scheme': (test) => {
test.equal(urlUtil().isFileScheme('/file/path/to/file'), false)
},
Expand Down