summaryrefslogtreecommitdiffstats
path: root/netwerk/cookie/test/unit/test_eviction.js
blob: 7f693ee94b27e8c4687e932ab52581f9d6215e76 (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
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
var {utils: Cu, interfaces: Ci, classes: Cc} = Components;

Cu.import("resource://gre/modules/Services.jsm");

const BASE_HOSTNAMES = ["example.org", "example.co.uk"];
const SUBDOMAINS = ["", "pub.", "www.", "other."];

const cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
const cm = cs.QueryInterface(Ci.nsICookieManager2);

function run_test() {
    var tests = [];
    Services.prefs.setIntPref("network.cookie.staleThreshold", 0);
    for (var host of BASE_HOSTNAMES) {
        var base = SUBDOMAINS[0] + host;
        var sub = SUBDOMAINS[1] + host;
        var other = SUBDOMAINS[2] + host;
        var another = SUBDOMAINS[3] + host;
        tests.push([host, test_basic_eviction.bind(this, base, sub, other, another)]);
        add_task(function* a() {
            var t = tests.splice(0, 1)[0];
            do_print('testing with host ' + t[0]);
            yield t[1]();
            cm.removeAll();
        });
        tests.push([host, test_domain_or_path_matches_not_both.bind(this, base, sub, other, another)]);
        add_task(function*() {
            var t = tests.splice(0, 1)[0];
            do_print('testing with host ' + t[0]);
            yield t[1]();
            cm.removeAll();
        });
    }
    add_task(function*() {
        yield test_localdomain();
        cm.removeAll();
    });

    add_task(function*() {
        yield test_path_prefix();
    });

    run_next_test();
}

// Verify that cookies that share a path prefix with the URI path are still considered
// candidates for eviction, since the paths do not actually match.
function* test_path_prefix() {
    Services.prefs.setIntPref("network.cookie.maxPerHost", 2);

    const BASE_URI = Services.io.newURI("http://example.org/", null, null);
    const BASE_BAR = Services.io.newURI("http://example.org/bar/", null, null);
    const BASE_BARBAR = Services.io.newURI("http://example.org/barbar/", null, null);

    yield setCookie("session_first", null, null, null, BASE_URI);
    yield setCookie("session_second", null, "/bar", null, BASE_BAR);
    verifyCookies(['session_first', 'session_second'], BASE_URI);

    yield setCookie("session_third", null, "/barbar", null, BASE_BARBAR);
    verifyCookies(['session_first', 'session_third'], BASE_URI);
}

// Verify that subdomains of localhost are treated as separate hosts and aren't considered
// candidates for eviction.
function* test_localdomain() {
    Services.prefs.setIntPref("network.cookie.maxPerHost", 2);

    const BASE_URI = Services.io.newURI("http://localhost", null, null);
    const BASE_BAR = Services.io.newURI("http://localhost/bar", null, null);
    const OTHER_URI = Services.io.newURI("http://other.localhost", null, null);
    const OTHER_BAR = Services.io.newURI("http://other.localhost/bar", null, null);
    
    yield setCookie("session_no_path", null, null, null, BASE_URI);
    yield setCookie("session_bar_path", null, "/bar", null, BASE_BAR);

    yield setCookie("session_no_path", null, null, null, OTHER_URI);
    yield setCookie("session_bar_path", null, "/bar", null, OTHER_BAR);

    verifyCookies(['session_no_path',
                   'session_bar_path'], BASE_URI);
    verifyCookies(['session_no_path',
                   'session_bar_path'], OTHER_URI);

    yield setCookie("session_another_no_path", null, null, null, BASE_URI);
    verifyCookies(['session_no_path',
                   'session_another_no_path'], BASE_URI);

    yield setCookie("session_another_no_path", null, null, null, OTHER_URI);
    verifyCookies(['session_no_path',
                   'session_another_no_path'], OTHER_URI);
}

// Ensure that cookies are still considered candidates for eviction if either the domain
// or path matches, but not both.
function* test_domain_or_path_matches_not_both(base_host,
                                               subdomain_host,
                                               other_subdomain_host,
                                               another_subdomain_host) {
    Services.prefs.setIntPref("network.cookie.maxPerHost", 2);

    const BASE_URI = Services.io.newURI("http://" + base_host, null, null);
    const PUB_FOO_PATH = Services.io.newURI("http://" + subdomain_host + "/foo/", null, null);
    const WWW_BAR_PATH = Services.io.newURI("http://" + other_subdomain_host + "/bar/", null, null);
    const OTHER_BAR_PATH = Services.io.newURI("http://" + another_subdomain_host + "/bar/", null, null);
    const PUB_BAR_PATH = Services.io.newURI("http://" + subdomain_host + "/bar/", null, null);
    const WWW_FOO_PATH = Services.io.newURI("http://" + other_subdomain_host + "/foo/", null, null);

    yield setCookie("session_pub_with_foo_path", subdomain_host, "/foo", null, PUB_FOO_PATH);
    yield setCookie("session_www_with_bar_path", other_subdomain_host, "/bar", null, WWW_BAR_PATH);
    verifyCookies(['session_pub_with_foo_path',
                   'session_www_with_bar_path'], BASE_URI);

    yield setCookie("session_pub_with_bar_path", subdomain_host, "/bar", null, PUB_BAR_PATH);
    verifyCookies(['session_www_with_bar_path',
                   'session_pub_with_bar_path'], BASE_URI);

    yield setCookie("session_other_with_bar_path", another_subdomain_host, "/bar", null, OTHER_BAR_PATH);
    verifyCookies(['session_pub_with_bar_path',
                   'session_other_with_bar_path'], BASE_URI);
}

function* test_basic_eviction(base_host, subdomain_host, other_subdomain_host) {
    Services.prefs.setIntPref("network.cookie.maxPerHost", 5);

    const BASE_URI = Services.io.newURI("http://" + base_host, null, null);
    const SUBDOMAIN_URI = Services.io.newURI("http://" + subdomain_host, null, null);
    const OTHER_SUBDOMAIN_URI = Services.io.newURI("http://" + other_subdomain_host, null, null);
    const FOO_PATH = Services.io.newURI("http://" + base_host + "/foo/", null, null);
    const BAR_PATH = Services.io.newURI("http://" + base_host + "/bar/", null, null);
    const ALL_SUBDOMAINS = '.' + base_host;
    const OTHER_SUBDOMAIN = other_subdomain_host;

    // Initialize the set of cookies with a mix of non-session cookies with no path,
    // and session cookies with explicit paths. Any subsequent cookies added will cause
    // existing cookies to be evicted.
    yield setCookie("non_session_non_path_non_domain", null, null, 100000, BASE_URI);
    yield setCookie("non_session_non_path_subdomain", ALL_SUBDOMAINS, null, 100000, SUBDOMAIN_URI);
    yield setCookie("session_non_path_pub_domain", OTHER_SUBDOMAIN, null, null, OTHER_SUBDOMAIN_URI);
    yield setCookie("session_foo_path", null, "/foo", null, FOO_PATH);
    yield setCookie("session_bar_path", null, "/bar", null, BAR_PATH);
    verifyCookies(['non_session_non_path_non_domain',
                   'non_session_non_path_subdomain',
                   'session_non_path_pub_domain',
                   'session_foo_path',
                   'session_bar_path'], BASE_URI);

    // Ensure that cookies set for the / path appear more recent.
    cs.getCookieString(OTHER_SUBDOMAIN_URI, null)
    verifyCookies(['non_session_non_path_non_domain',
                   'session_foo_path',
                   'session_bar_path',
                   'non_session_non_path_subdomain',
                   'session_non_path_pub_domain'], BASE_URI);

    // Evict oldest session cookie that does not match example.org/foo (session_bar_path)
    yield setCookie("session_foo_path_2", null, "/foo", null, FOO_PATH);
    verifyCookies(['non_session_non_path_non_domain',
                   'session_foo_path',
                   'non_session_non_path_subdomain',
                   'session_non_path_pub_domain',
                   'session_foo_path_2'], BASE_URI);

    // Evict oldest session cookie that does not match example.org/bar (session_foo_path)
    yield setCookie("session_bar_path_2", null, "/bar", null, BAR_PATH);
    verifyCookies(['non_session_non_path_non_domain',
                   'non_session_non_path_subdomain',
                   'session_non_path_pub_domain',
                   'session_foo_path_2',
                   'session_bar_path_2'], BASE_URI);

    // Evict oldest session cookie that does not match example.org/ (session_non_path_pub_domain)
    yield setCookie("non_session_non_path_non_domain_2", null, null, 100000, BASE_URI);
    verifyCookies(['non_session_non_path_non_domain',
                   'non_session_non_path_subdomain',
                   'session_foo_path_2',
                   'session_bar_path_2',
                   'non_session_non_path_non_domain_2'], BASE_URI);

    // Evict oldest session cookie that does not match example.org/ (session_foo_path_2)
    yield setCookie("session_non_path_non_domain_3", null, null, null, BASE_URI);
    verifyCookies(['non_session_non_path_non_domain',
                   'non_session_non_path_subdomain',
                   'session_bar_path_2',
                   'non_session_non_path_non_domain_2',
                   'session_non_path_non_domain_3'], BASE_URI);

    // Evict oldest session cookie; all such cookies match example.org/bar (session_bar_path_2)
    // note: this new cookie doesn't have an explicit path, but empty paths inherit the
    // request's path
    yield setCookie("non_session_bar_path_non_domain", null, null, 100000, BAR_PATH);
    verifyCookies(['non_session_non_path_non_domain',
                   'non_session_non_path_subdomain',
                   'non_session_non_path_non_domain_2',
                   'session_non_path_non_domain_3',
                   'non_session_bar_path_non_domain'], BASE_URI);

    // Evict oldest session cookie, even though it matches pub.example.org (session_non_path_non_domain_3)
    yield setCookie("non_session_non_path_pub_domain", null, null, 100000, OTHER_SUBDOMAIN_URI);
    verifyCookies(['non_session_non_path_non_domain',
                   'non_session_non_path_subdomain',
                   'non_session_non_path_non_domain_2',
                   'non_session_bar_path_non_domain',
                   'non_session_non_path_pub_domain'], BASE_URI);

    // All session cookies have been evicted.
    // Evict oldest non-session non-domain-matching cookie (non_session_non_path_pub_domain)
    yield setCookie("non_session_bar_path_non_domain_2", null, '/bar', 100000, BAR_PATH);
    verifyCookies(['non_session_non_path_non_domain',
                   'non_session_non_path_subdomain',
                   'non_session_non_path_non_domain_2',
                   'non_session_bar_path_non_domain',
                   'non_session_bar_path_non_domain_2'], BASE_URI);

    // Evict oldest non-session non-path-matching cookie (non_session_bar_path_non_domain)
    yield setCookie("non_session_non_path_non_domain_4", null, null, 100000, BASE_URI);
    verifyCookies(['non_session_non_path_non_domain',
                   'non_session_non_path_subdomain',
                   'non_session_non_path_non_domain_2',
                   'non_session_bar_path_non_domain_2',
                   'non_session_non_path_non_domain_4'], BASE_URI);

    // At this point all remaining cookies are non-session cookies, have a path of /,
    // and either don't have a domain or have one that matches subdomains.
    // They will therefore be evicted from oldest to newest if all new cookies added share
    // similar characteristics.
}

// Verify that the given cookie names exist, and are ordered from least to most recently accessed
function verifyCookies(names, uri) {
    do_check_eq(cm.countCookiesFromHost(uri.host), names.length);
    let cookies = cm.getCookiesFromHost(uri.host, {});
    let actual_cookies = [];
    while (cookies.hasMoreElements()) {
        let cookie = cookies.getNext().QueryInterface(Ci.nsICookie2);
        actual_cookies.push(cookie);
    }
    if (names.length != actual_cookies.length) {
        let left = names.filter(function(n) {
            return actual_cookies.findIndex(function(c) {
                return c.name == n;
            }) == -1;
        });
        let right = actual_cookies.filter(function(c) {
            return names.findIndex(function(n) {
                return c.name == n;
            }) == -1;
        }).map(function(c) { return c.name });
        if (left.length) {
            do_print("unexpected cookies: " + left);
        }
        if (right.length) {
            do_print("expected cookies: " + right);
        }
    }
    do_check_eq(names.length, actual_cookies.length);
    actual_cookies.sort(function(a, b) {
        if (a.lastAccessed < b.lastAccessed)
            return -1;
        if (a.lastAccessed > b.lastAccessed)
            return 1;
        return 0;
    });
    for (var i = 0; i < names.length; i++) {
        do_check_eq(names[i], actual_cookies[i].name);
        do_check_eq(names[i].startsWith('session'), actual_cookies[i].isSession);
    }
}

var lastValue = 0
function* setCookie(name, domain, path, maxAge, url) {
    let value = name + "=" + ++lastValue;
    var s = 'setting cookie ' + value;
    if (domain) {
        value += "; Domain=" + domain;
        s += ' (d=' + domain + ')';
    }
    if (path) {
        value += "; Path=" + path;
        s += ' (p=' + path + ')';
    }
    if (maxAge) {
        value += "; Max-Age=" + maxAge;
        s += ' (non-session)';
    } else {
        s += ' (session)';
    }
    s += ' for ' + url.spec;
    do_print(s);
    cs.setCookieStringFromHttp(url, null, null, value, null, null);
    return new Promise(function(resolve) {
        // Windows XP has low precision timestamps that cause our cookie eviction
        // algorithm to produce different results from other platforms. We work around
        // this by ensuring that there's a clear gap between each cookie update.
        do_timeout(10, resolve);
    })
}