This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 973
/
Copy pathfiltering.js
1158 lines (1044 loc) · 38.5 KB
/
filtering.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'
const messages = require('../js/constants/messages')
const electron = require('electron')
const session = electron.session
const BrowserWindow = electron.BrowserWindow
const webContents = electron.webContents
const webContentsCache = require('./browser/webContentsCache')
const appActions = require('../js/actions/appActions')
const appConfig = require('../js/constants/appConfig')
const hostContentSettings = require('./browser/contentSettings/hostContentSettings')
const downloadStates = require('../js/constants/downloadStates')
const urlParse = require('./common/urlParse')
const getSetting = require('../js/settings').getSetting
const {getExtensionsPath} = require('../js/lib/appUrlUtil')
const appUrlUtil = require('../js/lib/appUrlUtil')
const faviconUtil = require('../js/lib/faviconUtil')
const siteSettings = require('../js/state/siteSettings')
const settings = require('../js/constants/settings')
const userPrefs = require('../js/state/userPrefs')
const config = require('../js/constants/config')
const locale = require('./locale')
const {isSessionPartition} = require('../js/state/frameStateUtil')
const ipcMain = electron.ipcMain
const app = electron.app
const path = require('path')
const getOrigin = require('../js/lib/urlutil').getOrigin
const {isTorrentFile} = require('./browser/webtorrent')
const {adBlockResourceName} = require('./adBlock')
const {updateElectronDownloadItem} = require('./browser/electronDownloadItem')
const {fullscreenOption} = require('./common/constants/settingsEnums')
const isThirdPartyHost = require('./browser/isThirdPartyHost')
const extensionState = require('./common/state/extensionState')
const ledgerUtil = require('./common/lib/ledgerUtil')
const {cookieExceptions, isRefererException} = require('../js/data/siteHacks')
const {getBraverySettingsCache, updateBraverySettingsCache} = require('./common/cache/braverySettingsCache')
const {shouldDebugTabEvents} = require('./cmdLine')
const {getTorSocksProxy} = require('./channel')
const tor = require('./tor')
let appStore = null
const tabMessageBox = require('./browser/tabMessageBox')
const beforeSendHeadersFilteringFns = []
const beforeRequestFilteringFns = []
const beforeRedirectFilteringFns = []
const headersReceivedFilteringFns = []
let partitionsToInitialize = ['default']
let initializedPartitions = {}
const transparent1pxGif = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
const pdfjsOrigin = `chrome-extension://${config.PDFJSExtensionId}`
/**
* Maps partition name to the session object
*/
const registeredSessions = {}
/**
* Maps permission notification bar messages to their callback
*/
const permissionCallbacks = {}
/**
* A set to keep track of URLs fetching favicons that need special treatment later
*/
const faviconURLs = new Set()
const getBraverySettingsForUrl = (url, appState, isPrivate) => {
const cachedBraverySettings = getBraverySettingsCache(url, isPrivate)
if (cachedBraverySettings) {
return cachedBraverySettings
}
const savedSettings = siteSettings.getSiteSettingsForURL(appState.get('siteSettings'), url)
const tempSettings = siteSettings.getSiteSettingsForURL(appState.get('temporarySiteSettings'), url)
let braverySettings = siteSettings.activeSettings(savedSettings, appState, appConfig)
if (isPrivate && tempSettings) {
braverySettings = siteSettings.activeSettings(tempSettings, appState, appConfig)
}
updateBraverySettingsCache(url, isPrivate, braverySettings)
return braverySettings
}
module.exports.registerBeforeSendHeadersFilteringCB = (filteringFn) => {
beforeSendHeadersFilteringFns.push(filteringFn)
}
module.exports.registerBeforeRequestFilteringCB = (filteringFn) => {
beforeRequestFilteringFns.push(filteringFn)
}
module.exports.registerBeforeRedirectFilteringCB = (filteringFn) => {
beforeRedirectFilteringFns.push(filteringFn)
}
module.exports.registerHeadersReceivedFilteringCB = (filteringFn) => {
headersReceivedFilteringFns.push(filteringFn)
}
// Protocols which are safe to load in tor tabs
const whitelistedTorProtocols = ['http:', 'https:', 'chrome-extension:', 'chrome-devtools:']
if (process.env.NODE_ENV === 'development') {
// Needed for connection to webpack local server
whitelistedTorProtocols.push('ws:')
}
/**
* Register for notifications for webRequest.onBeforeRequest for a particular
* session.
* @param {object} session Session to add webRequest filtering on
*/
function registerForBeforeRequest (session, partition) {
const isPrivate = module.exports.isPrivate(partition)
session.webRequest.onBeforeRequest((details, muonCb) => {
if (details.url &&
(details.url.startsWith('chrome://brave') || details.url.startsWith('brave://')) &&
details.tabId !== -1) {
muonCb({ cancel: true })
return
}
if (partition === appConfig.tor.partition) {
if (!details.url) {
muonCb({ cancel: true })
return
}
// To minimize leakage risk, only allow whitelisted protocols in Tor
// sessions
const protocol = urlParse(details.url).protocol
if (!whitelistedTorProtocols.includes(protocol)) {
onBlockedInTor(details, muonCb)
return
}
} else {
const hostname = urlParse(details.url).hostname
// US-ASCII only in `.onion', so no need for locale-dependent
// case-insensitive comparisons.
if (typeof hostname === 'string' &&
hostname.toLowerCase().endsWith('.onion')) {
onBlockedOutsideTor(details, muonCb)
return
}
}
if (process.env.NODE_ENV === 'development') {
let page = appUrlUtil.getGenDir(details.url)
if (page) {
let redirectURL = 'http://localhost:' + (process.env.BRAVE_PORT || process.env.npm_package_config_port) + '/' + page
muonCb({
redirectURL
})
return
}
}
const url = details.url
// filter out special urls for fetching favicons
if (faviconUtil.isWrappedFaviconUrl(url)) {
const redirectURL = faviconUtil.unwrapFaviconUrl(url)
faviconURLs.add(redirectURL)
muonCb({ redirectURL })
return
}
if (shouldIgnoreUrl(details)) {
muonCb({})
return
}
const firstPartyUrl = module.exports.getMainFrameUrl(details)
// this can happen if the tab is closed and the webContents is no longer available
if (!firstPartyUrl) {
muonCb({})
return
}
if (!isPrivate && module.exports.isResourceEnabled('ledger') && module.exports.isResourceEnabled('ledgerMedia')) {
// Ledger media
const provider = ledgerUtil.getMediaProvider(url, firstPartyUrl, details.referrer)
if (provider) {
appActions.onLedgerMediaData(url, provider, details)
}
}
for (let i = 0; i < beforeRequestFilteringFns.length; i++) {
let results = beforeRequestFilteringFns[i](details, isPrivate)
const isAdBlock = (results.resourceName === appConfig.resourceNames.ADBLOCK) ||
(appConfig[results.resourceName] && appConfig[results.resourceName].resourceType === adBlockResourceName)
const isHttpsEverywhere = results.resourceName === appConfig.resourceNames.HTTPS_EVERYWHERE
const isTracker = results.resourceName === appConfig.resourceNames.TRACKING_PROTECTION
if (!module.exports.isResourceEnabled(results.resourceName, firstPartyUrl, isPrivate)) {
continue
}
if (results.cancel) {
let message = details.resourceType === 'mainFrame'
? messages.BLOCKED_PAGE
: messages.BLOCKED_RESOURCE
// Counts the number of ads and trackers
let parentResourceName = results.resourceName
// Adblock can have many different resource names for each alternate
// data file. But we always want the per level reporting to report
// it into the window adblock stats.
if (isAdBlock) {
parentResourceName = appConfig.resourceNames.ADBLOCK
}
if (isAdBlock || isTracker) {
appActions.addResourceCount(parentResourceName, 1)
}
// TODO(bridiver) - convert to appActions/appState
setImmediate(() => {
const tab = webContents.fromTabID(details.tabId)
if (tab && tab.hostWebContents) {
tab.hostWebContents.send(message, parentResourceName, {
tabId: details.tabId,
url: details.url
})
}
})
if (parentResourceName === appConfig.resourceNames.SAFE_BROWSING) {
let redirectURL = appUrlUtil.getTargetAboutUrl('about:safebrowsing#' + details.url)
muonCb({ redirectURL })
// Workaround #8905
appActions.loadURLRequested(details.tabId, redirectURL)
} else if (details.resourceType === 'image') {
muonCb({ redirectURL: transparent1pxGif })
} else {
muonCb({ cancel: true })
}
return
} else if (results.resourceName === 'siteHacks' && results.cancel === false) {
muonCb({})
return
}
if (results.redirectURL) {
// Show the ruleset that was applied and the URLs that were upgraded in
// siteinfo
if (results.ruleset) {
// Counts the number of httpsE redirects
if (isHttpsEverywhere) {
appActions.addResourceCount(results.resourceName, 1)
}
// TODO(bridiver) - convert to appActions/appState
setImmediate(() => {
const tab = webContents.fromTabID(details.tabId)
if (tab && tab.hostWebContents) {
tab.hostWebContents.send(messages.HTTPSE_RULE_APPLIED, results.ruleset, {
tabId: details.tabId,
url: details.url
})
}
})
}
muonCb({redirectURL: results.redirectURL})
return
}
}
// Redirect to non-script version of DDG when it's blocked
if (details.resourceType === 'mainFrame' &&
url.startsWith('https://duckduckgo.com/?q') &&
module.exports.isResourceEnabled('noScript', url, isPrivate)) {
muonCb({redirectURL: url.replace('?q=', 'html?q=')})
} else {
muonCb({})
}
})
}
/**
* Register for notifications for webRequest.onBeforeRedirect for a particular
* session.
* @param {object} session Session to add webRequest filtering on
*/
function registerForBeforeRedirect (session, partition) {
const isPrivate = module.exports.isPrivate(partition)
// Note that onBeforeRedirect listener doesn't take a callback
session.webRequest.onBeforeRedirect(function (details) {
// Using an electron binary which isn't from Brave
if (shouldIgnoreUrl(details)) {
return
}
for (let i = 0; i < beforeRedirectFilteringFns.length; i++) {
// Note that since this isn't supposed to have a return value, the
// redirect filtering function must check whether the resource is
// enabled and do nothing if it's not.
beforeRedirectFilteringFns[i](details, isPrivate)
}
})
}
module.exports.applyCookieSetting = (requestHeaders, url, firstPartyUrl, isPrivate) => {
const cookieSetting = module.exports.isResourceEnabled(appConfig.resourceNames.COOKIEBLOCK, firstPartyUrl, isPrivate)
if (cookieSetting) {
const targetHostname = urlParse(url || '').hostname
const firstPartyHostname = urlParse(firstPartyUrl).hostname
const targetOrigin = getOrigin(url)
const referer = requestHeaders['Referer']
if (cookieSetting === 'blockAllCookies' ||
isThirdPartyHost(firstPartyHostname, targetHostname)) {
let hasCookieException = false
const firstPartyOrigin = getOrigin(firstPartyUrl)
if (cookieExceptions.hasOwnProperty(firstPartyOrigin)) {
const subResources = cookieExceptions[firstPartyOrigin]
for (let i = 0; i < subResources.length; ++i) {
if (subResources[i] === targetOrigin) {
hasCookieException = true
break
} else if (subResources[i].includes('*')) {
const regSubResource = new RegExp(subResources[i].replace('//', '\\/\\/').replace('*', '.*'), 'g')
if (targetOrigin.match(regSubResource)) {
hasCookieException = true
break
}
}
}
}
// Clear cookie on third-party requests
if (requestHeaders['Cookie'] &&
firstPartyOrigin !== pdfjsOrigin && !hasCookieException) {
requestHeaders['Cookie'] = undefined
}
}
if (referer &&
cookieSetting !== 'allowAllCookies' &&
!isRefererException(targetHostname) &&
isThirdPartyHost(targetHostname, urlParse(referer).hostname)) {
// Spoof third party referer
requestHeaders['Referer'] = targetOrigin
}
}
return requestHeaders
}
/**
* Register for notifications for webRequest.onBeforeSendHeaders for
* a particular session.
* @param {object} The session to add webRequest filtering on
*/
function registerForBeforeSendHeaders (session, partition) {
// For efficiency, avoid calculating these settings on every request. This means the
// browser must be restarted for changes to take effect.
const sendDNT = getSetting(settings.DO_NOT_TRACK)
const isPrivate = module.exports.isPrivate(partition)
session.webRequest.onBeforeSendHeaders(function (details, muonCb) {
// strip cookies from all requests fetching favicons
if (faviconURLs.has(details.url)) {
faviconURLs.delete(details.url)
delete details.requestHeaders['Cookie']
}
// Using an electron binary which isn't from Brave
if (shouldIgnoreUrl(details)) {
muonCb({ requestHeaders: details.requestHeaders })
return
}
let requestHeaders = details.requestHeaders
const firstPartyUrl = module.exports.getMainFrameUrl(details)
// this can happen if the tab is closed and the webContents is no longer available
if (!firstPartyUrl) {
muonCb({})
return
}
for (let i = 0; i < beforeSendHeadersFilteringFns.length; i++) {
let results = beforeSendHeadersFilteringFns[i](details, isPrivate)
if (!module.exports.isResourceEnabled(results.resourceName, firstPartyUrl, isPrivate)) {
continue
}
if (results.cancel) {
muonCb({cancel: true})
return
}
if (results.requestHeaders) {
requestHeaders = results.requestHeaders
}
if (results.customCookie) {
requestHeaders.Cookie = results.customCookie
}
}
requestHeaders = module.exports.applyCookieSetting(requestHeaders, details.url, firstPartyUrl, isPrivate)
if (sendDNT) {
requestHeaders['DNT'] = '1'
}
muonCb({ requestHeaders })
})
}
function onBlocked (reasonKey, details, muonCb) {
const cb = () => muonCb({cancel: true})
if (details.tabId && details.resourceType === 'mainFrame') {
tabMessageBox.show(details.tabId, {
message: `${locale.translation(reasonKey)}`,
title: 'Brave',
buttons: [locale.translation('urlWarningOk')]
}, cb)
} else {
cb()
}
}
function onBlockedInTor (details, muonCb) {
onBlocked('urlBlockedInTor', details, muonCb)
}
function onBlockedOutsideTor (details, muonCb) {
onBlocked('urlBlockedOutsideTor', details, muonCb)
}
/**
* Register for notifications for webRequest.onHeadersReceived for a particular
* session.
* @param {object} session Session to add webRequest filtering on
*/
function registerForHeadersReceived (session, partition) {
const isPrivate = module.exports.isPrivate(partition)
session.webRequest.onHeadersReceived(function (details, muonCb) {
// Using an electron binary which isn't from Brave
if (shouldIgnoreUrl(details)) {
muonCb({})
return
}
if ((isTorrentFile(details)) && partition === appConfig.tor.partition) {
onBlockedInTor(details, muonCb)
return
}
const firstPartyUrl = module.exports.getMainFrameUrl(details)
// this can happen if the tab is closed and the webContents is no longer available
if (!firstPartyUrl) {
muonCb({})
return
}
let parsedTargetUrl = urlParse(details.url || '')
let parsedFirstPartyUrl = urlParse(firstPartyUrl)
const trackableSecurityHeaders = ['Strict-Transport-Security', 'Expect-CT',
'Public-Key-Pins', 'Public-Key-Pins-Report-Only']
if (isThirdPartyHost(parsedFirstPartyUrl.hostname, parsedTargetUrl.hostname)) {
trackableSecurityHeaders.forEach(function (header) {
delete details.responseHeaders[header]
delete details.responseHeaders[header.toLowerCase()]
})
}
for (let i = 0; i < headersReceivedFilteringFns.length; i++) {
let results = headersReceivedFilteringFns[i](details, isPrivate)
if (!module.exports.isResourceEnabled(results.resourceName, firstPartyUrl, isPrivate)) {
continue
}
if (results.cancel) {
muonCb({ cancel: true })
return
}
if (results.responseHeaders) {
muonCb({
responseHeaders: results.responseHeaders,
statusLine: results.statusLine
})
return
}
}
muonCb({
responseHeaders: details.responseHeaders,
statusLine: details.statusLine
})
})
}
/**
* Register permission request handler
* @param {Object} session to add permission request handler on
* @param {string} partition name of the partition
*/
function registerPermissionHandler (session, partition) {
const isPrivate = module.exports.isPrivate(partition)
// Keep track of per-site permissions granted for this session.
let permissions = null
session.setPermissionRequestHandler((mainFrameOrigin, requestingUrl, permissionTypes, muonCb) => {
if (!permissions) {
permissions = {
media: {
action: locale.translation('permissionCameraMicrophone')
},
geolocation: {
action: locale.translation('permissionLocation')
},
notifications: {
action: locale.translation('permissionNotifications')
},
midiSysex: {
action: locale.translation('permissionWebMidi')
},
pointerLock: {
action: locale.translation('permissionDisableCursor')
},
fullscreen: {
action: locale.translation('permissionFullscreen')
},
openExternal: {
action: locale.translation('permissionExternal')
},
protocolRegistration: {
action: locale.translation('permissionProtocolRegistration')
}
}
}
// TODO(bridiver) - the permission handling should be converted to an action because we should never call `appStore.getState()`
// Check whether there is a persistent site setting for this host
const appState = appStore.getState()
const isBraveOrigin = mainFrameOrigin.startsWith(`chrome-extension://${config.braveExtensionId}/`)
const isPDFOrigin = mainFrameOrigin.startsWith(`${pdfjsOrigin}/`)
let response = []
let position
if (!requestingUrl) {
response = new Array(permissionTypes.length)
response.fill(false, 0, permissionTypes.length)
muonCb(response)
return
}
for (let i = 0; i < permissionTypes.length; i++) {
const responseSizeThisIteration = response.length
const permission = permissionTypes[i]
const alwaysAllowFullscreen = module.exports.alwaysAllowFullscreen() === fullscreenOption.ALWAYS_ALLOW
const isFullscreen = permission === 'fullscreen'
const isOpenExternal = permission === 'openExternal'
let requestingOrigin
if (requestingUrl === appUrlUtil.getBraveExtIndexHTML() || isPDFOrigin || isBraveOrigin) {
// lookup, display and store site settings by the origin alias
requestingOrigin = isPDFOrigin ? 'PDF Viewer' : 'Brave Browser'
// display on all tabs
mainFrameOrigin = null
} else {
requestingOrigin = getOrigin(requestingUrl) || requestingUrl
}
if (isOpenExternal) {
// Open external is a special case since we want to apply the permission
// for the entire scheme to avoid cluttering the saved permissions. See
// https://github.com/brave/browser-laptop/issues/13642
const protocol = urlParse(requestingUrl).protocol
requestingOrigin = protocol ? `${protocol} URLs` : requestingUrl
mainFrameOrigin = null
position = 'global'
}
// Look up by host pattern since requestingOrigin is not necessarily
// a parseable URL
const settings = siteSettings.getSiteSettingsForHostPattern(appState.get('siteSettings'), requestingOrigin)
const tempSettings = siteSettings.getSiteSettingsForHostPattern(appState.get('temporarySiteSettings'), requestingOrigin)
if (!permissions[permission]) {
console.warn('WARNING: got unregistered permission request', permission)
response.push(false)
} else if (permission === 'geolocation' && partition === appConfig.tor.partition) {
// Never allow geolocation in Tor mode
response.push(false)
} else if (isFullscreen && mainFrameOrigin &&
// The Torrent Viewer extension is always allowed to show fullscreen media
mainFrameOrigin.startsWith('chrome-extension://' + config.torrentExtensionId)) {
response.push(true)
} else if (isFullscreen && alwaysAllowFullscreen) {
// Always allow fullscreen if setting is ON
response.push(true)
} else {
const permissionName = permission + 'Permission'
let isAllowed
if (settings) {
isAllowed = settings.get(permissionName)
}
// Private tabs inherit settings from normal tabs, but not vice versa.
if (isPrivate && tempSettings) {
isAllowed = tempSettings.get(permissionName)
}
if (typeof isAllowed === 'boolean') {
response.push(isAllowed)
}
}
const message = locale.translation('permissionMessage').replace(/{{\s*host\s*}}/, requestingOrigin).replace(/{{\s*permission\s*}}/, permissions[permission].action)
// If this is a duplicate, clear the previous callback and use the new one
if (permissionCallbacks[message]) {
permissionCallbacks[message](0, false)
}
const responseAutoAdded = responseSizeThisIteration !== response.length
if (!responseAutoAdded) {
appActions.showNotification({
buttons: [
{text: locale.translation('deny')},
{text: locale.translation('allow')}
],
position,
frameOrigin: getOrigin(mainFrameOrigin),
options: {
persist: !!requestingOrigin,
index: i
},
message
})
// Use a closure here for the index instead of passing an index to the
// function because ipcMain.on(messages.NOTIFICATION_RESPONSE above
// calls into the callback without knowing an index.
const index = i
permissionCallbacks[message] = (buttonIndex, persist) => {
// hide the notification if this was triggered automatically
appActions.hideNotification(message)
const result = !!(buttonIndex)
response[index] = result
if (persist) {
// remember site setting for this host
appActions.changeSiteSetting(requestingOrigin, permission + 'Permission', result, isPrivate)
}
if (response.length === permissionTypes.length) {
permissionCallbacks[message] = null
muonCb(response)
}
}
}
}
if (response.length === permissionTypes.length) {
muonCb(response)
}
})
}
function updateDownloadState (win, downloadId, item, state) {
updateElectronDownloadItem(win, downloadId, item, state)
if (!item) {
appActions.mergeDownloadDetail(downloadId, { state: downloadStates.INTERRUPTED })
return
}
const downloadItemStartTime = appStore.getState().getIn(['downloads', downloadId, 'startTime'])
appActions.mergeDownloadDetail(downloadId, {
startTime: downloadItemStartTime || new Date().getTime(),
savePath: item.getSavePath(),
url: item.getURL(),
filename: (item.getSavePath() && path.basename(item.getSavePath())) || item.getFilename(),
totalBytes: item.getTotalBytes(),
receivedBytes: item.getReceivedBytes(),
state
})
}
function registerForDownloadListener (session) {
var repaint = false
session.on('default-download-directory-changed', (e, newPath) => {
if (newPath !== getSetting(settings.DOWNLOAD_DEFAULT_PATH)) {
appActions.changeSetting(settings.DOWNLOAD_DEFAULT_PATH, newPath)
}
})
session.on('will-download', function (event, item, webContents) {
if (webContents.isDestroyed()) {
if (shouldDebugTabEvents) {
console.log(`Tab [DESTROYED] will-download`)
}
event.preventDefault()
return
}
if (shouldDebugTabEvents) {
const tabId = webContents.getId ? webContents.getId() : 'NOT_TAB'
console.log(`Tab [${tabId}] will-download`)
}
const hostWebContents = webContents.hostWebContents || webContents
const win = BrowserWindow.fromWebContents(hostWebContents) || BrowserWindow.getFocusedWindow()
item.setPrompt(getSetting(settings.DOWNLOAD_ALWAYS_ASK) || false)
const downloadId = item.getGuid()
repaint = true
item.on('updated', function (e, st) {
if (!item.getSavePath()) {
return
}
const state = item.isPaused() ? downloadStates.PAUSED : downloadStates.IN_PROGRESS
updateDownloadState(win, downloadId, item, state)
if (win && !win.isDestroyed() && !win.webContents.isDestroyed() && repaint) {
win.webContents.send(messages.SHOW_DOWNLOADS_TOOLBAR)
repaint = false
}
item.on('removed', function () {
updateElectronDownloadItem(downloadId, item, downloadStates.CANCELLED)
appActions.mergeDownloadDetail(downloadId)
})
// Change state if download path is protected
const fs = require('fs')
fs.access(path.dirname(item.getSavePath()), fs.constants.R_OK | fs.constants.W_OK, (err) => {
if (err) {
const state = downloadStates.UNAUTHORIZED
updateDownloadState(win, downloadId, item, state)
}
})
})
item.on('done', function (e, state) {
if (!item.getSavePath()) {
return
}
updateDownloadState(win, downloadId, item, state)
})
})
}
function registerForMagnetHandler (session, partition) {
if (partition === appConfig.tor.partition) {
return
}
const webtorrentUrl = appUrlUtil.getTorrentExtUrl('webtorrent.html')
try {
if (getSetting(settings.TORRENT_VIEWER_ENABLED)) {
// Loading webtorrentUrl from external sources will fail since it is
// not whitelisted in web_accessible_resources. However the protocol
// registration is needed so that onBeforeRequest can handle magnet:
// requests.
session.protocol.registerNavigatorHandler('magnet', `${webtorrentUrl}#%s`)
} else {
session.protocol.unregisterNavigatorHandler('magnet', `${webtorrentUrl}#%s`)
}
} catch (e) {
console.warn('Could not register magnet URL handler. Are you using the latest electron?')
}
}
module.exports.setTorNewIdentity = (url, tabId) => {
const ses = session.fromPartition(appConfig.tor.partition)
if (!ses || !url) {
return
}
ses.setTorNewIdentity(url, () => {
const tab = webContentsCache.getWebContents(tabId)
if (tab && !tab.isDestroyed()) {
tab.reload(true)
}
})
}
module.exports.relaunchTor = () => {
const ses = session.fromPartition(appConfig.tor.partition)
if (!ses) {
console.log('Tor session no longer exists. Cannot restart Tor.')
return
}
appActions.onTorOnline(false)
try {
console.log('tor: relaunch')
ses.relaunchTor()
} catch (e) {
appActions.onTorError(`Could not restart Tor: ${e}`)
}
}
function initSession (ses, partition) {
registeredSessions[partition] = ses
ses.setEnableBrotli(true)
ses.userPrefs.setDefaultZoomLevel(getSetting(settings.DEFAULT_ZOOM_LEVEL) || config.zoom.defaultValue)
}
const initPartition = (partition) => {
const isTorPartition = partition === appConfig.tor.partition
// Partitions can only be initialized once the app is ready
if (!app.isReady()) {
partitionsToInitialize.push(partition)
return
}
if (initializedPartitions[partition]) {
return
}
initializedPartitions[partition] = true
let fns = [initSession,
userPrefs.init,
hostContentSettings.init,
registerForBeforeRequest,
registerForBeforeRedirect,
registerForBeforeSendHeaders,
registerPermissionHandler,
registerForHeadersReceived,
registerForDownloadListener,
registerForMagnetHandler]
let options = {}
if (isSessionPartition(partition)) {
options.parent_partition = ''
}
if (isTorPartition) {
try {
setupTor()
} catch (e) {
appActions.onTorError(`Could not start Tor: ${e}`)
}
// TODO(riastradh): Duplicate logic in app/browser/tabs.js.
options.isolated_storage = true
options.parent_partition = ''
options.tor_proxy = getTorSocksProxy()
if (process.platform === 'win32') {
options.tor_path = path.join(getExtensionsPath('bin'), 'tor.exe')
} else {
options.tor_path = path.join(getExtensionsPath('bin'), 'tor')
}
}
let ses = session.fromPartition(partition, options)
fns.forEach((fn) => {
fn(ses, partition, module.exports.isPrivate(partition))
})
ses.on('register-navigator-handler', (e, protocol, location) => {
appActions.navigatorHandlerRegistered(ses.partition, protocol, location)
})
ses.on('unregister-navigator-handler', (e, protocol, location) => {
appActions.navigatorHandlerUnregistered(ses.partition, protocol, location)
})
ses.protocol.getNavigatorHandlers().forEach((handler) => {
appActions.navigatorHandlerRegistered(ses.partition, handler.protocol, handler.location)
})
}
module.exports.initPartition = initPartition
function setupTor () {
let timer = null
const setTorErrorOnTimeout = (delay, msg) => {
if (timer === null) {
timer = setTimeout(() => {
appActions.onTorError(msg)
console.log(`tor timeout: ${msg}`)
}, delay)
}
}
const initialized = () => {
if (timer !== null) {
clearTimeout(timer)
timer = null
}
}
const onTorError = (msg) => {
initialized()
appActions.onTorError(msg)
console.warn(`tor error: ${msg}`)
}
const onTorOnline = (online) => {
initialized()
appActions.onTorOnline(online)
}
// If Tor has not successfully initialized or thrown an error within 20s,
// assume it's broken.
setTorErrorOnTimeout(20000, 'Tor could not start.')
// Set up the tor daemon watcher. (NOTE: We don't actually start
// the tor daemon here; that happens in C++ code. But we do talk to
// its control socket.)
const torDaemon = new tor.TorDaemon()
torDaemon.setup((err) => {
if (err) {
onTorError(`Tor failed to make directories: ${err}`)
return
}
torDaemon.on('exit', () => onTorError('The Tor process has exited.'))
torDaemon.on('error', (err) => onTorError(`${err}`))
torDaemon.on('launch', (socksAddr) => {
const version = torDaemon.getVersion()
console.log(`tor: daemon listens on ${socksAddr}, version ${version}`)
if (version) {
appActions.setVersionInfo('Tor', version)
}
const bootstrapped = (err, progress) => {
if (err) {
console.warn(`tor: bootstrap ${progress}% error: ${err}`)
onTorError(`Tor bootstrap error: ${err}`)
return
}
appActions.onTorInitPercentage(progress)
}
const networkLiveness = (live) => {
if (live) {
// Network is now live.
onTorOnline(true)
} else if (timer === null) {
// We were online before; now we are not.
onTorOnline(false)
// Wait for tor to reconnect.
setTorErrorOnTimeout(17000, 'Tor could not reconnect.')
}
}
const circuitEstablished = (err, established) => {
if (err && established === null) {
onTorError(`Tor circuit error: ${err}`)
return
}
if (established) {
// Circuit is now established.
onTorOnline(true)
} else if (timer === null) {
// We were online before; now we are not.
onTorOnline(false)
// Wait for tor to reconnect.
setTorErrorOnTimeout(17000, 'Tor could not reconnect.')
}
}
torDaemon.onBootstrap(bootstrapped, (err) => {
if (err) {
onTorError(`Tor error subscribing to bootstrap event: ${err}`)
}
torDaemon.onNetworkLiveness(networkLiveness, (err) => {
if (err) {
onTorError(`Tor error subscribing to network liveness: ${err}`)
}
torDaemon.onCircuitEstablished(circuitEstablished, (err) => {
if (err) {
onTorError(
`Tor error subscribing to circuit establishment: ${err}`)
}
})
})
})
})
torDaemon.start()
})
}
const filterableProtocols = ['http:', 'https:', 'ws:', 'wss:', 'magnet:', 'file:']
function shouldIgnoreUrl (details) {
// data:, is a special origin from SecurityOrigin::urlWithUniqueSecurityOrigin
// and usually occurs when there is an https in an http main frame
if (details.firstPartyUrl === 'data:,' || details.url === 'data:,') {
return false
}
// Ensure host is well-formed (RFC 1035) and has a non-empty hostname
try {
// firstPartyUrl can be empty in some cases so fallback to the url
const firstPartyUrl = urlParse(details.firstPartyUrl || details.url)
if (!filterableProtocols.includes(firstPartyUrl.protocol)) {
return true
}
} catch (e) {
console.warn('Error parsing ' + details.firstPartyUrl)
}
try {
// TODO(bridiver) - handle RFS check and cancel http/https requests with 0 or > 255 length hostames
const parsedUrl = urlParse(details.url)
if (filterableProtocols.includes(parsedUrl.protocol)) {
return false
}
} catch (e) {
console.warn('Error parsing ' + details.url)
}
return true
}
module.exports.isPrivate = (partition) => {
const ses = session.fromPartition(partition)
if (!ses) {
return false
}
return ses.isOffTheRecord()
}
module.exports.init = (state, action, store) => {
appStore = store
partitionsToInitialize.forEach((partition) => {
initPartition(partition)
})
ipcMain.on(messages.NOTIFICATION_RESPONSE, (e, message, buttonIndex, persist) => {
if (permissionCallbacks[message]) {
permissionCallbacks[message](buttonIndex, persist)