tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

IntegrityPolicy.h (3354B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef IntegrityPolicy_h___
      8 #define IntegrityPolicy_h___
      9 
     10 #include "mozilla/EnumSet.h"
     11 #include "mozilla/EnumTypeTraits.h"
     12 #include "mozilla/Maybe.h"
     13 #include "nsIContentPolicy.h"
     14 #include "nsIIntegrityPolicy.h"
     15 #include "nsTArray.h"
     16 
     17 #define NS_INTEGRITYPOLICY_CONTRACTID "@mozilla.org/integritypolicy;1"
     18 
     19 class nsISFVDictionary;
     20 class nsILoadInfo;
     21 
     22 namespace mozilla {
     23 namespace ipc {
     24 class IntegrityPolicyArgs;
     25 }  // namespace ipc
     26 namespace dom {
     27 
     28 class IntegrityPolicy : public nsIIntegrityPolicy {
     29 public:
     30  NS_DECL_ISUPPORTS
     31  NS_DECL_NSISERIALIZABLE
     32  NS_DECL_NSIINTEGRITYPOLICY
     33 
     34  IntegrityPolicy() = default;
     35 
     36  static nsresult ParseHeaders(const nsACString& aHeader,
     37                               const nsACString& aHeaderRO,
     38                               IntegrityPolicy** aPolicy);
     39 
     40  enum class SourceType : uint8_t { Inline };
     41 
     42  // Trimmed down version of dom::RequestDestination
     43  enum class DestinationType : uint8_t { Script, Style };
     44 
     45  using Sources = EnumSet<SourceType>;
     46  using Destinations = EnumSet<DestinationType>;
     47 
     48  void PolicyContains(DestinationType aDestination, bool* aContains,
     49                      bool* aROContains) const;
     50 
     51  static Maybe<DestinationType> ContentTypeToDestinationType(
     52      nsContentPolicyType aType);
     53 
     54  static void ToArgs(const IntegrityPolicy* aPolicy,
     55                     mozilla::ipc::IntegrityPolicyArgs& aArgs);
     56 
     57  static void FromArgs(const mozilla::ipc::IntegrityPolicyArgs& aArgs,
     58                       IntegrityPolicy** aPolicy);
     59 
     60  void InitFromOther(IntegrityPolicy* aOther);
     61 
     62  static IntegrityPolicy* Cast(nsIIntegrityPolicy* aPolicy) {
     63    return static_cast<IntegrityPolicy*>(aPolicy);
     64  }
     65 
     66  static bool Equals(const IntegrityPolicy* aPolicy,
     67                     const IntegrityPolicy* aOtherPolicy);
     68 
     69 protected:
     70  virtual ~IntegrityPolicy();
     71 
     72 private:
     73  class Entry final {
     74   public:
     75    Entry(Sources aSources, Destinations aDestinations,
     76          nsTArray<nsCString>&& aEndpoints)
     77        : mSources(aSources),
     78          mDestinations(aDestinations),
     79          mEndpoints(std::move(aEndpoints)) {}
     80 
     81    Entry(const Entry& aOther)
     82        : mSources(aOther.mSources),
     83          mDestinations(aOther.mDestinations),
     84          mEndpoints(aOther.mEndpoints.Clone()) {}
     85 
     86    ~Entry() = default;
     87 
     88    static bool Equals(const Maybe<Entry>& aPolicy,
     89                       const Maybe<Entry>& aOtherPolicy);
     90 
     91    const Sources mSources;
     92    const Destinations mDestinations;
     93    const nsTArray<nsCString> mEndpoints;
     94  };
     95 
     96  Maybe<Entry> mEnforcement;
     97  Maybe<Entry> mReportOnly;
     98 };
     99 }  // namespace dom
    100 
    101 template <>
    102 struct MaxEnumValue<dom::IntegrityPolicy::SourceType> {
    103  static constexpr unsigned int value =
    104      static_cast<unsigned int>(dom::IntegrityPolicy::SourceType::Inline);
    105 };
    106 
    107 template <>
    108 struct MaxEnumValue<dom::IntegrityPolicy::DestinationType> {
    109  static constexpr unsigned int value =
    110      static_cast<unsigned int>(dom::IntegrityPolicy::DestinationType::Script);
    111 };
    112 
    113 }  // namespace mozilla
    114 
    115 #endif /* IntegrityPolicy_h___ */