DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d1ed7de67f5a)

VCS Links

Line Code
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

/* global Components, Services, dump, AppsUtils, NetUtil, XPCOMUtils */

"use strict";

const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const signatureFileExtension = ".sig";

this.EXPORTED_SYMBOLS = ["TrustedHostedAppsUtils"];

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

XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
  "resource://gre/modules/NetUtil.jsm");

#ifdef MOZ_WIDGET_ANDROID
// On Android, define the "debug" function as a binding of the "d" function
// from the AndroidLog module so it gets the "debug" priority and a log tag.
// We always report debug messages on Android because it's unnecessary
// to restrict reporting, per bug 1003469.
let debug = Cu
  .import("resource://gre/modules/AndroidLog.jsm", {})
  .AndroidLog.d.bind(null, "TrustedHostedAppsUtils");
#else
// Elsewhere, report debug messages only if dom.mozApps.debug is set to true.
// The pref is only checked once, on startup, so restart after changing it.
let debug = Services.prefs.getBoolPref("dom.mozApps.debug") ?
  aMsg => dump("-*- TrustedHostedAppsUtils.jsm : " + aMsg + "\n") :
  () => {};
#endif

/**
 * Verification functions for Trusted Hosted Apps.
 */
this.TrustedHostedAppsUtils = {

  /**
   * Check if the given host is pinned in the CA pinning database.
   */
  isHostPinned: function (aUrl) {
    let uri;
    try {
      uri = Services.io.newURI(aUrl, null, null);
    } catch(e) {
      debug("Host parsing failed: " + e);
      return false;
    }

    // TODO: use nsSiteSecurityService.isSecureURI()
    if (!uri.host || "https" != uri.scheme) {
      return false;
    }

    // Check certificate pinning
    let siteSecurityService;
    try {
      siteSecurityService = Cc["@mozilla.org/ssservice;1"]
        .getService(Ci.nsISiteSecurityService);
    } catch (e) {
      debug("nsISiteSecurityService error: " + e);
      // unrecoverable error, don't bug the user
      throw "CERTDB_ERROR";
    }

    if (siteSecurityService.isSecureHost(Ci.nsISiteSecurityService.HEADER_HPKP,
                                         uri.host, 0)) {
      debug("\tvalid certificate pinning for host: " + uri.host + "\n");
      return true;
    }

    debug("\tHost NOT pinned: " + uri.host + "\n");
    return false;
  },

  /**
   * Take a CSP policy string as input and ensure that it contains at
   * least the directives that are required ('script-src' and
   * 'style-src').  If the CSP policy string is 'undefined' or does
   * not contain some of the required csp directives the function will
   * return empty list with status set to false.  Otherwise a parsed
   * list of the unique sources listed from the required csp
   * directives is returned.
   */
  getCSPWhiteList: function(aCsp) {
    let isValid = false;
    let whiteList = [];
    let requiredDirectives = [ "script-src", "style-src" ];

    if (aCsp) {
      let validDirectives = [];
      let directives = aCsp.split(";");
      // TODO: Use nsIContentSecurityPolicy
      directives
        .map(aDirective => aDirective.trim().split(" "))
        .filter(aList => aList.length > 1)
        // we only restrict on requiredDirectives
        .filter(aList => (requiredDirectives.indexOf(aList[0]) != -1))
        .forEach(aList => {
          // aList[0] contains the directive name.
          // aList[1..n] contains sources.
          let directiveName = aList.shift();
          let sources = aList;

          if ((-1 == validDirectives.indexOf(directiveName))) {
            validDirectives.push(directiveName);
          }
          whiteList.push(...sources.filter(
             // 'self' is checked separately during manifest check
            aSource => (aSource !="'self'" && whiteList.indexOf(aSource) == -1)
          ));
        });

      // Check if all required directives are present.
      isValid = requiredDirectives.length === validDirectives.length;

      if (!isValid) {
        debug("White list doesn't contain all required directives!");
        whiteList = [];
      }
    }

    debug("White list contains " + whiteList.length + " hosts");
    return { list: whiteList, valid: isValid };
  },

  /**
   * Verify that the given csp is valid:
   *  1. contains required directives "script-src" and "style-src"
   *  2. required directives contain only "https" URLs
   *  3. domains of the restricted sources exist in the CA pinning database
   */
  verifyCSPWhiteList: function(aCsp) {
    let domainWhitelist = this.getCSPWhiteList(aCsp);
    if (!domainWhitelist.valid) {
      debug("TRUSTED_APPLICATION_WHITELIST_PARSING_FAILED");
      return false;
    }

    if (!domainWhitelist.list.every(aUrl => this.isHostPinned(aUrl))) {
      debug("TRUSTED_APPLICATION_WHITELIST_VALIDATION_FAILED");
      return false;
    }

    return true;
  },

  _verifySignedFile: function(aManifestStream, aSignatureStream, aCertDb) {
    let deferred = Promise.defer();

    let root = Ci.nsIX509CertDB.TrustedHostedAppPublicRoot;
    try {
      // Check if we should use the test certificates.
      // Please note that this should be changed if we ever allow chages to the
      // prefs since that would create a way for an attacker to use the test
      // root for real apps.
      let useTrustedAppTestCerts = Services.prefs
        .getBoolPref("dom.mozApps.use_trustedapp_test_certs");
      if (useTrustedAppTestCerts) {
        root = Ci.nsIX509CertDB.TrustedHostedAppTestRoot;
      }
    } catch (ex) { }

    aCertDb.verifySignedManifestAsync(
      root, aManifestStream, aSignatureStream,
      function(aRv, aCert) {
        debug("Signature verification returned code, cert & root: " + aRv + " " + aCert + " " + root);
        if (Components.isSuccessCode(aRv)) {
          deferred.resolve(aCert);
        } else if (aRv == Cr.NS_ERROR_FILE_CORRUPTED ||
                   aRv == Cr.NS_ERROR_SIGNED_MANIFEST_FILE_INVALID) {
          deferred.reject("MANIFEST_SIGNATURE_FILE_INVALID");
        } else {
          deferred.reject("MANIFEST_SIGNATURE_VERIFICATION_ERROR");
        }
      }
    );

    return deferred.promise;
  },

  verifySignedManifest: function(aApp, aAppId) {
    let deferred = Promise.defer();

    let certDb;
    try {
      certDb = Cc["@mozilla.org/security/x509certdb;1"]
                 .getService(Ci.nsIX509CertDB);
    } catch (e) {
      debug("nsIX509CertDB error: " + e);
      // unrecoverable error, don't bug the user
      throw "CERTDB_ERROR";
    }

    let mRequestChannel = NetUtil.newChannel(aApp.manifestURL)
                                 .QueryInterface(Ci.nsIHttpChannel);
    mRequestChannel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
    mRequestChannel.notificationCallbacks =
      AppsUtils.createLoadContext(aAppId, false);

    // The manifest signature must be located at the same path as the
    // manifest and have the same file name, only the file extension
    // should differ. Any fragment or query parameter will be ignored.
    let signatureURL;
    try {
      let mURL = Cc["@mozilla.org/network/io-service;1"]
        .getService(Ci.nsIIOService)
        .newURI(aApp.manifestURL, null, null)
        .QueryInterface(Ci.nsIURL);
      signatureURL = mURL.prePath +
        mURL.directory + mURL.fileBaseName + signatureFileExtension;
    } catch(e) {
      deferred.reject("SIGNATURE_PATH_INVALID");
      return;
    }

    let sRequestChannel = NetUtil.newChannel(signatureURL)
      .QueryInterface(Ci.nsIHttpChannel);
    sRequestChannel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
    sRequestChannel.notificationCallbacks =
      AppsUtils.createLoadContext(aAppId, false);
    let getAsyncFetchCallback = (resolve, reject) =>
        (aInputStream, aResult) => {
          if (!Components.isSuccessCode(aResult)) {
            debug("Failed to download file");
            reject("MANIFEST_FILE_UNAVAILABLE");
            return;
          }
          resolve(aInputStream);
        };

    Promise.all([
      new Promise((resolve, reject) => {
        NetUtil.asyncFetch(mRequestChannel,
                           getAsyncFetchCallback(resolve, reject));
      }),
      new Promise((resolve, reject) => {
        NetUtil.asyncFetch(sRequestChannel,
                           getAsyncFetchCallback(resolve, reject));
      })
    ]).then(([aManifestStream, aSignatureStream]) => {
      this._verifySignedFile(aManifestStream, aSignatureStream, certDb)
        .then(deferred.resolve, deferred.reject);
    }, deferred.reject);

    return deferred.promise;
  },

  verifyManifest: function(aData) {
    return new Promise((resolve, reject) => {
      // sanity check on manifest host's CA (proper CA check with
      // pinning is done by regular networking code)
      if (!this.isHostPinned(aData.app.manifestURL)) {
        reject("TRUSTED_APPLICATION_HOST_CERTIFICATE_INVALID");
        return;
      }
      if (!this.verifyCSPWhiteList(aData.app.manifest.csp)) {
        reject("TRUSTED_APPLICATION_WHITELIST_VALIDATION_FAILED");
        return;
      }
      this.verifySignedManifest(aData.app, aData.appId).then(resolve, reject);
    });
  }
};