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

Commit 35efd0e

Browse files
committed
Add basic permissions dialog
Adds a blocking permissions dialog for the 6 permission types currently supported in Electron. Auditors: @bbondy
1 parent 6ae3e36 commit 35efd0e

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

app/filtering.js

+66-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const getBaseDomain = require('../js/lib/baseDomain').getBaseDomain
1515
const getSetting = require('../js/settings').getSetting
1616
const settings = require('../js/constants/settings')
1717
const ipcMain = electron.ipcMain
18+
const dialog = electron.dialog
1819

1920
const beforeSendHeadersFilteringFns = []
2021
const beforeRequestFilteringFns = []
@@ -112,6 +113,70 @@ function registerForBeforeSendHeaders (session) {
112113
})
113114
}
114115

116+
/**
117+
* Register permission request handler
118+
* @param {Object} session to add permission request handler on
119+
*/
120+
function registerPermissionHandler (session) {
121+
// Keep track of per-site permissions granted for this session.
122+
// TODO: Localize strings
123+
let permissions = {
124+
media: {
125+
action: 'use your camera and/or microphone',
126+
hosts: {}
127+
},
128+
geolocation: {
129+
action: 'see your location',
130+
hosts: {}
131+
},
132+
notifications: {
133+
action: 'show you notifications',
134+
hosts: {}
135+
},
136+
midiSysex: {
137+
action: 'use web MIDI',
138+
hosts: {}
139+
},
140+
pointerLock: {
141+
action: 'disable your mouse cursor',
142+
hosts: {}
143+
},
144+
fullscreen: {
145+
action: 'be fullscreen',
146+
hosts: {}
147+
}
148+
}
149+
session.setPermissionRequestHandler((webContents, permission, cb) => {
150+
let host = urlParse(webContents.getURL()).host
151+
let isAllowed = permissions[permission].hosts[host]
152+
if (isAllowed !== undefined) {
153+
cb(isAllowed)
154+
} else {
155+
// TODO: Add option to remember decision between restarts.
156+
let result = dialog.showMessageBox({
157+
type: 'question',
158+
message: host + ' is requesting permission to ' + permissions[permission].action,
159+
buttons: ['Deny', 'Allow'],
160+
defaultId: 0,
161+
cancelId: 0
162+
})
163+
let isTemp = dialog.showMessageBox({
164+
type: 'question',
165+
title: 'Remember this decision?',
166+
message: 'Would you like to remember this decision on ' + host + ' until Brave closes?',
167+
buttons: ['Yes', 'No'],
168+
defaultId: 0,
169+
cancelId: 0
170+
})
171+
result = !!(result)
172+
cb(result)
173+
if (!isTemp) {
174+
permissions[permission].hosts[host] = result
175+
}
176+
}
177+
})
178+
}
179+
115180
module.exports.isThirdPartyHost = (baseContextHost, testHost) => {
116181
// TODO: Always return true if these are IP addresses that aren't the same
117182
if (!testHost || !baseContextHost) {
@@ -126,7 +191,7 @@ module.exports.isThirdPartyHost = (baseContextHost, testHost) => {
126191
}
127192

128193
function initForPartition (partition) {
129-
[registerForBeforeRequest, registerForBeforeSendHeaders].forEach(fn => {
194+
[registerPermissionHandler, registerForBeforeRequest, registerForBeforeSendHeaders].forEach(fn => {
130195
fn(session.fromPartition(partition))
131196
})
132197
}

0 commit comments

Comments
 (0)