summaryrefslogtreecommitdiffstats
path: root/caps/NullPrincipal.h
blob: cbce27c700dbdda3346d6bfad8e831a1e5038bb5 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

/**
 * This is the principal that has no rights and can't be accessed by
 * anything other than itself and chrome; null principals are not
 * same-origin with anything but themselves.
 */

#ifndef mozilla_NullPrincipal_h
#define mozilla_NullPrincipal_h

#include "nsIPrincipal.h"
#include "nsJSPrincipals.h"
#include "nsCOMPtr.h"

#include "mozilla/BasePrincipal.h"
#include "gtest/MozGtestFriend.h"

class nsIDocShell;
class nsIURI;
namespace Json {
class Value;
}

#define NS_NULLPRINCIPAL_CID                         \
  {                                                  \
    0xbd066e5f, 0x146f, 0x4472, {                    \
      0x83, 0x31, 0x7b, 0xfd, 0x05, 0xb1, 0xed, 0x90 \
    }                                                \
  }

#define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal"

namespace mozilla {

class NullPrincipal final : public BasePrincipal {
 public:
  static PrincipalKind Kind() { return eNullPrincipal; }

  NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
  uint32_t GetHashValue() override;
  NS_IMETHOD GetURI(nsIURI** aURI) override;
  NS_IMETHOD GetIsOriginPotentiallyTrustworthy(bool* aResult) override;
  NS_IMETHOD GetDomain(nsIURI** aDomain) override;
  NS_IMETHOD SetDomain(nsIURI* aDomain) override;
  NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
  NS_IMETHOD GetAddonId(nsAString& aAddonId) override;
  NS_IMETHOD GetPrecursorPrincipal(nsIPrincipal** aPrecursor) override;

  // Create a NullPrincipal, inheriting origin attributes from the given
  // principal.
  // If aInheritFrom is a content principal, or has a content principal
  // precursor, it will be used as the precursor for this principal.
  static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes(
      nsIPrincipal* aInheritFrom);

  // Create a new NullPrincipal with the specified OriginAttributes.
  //
  // If `aNullPrincipalURI` is specified, it must be a NS_NULLPRINCIPAL_SCHEME
  // URI previously created using `NullPrincipal::CreateURI`, and will be used
  // as the origin URI for this principal.
  static already_AddRefed<NullPrincipal> Create(
      const OriginAttributes& aOriginAttributes,
      nsIURI* aNullPrincipalURI = nullptr);

  static already_AddRefed<NullPrincipal> CreateWithoutOriginAttributes();

  // Generates a new unique `moz-nullprincipal:` URI. If `aPrecursor` is
  // specified, it will be included in the generated URI as the null principal's
  // precursor.
  //
  // The `aPrincipalID` attribute is used to force the creation of a
  // deterministic NullPrincipal in situations where that is required. Avoid
  // using this parameter unless absolutely necessary.
  static already_AddRefed<nsIURI> CreateURI(nsIPrincipal* aPrecursor = nullptr,
                                            const nsID* aPrincipalID = nullptr);

  virtual nsresult GetScriptLocation(nsACString& aStr) override;

  nsresult GetSiteIdentifier(SiteIdentifier& aSite) override {
    aSite.Init(this);
    return NS_OK;
  }

  virtual nsresult PopulateJSONObject(Json::Value& aObject) override;

  // Serializable keys are the valid enum fields the serialization supports
  enum SerializableKeys : uint8_t { eSpec = 0, eSuffix, eMax = eSuffix };
  typedef mozilla::BasePrincipal::KeyValT<SerializableKeys> KeyVal;

  static already_AddRefed<BasePrincipal> FromProperties(
      nsTArray<NullPrincipal::KeyVal>& aFields);

  class Deserializer : public BasePrincipal::Deserializer {
   public:
    NS_IMETHOD Read(nsIObjectInputStream* aStream) override;
  };

 protected:
  NullPrincipal(nsIURI* aURI, const nsACString& aOriginNoSuffix,
                const OriginAttributes& aOriginAttributes);

  virtual ~NullPrincipal() = default;

  bool SubsumesInternal(nsIPrincipal* aOther,
                        DocumentDomainConsideration aConsideration) override {
    MOZ_ASSERT(aOther);
    return FastEquals(aOther);
  }

  bool MayLoadInternal(nsIURI* aURI) override;

  const nsCOMPtr<nsIURI> mURI;

 private:
  FRIEND_TEST(NullPrincipalPrecursor, EscapingRoundTrips);

  static void EscapePrecursorQuery(nsACString& aPrecursorQuery);
  static void UnescapePrecursorQuery(nsACString& aPrecursorQuery);
};

}  // namespace mozilla

#endif  // mozilla_NullPrincipal_h