tor-browser

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

ExpandedPrincipalJSONHandler.h (3746B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef mozilla_ExpandedPrincipalJSONHandler_h
      7 #define mozilla_ExpandedPrincipalJSONHandler_h
      8 
      9 #include <stddef.h>  // size_t
     10 #include <stdint.h>  // uint32_t
     11 
     12 #include "js/TypeDecls.h"  // JS::Latin1Char
     13 
     14 #include "mozilla/Maybe.h"   // Maybe
     15 #include "mozilla/RefPtr.h"  // RefPtr
     16 
     17 #include "nsCOMPtr.h"      // nsCOMPtr
     18 #include "nsDebug.h"       // NS_WARNING
     19 #include "nsIPrincipal.h"  // nsIPrincipal
     20 #include "nsTArray.h"      // nsTArray
     21 
     22 #include "OriginAttributes.h"
     23 #include "ExpandedPrincipal.h"
     24 #include "SubsumedPrincipalJSONHandler.h"
     25 #include "SharedJSONHandler.h"
     26 
     27 namespace mozilla {
     28 
     29 // JSON parse handler for an inner object for ExpandedPrincipal.
     30 //
     31 // # Legacy format
     32 //
     33 //                           inner object
     34 //                                |
     35 //       -------------------------------------------------
     36 //       |                                               |
     37 // {"2": {"0": "eyIxIjp7IjAiOiJodHRwczovL2EuY29tLyJ9fQ=="}}
     38 //   |     |                      |
     39 //   |     ----------           Value
     40 //   |              |
     41 // PrincipalKind    |
     42 //                  |
     43 //           SerializableKeys
     44 //
     45 // The value is a CSV list of Base64 encoded prinipcals.
     46 //
     47 // # New format
     48 //
     49 //                           inner object
     50 //                                |
     51 //       -------------------------------------------
     52 //       |                                         |
     53 //       |               Subsumed principals       |
     54 //       |                       |                 |
     55 //       |     ------------------------------------|
     56 //       |     |                                  ||
     57 // {"2": {"0": [{"1": {"0": https://mozilla.com"}}]}}
     58 //   |            |                  |
     59 //   --------------                Value
     60 //         |
     61 //   PrincipalKind
     62 //
     63 // Used by PrincipalJSONHandler.
     64 class ExpandedPrincipalJSONHandler : public PrincipalJSONHandlerShared {
     65  enum class State {
     66    Init,
     67 
     68    // After the inner object's '{'.
     69    StartObject,
     70 
     71    // After the property key for eSpecs.
     72    SpecsKey,
     73 
     74    // After the property key for eSuffix.
     75    SuffixKey,
     76 
     77    // After the subsumed principals array's '['.
     78    StartArray,
     79 
     80    // Subsumed principals array's item.
     81    // Delegates to mSubsumedHandler until the subsumed object's '}'.
     82    SubsumedPrincipal,
     83 
     84    // After the property value for eSpecs or eSuffix,
     85    // including after the subsumed principals array's ']'.
     86    AfterPropertyValue,
     87 
     88    // After the inner object's '}'.
     89    EndObject,
     90 
     91    Error,
     92  };
     93 
     94 public:
     95  ExpandedPrincipalJSONHandler() = default;
     96  virtual ~ExpandedPrincipalJSONHandler() = default;
     97 
     98  virtual bool startObject() override;
     99 
    100  using PrincipalJSONHandlerShared::propertyName;
    101  virtual bool propertyName(const JS::Latin1Char* name, size_t length) override;
    102 
    103  virtual bool endObject() override;
    104 
    105  virtual bool startArray() override;
    106  virtual bool endArray() override;
    107 
    108  using PrincipalJSONHandlerShared::stringValue;
    109  virtual bool stringValue(const JS::Latin1Char* str, size_t length) override;
    110 
    111  bool HasAccepted() const { return mState == State::EndObject; }
    112 
    113 protected:
    114  virtual void SetErrorState() override { mState = State::Error; }
    115 
    116 private:
    117  bool ProcessSubsumedResult(bool aResult);
    118 
    119 private:
    120  State mState = State::Init;
    121 
    122  nsTArray<nsCOMPtr<nsIPrincipal>> mAllowList;
    123  OriginAttributes mAttrs;
    124  Maybe<SubsumedPrincipalJSONHandler> mSubsumedHandler;
    125 };
    126 
    127 }  // namespace mozilla
    128 
    129 #endif  // mozilla_ExpandedPrincipalJSONHandler_h