summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/webextensions/AddonContentPolicy.cpp
blob: 90e53b2ea8bd8a06f1654155e6cbe57ba634f44c (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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "AddonContentPolicy.h"

#include "mozilla/dom/nsCSPUtils.h"
#include "nsCOMPtr.h"
#include "nsContentPolicyUtils.h"
#include "nsContentTypeParser.h"
#include "nsContentUtils.h"
#include "nsIConsoleService.h"
#include "nsIContentSecurityPolicy.h"
#include "nsIContent.h"
#include "nsIDocument.h"
#include "nsIEffectiveTLDService.h"
#include "nsIScriptError.h"
#include "nsIStringBundle.h"
#include "nsIUUIDGenerator.h"
#include "nsIURI.h"
#include "nsNetCID.h"
#include "nsNetUtil.h"

using namespace mozilla;

/* Enforces content policies for WebExtension scopes. Currently:
 *
 *  - Prevents loading scripts with a non-default JavaScript version.
 *  - Checks custom content security policies for sufficiently stringent
 *    script-src and object-src directives.
 */

#define VERSIONED_JS_BLOCKED_MESSAGE \
  u"Versioned JavaScript is a non-standard, deprecated extension, and is " \
  u"not supported in WebExtension code. For alternatives, please see: " \
  u"https://developer.mozilla.org/Add-ons/WebExtensions/Tips"

AddonContentPolicy::AddonContentPolicy()
{
}

AddonContentPolicy::~AddonContentPolicy()
{
}

NS_IMPL_ISUPPORTS(AddonContentPolicy, nsIContentPolicy, nsIAddonContentPolicy)

static nsresult
GetWindowIDFromContext(nsISupports* aContext, uint64_t *aResult)
{
  NS_ENSURE_TRUE(aContext, NS_ERROR_FAILURE);

  nsCOMPtr<nsIContent> content = do_QueryInterface(aContext);
  NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);

  nsCOMPtr<nsIDocument> document = content->OwnerDoc();
  NS_ENSURE_TRUE(document, NS_ERROR_FAILURE);

  nsCOMPtr<nsPIDOMWindowInner> window = document->GetInnerWindow();
  NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);

  *aResult = window->WindowID();
  return NS_OK;
}

static nsresult
LogMessage(const nsAString &aMessage, nsIURI* aSourceURI, const nsAString &aSourceSample,
           nsISupports* aContext)
{
  nsCOMPtr<nsIScriptError> error = do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
  NS_ENSURE_TRUE(error, NS_ERROR_OUT_OF_MEMORY);

  nsCString sourceName = aSourceURI->GetSpecOrDefault();

  uint64_t windowID = 0;
  GetWindowIDFromContext(aContext, &windowID);

  nsresult rv =
    error->InitWithWindowID(aMessage, NS_ConvertUTF8toUTF16(sourceName),
                            aSourceSample, 0, 0, nsIScriptError::errorFlag,
                            "JavaScript", windowID);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIConsoleService> console = do_GetService(NS_CONSOLESERVICE_CONTRACTID);
  NS_ENSURE_TRUE(console, NS_ERROR_OUT_OF_MEMORY);

  console->LogMessage(error);
  return NS_OK;
}


// Content policy enforcement:

NS_IMETHODIMP
AddonContentPolicy::ShouldLoad(uint32_t aContentType,
                               nsIURI* aContentLocation,
                               nsIURI* aRequestOrigin,
                               nsISupports* aContext,
                               const nsACString& aMimeTypeGuess,
                               nsISupports* aExtra,
                               nsIPrincipal* aRequestPrincipal,
                               int16_t* aShouldLoad)
{
  MOZ_ASSERT(aContentType == nsContentUtils::InternalContentPolicyTypeToExternal(aContentType),
             "We should only see external content policy types here.");

  *aShouldLoad = nsIContentPolicy::ACCEPT;

  if (!aRequestOrigin) {
    return NS_OK;
  }

  // Only apply this policy to requests from documents loaded from
  // moz-extension URLs, or to resources being loaded from moz-extension URLs.
  bool equals;
  if (!((NS_SUCCEEDED(aContentLocation->SchemeIs("moz-extension", &equals)) && equals) ||
        (NS_SUCCEEDED(aRequestOrigin->SchemeIs("moz-extension", &equals)) && equals))) {
    return NS_OK;
  }

  if (aContentType == nsIContentPolicy::TYPE_SCRIPT) {
    NS_ConvertUTF8toUTF16 typeString(aMimeTypeGuess);
    nsContentTypeParser mimeParser(typeString);

    // Reject attempts to load JavaScript scripts with a non-default version.
    nsAutoString mimeType, version;
    if (NS_SUCCEEDED(mimeParser.GetType(mimeType)) &&
        nsContentUtils::IsJavascriptMIMEType(mimeType) &&
        NS_SUCCEEDED(mimeParser.GetParameter("version", version))) {
      *aShouldLoad = nsIContentPolicy::REJECT_REQUEST;

      LogMessage(NS_MULTILINE_LITERAL_STRING(VERSIONED_JS_BLOCKED_MESSAGE),
                 aRequestOrigin, typeString, aContext);
      return NS_OK;
    }
  }

  return NS_OK;
}

NS_IMETHODIMP
AddonContentPolicy::ShouldProcess(uint32_t aContentType,
                                  nsIURI* aContentLocation,
                                  nsIURI* aRequestOrigin,
                                  nsISupports* aRequestingContext,
                                  const nsACString& aMimeTypeGuess,
                                  nsISupports* aExtra,
                                  nsIPrincipal* aRequestPrincipal,
                                  int16_t* aShouldProcess)
{
  MOZ_ASSERT(aContentType == nsContentUtils::InternalContentPolicyTypeToExternal(aContentType),
             "We should only see external content policy types here.");

  *aShouldProcess = nsIContentPolicy::ACCEPT;
  return NS_OK;
}


// CSP Validation:

static const char* allowedSchemes[] = {
  "blob",
  "filesystem",
  nullptr
};

static const char* allowedHostSchemes[] = {
  "https",
  "moz-extension",
  nullptr
};

/**
 * Validates a CSP directive to ensure that it is sufficiently stringent.
 * In particular, ensures that:
 *
 *  - No remote sources are allowed other than from https: schemes
 *
 *  - No remote sources specify host wildcards for generic domains
 *    (*.blogspot.com, *.com, *)
 *
 *  - All remote sources and local extension sources specify a host
 *
 *  - No scheme sources are allowed other than blob:, filesystem:,
 *    moz-extension:, and https:
 *
 *  - No keyword sources are allowed other than 'none', 'self', 'unsafe-eval',
 *    and hash sources.
 */
class CSPValidator final : public nsCSPSrcVisitor {
  public:
    CSPValidator(nsAString& aURL, CSPDirective aDirective, bool aDirectiveRequired = true) :
      mURL(aURL),
      mDirective(CSP_CSPDirectiveToString(aDirective)),
      mFoundSelf(false)
    {
      // Start with the default error message for a missing directive, since no
      // visitors will be called if the directive isn't present.
      if (aDirectiveRequired) {
        FormatError("csp.error.missing-directive");
      }
    }

    // Visitors

    bool visitSchemeSrc(const nsCSPSchemeSrc& src) override
    {
      nsAutoString scheme;
      src.getScheme(scheme);

      if (SchemeInList(scheme, allowedHostSchemes)) {
        FormatError("csp.error.missing-host", scheme);
        return false;
      }
      if (!SchemeInList(scheme, allowedSchemes)) {
        FormatError("csp.error.illegal-protocol", scheme);
        return false;
      }
      return true;
    };

    bool visitHostSrc(const nsCSPHostSrc& src) override
    {
      nsAutoString scheme, host;

      src.getScheme(scheme);
      src.getHost(host);

      if (scheme.LowerCaseEqualsLiteral("https")) {
        if (!HostIsAllowed(host)) {
          FormatError("csp.error.illegal-host-wildcard", scheme);
          return false;
        }
      } else if (scheme.LowerCaseEqualsLiteral("moz-extension")) {
        // The CSP parser silently converts 'self' keywords to the origin
        // URL, so we need to reconstruct the URL to see if it was present.
        if (!mFoundSelf) {
          nsAutoString url(u"moz-extension://");
          url.Append(host);

          mFoundSelf = url.Equals(mURL);
        }

        if (host.IsEmpty() || host.EqualsLiteral("*")) {
          FormatError("csp.error.missing-host", scheme);
          return false;
        }
      } else if (!SchemeInList(scheme, allowedSchemes)) {
        FormatError("csp.error.illegal-protocol", scheme);
        return false;
      }

      return true;
    };

    bool visitKeywordSrc(const nsCSPKeywordSrc& src) override
    {
      switch (src.getKeyword()) {
      case CSP_NONE:
      case CSP_SELF:
      case CSP_UNSAFE_EVAL:
        return true;

      default:
        NS_ConvertASCIItoUTF16 keyword(CSP_EnumToKeyword(src.getKeyword()));

        FormatError("csp.error.illegal-keyword", keyword);
        return false;
      }
    };

    bool visitNonceSrc(const nsCSPNonceSrc& src) override
    {
      FormatError("csp.error.illegal-keyword", NS_LITERAL_STRING("'nonce-*'"));
      return false;
    };

    bool visitHashSrc(const nsCSPHashSrc& src) override
    {
      return true;
    };

    // Accessors

    inline nsAString& GetError()
    {
      return mError;
    };

    inline bool FoundSelf()
    {
      return mFoundSelf;
    };


    // Formatters

    template <typename... T>
    inline void FormatError(const char* aName, const T ...aParams)
    {
      const char16_t* params[] = { mDirective.get(), aParams.get()... };
      FormatErrorParams(aName, params, MOZ_ARRAY_LENGTH(params));
    };

  private:
    // Validators

    bool HostIsAllowed(nsAString& host)
    {
      if (host.First() == '*') {
        if (host.EqualsLiteral("*") || host[1] != '.') {
          return false;
        }

        host.Cut(0, 2);

        nsCOMPtr<nsIEffectiveTLDService> tldService =
          do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);

        if (!tldService) {
          return false;
        }

        NS_ConvertUTF16toUTF8 cHost(host);
        nsAutoCString publicSuffix;

        nsresult rv = tldService->GetPublicSuffixFromHost(cHost, publicSuffix);

        return NS_SUCCEEDED(rv) && !cHost.Equals(publicSuffix);
      }

      return true;
    };

    bool SchemeInList(nsAString& scheme, const char** schemes)
    {
      for (; *schemes; schemes++) {
        if (scheme.LowerCaseEqualsASCII(*schemes)) {
          return true;
        }
      }
      return false;
    };


    // Formatters

    already_AddRefed<nsIStringBundle>
    GetStringBundle()
    {
      nsCOMPtr<nsIStringBundleService> sbs =
        mozilla::services::GetStringBundleService();
      NS_ENSURE_TRUE(sbs, nullptr);

      nsCOMPtr<nsIStringBundle> stringBundle;
      sbs->CreateBundle("chrome://global/locale/extensions.properties",
                        getter_AddRefs(stringBundle));

      return stringBundle.forget();
    };

    void FormatErrorParams(const char* aName, const char16_t** aParams, int32_t aLength)
    {
      nsresult rv = NS_ERROR_FAILURE;

      nsCOMPtr<nsIStringBundle> stringBundle = GetStringBundle();

      if (stringBundle) {
        NS_ConvertASCIItoUTF16 name(aName);

        rv = stringBundle->FormatStringFromName(name.get(), aParams, aLength,
                                                getter_Copies(mError));
      }

      if (NS_WARN_IF(NS_FAILED(rv))) {
        mError.AssignLiteral("An unexpected error occurred");
      }
    };


    // Data members

    nsAutoString mURL;
    NS_ConvertASCIItoUTF16 mDirective;
    nsXPIDLString mError;

    bool mFoundSelf;
};

/**
 * Validates a custom content security policy string for use by an add-on.
 * In particular, ensures that:
 *
 *  - Both object-src and script-src directives are present, and meet
 *    the policies required by the CSPValidator class
 *
 *  - The script-src directive includes the source 'self'
 */
NS_IMETHODIMP
AddonContentPolicy::ValidateAddonCSP(const nsAString& aPolicyString,
                                     nsAString& aResult)
{
  nsresult rv;

  // Validate against a randomly-generated extension origin.
  // There is no add-on-specific behavior in the CSP code, beyond the ability
  // for add-ons to specify a custom policy, but the parser requires a valid
  // origin in order to operate correctly.
  nsAutoString url(u"moz-extension://");
  {
    nsCOMPtr<nsIUUIDGenerator> uuidgen = services::GetUUIDGenerator();
    NS_ENSURE_TRUE(uuidgen, NS_ERROR_FAILURE);

    nsID id;
    rv = uuidgen->GenerateUUIDInPlace(&id);
    NS_ENSURE_SUCCESS(rv, rv);

    char idString[NSID_LENGTH];
    id.ToProvidedString(idString);

    MOZ_RELEASE_ASSERT(idString[0] == '{' && idString[NSID_LENGTH - 2] == '}',
                       "UUID generator did not return a valid UUID");

    url.AppendASCII(idString + 1, NSID_LENGTH - 3);
  }


  RefPtr<BasePrincipal> principal =
    BasePrincipal::CreateCodebasePrincipal(NS_ConvertUTF16toUTF8(url));

  nsCOMPtr<nsIContentSecurityPolicy> csp;
  rv = principal->EnsureCSP(nullptr, getter_AddRefs(csp));
  NS_ENSURE_SUCCESS(rv, rv);


  csp->AppendPolicy(aPolicyString, false, false);

  const nsCSPPolicy* policy = csp->GetPolicy(0);
  if (!policy) {
    CSPValidator validator(url, nsIContentSecurityPolicy::SCRIPT_SRC_DIRECTIVE);
    aResult.Assign(validator.GetError());
    return NS_OK;
  }

  bool haveValidDefaultSrc = false;
  {
    CSPDirective directive = nsIContentSecurityPolicy::DEFAULT_SRC_DIRECTIVE;
    CSPValidator validator(url, directive);

    haveValidDefaultSrc = policy->visitDirectiveSrcs(directive, &validator);
  }

  aResult.SetIsVoid(true);
  {
    CSPDirective directive = nsIContentSecurityPolicy::SCRIPT_SRC_DIRECTIVE;
    CSPValidator validator(url, directive, !haveValidDefaultSrc);

    if (!policy->visitDirectiveSrcs(directive, &validator)) {
      aResult.Assign(validator.GetError());
    } else if (!validator.FoundSelf()) {
      validator.FormatError("csp.error.missing-source", NS_LITERAL_STRING("'self'"));
      aResult.Assign(validator.GetError());
    }
  }

  if (aResult.IsVoid()) {
    CSPDirective directive = nsIContentSecurityPolicy::OBJECT_SRC_DIRECTIVE;
    CSPValidator validator(url, directive, !haveValidDefaultSrc);

    if (!policy->visitDirectiveSrcs(directive, &validator)) {
      aResult.Assign(validator.GetError());
    }
  }

  return NS_OK;
}