This file is indexed.

/usr/share/xul-ext/monkeysphere/modules/monkeysphere.jsm is in xul-ext-monkeysphere 0.8-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
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
// -*-js2-*-
// Monkeysphere XUL extension module
// Copyright © 2010 Jameson Rollins <jrollins@finestructure.net>,
//                  Daniel Kahn Gillmor <dkg@fifthhorseman.net>,
//                  mike castleman <m@mlcastle.net>,
//                  Matthew James Goins <mjgoins@openflows.com>
//
// 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.

var EXPORTED_SYMBOLS = [
  "agent_socket",
  "log",
  "elog",
  "isRelevantURI",
  "setStatus",
  "createAgentPostData",
  "getInvalidCert",
  "overrides"
];

////////////////////////////////////////////////////////////
// PREFERENCES AND ENVIRONMENT
////////////////////////////////////////////////////////////

// preferences, in about:config
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.monkeysphere.");

// select agent URL from environment variable or explicitly-set preference.
// "http://localhost:8901" <-- NO TRAILING SLASH
var agent_socket = function() {
  var envvar = "MONKEYSPHERE_VALIDATION_AGENT_SOCKET";;
  try {
    envvar = prefs.getCharPref("validation_agent_socket_environment_variable");
  } catch (e) {
    log("falling back to built-in environment variable: " + envvar);
  }
  log("using environment variable " + envvar);
  // get the agent URL from the environment
  // https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIEnvironment
  var ret = Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get(envvar);
  // return error if agent URL not set
  if(!ret) {
    ret = "http://localhost:8901";;
    try {
      ret = prefs.getCharPref("default_socket");
    } catch (e) {
      log("falling back to built-in default socket location: " + ret);
    }

    log(envvar + " environment variable not set.  Using default of " + ret);
  }
  // replace trailing slashes
  ret = ret.replace(/\/*$/, '');
  log("agent socket: " + ret);

  return ret;
};

////////////////////////////////////////////////////////////
// LOG FUNCTIONS
////////////////////////////////////////////////////////////

var loglevels = {
  silent: 0,
  quiet: 0.25,
  fatal: 0.5,
  error: 1,
  info: 2,
  verbose: 3,
  debug: 4,
  debug1: 4,
  debug2: 5,
  debug3: 6
};

var log = function(line) {
  return mslog('info', line);
};

//////////////////////////////////////////////////////////
var mslog = function(level, line) {
  var syslevel = prefs.getCharPref("log_level").toLowerCase();

  level = loglevels[level];
  syslevel = loglevels[syslevel];
  if (typeof syslevel == 'undefined' ) {
    syslevel = loglevels['info'];
  }

  if(syslevel < level) {
    return;
  }

  try {
    var message = "monkeysphere: " + line;

    dump(message + "\n");
    try {
      // this line works in extensions
      Firebug.Console.log(message);
    } catch(e) {
      // ignore, this will blow up if Firebug is not installed
    }
    try {
      console.log(message); // this line works in HTML files
    } catch(e) {
      // ignore, this will blow up if Firebug is not installed
    }
  } catch(e) {
    alert(e);
  }
};

var objdump = function(obj) {
  for (var key in obj) {
    log("dump: " + key + " = " + obj[key]);
  }
};

////////////////////////////////////////////////////////////
// SITE URI CHECK FUNCTION
////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////
// check uri is relevant to monkeysphere
var isRelevantURI = function(uri) {
  ////////////////////////////////////////
  // check host
  try {
    var host = uri.host;
  } catch(e) {
    log("host data empty.");
    return null;
  }

  ////////////////////////////////////////
  // check scheme
  try {
    var scheme = uri.scheme;
  } catch(e) {
    log("scheme data empty.");
    return null;
  }

  log("url: " + uri.asciiSpec);

  ////////////////////////////////////////
  // check if scheme is https
  if(scheme != "https") {
    log("scheme not https.");
    return null;
  }

  // if uri is relevant for monkeysphere return true
  return true;
};

////////////////////////////////////////////////////////////
// STATUS FUNCTIONS
////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////
// set site monkeysphere status
var setStatus = function(browser, state, message) {
  if ( typeof message === 'undefined' ) {
    message = "";
    log("set browser status: " + state);
  } else {
    log("set browser status: " + state + ', ' + message);
  }
  browser.monkeysphere = { state: state, message: message };
};

//////////////////////////////////////////////////////////
// clear site monkeysphere status for browser
var clearStatus = function(browser) {
  log("clear browser status");
  delete browser.monkeysphere;
};

////////////////////////////////////////////////////////////
// AGENT POST DATA FUNCTION
////////////////////////////////////////////////////////////

var createAgentPostData = function(uri, cert) {
  // get certificate info
  var cert_length = {};
  var dummy = {};
  var cert_data = cert.getRawDER(cert_length, dummy);

  // "agent post data"
  var apd = {
    uri: uri,
    cert: cert,
    data: {
      context: uri.scheme,
      peer: uri.hostPort,
      pkc: {
        type: "x509der",
        data: cert_data
      }
    },
    toJSON: function() {
      return JSON.stringify(this.data);
    },
    toOverrideLabel: function() {
      return this.data.context + '|' + this.data.peer + '|' + this.data.pkc.type + '|' + this.data.pkc.data;
    },
    log: function() {
      log("agent post data:");
      log("  context: " + this.data.context);
      log("  peer: " + this.data.peer);
      log("  pkc.type: " + this.data.pkc.type);
      //log("  pkc.data: " + this.data.pkc.data); // this can be big
      //log("  JSON: " + this.toJSON());
    }
  };

  return apd;
};

////////////////////////////////////////////////////////////
// CERT FUNCTIONS
////////////////////////////////////////////////////////////

// certificate override service class
// http://www.oxymoronical.com/experiments/xpcomref/applications/Firefox/3.5/interfaces/nsICertOverrideService
var certOverrideService = Components.classes["@mozilla.org/security/certoverride;1"].getService(Components.interfaces.nsICertOverrideService);

//////////////////////////////////////////////////////////
// FWIW, aWebProgress listener has:
// securityUI = [xpconnect wrapped (nsISupports, nsISecureBrowserUI, nsISSLStatusProvider)]
// but i don't think it can be used because it doesn't hold invalid cert info
// FIXME: is there a better way to get the cert for the actual current connection?
var getInvalidCert = function(uri) {
  try {
    var cert = getInvalidCertSSLStatus(uri).QueryInterface(Components.interfaces.nsISSLStatus).serverCert;
    printCertInfo(cert);
    return cert;
  } catch(e) {
    return null;
  }
};

//////////////////////////////////////////////////////////
// gets current ssl status info
// http://www.oxymoronical.com/experiments/apidocs/interface/nsIRecentBadCertsService
var getInvalidCertSSLStatus = function(uri) {
  var recentCertsService
  if (typeof Components.classes["@mozilla.org/security/recentbadcerts;1"]
      !== "undefined") { 
  recentCertsService =
    Components.classes["@mozilla.org/security/recentbadcerts;1"].getService(Components.interfaces.nsIRecentBadCertsService);
  } else if (typeof Components.classes["@mozilla.org/security/x509certdb;1"]
             !== "undefined") {
    // new method for firefox 20
    var certDB = Components.classes["@mozilla.org/security/x509certdb;1"]
    	         .getService(Components.interfaces.nsIX509CertDB);
    if (!certDB)
       return null;

    var privateBrowsingEnabled = false;

    if(typeof Components.classes["@mozilla.org/security/privatebrowsing;1"]
             !== "undefined")
    {
        var pbs = Components.classes["@mozilla.org/privatebrowsing;1"]
             .getService(Components.interfaces.nsIPrivateBrowsingService);
        privateBrowsingEnabled = pbs.privateBrowsingEnabled;
    }
    else
    {
        // Support the per-window based private mode
        // https://developer.mozilla.org/EN/docs/Supporting_per-window_private_browsing
        // This needs the window argument passed down, for now disabling private mode
        //Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
        //privateBrowsingEnabled = PrivateBrowsingUtils.isWindowPrivate(window);
        privateBrowsingEnabled = false;
    }
    recentCertsService = certDB.getRecentBadCerts(privateBrowsingEnabled);
  } else {
     // they updated firefox method to get certs again
     log("error: No way to get invalid cert status!");
     recentCertsService = null;
  } 
  if (!recentCertsService)
    return null;

  var port = uri.port;
  if(port == -1)
    port = 443;
  var hostWithPort = uri.host + ":" + port;

  var SSLStatus = recentCertsService.getRecentBadCert(hostWithPort);
  if (!SSLStatus)
    return null;

  return SSLStatus;
};

//////////////////////////////////////////////////////////
// Print SSL certificate details
// https://developer.mozilla.org/En/How_to_check_the_security_state_of_an_XMLHTTPRequest_over_SSL
var printCertInfo = function(cert) {
  const Ci = Components.interfaces;

  log("certificate:");

  if(cert.isDomainMismatch)
      log("SSL Status: Domain mismatch");

  if(cert.isNotValidAtThisTime)
      log("SSL Status: Not valid at this time");

  if(cert.isUntrusted)
      log("SSL Status: Untrusted");

  log("  Common Name: " + cert.commonName);
  log("  Organisation: " + cert.organization);
  log("  Issuer: " + cert.issuerOrganization);
  log("  SHA1 fingerprint: " + cert.sha1Fingerprint);

  var validity = cert.validity.QueryInterface(Ci.nsIX509CertValidity);
  log("  Valid from: " + validity.notBeforeGMT);
  log("  Valid until: " + validity.notAfterGMT);
};

////////////////////////////////////////////////////////////
// OVERRIDE CACHE OBJECT
////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////
// object to store and retrieve data about monkeysphere status for sites
// uses string of apd as key, and agent response as data
var overrides = (
  function() {
    // response cache object
    var responses = {};

    return {

      // set override
      set: function(apd, agentResponse) {
        log("**** SET OVERRIDE ****");

        var uri = apd.uri;

        var cert = apd.cert;

        var SSLStatus = getInvalidCertSSLStatus(uri);
        var overrideBits = 0;

        // set override bits
        // FIXME: should this just be for all flags by default?
        if(SSLStatus.isUntrusted) {
          log("flag: ERROR_UNTRUSTED");
          overrideBits |= certOverrideService.ERROR_UNTRUSTED;
        }
        if(SSLStatus.isDomainMismatch) {
          log("flag: ERROR_MISMATCH");
          overrideBits |= certOverrideService.ERROR_MISMATCH;
        }
        if(SSLStatus.isNotValidAtThisTime) {
          log("flag: ERROR_TIME");
          overrideBits |= certOverrideService.ERROR_TIME;
        }

        log("overrideBits: " + overrideBits);

        log("set cert override: " + uri.asciiHost + ":" + uri.port);
        certOverrideService.rememberValidityOverride(uri.asciiHost,
                                                     uri.port,
                                                     cert,
                                                     overrideBits,
                                                     true);

        log("setting cache");
        apd.log();
        responses[apd.toOverrideLabel()] = agentResponse;
      },

      // return response object
      response: function(apd) {
        return responses[apd.toOverrideLabel()];
      },

      // return override status as bool, true for override set
      certStatus: function(apd) {
        var uri = apd.uri;
        var aHashAlg = {};
        var aFingerprint = {};
        var aOverrideBits = {};
        var aIsTemporary = {};
        return certOverrideService.getValidityOverride(uri.asciiHost,
                                                       uri.port,
                                                       aHashAlg,
                                                       aFingerprint,
                                                       aOverrideBits,
                                                       aIsTemporary);
      },

      // clear override
      clear: function(apd) {
        log("**** CLEAR OVERRIDE ****");
        var uri = apd.uri;
        log("clearing cert override");
        certOverrideService.clearValidityOverride(uri.asciiHost, uri.port);
        log("clearing cache");
        apd.log();
        delete responses[apd.toOverrideLabel()];
      }
    };
  }
)();