tor-browser

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

StorageUtils.cpp (3452B)


      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 #include "StorageUtils.h"
      8 
      9 #include "mozilla/OriginAttributes.h"
     10 #include "mozilla/StorageOriginAttributes.h"
     11 #include "nsDebug.h"
     12 #include "nsIPrincipal.h"
     13 #include "nsIURI.h"
     14 #include "nsIURL.h"
     15 #include "nsNetUtil.h"
     16 #include "nsPrintfCString.h"
     17 
     18 namespace mozilla::dom::StorageUtils {
     19 
     20 bool PrincipalsEqual(nsIPrincipal* aObjectPrincipal,
     21                     nsIPrincipal* aSubjectPrincipal) {
     22  if (!aSubjectPrincipal) {
     23    return true;
     24  }
     25 
     26  if (!aObjectPrincipal) {
     27    return false;
     28  }
     29 
     30  return aSubjectPrincipal->Equals(aObjectPrincipal);
     31 }
     32 
     33 void ReverseString(const nsACString& aSource, nsACString& aResult) {
     34  nsACString::const_iterator sourceBegin, sourceEnd;
     35  aSource.BeginReading(sourceBegin);
     36  aSource.EndReading(sourceEnd);
     37 
     38  aResult.SetLength(aSource.Length());
     39  auto destEnd = aResult.EndWriting();
     40 
     41  while (sourceBegin != sourceEnd) {
     42    *(--destEnd) = *sourceBegin;
     43    ++sourceBegin;
     44  }
     45 }
     46 
     47 nsresult CreateReversedDomain(const nsACString& aAsciiDomain,
     48                              nsACString& aKey) {
     49  if (aAsciiDomain.IsEmpty()) {
     50    return NS_ERROR_NOT_AVAILABLE;
     51  }
     52 
     53  ReverseString(aAsciiDomain, aKey);
     54 
     55  aKey.Append('.');
     56  return NS_OK;
     57 }
     58 
     59 // This is only a compatibility code for schema version 0.  Returns the 'scope'
     60 // key in the schema version 0 format for the scope column.
     61 nsCString Scheme0Scope(const nsACString& aOriginSuffix,
     62                       const nsACString& aOriginNoSuffix) {
     63  nsCString result;
     64 
     65  StorageOriginAttributes oa;
     66  if (!aOriginSuffix.IsEmpty()) {
     67    DebugOnly<bool> success = oa.PopulateFromSuffix(aOriginSuffix);
     68    MOZ_ASSERT(success);
     69  }
     70 
     71  if (oa.InIsolatedMozBrowser()) {
     72    result.AppendInt(0);  // This is the appId to be removed.
     73    result.Append(':');
     74    result.Append(oa.InIsolatedMozBrowser() ? 't' : 'f');
     75    result.Append(':');
     76  }
     77 
     78  // If there is more than just appid and/or inbrowser stored in origin
     79  // attributes, put it to the schema 0 scope as well.  We must do that
     80  // to keep the scope column unique (same resolution as schema 1 has
     81  // with originAttributes and originKey columns) so that switch between
     82  // schema 1 and 0 always works in both ways.
     83  nsAutoCString remaining;
     84  oa.SetInIsolatedMozBrowser(false);
     85  oa.CreateSuffix(remaining);
     86  if (!remaining.IsEmpty()) {
     87    MOZ_ASSERT(!aOriginSuffix.IsEmpty());
     88 
     89    if (result.IsEmpty()) {
     90      // Must contain the old prefix, otherwise we won't search for the whole
     91      // origin attributes suffix.
     92      result.AppendLiteral("0:f:");
     93    }
     94 
     95    // Append the whole origin attributes suffix despite we have already stored
     96    // appid and inbrowser.  We are only looking for it when the scope string
     97    // starts with "$appid:$inbrowser:" (with whatever valid values).
     98    //
     99    // The OriginAttributes suffix is a string in a form like:
    100    // "^addonId=101&userContextId=5" and it's ensured it always starts with '^'
    101    // and never contains ':'.  See OriginAttributes::CreateSuffix.
    102    result.Append(aOriginSuffix);
    103    result.Append(':');
    104  }
    105 
    106  result.Append(aOriginNoSuffix);
    107 
    108  return result;
    109 }
    110 
    111 }  // namespace mozilla::dom::StorageUtils