summaryrefslogtreecommitdiffstats
path: root/extensions/cookie/test/unit/test_cookies_thirdparty.js
blob: a39f807f2256a676ee5019dc32aa9abf0caeb9ec (plain)
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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// test third party cookie blocking, for the cases:
// 1) with null channel
// 2) with channel, but with no docshell parent

function run_test() {
  // Create URIs and channels pointing to foo.com and bar.com.
  // We will use these to put foo.com into first and third party contexts.
  var spec1 = "http://foo.com/foo.html";
  var spec2 = "http://bar.com/bar.html";
  var uri1 = NetUtil.newURI(spec1);
  var uri2 = NetUtil.newURI(spec2);
  var channel1 = NetUtil.newChannel({uri: uri1, loadUsingSystemPrincipal: true});
  var channel2 = NetUtil.newChannel({uri: uri2, loadUsingSystemPrincipal: true});

  // test with cookies enabled
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
  do_set_cookies(uri1, channel1, true, [1, 2, 3, 4]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [1, 2, 3, 4]);
  Services.cookies.removeAll();

  // test with third party cookies blocked
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 1);
  do_set_cookies(uri1, channel1, true, [0, 0, 0, 0]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
  Services.cookies.removeAll();

  // Force the channel URI to be used when determining the originating URI of
  // the channel.
  var httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
  var httpchannel2 = channel2.QueryInterface(Ci.nsIHttpChannelInternal);
  httpchannel1.forceAllowThirdPartyCookie = true;
  httpchannel2.forceAllowThirdPartyCookie = true;

  // test with cookies enabled
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
  do_set_cookies(uri1, channel1, true, [1, 2, 3, 4]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [1, 2, 3, 4]);
  Services.cookies.removeAll();

  // test with third party cookies blocked
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 1);
  do_set_cookies(uri1, channel1, true, [0, 1, 1, 2]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
  Services.cookies.removeAll();

  // test with third party cookies limited
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 3);
  do_set_cookies(uri1, channel1, true, [0, 1, 2, 3]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
  Services.cookies.removeAll();
  do_set_single_http_cookie(uri1, channel1, 1);
  do_set_cookies(uri1, channel2, true, [2, 3, 4, 5]);
  Services.cookies.removeAll();

  // Test per-site 3rd party cookie blocking with cookies enabled
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
  var kPermissionType = "cookie";
  var ALLOW_FIRST_PARTY_ONLY = 9;
  // ALLOW_FIRST_PARTY_ONLY overrides
  Services.perms.add(uri1, kPermissionType, ALLOW_FIRST_PARTY_ONLY);
  do_set_cookies(uri1, channel1, true, [0, 1, 1, 2]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
  Services.cookies.removeAll();

  // Test per-site 3rd party cookie blocking with 3rd party cookies disabled
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 1);
  do_set_cookies(uri1, channel1, true, [0, 1, 1, 2]);
  Services.cookies.removeAll();
  // No preference has been set for uri2, but it should act as if
  // ALLOW_FIRST_PARTY_ONLY has been set
  do_set_cookies(uri2, channel2, true, [0, 1, 1, 2]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
  Services.cookies.removeAll();

  // Test per-site 3rd party cookie blocking with 3rd party cookies limited
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 3);
  do_set_cookies(uri1, channel1, true, [0, 1, 1, 2]);
  Services.cookies.removeAll();
  // No preference has been set for uri2, but it should act as if
  // LIMIT_THIRD_PARTY has been set
  do_set_cookies(uri2, channel2, true, [0, 1, 2, 3]);
  Services.cookies.removeAll();
  do_set_single_http_cookie(uri2, channel2, 1);
  do_set_cookies(uri2, channel2, true, [2, 3, 4, 5]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
  Services.cookies.removeAll();
  do_set_single_http_cookie(uri1, channel1, 1);
  do_set_cookies(uri1, channel2, true, [1, 1, 1, 1]);
  Services.cookies.removeAll();

  // Test per-site 3rd party cookie limiting with cookies enabled
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
  var kPermissionType = "cookie";
  var LIMIT_THIRD_PARTY = 10;
  // LIMIT_THIRD_PARTY overrides
  Services.perms.add(uri1, kPermissionType, LIMIT_THIRD_PARTY);
  do_set_cookies(uri1, channel1, true, [0, 1, 2, 3]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
  Services.cookies.removeAll();
  do_set_single_http_cookie(uri1, channel1, 1);
  do_set_cookies(uri1, channel2, true, [2, 3, 4, 5]);
  Services.cookies.removeAll();

  // Test per-site 3rd party cookie limiting with 3rd party cookies disabled
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 1);
  do_set_cookies(uri1, channel1, true, [0, 1, 2, 3]);
  Services.cookies.removeAll();
  // No preference has been set for uri2, but it should act as if
  // ALLOW_FIRST_PARTY_ONLY has been set
  do_set_cookies(uri2, channel2, true, [0, 1, 1, 2]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
  Services.cookies.removeAll();
  do_set_single_http_cookie(uri1, channel1, 1);
  do_set_cookies(uri1, channel2, true, [2, 3, 4, 5]);
  Services.cookies.removeAll();

  // Test per-site 3rd party cookie limiting with 3rd party cookies limited
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 3);
  do_set_cookies(uri1, channel1, true, [0, 1, 2, 3]);
  Services.cookies.removeAll();
  // No preference has been set for uri2, but it should act as if
  // LIMIT_THIRD_PARTY has been set
  do_set_cookies(uri2, channel2, true, [0, 1, 2, 3]);
  Services.cookies.removeAll();
  do_set_single_http_cookie(uri2, channel2, 1);
  do_set_cookies(uri2, channel2, true, [2, 3, 4, 5]);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
  Services.cookies.removeAll();
  do_set_single_http_cookie(uri1, channel1, 1);
  do_set_cookies(uri1, channel2, true, [2, 3, 4, 5]);
  Services.cookies.removeAll();
}