tor-browser

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

OriginParser.h (5035B)


      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 file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef DOM_QUOTA_ORIGINPARSER_H_
      8 #define DOM_QUOTA_ORIGINPARSER_H_
      9 
     10 #include "mozilla/Attributes.h"
     11 #include "mozilla/dom/Nullable.h"
     12 #include "nsCharSeparatedTokenizer.h"
     13 #include "nsString.h"
     14 #include "nsTArray.h"
     15 
     16 namespace mozilla {
     17 
     18 class OriginAttributes;
     19 class OriginAttributesPattern;
     20 
     21 namespace dom::quota {
     22 
     23 class MOZ_STACK_CLASS OriginParser final {
     24 public:
     25  enum ResultType { InvalidOrigin, ObsoleteOrigin, ValidOrigin };
     26 
     27 private:
     28  using Tokenizer =
     29      nsCCharSeparatedTokenizerTemplate<NS_TokenizerIgnoreNothing>;
     30 
     31  enum SchemeType { eNone, eFile, eAbout, eChrome };
     32 
     33  enum State {
     34    eExpectingAppIdOrScheme,
     35    eExpectingInMozBrowser,
     36    eExpectingScheme,
     37    eExpectingEmptyToken1,
     38    eExpectingEmptyToken2,
     39    eExpectingEmptyTokenOrUniversalFileOrigin,
     40    eExpectingHost,
     41    eExpectingPort,
     42    eExpectingEmptyTokenOrDriveLetterOrPathnameComponent,
     43    eExpectingEmptyTokenOrPathnameComponent,
     44    eExpectingEmptyToken1OrHost,
     45 
     46    // We transit from eExpectingHost to this state when we encounter a host
     47    // beginning with "[" which indicates an IPv6 literal. Because we mangle the
     48    // IPv6 ":" delimiter to be a "+", we will receive separate tokens for each
     49    // portion of the IPv6 address, including a final token that ends with "]".
     50    // (Note that we do not mangle "[" or "]".) Note that the URL spec
     51    // explicitly disclaims support for "<zone_id>" and so we don't have to deal
     52    // with that.
     53    eExpectingIPV6Token,
     54    eComplete,
     55    eHandledTrailingSeparator
     56  };
     57 
     58  const nsCString mOrigin;
     59  Tokenizer mTokenizer;
     60 
     61  nsCString mScheme;
     62  nsCString mHost;
     63  Nullable<uint32_t> mPort;
     64  nsTArray<nsCString> mPathnameComponents;
     65  nsCString mHandledTokens;
     66 
     67  SchemeType mSchemeType;
     68  State mState;
     69  bool mUniversalFileOrigin;
     70  bool mMaybeDriveLetter;
     71  bool mError;
     72  bool mMaybeObsolete;
     73 
     74  // Number of group which a IPv6 address has. Should be less than 9.
     75  uint8_t mIPGroup;
     76 
     77 public:
     78  explicit OriginParser(const nsACString& aOrigin)
     79      : mOrigin(aOrigin),
     80        mTokenizer(aOrigin, '+'),
     81        mSchemeType(eNone),
     82        mState(eExpectingAppIdOrScheme),
     83        mUniversalFileOrigin(false),
     84        mMaybeDriveLetter(false),
     85        mError(false),
     86        mMaybeObsolete(false),
     87        mIPGroup(0) {}
     88 
     89  static ResultType ParseOrigin(const nsACString& aOrigin, nsCString& aSpec,
     90                                OriginAttributes* aAttrs,
     91                                nsCString& aOriginalSuffix);
     92 
     93  ResultType Parse(nsACString& aSpec);
     94 
     95 private:
     96  void HandleScheme(const nsDependentCSubstring& aToken);
     97 
     98  void HandlePathnameComponent(const nsDependentCSubstring& aToken);
     99 
    100  void HandleToken(const nsDependentCSubstring& aToken);
    101 
    102  void HandleTrailingSeparator();
    103 };
    104 
    105 bool IsUUIDOrigin(const nsCString& aOrigin);
    106 
    107 /**
    108 * Checks whether the given origin attributes suffix corresponds to a specific
    109 * user context, based on the provided `userContextId` value.
    110 *
    111 * This function parses the input suffix into an `OriginAttributes` object
    112 * and evaluates the `userContextId` attribute. If the attribute matches the
    113 * given `aUserContextId`, the suffix is considered to belong to that user
    114 * context. Other attributes in the suffix are ignored.
    115 *
    116 * @param aSuffix The origin attributes suffix to check. This must be a valid
    117 *   suffix; otherwise, the code will trigger an assertion failure.
    118 * @param aUserContextId The `userContextId` value to compare against the
    119 *   attribute in the suffix.
    120 *
    121 * @return `true` if the `userContextId` attribute matches `aUserContextId`,
    122 * `false` otherwise.
    123 *
    124 * @note The input must be a valid suffix. Invalid inputs will cause a
    125 * diagnostic assertion failure because of `MOZ_ALWAYS_TRUE`.
    126 */
    127 bool IsUserContextSuffix(const nsACString& aSuffix, uint32_t aUserContextId);
    128 
    129 /**
    130 * Checks whether the given `OriginAttributesPattern` matches a specific
    131 * user context, based on the provided `userContextId`.
    132 *
    133 * This function evaluates the `mUserContextId` attribute of the provided
    134 * pattern. If the attribute is not set (`WasPassed` returns false), the
    135 * function returns `false`. If it is set, the function compares its value
    136 * against the specified `aUserContextId`.
    137 *
    138 * @param aPattern The `OriginAttributesPattern` to check.
    139 * @param aUserContextId The expected `userContextId` to compare against
    140 *   the `mUserContextId` attribute in the pattern.
    141 *
    142 * @return `true` if `mUserContextId` is set and matches `aUserContextId`,
    143 * `false` otherwise.
    144 */
    145 bool IsUserContextPattern(const OriginAttributesPattern& aPattern,
    146                          uint32_t aUserContextId);
    147 
    148 }  // namespace dom::quota
    149 }  // namespace mozilla
    150 
    151 #endif  // DOM_QUOTA_ORIGINPARSER_H_