Files
palemoon27/netwerk/protocol/http/nsHttpAuthCache.cpp
T
roytam1 393bc6639f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1209162 - Create OriginAttributes subtypes. IGNORE IDL r=sicking. (c2cbe04ef3)
- Bug 1220570 - Potential cookie lost while downgrading from Aurora 44 to 43. r=jduell (1a0111c842)
- Bug 1217456: Add a security flag for controlling redirects. Use this flag in fetch() implementation. r=bkelly,jduell (79d449e479)
- Bug 1112040 - Add a mochitest. r=bholley (566a05f720)
- Bug 1171215 - Compute third-partyness in the loadinfo instead of nsIHttpChannelInternal so that other protocols correctly respect the third-party cookie pref. r=sicking/ckerschb (06f7a10a83)
- better backport of  Bug 485941 - Stack overflow using overly-deep XML tree (DoS). r=bzbarsky (ac43feeffa)
- Bug 1182546 - Use channel->Open2() in parser/htmlparser/nsExpatDriver.cpp (r=bz) (42768f373a)
- Bug 1163435 part 1 - [css-grid][css-flexbox] Propagate an explicit CB width/height to the reflow state to resolve percentage lengths for grid items properly. Resolve percent against the size in the same axis for abs.pos. children too. r=dholbert (a55463fb05)
- Bug 1163435 part 2 - tests. (bb683c5fc6)
- Bug 1223282 - Make NS_AUTOMARGIN be a different value than NS_UNCONSTRAINEDSIZE to avoid having clamped huge margin values be interpreted as auto margins. r=roc (4cdfe0f277)
- Bug 1224230 - Explicitly store the lineContainer's writing mode in InlineIntrinsicISizeData. r=dbaron (6474515223)
- Bug 1221043. Revert to including trailing whitespace for accessibility APIs. r=marcoz,mats (406018c163)
- Bug 1227113 - Fix some indentation issues in ServiceWorkerManager, r=janv (2b343bde09)
- Bug 1223116 P1 Expose nsIServiceWorkerManager.shouldReportToWindow(). r=catalinb (02899e429d)
- Bug 1226441 - Part 1: Add wpt test verifying fetch event waits for activate to complete; r=catalinb (e8eb3e6e7a)
- Bug 1209865 - Add gecko profiler marker when mark() of User Timing API is called. r=baku (f48d76e395)
- Bug 1169068 - Performance.translateTime(), r=bz (38cd1c31b2)
- Bug 1226441 - Part 2: Delay functional event dispatch until service worker is activated; r=catalinb (778cd3dd24)
- Bug 1178233 - [non-e10s] The update process doesn't work within about:serviceworkers in non-e10s mode. Test. r=baku (4f8b6f53f8)
- Bug 1188545 - Disable unstable test: test_aboutserviceworkers.html. a=testonly (4bbe106693)
- Bug 1219255 - We should be able to attach to a service worker;r=amarchesini (0d6b71b4ec)
- Bug 1222464 - Part 2: Implement FetchEvent.clientId; r=jdm (9c8abd62dd)
- Bug 1218150 - Mark the members of Clients as NewObject; r=bzbarsky (b6b00a586c)
- Bug 1222464 - Part 3: Implement Clients.get(); r=jdm (f5ca60d801)
- Bug 1222464 - Part 1: Save a client ID for top-level navigations on the docshell and assign it as the document ID when we start loading the document; r=jdm (7dcb5ce2b6)
- Bug 1218141 - Add some SameObject and NewObject annotations to ServiceWorkerGlobalScope; r=bzbarsky (5019f58c7a)
- Bug 1218190 - Add a pref to enable Clients.openWindow, r=catalinb (dbb6d007dd)
- Bug 1218142 - Remove ServiceWorkerGlobalScope.onbeforeevicted/onevicted; r=bzbarsky (029de6f8ec)
- Bug 1218146 - Move WindowClient.frameType to Client.frameType; r=bzbarsky (00f0211276)
- Bug 1218147 - Make WindowClient.focus() NewObject; r=bzbarsky (3c6aea4b67)
- Bug 1189659 - Part 1 - Continue service worker job queue when life cycle events expire. r=bkelly (aa09cd9c60)
- Bug 1227932 - Fix Service Workers SoftUpdate and registration.update code paths. r=ehsan (24567b23c0)
- Bug 1189659 - Part 2 - Remove set of scopes being updated from ServiceWorkerManager. r=bkelly (ce581b095c)
- Bug 1189659 - Part 3 - Use separate synchronization queues for service worker register jobs and install jobs. r=bkelly (9c408a22ed)
- Bug 1189659 - Part 4 - Fix race in test_install_event.html. r=bkelly (3186ffb808)
- Bug 1189659 - Part 5 - Fix race in skip-waiting.https.html and add some logging for SkipWaitingFlag in ServiceWorkerManager. r=ehsan (4e5ddda6f3)
- Bug 1229056 - Implement ClientQueryOptions.includeUncontrolled; r=jdm (dbe56aa60d)
- namespace (3b0863d42d)
- Bug 1201127 - Return the same ServiceWorkerRegistration object from service worker APIs dealing with the same underlying registration object; r=jdm (c542688ae0)
- Bug 1171583 - Remove mutable warning from |nsSimpleURI::SetUserPass|. r=bz (73934deaad)
- Bug 1206199 - Extend channelwrapper to mediate OnStartRequest, OnStopRequest, OnDataAvailable (r=sicking) (758a7ec65c)
- Bug 1186783 (part 4) - Replace nsBaseHashtable::EnumerateRead() calls in netwerk/ with iterators. r=valentin. (681bdba278)
- Bug 1186783 (part 3) - Replace nsBaseHashtable::EnumerateRead() calls in netwerk/ with iterators. r=valentin. (76b8b7191e)
- Bug 1186783 (part 2) - Replace nsBaseHashtable::EnumerateRead() calls in netwerk/ with iterators. r=valentin. (5c0743ac49)
- Bug 1186783 (part 1) - Replace nsBaseHashtable::EnumerateRead() calls in netwerk/ with iterators. r=valentin. (952cc720cc)
- Bug 1186783 (part 5) - Replace nsBaseHashtable::EnumerateRead() calls in netwerk/ with iterators. r=valentin. (25b9735c52)
- Bug 1186783 (part 1) - Replace nsBaseHashtable::EnumerateRead() calls in netwerk/. r=michal. (ae52425809)
- Bug 1186783 (follow-up) - Bustage fix for Gonk. (d4a1b769bd)
- add back some hotfix stuff, even if unused (fe32076c5b)
- Bug 1068087: Switch about:plugins to run remotely. r=mconley (bc4316dd03)
- Bug 1214058: Part 1 - Add a simplified JSON-based add-on update protocol. r=Mossop (a3198884d5)
- Bug 1214058: Part 2 - Run add-on update tests against comparable JSON and RDF manifests. r=Mossop (aa6a796e6f)
- Bug 1152977 - Enable by default DEAA for desktop platforms that use OpenGL compositor. r=jmuizelaar (bfa9efd5c8)
2023-03-16 14:16:15 +08:00

604 lines
17 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// HttpLog.h should generally be included first
#include "HttpLog.h"
#include "nsHttpAuthCache.h"
#include <stdlib.h>
#include "mozilla/Attributes.h"
#include "nsString.h"
#include "nsCRT.h"
#include "mozIApplicationClearPrivateDataParams.h"
#include "nsIObserverService.h"
#include "mozilla/Services.h"
#include "mozilla/DebugOnly.h"
#include "nsNetUtil.h"
namespace mozilla {
namespace net {
static inline void
GetAuthKey(const char *scheme, const char *host, int32_t port, nsACString const &originSuffix, nsCString &key)
{
key.Truncate();
key.Append(originSuffix);
key.Append(':');
key.Append(scheme);
key.AppendLiteral("://");
key.Append(host);
key.Append(':');
key.AppendInt(port);
}
// return true if the two strings are equal or both empty. an empty string
// is either null or zero length.
static bool
StrEquivalent(const char16_t *a, const char16_t *b)
{
static const char16_t emptyStr[] = {0};
if (!a)
a = emptyStr;
if (!b)
b = emptyStr;
return nsCRT::strcmp(a, b) == 0;
}
//-----------------------------------------------------------------------------
// nsHttpAuthCache <public>
//-----------------------------------------------------------------------------
nsHttpAuthCache::nsHttpAuthCache()
: mDB(nullptr)
, mObserver(new OriginClearObserver(this))
{
nsCOMPtr<nsIObserverService> obsSvc = services::GetObserverService();
if (obsSvc) {
obsSvc->AddObserver(mObserver, "clear-origin-data", false);
}
}
nsHttpAuthCache::~nsHttpAuthCache()
{
if (mDB)
ClearAll();
nsCOMPtr<nsIObserverService> obsSvc = services::GetObserverService();
if (obsSvc) {
obsSvc->RemoveObserver(mObserver, "clear-origin-data");
mObserver->mOwner = nullptr;
}
}
nsresult
nsHttpAuthCache::Init()
{
NS_ENSURE_TRUE(!mDB, NS_ERROR_ALREADY_INITIALIZED);
LOG(("nsHttpAuthCache::Init\n"));
mDB = PL_NewHashTable(128, (PLHashFunction) PL_HashString,
(PLHashComparator) PL_CompareStrings,
(PLHashComparator) 0, &gHashAllocOps, this);
if (!mDB)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
nsresult
nsHttpAuthCache::GetAuthEntryForPath(const char *scheme,
const char *host,
int32_t port,
const char *path,
nsACString const &originSuffix,
nsHttpAuthEntry **entry)
{
LOG(("nsHttpAuthCache::GetAuthEntryForPath [key=%s://%s:%d path=%s]\n",
scheme, host, port, path));
nsAutoCString key;
nsHttpAuthNode *node = LookupAuthNode(scheme, host, port, originSuffix, key);
if (!node)
return NS_ERROR_NOT_AVAILABLE;
*entry = node->LookupEntryByPath(path);
return *entry ? NS_OK : NS_ERROR_NOT_AVAILABLE;
}
nsresult
nsHttpAuthCache::GetAuthEntryForDomain(const char *scheme,
const char *host,
int32_t port,
const char *realm,
nsACString const &originSuffix,
nsHttpAuthEntry **entry)
{
LOG(("nsHttpAuthCache::GetAuthEntryForDomain [key=%s://%s:%d realm=%s]\n",
scheme, host, port, realm));
nsAutoCString key;
nsHttpAuthNode *node = LookupAuthNode(scheme, host, port, originSuffix, key);
if (!node)
return NS_ERROR_NOT_AVAILABLE;
*entry = node->LookupEntryByRealm(realm);
return *entry ? NS_OK : NS_ERROR_NOT_AVAILABLE;
}
nsresult
nsHttpAuthCache::SetAuthEntry(const char *scheme,
const char *host,
int32_t port,
const char *path,
const char *realm,
const char *creds,
const char *challenge,
nsACString const &originSuffix,
const nsHttpAuthIdentity *ident,
nsISupports *metadata)
{
nsresult rv;
LOG(("nsHttpAuthCache::SetAuthEntry [key=%s://%s:%d realm=%s path=%s metadata=%x]\n",
scheme, host, port, realm, path, metadata));
if (!mDB) {
rv = Init();
if (NS_FAILED(rv)) return rv;
}
nsAutoCString key;
nsHttpAuthNode *node = LookupAuthNode(scheme, host, port, originSuffix, key);
if (!node) {
// create a new entry node and set the given entry
node = new nsHttpAuthNode();
if (!node)
return NS_ERROR_OUT_OF_MEMORY;
rv = node->SetAuthEntry(path, realm, creds, challenge, ident, metadata);
if (NS_FAILED(rv))
delete node;
else
PL_HashTableAdd(mDB, strdup(key.get()), node);
return rv;
}
return node->SetAuthEntry(path, realm, creds, challenge, ident, metadata);
}
void
nsHttpAuthCache::ClearAuthEntry(const char *scheme,
const char *host,
int32_t port,
const char *realm,
nsACString const &originSuffix)
{
if (!mDB)
return;
nsAutoCString key;
GetAuthKey(scheme, host, port, originSuffix, key);
PL_HashTableRemove(mDB, key.get());
}
nsresult
nsHttpAuthCache::ClearAll()
{
LOG(("nsHttpAuthCache::ClearAll\n"));
if (mDB) {
PL_HashTableDestroy(mDB);
mDB = 0;
}
return NS_OK;
}
//-----------------------------------------------------------------------------
// nsHttpAuthCache <private>
//-----------------------------------------------------------------------------
nsHttpAuthNode *
nsHttpAuthCache::LookupAuthNode(const char *scheme,
const char *host,
int32_t port,
nsACString const &originSuffix,
nsCString &key)
{
if (!mDB)
return nullptr;
GetAuthKey(scheme, host, port, originSuffix, key);
return (nsHttpAuthNode *) PL_HashTableLookup(mDB, key.get());
}
void *
nsHttpAuthCache::AllocTable(void *self, size_t size)
{
return moz_xmalloc(size);
}
void
nsHttpAuthCache::FreeTable(void *self, void *item)
{
free(item);
}
PLHashEntry *
nsHttpAuthCache::AllocEntry(void *self, const void *key)
{
return (PLHashEntry *) moz_xmalloc(sizeof(PLHashEntry));
}
void
nsHttpAuthCache::FreeEntry(void *self, PLHashEntry *he, unsigned flag)
{
if (flag == HT_FREE_VALUE) {
// this would only happen if PL_HashTableAdd were to replace an
// existing entry in the hash table, but we _always_ do a lookup
// before adding a new entry to avoid this case.
NS_NOTREACHED("should never happen");
}
else if (flag == HT_FREE_ENTRY) {
// three wonderful flavors of freeing memory ;-)
delete (nsHttpAuthNode *) he->value;
free((char *) he->key);
free(he);
}
}
PLHashAllocOps nsHttpAuthCache::gHashAllocOps =
{
nsHttpAuthCache::AllocTable,
nsHttpAuthCache::FreeTable,
nsHttpAuthCache::AllocEntry,
nsHttpAuthCache::FreeEntry
};
NS_IMPL_ISUPPORTS(nsHttpAuthCache::OriginClearObserver, nsIObserver)
NS_IMETHODIMP
nsHttpAuthCache::OriginClearObserver::Observe(nsISupports *subject,
const char * topic,
const char16_t * data_unicode)
{
NS_ENSURE_TRUE(mOwner, NS_ERROR_NOT_AVAILABLE);
OriginAttributesPattern pattern;
if (!pattern.Init(nsDependentString(data_unicode))) {
NS_ERROR("Cannot parse origin attributes pattern");
return NS_ERROR_FAILURE;
}
mOwner->ClearOriginData(pattern);
return NS_OK;
}
static int
RemoveEntriesForPattern(PLHashEntry *entry, int32_t number, void *arg)
{
nsDependentCString key(static_cast<const char*>(entry->key));
// Extract the origin attributes suffix from the key.
int32_t colon = key.Find(NS_LITERAL_CSTRING(":"));
MOZ_ASSERT(colon != kNotFound);
nsDependentCSubstring oaSuffix;
oaSuffix.Rebind(key.BeginReading(), colon);
// Build the NeckoOriginAttributes object of it...
NeckoOriginAttributes oa;
DebugOnly<bool> rv = oa.PopulateFromSuffix(oaSuffix);
MOZ_ASSERT(rv);
// ...and match it against the given pattern.
OriginAttributesPattern const *pattern = static_cast<OriginAttributesPattern const*>(arg);
if (pattern->Matches(oa)) {
return HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE;
}
return HT_ENUMERATE_NEXT;
}
void
nsHttpAuthCache::ClearOriginData(OriginAttributesPattern const &pattern)
{
if (!mDB) {
return;
}
PL_HashTableEnumerateEntries(mDB, RemoveEntriesForPattern, (void*)&pattern);
}
//-----------------------------------------------------------------------------
// nsHttpAuthIdentity
//-----------------------------------------------------------------------------
nsresult
nsHttpAuthIdentity::Set(const char16_t *domain,
const char16_t *user,
const char16_t *pass)
{
char16_t *newUser, *newPass, *newDomain;
int domainLen = domain ? NS_strlen(domain) : 0;
int userLen = user ? NS_strlen(user) : 0;
int passLen = pass ? NS_strlen(pass) : 0;
int len = userLen + 1 + passLen + 1 + domainLen + 1;
newUser = (char16_t *) moz_xmalloc(len * sizeof(char16_t));
if (!newUser)
return NS_ERROR_OUT_OF_MEMORY;
if (user)
memcpy(newUser, user, userLen * sizeof(char16_t));
newUser[userLen] = 0;
newPass = &newUser[userLen + 1];
if (pass)
memcpy(newPass, pass, passLen * sizeof(char16_t));
newPass[passLen] = 0;
newDomain = &newPass[passLen + 1];
if (domain)
memcpy(newDomain, domain, domainLen * sizeof(char16_t));
newDomain[domainLen] = 0;
// wait until the end to clear member vars in case input params
// reference our members!
if (mUser)
free(mUser);
mUser = newUser;
mPass = newPass;
mDomain = newDomain;
return NS_OK;
}
void
nsHttpAuthIdentity::Clear()
{
if (mUser) {
free(mUser);
mUser = nullptr;
mPass = nullptr;
mDomain = nullptr;
}
}
bool
nsHttpAuthIdentity::Equals(const nsHttpAuthIdentity &ident) const
{
// we could probably optimize this with a single loop, but why bother?
return StrEquivalent(mUser, ident.mUser) &&
StrEquivalent(mPass, ident.mPass) &&
StrEquivalent(mDomain, ident.mDomain);
}
//-----------------------------------------------------------------------------
// nsHttpAuthEntry
//-----------------------------------------------------------------------------
nsHttpAuthEntry::~nsHttpAuthEntry()
{
if (mRealm)
free(mRealm);
while (mRoot) {
nsHttpAuthPath *ap = mRoot;
mRoot = mRoot->mNext;
free(ap);
}
}
nsresult
nsHttpAuthEntry::AddPath(const char *aPath)
{
// null path matches empty path
if (!aPath)
aPath = "";
nsHttpAuthPath *tempPtr = mRoot;
while (tempPtr) {
const char *curpath = tempPtr->mPath;
if (strncmp(aPath, curpath, strlen(curpath)) == 0)
return NS_OK; // subpath already exists in the list
tempPtr = tempPtr->mNext;
}
//Append the aPath
nsHttpAuthPath *newAuthPath;
int newpathLen = strlen(aPath);
newAuthPath = (nsHttpAuthPath *) moz_xmalloc(sizeof(nsHttpAuthPath) + newpathLen);
if (!newAuthPath)
return NS_ERROR_OUT_OF_MEMORY;
memcpy(newAuthPath->mPath, aPath, newpathLen+1);
newAuthPath->mNext = nullptr;
if (!mRoot)
mRoot = newAuthPath; //first entry
else
mTail->mNext = newAuthPath; // Append newAuthPath
//update the tail pointer.
mTail = newAuthPath;
return NS_OK;
}
nsresult
nsHttpAuthEntry::Set(const char *path,
const char *realm,
const char *creds,
const char *chall,
const nsHttpAuthIdentity *ident,
nsISupports *metadata)
{
char *newRealm, *newCreds, *newChall;
int realmLen = realm ? strlen(realm) : 0;
int credsLen = creds ? strlen(creds) : 0;
int challLen = chall ? strlen(chall) : 0;
int len = realmLen + 1 + credsLen + 1 + challLen + 1;
newRealm = (char *) moz_xmalloc(len);
if (!newRealm)
return NS_ERROR_OUT_OF_MEMORY;
if (realm)
memcpy(newRealm, realm, realmLen);
newRealm[realmLen] = 0;
newCreds = &newRealm[realmLen + 1];
if (creds)
memcpy(newCreds, creds, credsLen);
newCreds[credsLen] = 0;
newChall = &newCreds[credsLen + 1];
if (chall)
memcpy(newChall, chall, challLen);
newChall[challLen] = 0;
nsresult rv = NS_OK;
if (ident) {
rv = mIdent.Set(*ident);
}
else if (mIdent.IsEmpty()) {
// If we are not given an identity and our cached identity has not been
// initialized yet (so is currently empty), initialize it now by
// filling it with nulls. We need to do that because consumers expect
// that mIdent is initialized after this function returns.
rv = mIdent.Set(nullptr, nullptr, nullptr);
}
if (NS_FAILED(rv)) {
free(newRealm);
return rv;
}
rv = AddPath(path);
if (NS_FAILED(rv)) {
free(newRealm);
return rv;
}
// wait until the end to clear member vars in case input params
// reference our members!
if (mRealm)
free(mRealm);
mRealm = newRealm;
mCreds = newCreds;
mChallenge = newChall;
mMetaData = metadata;
return NS_OK;
}
//-----------------------------------------------------------------------------
// nsHttpAuthNode
//-----------------------------------------------------------------------------
nsHttpAuthNode::nsHttpAuthNode()
{
LOG(("Creating nsHttpAuthNode @%x\n", this));
}
nsHttpAuthNode::~nsHttpAuthNode()
{
LOG(("Destroying nsHttpAuthNode @%x\n", this));
mList.Clear();
}
nsHttpAuthEntry *
nsHttpAuthNode::LookupEntryByPath(const char *path)
{
nsHttpAuthEntry *entry;
// null path matches empty path
if (!path)
path = "";
// look for an entry that either matches or contains this directory.
// ie. we'll give out credentials if the given directory is a sub-
// directory of an existing entry.
for (uint32_t i=0; i<mList.Length(); ++i) {
entry = mList[i];
nsHttpAuthPath *authPath = entry->RootPath();
while (authPath) {
const char *entryPath = authPath->mPath;
// proxy auth entries have no path, so require exact match on
// empty path string.
if (entryPath[0] == '\0') {
if (path[0] == '\0')
return entry;
}
else if (strncmp(path, entryPath, strlen(entryPath)) == 0)
return entry;
authPath = authPath->mNext;
}
}
return nullptr;
}
nsHttpAuthEntry *
nsHttpAuthNode::LookupEntryByRealm(const char *realm)
{
nsHttpAuthEntry *entry;
// null realm matches empty realm
if (!realm)
realm = "";
// look for an entry that matches this realm
uint32_t i;
for (i=0; i<mList.Length(); ++i) {
entry = mList[i];
if (strcmp(realm, entry->Realm()) == 0)
return entry;
}
return nullptr;
}
nsresult
nsHttpAuthNode::SetAuthEntry(const char *path,
const char *realm,
const char *creds,
const char *challenge,
const nsHttpAuthIdentity *ident,
nsISupports *metadata)
{
// look for an entry with a matching realm
nsHttpAuthEntry *entry = LookupEntryByRealm(realm);
if (!entry) {
entry = new nsHttpAuthEntry(path, realm, creds, challenge, ident, metadata);
if (!entry)
return NS_ERROR_OUT_OF_MEMORY;
mList.AppendElement(entry);
}
else {
// update the entry...
entry->Set(path, realm, creds, challenge, ident, metadata);
}
return NS_OK;
}
void
nsHttpAuthNode::ClearAuthEntry(const char *realm)
{
nsHttpAuthEntry *entry = LookupEntryByRealm(realm);
if (entry) {
mList.RemoveElement(entry); // double search OK
}
}
} // namespace net
} // namespace mozilla