Skip to content

Commit

Permalink
Bug 1734538 - Use NS_NewURI instead of MozURL. r=dom-storage-reviewer…
Browse files Browse the repository at this point in the history
…s,jesup,asuth,janv

Differential Revision: https://phabricator.services.mozilla.com/D181805
  • Loading branch information
janriokrause committed Nov 15, 2023
1 parent f569cd0 commit de38d08
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 79 deletions.
19 changes: 13 additions & 6 deletions dom/cache/DBSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
#include "mozilla/dom/cache/TypeUtils.h"
#include "mozilla/dom/cache/Types.h"
#include "mozilla/dom/quota/ResultExtensions.h"
#include "mozilla/net/MozURL.h"
#include "mozilla/psm/TransportSecurityInfo.h"
#include "nsCOMPtr.h"
#include "nsCharSeparatedTokenizer.h"
#include "nsComponentManagerUtils.h"
#include "nsHttp.h"
#include "nsIContentPolicy.h"
#include "nsICryptoHash.h"
#include "nsIURI.h"
#include "nsNetCID.h"
#include "nsPrintfCString.h"
#include "nsTArray.h"
Expand Down Expand Up @@ -1972,11 +1972,12 @@ Result<SavedResponse, nsresult> ReadResponse(mozIStorageConnection& aConn,
return Err(NS_ERROR_FAILURE);
}

RefPtr<net::MozURL> url;
QM_TRY(MOZ_TO_RESULT(net::MozURL::Init(getter_AddRefs(url), specNoSuffix)));
nsCOMPtr<nsIURI> url;
QM_TRY(MOZ_TO_RESULT(NS_NewURI(getter_AddRefs(url), specNoSuffix)));

#ifdef DEBUG
nsDependentCSubstring scheme = url->Scheme();
nsAutoCString scheme;
QM_TRY(MOZ_TO_RESULT(url->GetScheme(scheme)));

MOZ_ASSERT(
scheme == "http" || scheme == "https" || scheme == "file" ||
Expand All @@ -1998,11 +1999,17 @@ Result<SavedResponse, nsresult> ReadResponse(mozIStorageConnection& aConn,
scheme == "moz-extension");
#endif

nsCOMPtr<nsIPrincipal> principal =
BasePrincipal::CreateContentPrincipal(url, attrs);
if (!principal) {
return Err(NS_ERROR_NULL_POINTER);
}

nsCString origin;
url->Origin(origin);
QM_TRY(MOZ_TO_RESULT(principal->GetOriginNoSuffix(origin)));

nsCString baseDomain;
QM_TRY(MOZ_TO_RESULT(url->BaseDomain(baseDomain)));
QM_TRY(MOZ_TO_RESULT(principal->GetBaseDomain(baseDomain)));

savedResponse.mValue.principalInfo() =
Some(mozilla::ipc::ContentPrincipalInfo(attrs, origin, specNoSuffix,
Expand Down
80 changes: 19 additions & 61 deletions dom/localstorage/LocalStorageCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
#include "mozilla/dom/StorageUtils.h"
#include "mozilla/dom/quota/ResultExtensions.h"
#include "mozilla/ipc/PBackgroundSharedTypes.h"
#include "mozilla/net/MozURL.h"
#include "mozilla/net/WebSocketFrame.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsIURL.h"
#include "nsNetUtil.h"
#include "nsPrintfCString.h"
#include "nsString.h"
#include "nsStringFlags.h"
Expand Down Expand Up @@ -79,77 +80,34 @@ bool CachedNextGenLocalStorageEnabled() {

Result<std::pair<nsCString, nsCString>, nsresult> GenerateOriginKey2(
const mozilla::ipc::PrincipalInfo& aPrincipalInfo) {
OriginAttributes attrs;
nsCString spec;

switch (aPrincipalInfo.type()) {
case mozilla::ipc::PrincipalInfo::TNullPrincipalInfo: {
const auto& info = aPrincipalInfo.get_NullPrincipalInfo();

attrs = info.attrs();
spec = info.spec();

break;
}

case mozilla::ipc::PrincipalInfo::TContentPrincipalInfo: {
const auto& info = aPrincipalInfo.get_ContentPrincipalInfo();

attrs = info.attrs();
spec = info.spec();

break;
}

default: {
spec.SetIsVoid(true);

break;
}
}

if (spec.IsVoid()) {
if (aPrincipalInfo.type() !=
mozilla::ipc::PrincipalInfo::TNullPrincipalInfo &&
aPrincipalInfo.type() !=
mozilla::ipc::PrincipalInfo::TContentPrincipalInfo) {
return Err(NS_ERROR_UNEXPECTED);
}

nsCString originAttrSuffix;
attrs.CreateSuffix(originAttrSuffix);

RefPtr<MozURL> specURL;
QM_TRY(MOZ_TO_RESULT(MozURL::Init(getter_AddRefs(specURL), spec)));

nsCString host(specURL->Host());
uint32_t length = host.Length();
if (length > 0 && host.CharAt(0) == '[' && host.CharAt(length - 1) == ']') {
host = Substring(host, 1, length - 2);
Result<nsCOMPtr<nsIPrincipal>, nsresult> p =
PrincipalInfoToPrincipal(aPrincipalInfo);
if (p.isErr()) {
return Err(p.unwrapErr());
}

nsCString domainOrigin(host);

if (domainOrigin.IsEmpty()) {
// For the file:/// protocol use the exact directory as domain.
if (specURL->Scheme().EqualsLiteral("file")) {
domainOrigin.Assign(specURL->Directory());
}
nsCOMPtr<nsIPrincipal> principal = p.unwrap();
if (!principal) {
return Err(NS_ERROR_NULL_POINTER);
}

// Append reversed domain
nsAutoCString reverseDomain;
nsresult rv = StorageUtils::CreateReversedDomain(domainOrigin, reverseDomain);
nsCString originKey;
nsresult rv = principal->GetStorageOriginKey(originKey);
if (NS_FAILED(rv)) {
return Err(rv);
}

nsCString originKey = reverseDomain;

// Append scheme
originKey.Append(':');
originKey.Append(specURL->Scheme());

// Append port if any
int32_t port = specURL->RealPort();
if (port != -1) {
originKey.AppendPrintf(":%d", port);
nsCString originAttrSuffix;
rv = principal->GetOriginSuffix(originAttrSuffix);
if (NS_FAILED(rv)) {
return Err(rv);
}

return std::make_pair(std::move(originAttrSuffix), std::move(originKey));
Expand Down
24 changes: 18 additions & 6 deletions dom/serviceworkers/ServiceWorkerOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "nsIPushErrorReporter.h"
#include "nsISupportsImpl.h"
#include "nsITimer.h"
#include "nsIURI.h"
#include "nsServiceManagerUtils.h"
#include "nsTArray.h"
#include "nsThreadUtils.h"
Expand Down Expand Up @@ -62,7 +63,6 @@
#include "mozilla/dom/WorkerScope.h"
#include "mozilla/extensions/ExtensionBrowser.h"
#include "mozilla/ipc/IPCStreamUtils.h"
#include "mozilla/net/MozURL.h"

namespace mozilla::dom {

Expand Down Expand Up @@ -669,7 +669,7 @@ class PushEventOp final : public ExtendableEventOp {
RootedDictionary<PushEventInit> pushEventInit(aCx);

if (args.data().type() != OptionalPushData::Tvoid_t) {
auto& bytes = args.data().get_ArrayOfuint8_t();
const auto& bytes = args.data().get_ArrayOfuint8_t();
JSObject* data = Uint8Array::Create(aCx, bytes, result);

if (result.Failed()) {
Expand Down Expand Up @@ -999,9 +999,9 @@ class MessageEventOp final : public ExtendableEventOp {
init.mPorts = std::move(ports);
}

RefPtr<net::MozURL> mozUrl;
nsresult result = net::MozURL::Init(
getter_AddRefs(mozUrl), mArgs.get_ServiceWorkerMessageEventOpArgs()
nsCOMPtr<nsIURI> url;
nsresult result = NS_NewURI(getter_AddRefs(url),
mArgs.get_ServiceWorkerMessageEventOpArgs()
.clientInfoAndState()
.info()
.url());
Expand All @@ -1011,8 +1011,20 @@ class MessageEventOp final : public ExtendableEventOp {
return false;
}

OriginAttributes attrs;
nsCOMPtr<nsIPrincipal> principal =
BasePrincipal::CreateContentPrincipal(url, attrs);
if (!principal) {
return false;
}

nsCString origin;
mozUrl->Origin(origin);
result = principal->GetOriginNoSuffix(origin);
if (NS_WARN_IF(NS_FAILED(result))) {
RejectAll(result);
rv.SuppressException();
return false;
}

CopyUTF8toUTF16(origin, init.mOrigin);

Expand Down
22 changes: 16 additions & 6 deletions dom/serviceworkers/ServiceWorkerRegistrar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "ServiceWorkerManager.h"
#include "mozilla/dom/ServiceWorkerRegistrarTypes.h"
#include "mozilla/dom/DOMException.h"
#include "mozilla/net/MozURL.h"
#include "mozilla/StaticPrefs_dom.h"

#include "nsIEventTarget.h"
Expand All @@ -18,6 +17,7 @@
#include "nsIOutputStream.h"
#include "nsISafeOutputStream.h"
#include "nsIServiceWorkerManager.h"
#include "nsIURI.h"
#include "nsIWritablePropertyBag2.h"

#include "MainThreadUtils.h"
Expand Down Expand Up @@ -66,15 +66,25 @@ StaticRefPtr<ServiceWorkerRegistrar> gServiceWorkerRegistrar;

nsresult GetOriginAndBaseDomain(const nsACString& aURL, nsACString& aOrigin,
nsACString& aBaseDomain) {
RefPtr<net::MozURL> url;
nsresult rv = net::MozURL::Init(getter_AddRefs(url), aURL);
nsCOMPtr<nsIURI> url;
nsresult rv = NS_NewURI(getter_AddRefs(url), aURL);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}

url->Origin(aOrigin);
OriginAttributes attrs;
nsCOMPtr<nsIPrincipal> principal =
BasePrincipal::CreateContentPrincipal(url, attrs);
if (!principal) {
return NS_ERROR_NULL_POINTER;
}

rv = principal->GetOriginNoSuffix(aOrigin);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}

rv = url->BaseDomain(aBaseDomain);
rv = principal->GetBaseDomain(aBaseDomain);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
Expand Down Expand Up @@ -1393,7 +1403,7 @@ ServiceWorkerRegistrar::GetState(nsIPropertyBag** aBagOut) {
#define RELEASE_ASSERT_SUCCEEDED(rv, name) \
do { \
if (NS_FAILED(rv)) { \
if (rv == NS_ERROR_XPC_JAVASCRIPT_ERROR_WITH_DETAILS) { \
if ((rv) == NS_ERROR_XPC_JAVASCRIPT_ERROR_WITH_DETAILS) { \
if (auto* context = CycleCollectedJSContext::Get()) { \
if (RefPtr<Exception> exn = context->GetPendingException()) { \
MOZ_CRASH_UNSAFE_PRINTF("Failed to get " name ": %s", \
Expand Down

0 comments on commit de38d08

Please sign in to comment.