This file is indexed.

/usr/share/xul-ext/self-destructing-cookies/lib/cookietracker.js is in xul-ext-self-destructing-cookies 0.4.12-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
/* Copyright (c) 2013 Ove Sörensen <sdc@elektro-eel.org>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 02110-1301, USA.
*/

"use strict";

var timers = require("sdk/timers");
var {Cc, Ci} = require("chrome");
var DomainTree = require("./domaintree").DomainTree;
var cookieManager = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
var eTLDService = Cc["@mozilla.org/network/effective-tld-service;1"].getService(Ci.nsIEffectiveTLDService);

const STYLE_TOP = 0;
const STYLE_FRAME = 1;

// this class tracks cookies and domain references to them, eventually expiring unused cookies
exports.CookieTracker = function CookieTracker(options) {
    options = options || {};

    this.onExpired = options.onExpired;
    this.isExpired = options.isExpired;
    this.canAccess = options.canAccess;
    this.setGracePeriod(options.gracePeriod);
    this.armed = options.armed;
    this.cookieHash = {};
    this.domainTree = new DomainTree();
    this.expirationHandles = {};
}

// introduce a new cookie to the tracker
exports.CookieTracker.prototype.trackCookie = function(cookie) {
  // ignore the cookie if we aren't armed
  if (!this.armed) return;
  // if it is new, count how many old workers can access it
  if (this.cookieHash[this.cookieToKey(cookie)] == undefined) {
    this.cookieHash[this.cookieToKey(cookie)] = this.countWorkers(cookie);
  }

  //console.debug("track "+cookie.host+": "+this.cookieHash[this.cookieToKey(cookie)]);

  // already expired?
  if (this.isExpired(cookie, this.cookieHash[this.cookieToKey(cookie)])) {
    this.scheduleExpiration(cookie, true);
  }
}

// no longer track a cookie
exports.CookieTracker.prototype.dropCookie = function(cookie) {
  // ignore the cookie if we aren't armed
  if (!this.armed) return;
  //console.debug("drop "+cookie.host+": "+this.cookieHash[this.cookieToKey(cookie)]);
  this.cancelExpiration(cookie);
  delete this.cookieHash[this.cookieToKey(cookie)];
}

// adjust refcounts for a new worker
exports.CookieTracker.prototype.incRefs = function(url, style) {
    //console.debug("incRefs "+url.host);
    if (url.host == null) return;

    // count a new worker for this url
    this.domainTree.incRefs(url.host, style, 1);

    // if we aren't armed, we only track domains, not cookies
    if (!this.armed) return;

    // find all of the host's cookies
    var e = cookieManager.getCookiesFromHost(url.host);
    while (e.hasMoreElements()) {
        var c = e.getNext().QueryInterface(Ci.nsICookie2);
        // getcookiesfromhost may be too coarse, filter further
        if (this.canAccess(url.host, c)) {
            if (!this.knowsCookie(c)) {
                // this will already count the new host
                this.trackCookie(c);
            } else {
                this.cookieHash[this.cookieToKey(c)][style] += 1;
            }
            //console.debug("-> "+c.host+": "+this.cookieHash[this.cookieToKey(c)]);

            // check if pending expirations must be cancelled
            if (!this.isExpired(c, this.cookieHash[this.cookieToKey(c)])) {
                this.cancelExpiration(c);
            }
        }
    }

    // process the localstorage pseudo-cookie if it exists for this host
    if (this.knowsCookie(this.localStorageCookie(url.host))) {
      var c = this.localStorageCookie(url.host);
      this.cookieHash[this.cookieToKey(c)][style] += 1;

      if (!this.isExpired(c, this.cookieHash[this.cookieToKey(c)])) {
        this.cancelExpiration(c);
      }
    }
}

// lower refcounts after a worker has left
exports.CookieTracker.prototype.decRefs = function(url, style) {
    //console.debug("decRefs "+url.host);
    if (url.host == null || this.domainTree.get(url.host) == undefined) return;

    this.domainTree.decRefs(url.host, style, 1);
    // removing will be a noop if the domain still has references or children
    this.domainTree.remove(url.host);

    // if we aren't armed, we only track domains, not cookies
    if (!this.armed) return;

    // find all of the host's cookies
    var e = cookieManager.getCookiesFromHost(url.host);
    while (e.hasMoreElements()) {
        var c = e.getNext().QueryInterface(Ci.nsICookie2);
        // getcookiesfromhost may be too coarse, filter further
        if (this.canAccess(url.host, c)) {
            if (this.knowsCookie(c)) {
                this.cookieHash[this.cookieToKey(c)][style] -= 1;
                //console.debug("-> "+c.host+": "+this.cookieHash[this.cookieToKey(c)]);
                if (this.cookieHash[this.cookieToKey(c)][style] < 0) {
                  // this should never happen
                  console.error("NEGATIVE REFCOUNT: " + c.host + ": " + this.cookieHash[this.cookieToKey(c)]);
                }

                // expire if necessary
                if (this.isExpired(c, this.cookieHash[this.cookieToKey(c)])) {
                    this.scheduleExpiration(c, (this.cookieHash[this.cookieToKey(c)][STYLE_FRAME] > 0 ? true : false));
                }
            } else {
                // not actually serious, but unexpected
                console.debug("unknown cookie: " + c.host);
            }
        }
    }

    // process the localstorage pseudo-cookie if it exists for this host
    if (this.knowsCookie(this.localStorageCookie(url.host))) {
      var c = this.localStorageCookie(url.host);
      this.cookieHash[this.cookieToKey(c)][style] -= 1;

      if (this.isExpired(c, this.cookieHash[this.cookieToKey(c)])) {
        this.scheduleExpiration(c, (this.cookieHash[this.cookieToKey(c)][STYLE_FRAME] > 0 ? true : false));
      }
    }
}

// reset tracker state, cancels pending expirations
exports.CookieTracker.prototype.clear = function(cleardomains) {
    //console.debug("clear");
    for (var e in this.expirationHandles) {
        timers.clearTimeout(this.expirationHandles[e]);
    }
    this.expirationHandles = {};
    this.cookieHash = {};
    if (cleardomains) this.domainTree = new DomainTree();
}

// set a new access policy and clears all tracked cookies and expirations
exports.CookieTracker.prototype.setCanAccess = function(cafunc) {
    this.canAccess = cafunc;
    this.clear(false);
}

// update the grace period
exports.CookieTracker.prototype.setGracePeriod = function(t) {
    // grace period must be at least 1 second and not too long
    this.gracePeriod = Math.min(t, 1000);
    this.gracePeriod = Math.max(this.gracePeriod, 1);
}

// arm or disarm the tracker
exports.CookieTracker.prototype.setArmed = function(state) {
  this.armed = state;
  // untrack all cookies and cancel all expirations if we are no longer armed
  if (!state) this.clear(false);
}

// do we track this cookie?
exports.CookieTracker.prototype.knowsCookie = function(cookie) {
    return (this.cookieHash[this.cookieToKey(cookie)] != undefined);
}

// generate a pseudo-cookie for a domain's localstorage
exports.CookieTracker.prototype.localStorageCookie = function(domain) {
  var bd = domain;
  try {
    bd = "." + eTLDService.getBaseDomainFromHost((domain.startsWith(".") ? domain.substr(1) : domain), 0);
  } catch (e) {
    // fall back to the domain itself
    // this space intentionally left blank
  }
  var cookie = {
    host: bd,
    name: "localstorage",
    path: "",
    localStorage: true
  };

  return cookie;
}

// generate a hash key for a cookie
exports.CookieTracker.prototype.cookieToKey = function(cookie) {
    return (cookie.localStorage ? "l;" : "c;") + cookie.host + ";" + cookie.path +";" + cookie.name;
}

// count the number of workers that can access the cookie
exports.CookieTracker.prototype.countWorkers = function(cookie) {
    var cnt = [0, 0]
    // start looking from the base domain, sufficient for strict and relaxed access policies
    var bd = cookie.host.startsWith(".") ? cookie.host.substr(1) : cookie.host;
    try {
        bd = eTLDService.getBaseDomainFromHost(bd, 0);
    } catch (e) {
        // fall back to the domain itself
        // this space intentionally left blank
        // FIXME we really should start from the tld instead
    }

    var hosts = this.domainTree.allSubDomains(bd);

    for (var h in hosts) {
        if (this.canAccess(hosts[h], cookie)) {
            var refs = this.domainTree.getRefCounts(hosts[h]);
            cnt[STYLE_TOP] += refs[STYLE_TOP];
            cnt[STYLE_FRAME] += refs[STYLE_FRAME];
        }
    }

    return cnt;
}

// the cookie can now expire after the grace period
exports.CookieTracker.prototype.scheduleExpiration = function(cookie, tracking) {
    var handler = this.onExpired;
    var key = this.cookieToKey(cookie);
    var handles = this.expirationHandles;
    this.cancelExpiration(cookie);
    this.expirationHandles[key] = timers.setTimeout(function(){delete handles[key]; handler(cookie, tracking, false)}, this.gracePeriod * 1000);
}

// cookie was used during the grace period
exports.CookieTracker.prototype.cancelExpiration = function(cookie) {
    if (this.expirationHandles[this.cookieToKey(cookie)] != undefined) {
        timers.clearTimeout(this.expirationHandles[this.cookieToKey(cookie)]);
        //console.debug("cancel expiration: "+this.cookieToKey(cookie));
        delete this.expirationHandles[this.cookieToKey(cookie)];
    }
}

// statistics for debugging etc.
exports.CookieTracker.prototype.collectStatistics = function() {
    var countProps = function(x, filter) {
      var cnt = 0;
      for (var i in x) {
        if (filter) {
          if (i.startsWith(filter)) cnt++;
        } else {
          cnt++;
        }
      }
      return cnt;
    }

    var stats = {
        cookies: countProps(this.cookieHash, "c"),
        scopes: countProps(this.cookieHash, "l"),
        domains: this.domainTree.countDomains(),
        expiring: countProps(this.expirationHandles),
        toString: function(){ return "Domains: "+this.domains+"  Cookies: "+this.cookies+"  Scopes: "+this.scopes+"  Expiring: "+this.expiring }
    }

    return stats;
}