tor-browser

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

CookieParser.h (3510B)


      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_net_CookieParser_h
      7 #define mozilla_net_CookieParser_h
      8 
      9 #include "CookieCommons.h"
     10 
     11 #include "CookieValidation.h"
     12 #include "mozilla/net/NeckoChannelParams.h"
     13 #include "nsCOMPtr.h"
     14 #include "nsTArray.h"
     15 
     16 class nsIConsoleReportCollector;
     17 class nsIURI;
     18 
     19 namespace mozilla {
     20 namespace net {
     21 
     22 class CookieParser final {
     23 public:
     24  enum Rejection {
     25    // The cookie is OK or not parsed yet.
     26    NoRejection,
     27 
     28    RejectedInvalidCharAttributes,
     29    RejectedHttpOnlyButFromScript,
     30    RejectedForeignNoPartitionedError,
     31    RejectedByPermissionManager,
     32    RejectedNonsecureOverSecure,
     33  };
     34 
     35  CookieParser(nsIConsoleReportCollector* aCRC, nsIURI* aHostURI);
     36  ~CookieParser();
     37 
     38  nsIURI* HostURI() const { return mHostURI; }
     39 
     40  void Parse(const nsACString& aBaseDomain, bool aRequireHostMatch,
     41             CookieStatus aStatus, nsCString& aCookieHeader,
     42             const nsACString& aDateHeader, bool aFromHttp,
     43             bool aIsForeignAndNotAddon, bool aPartitionedOnly,
     44             bool aIsInPrivateBrowsing, bool aOn3pcbException,
     45             int64_t aCurrentTimeInMSec);
     46 
     47  bool ContainsCookie() const {
     48    return mValidation && mValidation->Result() == nsICookieValidation::eOK;
     49  }
     50 
     51  void RejectCookie(Rejection aRejection);
     52 
     53  CookieStruct& CookieData() {
     54    MOZ_ASSERT(ContainsCookie());
     55    return mCookieData;
     56  }
     57 
     58  void GetCookieString(nsACString& aCookieString) const;
     59 
     60  // Public for testing
     61  bool ParseMaxAgeAttribute(const nsACString& aMaxage, int64_t* aValue);
     62 
     63 private:
     64  static void GetTokenValue(nsACString::const_char_iterator& aIter,
     65                            nsACString::const_char_iterator& aEndIter,
     66                            nsDependentCSubstring& aTokenString,
     67                            nsDependentCSubstring& aTokenValue,
     68                            bool& aEqualsFound);
     69 
     70  void ParseAttributes(nsCString& aCookieHeader, nsACString& aExpires,
     71                       nsACString& aMaxage, bool& aAcceptedByParser);
     72 
     73  // expiry will be stored in milliseconds.
     74  bool GetExpiry(CookieStruct& aCookieData, const nsACString& aExpires,
     75                 const nsACString& aMaxage, const nsACString& aDateHeader,
     76                 bool aFromHttp);
     77 
     78  static bool CheckAttributeSize(const nsACString& currentValue,
     79                                 const char* aAttribute,
     80                                 const nsACString& aValue,
     81                                 CookieParser* aParser = nullptr);
     82  static void FixPath(CookieStruct& aCookieData, nsIURI* aHostURI);
     83  static void FixDomain(CookieStruct& aCookieData, nsIURI* aHostURI,
     84                        const nsACString& aBaseDomain, bool aRequireHostMatch);
     85 
     86  nsCOMPtr<nsIConsoleReportCollector> mCRC;
     87  nsCOMPtr<nsIURI> mHostURI;
     88 
     89  Rejection mRejection = NoRejection;
     90  RefPtr<CookieValidation> mValidation;
     91 
     92  struct Warnings {
     93    nsTArray<const char*> mAttributeOversize;
     94    nsTArray<const char*> mAttributeOverwritten;
     95 
     96    bool mInvalidSameSiteAttribute = false;
     97    bool mInvalidMaxAgeAttribute = false;
     98    bool mForeignNoPartitionedWarning = false;
     99  } mWarnings;
    100 
    101  CookieStruct mCookieData;
    102  nsCString mCookieString;
    103 };
    104 
    105 }  // namespace net
    106 }  // namespace mozilla
    107 
    108 #endif  // mozilla_net_CookieParser_h