forked from Rob--W/cookie-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie-manager-firefox.js
1130 lines (1052 loc) · 44.8 KB
/
cookie-manager-firefox.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
/* globals chrome */
/* globals console */
/* globals isPartOfDomain */
/* globals cookieValidators */
/* jshint browser: true */
/* jshint esversion: 6 */
'use strict';
if (typeof browser !== 'undefined') {
// Firefox bugs...
let {
getAll: cookiesGetAll,
getAllCookieStores: cookiesGetAllCookieStores,
set: cookiesSet,
} = chrome.cookies;
let isPrivate = (details) => {
return details.storeId ?
details.storeId === 'firefox-private' :
chrome.extension.inIncognitoContext;
};
let withLastError = function(callback, error) {
if (callback) {
let chromeRuntime = chrome.runtime;
try {
chrome.runtime = Object.create(chrome.runtime, {
lastError: { value: error },
});
callback();
} finally {
chrome.runtime = chromeRuntime;
}
} else {
// I always set a callback, but throw just in case I don't to not hide errors.
throw error;
}
};
chrome.cookies.getAll = function(details, callback) {
callback = getAllCallbackWithoutImmutableCookies(details, callback);
if (!isPrivate(details) || !details.url && !details.domain) {
cookiesGetAll(details, callback);
return;
}
runWithoutPrivateCookieBugs(function() {
cookiesGetAll(details, callback);
}, function() {
privateCookiesGetAll(details, callback);
});
};
let privateCookiesGetAll = function(details, callback) {
// Work around bugzil.la/1318948.
// and work around bugzil.la/1381197.
var {domain, url} = details;
url = url && new URL(url);
var allDetails = Object.assign({}, details);
delete allDetails.domain;
delete allDetails.url;
cookiesGetAll(allDetails, function(cookies) {
if (!cookies) {
callback(cookies);
return;
}
cookies = cookies.filter(function(cookie) {
if (url) {
if (cookie.hostOnly && url.hostname !== cookie.domain)
return false;
if (!isPartOfDomain(cookie.domain, url.hostname))
return false;
if (cookie.secure && url.protocol !== 'https:')
return false;
if (cookie.path !== '/' && !(url.pathname + '//').startsWith(cookie.path + '/'))
return false;
}
if (domain) {
if (!isPartOfDomain(cookie.domain, domain))
return false;
}
return true;
});
callback(cookies);
});
};
chrome.cookies.getAllCookieStores = function(callback) {
cookiesGetAllCookieStores(function(cookieStores) {
if (cookieStores) {
callback(cookieStores);
return;
}
// In Firefox for Android before version 54, chrome.cookies.getAllCookieStores
// fails due to the lack of tabs API support.
cookieStores = [{
id: 'firefox-default',
tabIds: [],
}, {
id: 'firefox-private',
tabIds: [],
}];
callback(cookieStores);
});
};
let pendingPrivateCookieRequests = [];
let hasNoPendingCookieRequests = true;
let queueRequestToSetCookies = function(cookie, callback = function() {}) {
// Queue cookie requests so that when chrome.cookies.set is called in a loop,
// that similar cookies are grouped together in a single request.
pendingPrivateCookieRequests.push([cookie, callback]);
if (hasNoPendingCookieRequests) {
hasNoPendingCookieRequests = false;
if (!chrome.extension.inIncognitoContext) {
Promise.resolve().then(function() {
hasNoPendingCookieRequests = true;
var requests = pendingPrivateCookieRequests.splice(0);
var callbacks = requests.map(([cookie, callback]) => callback);
var error = new Error(
'Cannot modify ' + requests.length +
' private cookies due to browser bugs.' +
' Please open the Cookie Manager in private browsing mode and try again.');
withLastError(function() {
callbacks.forEach(function(callback) {
callback();
});
}, error);
});
return;
}
// It is important that getConsentForRequests returns a promise, because that
// ensures that multiple chrome.cookies.set calls in a loop are grouped together.
getConsentForRequests().then(function() {
hasNoPendingCookieRequests = true;
var requests = pendingPrivateCookieRequests.splice(0);
var cookies = requests.map(([cookie, callback]) => cookie);
var callbacks = requests.map(([cookie, callback]) => callback);
setCookiesInPrivateMode(cookies).then(function(results) {
var error = results.errorMessage && new Error(results.errorMessage);
callbacks.forEach(function(callback, i) {
if (results[i]) {
callback();
} else {
withLastError(callback, error || {message: 'Unknown error'});
}
});
});
}, function(error) {
hasNoPendingCookieRequests = true;
var requests = pendingPrivateCookieRequests.splice(0);
var callbacks = requests.map(([cookie, callback]) => callback);
withLastError(function() {
callbacks.forEach(function(callback) {
callback();
});
}, error);
});
}
};
// It's assumed that the |cookie| parameter is not modified by the caller after calling us.
chrome.cookies.set = function(cookie, callback) {
function setWithCookiesAPI() {
if (!('expirationDate' in cookie) || cookie.expirationDate > Date.now() / 1000) {
cookiesSet(cookie, callback);
return;
}
// Requesting to delete cookie. Need to check whether it was really deleted.
// Work-around to cookies still being in the database but not expired.
// These are visible to cookies.getAll - https://bugzil.la/1388873
cookiesSet(cookie, function(newCookie) {
if (!newCookie) {
// Successfully modified.
callback(newCookie);
return;
}
console.log('Temporarily unexpiring cookie to forcibly remove it (bug 1388873).');
// The work-around is to first unexpire the cookie,
// and then to try and expire it again.
newCookie = Object.assign({}, cookie);
cookie.expirationDate = Date.now() / 1000 + 60;
cookiesSet(cookie, function() {
cookie.expirationDate = 0;
cookiesSet(cookie, function(newCookie2) {
if (!newCookie2) {
callback(newCookie2);
} else {
withLastError(callback, {
message: 'Cannot delete an already-expired cookie. ' +
'The browser will automatically remove it in the future.',
});
}
});
});
});
}
if (!isPrivate(cookie)) {
setWithCookiesAPI();
return;
}
runWithoutPrivateCookieBugs(function() {
setWithCookiesAPI();
}, function() {
queueRequestToSetCookies(cookie, callback);
});
};
}
var cookiesAPIwithFirstPartyDomainSupport = false;
// Return a callback that is passed to cookies.getAll(details, callback),
// but without immutable cookies, such as safe browsing cookies while bug 1381197 is open.
function getAllCallbackWithoutImmutableCookies(details, callback) {
// The cookies used for Safebrowsing requests end up in a different cookie jar,
// but Firefox's cookies API does not show any difference between the two.
function isGoogleNIDCookie(c) {
return c.storeId === 'firefox-default' &&
c.domain === '.google.com' &&
c.httpOnly &&
c.name === 'NID';
}
if (details.domain || details.url || cookiesAPIwithFirstPartyDomainSupport) {
// Because of https://bugzil.la/1381197#c2 , if the domain/url is set, the getAll query
// does not include SB cookies.
// If https://bugzil.la/1381197 has been fixed, then we can also return the callback as-is.
return callback;
}
return function(cookies) {
if (!cookies || !cookies.length) {
callback(cookies);
return;
}
if ('firstPartyDomain' in cookies[0]) {
// Apparently the patches for https://bugzil.la/1381197 have landed.
cookiesAPIwithFirstPartyDomainSupport = true;
callback(cookies);
return;
}
var googleNidCookies = cookies && cookies.filter(isGoogleNIDCookie);
if (!googleNidCookies.length) {
callback(cookies);
return;
}
// We cannot use chrome.cookies.getAll because we patch and overwrite it.
window.browser.cookies.getAll({
// Because of https://bugzil.la/1381197#c2 , the result excludes SB cookies.
domain: '.google.com',
name: 'NID',
storeId: 'firefox-default',
}).then(function(cookiesNoSB) {
cookiesNoSB = cookiesNoSB.filter(isGoogleNIDCookie);
cookies = cookies.filter(function(c) {
if (!isGoogleNIDCookie(c)) {
return true;
}
var i = cookiesNoSB.findIndex(function(cNoSB) {
return c.value === cNoSB.value &&
c.path === cNoSB.path &&
c.secure === cNoSB.secure &&
c.httpOnly === cNoSB.httpOnly &&
c.expirationDate === cNoSB.expirationDate;
});
if (i === -1) {
// This is a safe browsing cookie.
return false;
}
cookiesNoSB.splice(i, 1);
return true;
});
callback(cookies);
});
};
}
// Checks whether the browser supports the cookies API without bugs.
// If (likely) bug-free, callbackNoBugs is called.
// Otherwise callbackWithBugs is called, which marks the cookies API as unusable,
// and forces cookies to be modified via actual network requests.
// - runWithoutPrivateCookieBugs.needsFirstPartyRequest is set to true if the network request has
// to happen via a main-frame navigation.
function runWithoutPrivateCookieBugs(callbackNoBugs, callbackWithBugs) {
// There are several bugs in Firefox with private cookies.
//
// Firefox before 56:
// - cookies cannot be modified - bugzil.la/1354229
// - cookies cannot be filtered by 'url' or 'domain' - bugzil.la/1318948
//
// Firefox (all versions):
// - cookies cannot be modified or queried by 'url' or 'domain' when FPI is enabled, i.e.
// privacy.firstparty.isolate is true - bugzil.la/1381197
// - cookies in the safebrowsing cookie jar can never be modified.
if (!runWithoutPrivateCookieBugs.cachedResultPromise) {
runWithoutPrivateCookieBugs.cachedResultPromise = new Promise(checkPrivateCookieBugs);
}
runWithoutPrivateCookieBugs.cachedResultPromise.then(callbackNoBugs, callbackWithBugs);
}
function checkPrivateCookieBugs(callbackNoBugs, callbackWithBugs) {
// Even if we detect that third-party cookies are disabled, we cannot fall back to first-party
// cookies if we cannot open tabs through the tabs API, e.g. in Firefox for Android before 54.
// Even if the tabs API is available, we don't want to try opening tabs if private windows are
// not supported, e.g. in all versions of Firefox for Android.
var canSimulateFirstPartyRequests = !!(chrome.tabs && chrome.windows);
var browserPrivatebrowsingAutostart = false;
if (chrome.extension.inIncognitoContext) {
try {
browserPrivatebrowsingAutostart =
chrome.extension.getBackgroundPage().chrome.extension.inIncognitoContext;
} catch (e) {
// This can happen if the current tab's OriginAttributes does not match the background
// page's. E.g. private browsing mode mismatch.
// Or maybe the background page was shut down.
console.warn('Cannot determine status of browser.privatebrowsing.autostart: ' + e);
}
}
// In the TOR Browser, browser.privatebrowsing.autostart=true by default.
// We are mainly interested in the following defaults of the TOR browser:
// - First-party isolation (FPI) is enabled.
// - Third-party cookies are disabled.
// - Private browsing mode is always enabled.
//
// For the following reasons:
// - Because of FPI, the cookies API cannot edit cookies - bugzil.la/1381197
// - Because of disabled third-party cookies, the only cookies are first-party cookies.
// Consequently, by forcing our cookie requests to be first-party, all cookies can be edited.
// - We ducktype the TOR browser: If private browsing mode is enabled, then assume that
// FPI and third-party cookies are enabled too.
//
// TODO: Hopefully the FPI bugs are fixed in Firefox 59, so we can improve this feature
// detection and support FPI through the cookies API - see the discussion at bugzil.la/1362834
if (browserPrivatebrowsingAutostart && canSimulateFirstPartyRequests) {
// FPI is likely enabled, need to force 1st-party requests.
runWithoutPrivateCookieBugs.needsFirstPartyRequest = true;
callbackWithBugs();
return;
}
// NOTE: After this point, we are unable to detect whether FPI is enabled.
// If FPI is enabled, then we cannot modify cookies.
// In the default Firefox release, FPI is disabled, so we should not/rarely be affected.
// <applet> was removed from Firefox 56 (bugzil.la/1279218),
// so if it is present, then we are in an engine based on Firefox 56 and certainly buggy.
if (typeof HTMLAppletElement !== 'undefined') {
if (!canSimulateFirstPartyRequests) {
runWithoutPrivateCookieBugs.needsFirstPartyRequest = false;
callbackWithBugs();
return;
}
checkThirdPartyCookiesEnabled(function() {
runWithoutPrivateCookieBugs.needsFirstPartyRequest = false;
callbackWithBugs();
}, function() {
// Third-party cookies disabled, need to force 1st-party requests.
runWithoutPrivateCookieBugs.needsFirstPartyRequest = true;
callbackWithBugs();
});
return;
}
runWithoutPrivateCookieBugs.needsFirstPartyRequest = false;
callbackNoBugs();
}
// Quickly checks whether third-party cookies are enabled.
function checkThirdPartyCookiesEnabled(isEnabled, isDisabled) {
var dummyCookie = {
url: 'http://cookie-manager-firefox.local',
name: 'cookie-manager-test-cookie-' + Math.random(),
value: 'dummy-test-value',
// No expirationDate = session cookie.
// No storeId = inherit from current context.
};
function cookiesSet(cookie, callback) {
// We cannot use chrome.cookies.set because we patch and overwrite it.
window.browser.cookies.set(cookie).then(callback);
}
var img = new Image();
// The onBeforeSendHeaders will always be triggered, even if the target is unreachable.
chrome.webRequest.onBeforeSendHeaders.addListener(function listener({requestHeaders}) {
chrome.webRequest.onBeforeSendHeaders.removeListener(listener);
// If the cookie is set, third-party cookies are enabled.
// If the cookie is not set, third-party cookies are disabled (e.g. in TOR Browser).
var isThirdPartyCookisEnabled = requestHeaders.some(({name, value}) => {
return /^cookie$/i.test(name) &&
value.includes(dummyCookie.name + '=' + dummyCookie.value);
});
// Delete the cookie.
dummyCookie.expirationDate = 0;
cookiesSet(dummyCookie, function() {
if (isThirdPartyCookisEnabled) {
isEnabled();
} else {
isDisabled();
}
});
return {cancel: true};
}, {
urls: [dummyCookie.url + '/*'],
types: ['image'],
}, ['requestHeaders', 'blocking']);
cookiesSet(dummyCookie, function() {
img.src = dummyCookie.url;
});
}
function getConsentForRequests() {
return new Promise(function(resolve, reject) {
var defaultSettings = {
consentedToRequests: false,
consentedToTabs: false,
};
chrome.storage.local.get(defaultSettings, function(items) {
items = items || defaultSettings;
var needsFirstPartyRequest = runWithoutPrivateCookieBugs.needsFirstPartyRequest;
var needsConsent = !items.consentedToRequests ||
(needsFirstPartyRequest && !items.consentedToTabs);
if (!needsConsent) {
resolve();
return;
}
var consentMessage =
'Private cookies cannot directly be modified because of browser bugs.\n' +
'Cookies can be modified anyway by sending a HTTP request to the sites of the cookies.\n' +
(needsFirstPartyRequest ?
'Because third-party cookies are blocked, new tabs need to be opened.' :
// The following message is only true for Firefox 56 with default settings.
// If the user enables FPI, then we can usually not even work around the bug,
// except under specific circumstances (such as in the TOR browser).
'The last bug (bug 1354229) has been fixed in Firefox 56.') +
'\n\n' +
'Do you want to allow the Cookie Manager to send requests to modify cookies?';
if (window.confirm(consentMessage)) {
var newItems = {
consentedToRequests: true,
};
if (needsFirstPartyRequest) {
newItems.consentedToTabs = true;
}
chrome.storage.local.set(newItems, function() {
resolve();
});
} else {
reject(new Error('Cannot modify private cookies because of browser bugs, ' +
'and you did not give the permission to work around these bugs.'));
}
});
});
}
/**
* Convert a cookie to a value that can be used as a value for the Set-Cookie HTTP header.
**/
function cookieToHeaderValue(cookie) {
// These checks should all pass because the cookie should have been validated.
assertValid(cookieValidators.name(cookie.name));
assertValid(cookieValidators.value(cookie.value));
if (cookie.domain) assertValid(cookieValidators.domain(cookie.domain, new URL(cookie.url).hostname));
if (cookie.path) assertValid(cookieValidators.path(cookie.path));
if (cookie.expirationDate) assertValid(cookieValidators.expirationDate(cookie.expirationDate));
function assertValid(m) {
if (m) throw new Error('Invalid cookie: ' + m);
}
var parts = [cookie.name + '=' + cookie.value];
if (cookie.path)
parts.push('path=' + cookie.path);
if (cookie.domain && !cookie.hostOnly)
parts.push('domain=' + cookie.domain);
if (typeof cookie.expirationDate === 'number' && !cookie.session)
parts.push('expires=' + new Date(cookie.expirationDate * 1000).toGMTString());
if (cookie.secure)
parts.push('secure');
if (cookie.httpOnly)
parts.push('httponly');
// Note: In practice the SameSite flag is likely ignored because we use the generated
// cookie header in a cross-site request.
if (cookie.sameSite && cookie.sameSite !== 'no_restriction')
parts.push('samesite=' + cookie.sameSite);
return parts.join('; ');
}
/**
* Generate a pseudo-ramdom unique number.
*/
function getRandomUniqueNumber(obj) {
var randomState = obj._randomNumberState;
if (!randomState) {
// We use a random step size instead of a fixed counter,
// to avoid potential information leakage across domains.
// (if we simply start with 0 and increment by 1 at every
// request for a number, then the recipient of the number
// can derive how many cookies are stored in the browser).
randomState = obj._randomNumberState = {
value: Math.floor(Date.now() * Math.random()),
stepSize: 100 + Math.floor(Math.random() * 900),
count: 0,
};
}
randomState.value = randomState.value + (++randomState.count) * randomState.stepSize;
var slack = Math.floor(Math.random() * randomState.stepSize);
if (randomState > Number.MAX_SAFE_INTEGER - slack) {
randomState.value -= Number.MAX_SAFE_INTEGER;
randomState.count = 0;
}
return randomState.value + slack;
}
// Append non-holey array |arrayIn| to |arrayOut|.
function arrayAppend(arrayOut, arrayIn) {
try {
[].push.apply(arrayOut, arrayIn);
} catch (e) {
// arrayIn is too large; stack overflow.
// Insert one-by-one.
arrayIn.forEach(function(elem) {
arrayOut.push(elem);
});
}
}
var MAX_TEMPORARY_TABS = 20;
var _temporaryTabCount = 0;
var _temporaryTabPort = null;
var _temporaryTabQueue = [];
function openTemporaryHiddenTab(url, callback) {
// See similar assertion near sendFirstPartyRequest.
console.assert(chrome.extension.inIncognitoContext, 'inIncognitoContext === true');
if (_temporaryTabCount === MAX_TEMPORARY_TABS) {
_temporaryTabQueue.push([url, callback]);
return;
}
++_temporaryTabCount;
chrome.tabs.onRemoved.addListener(tabsOnRemoved);
var createdTabId;
chrome.tabs.create({
url: url,
active: false,
}, function(tab) {
callback(tab, chrome.runtime.lastError);
// This is not expected to happen, but just in case:
if (tab) {
createdTabId = tab.id;
// Register the tab ID with the background page so that if the user closes the
// current tab, that all other temporary tabs are gone too.
if (!_temporaryTabPort) {
_temporaryTabPort = chrome.runtime.connect({
name: 'kill-tabs-on-unload',
});
}
_temporaryTabPort.postMessage({
createdTabId: createdTabId,
});
// Not expected to happen. Can happen if the background page somehow reloads.
_temporaryTabPort.onDisconnect.addListener(function(port) {
if (port === _temporaryTabPort) {
_temporaryTabPort = null;
}
});
} else {
tabIsRemoved();
}
});
function tabsOnRemoved(removedTabId) {
if (removedTabId === createdTabId) {
tabIsRemoved();
_temporaryTabPort.postMessage({
removedTabId: createdTabId,
});
}
}
function tabIsRemoved() {
chrome.tabs.onRemoved.removeListener(tabsOnRemoved);
--_temporaryTabCount;
if (_temporaryTabQueue.length) {
var [url, callback] = _temporaryTabQueue.shift();
openTemporaryHiddenTab(url, callback);
} else if (_temporaryTabCount === 0) {
_temporaryTabCount.disconnect();
_temporaryTabCount = null;
}
}
}
/**
* Set the given cookies in a request to the given domain.
* All cookies must be part of the given domain and storeId.
* The request will be made with the cookie jar of the current extension context.
* If any of the cookies have the Secure flag, the https:-scheme is used;
* otherwise http: is used.
*
* @returns {Promise<boolean>} Whether the cookies have been set.
*/
function sendRequestToSetCookies(domain, cookies) {
// When third-party cookies are disabled, cookies cannot be modified via a
// hidden cross-domain request. We have to trigger a main frame navigation
// in order to be able to modify cookies.
//
// See checkPrivateCookieBugs for more information.
var needsFirstPartyRequest = runWithoutPrivateCookieBugs.needsFirstPartyRequest;
var cookieHeaderValues = cookies.map(cookieToHeaderValue);
// If any cookie has the Secure flag, then the request must go over HTTPs.
var url = (cookies.some((cookie) => cookie.secure) ? 'https://' : 'http://') +
domain + '/?' + getRandomUniqueNumber(sendRequestToSetCookies);
var requestFilter = {
urls: [url],
types: needsFirstPartyRequest ? ['main_frame'] : ['image'],
};
var affectedRequestId;
var affectedTabId;
var didSetCookie = false;
if (url.startsWith('http:')) {
// Account for HTTP Strict Transport Security (HSTS) upgrades.
requestFilter.urls.push(url.replace('http', 'https'));
}
var cleanupFunctions = [];
function addListener(target, listener, ...args) {
target.addListener(listener, ...args);
cleanupFunctions.push(function() {
target.removeListener(listener);
});
}
addListener(chrome.webRequest.onBeforeRequest, onBeforeRequest, requestFilter, ['blocking']);
addListener(chrome.webRequest.onBeforeSendHeaders,
onBeforeSendHeaders, requestFilter, ['requestHeaders', 'blocking']);
addListener(chrome.webRequest.onHeadersReceived,
onHeadersReceived, requestFilter, ['responseHeaders', 'blocking']);
function sendThirdPartyRequest(resolve) {
// WebRequest event listeners are registered asynchronously. Make a roundtrip
// via the parent process to make sure that the event listener has been
// registered.
// (The specific API call does not matter here, any async API will do.)
chrome.runtime.getPlatformInfo(function() {
var img = new Image();
img.onload = img.onerror = resolve;
img.src = url;
// Ensure that the function does not stall forever.
setTimeout(function() {
img.onload = img.onerror = null;
// Cancel the request if needed.
img.src = '';
// Make another round-trip just in case there was a pending
// response.
chrome.runtime.getPlatformInfo(function() {
resolve();
});
}, 2000);
});
}
function sendFirstPartyRequest(resolve, reject) {
// Open incongito tab in current window to trigger request.
// Without an explicit windowId, the tabs.create API opens a tab in the current window.
// The caller ensures that the current window is an incognito window.
console.assert(chrome.extension.inIncognitoContext, 'inIncognitoContext === true');
addListener(chrome.tabs.onRemoved, function(tabId) {
if (affectedTabId === tabId) {
resolve();
}
});
addListener(chrome.webRequest.onErrorOccurred, function(details) {
if (details.requestId !== affectedRequestId) return;
resolve();
}, requestFilter);
addListener(chrome.webRequest.onResponseStarted, function(details) {
if (details.requestId !== affectedRequestId) return;
resolve();
}, requestFilter);
// This happens when the server responds with a redirect, and we rewrite it to a JS-URL.
// webRequest.onErrorOccurred is not triggered.
addListener(chrome.webNavigation.onErrorOccurred, function(details) {
if (details.tabId !== affectedTabId) return;
resolve();
});
var shouldRemoveTab = false;
cleanupFunctions.push(function() {
if (affectedTabId) {
chrome.tabs.remove(affectedTabId);
} else {
shouldRemoveTab = true;
}
});
openTemporaryHiddenTab(url, function(tab, error) {
if (error) {
reject(error);
return;
}
// Also set in onBeforeRequest, but set it here too in case the request never succeeds.
affectedTabId = tab.id;
if (shouldRemoveTab) {
chrome.tabs.remove(affectedTabId);
}
});
}
return new Promise(function(resolve) {
if (needsFirstPartyRequest) {
sendFirstPartyRequest(resolve);
} else {
sendThirdPartyRequest(resolve);
}
}).then(function() {
cleanupFunctions.forEach((cleanup) => cleanup());
return didSetCookie;
}, function(error) {
cleanupFunctions.forEach((cleanup) => cleanup());
throw error;
});
function onBeforeRequest(details) {
if (affectedRequestId) return;
affectedRequestId = details.requestId;
affectedTabId = details.tabId;
chrome.webRequest.onBeforeRequest.removeListener(onBeforeRequest);
}
function onBeforeSendHeaders(details) {
if (details.requestId !== affectedRequestId) return;
// Remove cookies in request to prevent the server from recognizing the
// client.
var requestHeaders = details.requestHeaders.filter(function(header) {
return !/^(cookie|authorization)$/i.test(header.name);
});
if (requestHeaders.length !== details.requestHeaders.length) {
return {
requestHeaders: requestHeaders,
};
}
}
function onHeadersReceived(details) {
if (details.requestId !== affectedRequestId) return;
var responseHeaders = details.responseHeaders.filter(function(header) {
return !/^(set-cookie2?|location)$/i.test(header.name);
});
if (needsFirstPartyRequest) {
// The main-frame request is aborted as soon as possible.
// Block all scripts and other resources just in case.
responseHeaders = responseHeaders.filter(function(header) {
return !/^(content-security-policy|www-authenticate)$/i.test(header.name);
});
responseHeaders.push({
name: 'Content-Security-Policy',
value: 'default-src \'none\'',
});
}
if (details.statusCode >= 300 && details.statusCode < 400) {
responseHeaders.push({
name: 'Location',
// jshint scripturl:true
value: 'javascript:// Dummy local URL to block redirect',
// jshint scripturl:false
});
}
responseHeaders.push({
name: 'Set-Cookie',
value: cookieHeaderValues.join('\n'),
});
didSetCookie = true;
return {
responseHeaders: responseHeaders,
};
}
}
// To minimize the number of requests, we shall use a tree structure.
// Each DomainPart represents a part of a domain, e.g.
// com
// / \
// example.com ample.com
// / / \
// www.example.com sub.ample.com sub2.ample.com
// /
// www.sub.ample.com
// In the above example, the top DomainPart has .part = "com",
// and the .part of its two children are "example.com" and "ample.com".
class DomainPart {
constructor(domain, parentDomainPart) {
this.domain = domain;
this.parentDomainPart = parentDomainPart;
this.children = [];
this.removedChildren = [];
this.secureDomainCookies = [];
this.insecureDomainCookies = [];
this.secureHostOnlyCookies = [];
this.insecureHostOnlyCookies = [];
this.maySendHttpRequest = true;
this.maySendHttpsRequest = true;
}
// Add a cookie as a node to the tree.
// domainPartsPrefix should be an array of the domain, e.g. ['www', 'example', 'com'].
// The list will be modified by this method.
addNode(domain, cookie) {
if (domain === this.domain) {
if (cookie.hostOnly) {
if (cookie.secure) {
this.secureHostOnlyCookies.push(cookie);
} else {
this.insecureHostOnlyCookies.push(cookie);
}
} else if (cookie.secure) {
this.secureDomainCookies.push(cookie);
} else {
this.insecureDomainCookies.push(cookie);
}
return;
}
var indexBeforeThisDomain = domain.length - this.domain.length - 1;
console.assert(indexBeforeThisDomain > 0); // Can't be 0, otherwise domain === this.domain.
// If dot is found, then we want the part after the dot.
// If dot is not found (-1), then we want the full string (i.e. starting at index 0).
var dotIndex = domain.lastIndexOf('.', indexBeforeThisDomain - 1) + 1;
var domainSuffix = domain.substr(dotIndex);
// The number of children per node is expected to be small.
// So let's use a linear search, opposed to storing the parts in a map.
var childNode = this.children.find((child) => {
return child.domain === domainSuffix;
});
if (!childNode) {
childNode = new DomainPart(domainSuffix, this);
this.children.push(childNode);
}
childNode.addNode(domain, cookie);
}
forEachBottomUp(callback, includeRemovedChildren) {
this.children.forEach((child) => {
child.forEachBottomUp(callback, includeRemovedChildren);
});
if (includeRemovedChildren) {
this.removedChildren.forEach((child) => {
child.forEachBottomUp(callback, includeRemovedChildren);
});
}
callback(this);
}
removeNodeIfLeaf() {
if (this.children.length > 0 || !this.parentDomainPart) {
return;
}
var i = this.parentDomainPart.children.indexOf(this);
if (i >= 0) {
this.parentDomainPart.children.splice(i, 1);
this.parentDomainPart.removedChildren.push(this);
this.removeNodeIfLeaf();
}
}
getHostOnlyNodes() {
var hostOnlyNodes = [];
this.forEachBottomUp((node) => {
if (node.secureHostOnlyCookies.length || node.insecureHostOnlyCookies.length) {
hostOnlyNodes.push(node);
}
});
return hostOnlyNodes;
}
getLeafNodes() {
var leafNodes = [];
this.forEachBottomUp((node) => {
if (node.children.length === 0) {
leafNodes.push(node);
}
});
if (leafNodes.length && !leafNodes[leafNodes.length - 1].parentDomainPart) {
// Exclude the root.
leafNodes.pop();
}
return leafNodes;
}
getUnprocessedCookies() {
var cookies = [];
this.forEachBottomUp((node) => {
arrayAppend(cookies, node.secureDomainCookies);
arrayAppend(cookies, node.insecureDomainCookies);
arrayAppend(cookies, node.secureHostOnlyCookies);
arrayAppend(cookies, node.insecureHostOnlyCookies);
}, true);
return cookies;
}
// Whether it makes sense to send a request for the domain associated with
// this node.
isNodeFinished() {
if (!this.maySendHttpRequest && !this.maySendHttpsRequest) {
return true;
}
return this.secureHostOnlyCookies.length === 0 &&
this.insecureHostOnlyCookies.length === 0 &&
this.secureDomainCookies.length === 0 &&
this.insecureDomainCookies.length === 0;
}
sendRequestWithHostOnlyAndDomainCookies() {
return this._setCookiesByRequest(true);
}
sendRequestWithDomainCookies() {
return this._setCookiesByRequest(false);
}
_getMatchingCookies(includeHostOnly, includeSecure) {
var cookies = [];
if (includeSecure) {
arrayAppend(cookies, this.secureDomainCookies);
}
arrayAppend(cookies, this.insecureDomainCookies);
if (includeHostOnly) {
if (includeSecure) {
arrayAppend(cookies, this.secureHostOnlyCookies);
}
arrayAppend(cookies, this.insecureHostOnlyCookies);
}
if (this.parentDomainPart) { // Only false for the root node.
// includeHostOnly is unconditionally false because domain cookies
// can only be set for a request to that specific domain.
arrayAppend(cookies,
this.parentDomainPart._getMatchingCookies(false, includeSecure));
}
return cookies;
}
_unsetMatchingCookies(includeHostOnly, includeSecure) {
if (includeSecure) {
this.secureDomainCookies.length = 0;
}
this.insecureDomainCookies.length = 0;
if (includeHostOnly) {
if (includeSecure) {
this.secureHostOnlyCookies.length = 0;
}
this.insecureHostOnlyCookies.length = 0;
}
if (this.parentDomainPart) { // Only false for the root node.
this.parentDomainPart._unsetMatchingCookies(true, includeSecure);
}
}
// The algorithm runs in two passes:
// 1) host-only cookies anywhere in the tree.
// 2) domain-cookies at the leaves.
// This method must be called twice, first with includeHostOnly=true, and
// then again with includeHostOnly=false.
_setCookiesByRequest(includeHostOnly) {
if (!this.maySendHttpRequest && !this.maySendHttpsRequest) {
return Promise.resolve();
}
// We always try sending a HTTPS request, unless we already tried before.
var includeSecure = this.maySendHttpsRequest;
this.maySendHttpsRequest = false;
var cookies = this._getMatchingCookies(includeHostOnly, includeSecure);
if (cookies.every((cookie) => !cookie.secure)) {
this.maySendHttpRequest = false;
}
if (!cookies.length) {
this.maySendHttpRequest = false;
return Promise.resolve();
}
return sendRequestToSetCookies(this.domain, cookies)
.then((didSetCookie) => {
if (didSetCookie) {
// There is no future need for a HTTP request.
this.maySendHttpRequest = false;
return true;
}
if (cookies.every((cookie) => cookie.secure) ||
cookies.every((cookie) => !cookie.secure)) {