Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 sw=2 et tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "mozilla/DebugOnly.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsXBLDocumentInfo.h" |
michael@0 | 10 | #include "nsIDocument.h" |
michael@0 | 11 | #include "nsXBLPrototypeBinding.h" |
michael@0 | 12 | #include "nsIScriptObjectPrincipal.h" |
michael@0 | 13 | #include "nsIScriptContext.h" |
michael@0 | 14 | #include "nsIDOMDocument.h" |
michael@0 | 15 | #include "nsIDOMScriptObjectFactory.h" |
michael@0 | 16 | #include "jsapi.h" |
michael@0 | 17 | #include "jsfriendapi.h" |
michael@0 | 18 | #include "nsIURI.h" |
michael@0 | 19 | #include "nsIConsoleService.h" |
michael@0 | 20 | #include "nsIScriptError.h" |
michael@0 | 21 | #include "nsIChromeRegistry.h" |
michael@0 | 22 | #include "nsIPrincipal.h" |
michael@0 | 23 | #include "nsJSPrincipals.h" |
michael@0 | 24 | #include "nsIScriptSecurityManager.h" |
michael@0 | 25 | #include "nsContentUtils.h" |
michael@0 | 26 | #include "nsCxPusher.h" |
michael@0 | 27 | #include "nsDOMJSUtils.h" |
michael@0 | 28 | #include "mozilla/Services.h" |
michael@0 | 29 | #include "xpcpublic.h" |
michael@0 | 30 | #include "mozilla/scache/StartupCache.h" |
michael@0 | 31 | #include "mozilla/scache/StartupCacheUtils.h" |
michael@0 | 32 | #include "nsCCUncollectableMarker.h" |
michael@0 | 33 | #include "mozilla/dom/BindingUtils.h" |
michael@0 | 34 | #include "mozilla/dom/URL.h" |
michael@0 | 35 | |
michael@0 | 36 | using namespace mozilla; |
michael@0 | 37 | using namespace mozilla::scache; |
michael@0 | 38 | using namespace mozilla::dom; |
michael@0 | 39 | |
michael@0 | 40 | static const char kXBLCachePrefix[] = "xblcache"; |
michael@0 | 41 | |
michael@0 | 42 | /* Implementation file */ |
michael@0 | 43 | |
michael@0 | 44 | static PLDHashOperator |
michael@0 | 45 | TraverseProtos(const nsACString &aKey, nsXBLPrototypeBinding *aProto, void* aClosure) |
michael@0 | 46 | { |
michael@0 | 47 | nsCycleCollectionTraversalCallback *cb = |
michael@0 | 48 | static_cast<nsCycleCollectionTraversalCallback*>(aClosure); |
michael@0 | 49 | aProto->Traverse(*cb); |
michael@0 | 50 | return PL_DHASH_NEXT; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | static PLDHashOperator |
michael@0 | 54 | UnlinkProtoJSObjects(const nsACString &aKey, nsXBLPrototypeBinding *aProto, void* aClosure) |
michael@0 | 55 | { |
michael@0 | 56 | aProto->UnlinkJSObjects(); |
michael@0 | 57 | return PL_DHASH_NEXT; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | struct ProtoTracer |
michael@0 | 61 | { |
michael@0 | 62 | const TraceCallbacks &mCallbacks; |
michael@0 | 63 | void *mClosure; |
michael@0 | 64 | }; |
michael@0 | 65 | |
michael@0 | 66 | static PLDHashOperator |
michael@0 | 67 | TraceProtos(const nsACString &aKey, nsXBLPrototypeBinding *aProto, void* aClosure) |
michael@0 | 68 | { |
michael@0 | 69 | ProtoTracer* closure = static_cast<ProtoTracer*>(aClosure); |
michael@0 | 70 | aProto->Trace(closure->mCallbacks, closure->mClosure); |
michael@0 | 71 | return PL_DHASH_NEXT; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | NS_IMPL_CYCLE_COLLECTION_CLASS(nsXBLDocumentInfo) |
michael@0 | 75 | |
michael@0 | 76 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXBLDocumentInfo) |
michael@0 | 77 | if (tmp->mBindingTable) { |
michael@0 | 78 | tmp->mBindingTable->EnumerateRead(UnlinkProtoJSObjects, nullptr); |
michael@0 | 79 | } |
michael@0 | 80 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument) |
michael@0 | 81 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
michael@0 | 82 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXBLDocumentInfo) |
michael@0 | 83 | if (tmp->mDocument && |
michael@0 | 84 | nsCCUncollectableMarker::InGeneration(cb, tmp->mDocument->GetMarkedCCGeneration())) { |
michael@0 | 85 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS |
michael@0 | 86 | return NS_SUCCESS_INTERRUPTED_TRAVERSE; |
michael@0 | 87 | } |
michael@0 | 88 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument) |
michael@0 | 89 | if (tmp->mBindingTable) { |
michael@0 | 90 | tmp->mBindingTable->EnumerateRead(TraverseProtos, &cb); |
michael@0 | 91 | } |
michael@0 | 92 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS |
michael@0 | 93 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
michael@0 | 94 | NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsXBLDocumentInfo) |
michael@0 | 95 | if (tmp->mBindingTable) { |
michael@0 | 96 | ProtoTracer closure = { aCallbacks, aClosure }; |
michael@0 | 97 | tmp->mBindingTable->EnumerateRead(TraceProtos, &closure); |
michael@0 | 98 | } |
michael@0 | 99 | NS_IMPL_CYCLE_COLLECTION_TRACE_END |
michael@0 | 100 | |
michael@0 | 101 | static void |
michael@0 | 102 | UnmarkXBLJSObject(void* aP, const char* aName, void* aClosure) |
michael@0 | 103 | { |
michael@0 | 104 | JS::ExposeObjectToActiveJS(static_cast<JSObject*>(aP)); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | static PLDHashOperator |
michael@0 | 108 | UnmarkProtos(const nsACString &aKey, nsXBLPrototypeBinding *aProto, void* aClosure) |
michael@0 | 109 | { |
michael@0 | 110 | aProto->Trace(TraceCallbackFunc(UnmarkXBLJSObject), nullptr); |
michael@0 | 111 | return PL_DHASH_NEXT; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | void |
michael@0 | 115 | nsXBLDocumentInfo::MarkInCCGeneration(uint32_t aGeneration) |
michael@0 | 116 | { |
michael@0 | 117 | if (mDocument) { |
michael@0 | 118 | mDocument->MarkUncollectableForCCGeneration(aGeneration); |
michael@0 | 119 | } |
michael@0 | 120 | // Unmark any JS we hold |
michael@0 | 121 | if (mBindingTable) { |
michael@0 | 122 | mBindingTable->EnumerateRead(UnmarkProtos, nullptr); |
michael@0 | 123 | } |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXBLDocumentInfo) |
michael@0 | 127 | NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) |
michael@0 | 128 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 129 | NS_INTERFACE_MAP_END |
michael@0 | 130 | |
michael@0 | 131 | NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXBLDocumentInfo) |
michael@0 | 132 | NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXBLDocumentInfo) |
michael@0 | 133 | |
michael@0 | 134 | nsXBLDocumentInfo::nsXBLDocumentInfo(nsIDocument* aDocument) |
michael@0 | 135 | : mDocument(aDocument), |
michael@0 | 136 | mScriptAccess(true), |
michael@0 | 137 | mIsChrome(false), |
michael@0 | 138 | mFirstBinding(nullptr) |
michael@0 | 139 | { |
michael@0 | 140 | nsIURI* uri = aDocument->GetDocumentURI(); |
michael@0 | 141 | if (IsChromeURI(uri)) { |
michael@0 | 142 | // Cache whether or not this chrome XBL can execute scripts. |
michael@0 | 143 | nsCOMPtr<nsIXULChromeRegistry> reg = |
michael@0 | 144 | mozilla::services::GetXULChromeRegistryService(); |
michael@0 | 145 | if (reg) { |
michael@0 | 146 | bool allow = true; |
michael@0 | 147 | reg->AllowScriptsForPackage(uri, &allow); |
michael@0 | 148 | mScriptAccess = allow; |
michael@0 | 149 | } |
michael@0 | 150 | mIsChrome = true; |
michael@0 | 151 | } else { |
michael@0 | 152 | // If this binding isn't running with system principal, then it's running |
michael@0 | 153 | // from a remote-XUL whitelisted domain. This is already a not-really- |
michael@0 | 154 | // supported configuration (among other things, we don't use XBL scopes in |
michael@0 | 155 | // that configuration for compatibility reasons). But we should still at |
michael@0 | 156 | // least make an effort to prevent binding code from running if content |
michael@0 | 157 | // script is disabled or if the source domain is blacklisted (since the |
michael@0 | 158 | // source domain for remote XBL must always be the same as the source domain |
michael@0 | 159 | // of the bound content). |
michael@0 | 160 | // |
michael@0 | 161 | // If we just ask the binding document if script is enabled, it will |
michael@0 | 162 | // discover that it has no inner window, and return false. So instead, we |
michael@0 | 163 | // short-circuit the normal compartment-managed script-disabling machinery, |
michael@0 | 164 | // and query the policy for the URI directly. |
michael@0 | 165 | bool allow; |
michael@0 | 166 | nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager(); |
michael@0 | 167 | nsresult rv = ssm->PolicyAllowsScript(uri, &allow); |
michael@0 | 168 | mScriptAccess = NS_SUCCEEDED(rv) && allow; |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | nsXBLDocumentInfo::~nsXBLDocumentInfo() |
michael@0 | 173 | { |
michael@0 | 174 | mozilla::DropJSObjects(this); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | nsXBLPrototypeBinding* |
michael@0 | 178 | nsXBLDocumentInfo::GetPrototypeBinding(const nsACString& aRef) |
michael@0 | 179 | { |
michael@0 | 180 | if (!mBindingTable) |
michael@0 | 181 | return nullptr; |
michael@0 | 182 | |
michael@0 | 183 | if (aRef.IsEmpty()) { |
michael@0 | 184 | // Return our first binding |
michael@0 | 185 | return mFirstBinding; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | return mBindingTable->Get(aRef); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | nsresult |
michael@0 | 192 | nsXBLDocumentInfo::SetPrototypeBinding(const nsACString& aRef, nsXBLPrototypeBinding* aBinding) |
michael@0 | 193 | { |
michael@0 | 194 | if (!mBindingTable) { |
michael@0 | 195 | mBindingTable = new nsClassHashtable<nsCStringHashKey, nsXBLPrototypeBinding>(); |
michael@0 | 196 | mozilla::HoldJSObjects(this); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | NS_ENSURE_STATE(!mBindingTable->Get(aRef)); |
michael@0 | 200 | mBindingTable->Put(aRef, aBinding); |
michael@0 | 201 | |
michael@0 | 202 | return NS_OK; |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | void |
michael@0 | 206 | nsXBLDocumentInfo::RemovePrototypeBinding(const nsACString& aRef) |
michael@0 | 207 | { |
michael@0 | 208 | if (mBindingTable) { |
michael@0 | 209 | nsAutoPtr<nsXBLPrototypeBinding> bindingToRemove; |
michael@0 | 210 | mBindingTable->RemoveAndForget(aRef, bindingToRemove); |
michael@0 | 211 | |
michael@0 | 212 | // We do not want to destroy the binding, so just forget it. |
michael@0 | 213 | bindingToRemove.forget(); |
michael@0 | 214 | } |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | // Callback to enumerate over the bindings from this document and write them |
michael@0 | 218 | // out to the cache. |
michael@0 | 219 | static PLDHashOperator |
michael@0 | 220 | WriteBinding(const nsACString &aKey, nsXBLPrototypeBinding *aProto, void* aClosure) |
michael@0 | 221 | { |
michael@0 | 222 | aProto->Write((nsIObjectOutputStream*)aClosure); |
michael@0 | 223 | |
michael@0 | 224 | return PL_DHASH_NEXT; |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | // static |
michael@0 | 228 | nsresult |
michael@0 | 229 | nsXBLDocumentInfo::ReadPrototypeBindings(nsIURI* aURI, nsXBLDocumentInfo** aDocInfo) |
michael@0 | 230 | { |
michael@0 | 231 | *aDocInfo = nullptr; |
michael@0 | 232 | |
michael@0 | 233 | nsAutoCString spec(kXBLCachePrefix); |
michael@0 | 234 | nsresult rv = PathifyURI(aURI, spec); |
michael@0 | 235 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 236 | |
michael@0 | 237 | StartupCache* startupCache = StartupCache::GetSingleton(); |
michael@0 | 238 | NS_ENSURE_TRUE(startupCache, NS_ERROR_FAILURE); |
michael@0 | 239 | |
michael@0 | 240 | nsAutoArrayPtr<char> buf; |
michael@0 | 241 | uint32_t len; |
michael@0 | 242 | rv = startupCache->GetBuffer(spec.get(), getter_Transfers(buf), &len); |
michael@0 | 243 | // GetBuffer will fail if the binding is not in the cache. |
michael@0 | 244 | if (NS_FAILED(rv)) |
michael@0 | 245 | return rv; |
michael@0 | 246 | |
michael@0 | 247 | nsCOMPtr<nsIObjectInputStream> stream; |
michael@0 | 248 | rv = NewObjectInputStreamFromBuffer(buf, len, getter_AddRefs(stream)); |
michael@0 | 249 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 250 | buf.forget(); |
michael@0 | 251 | |
michael@0 | 252 | // The file compatibility.ini stores the build id. This is checked in |
michael@0 | 253 | // nsAppRunner.cpp and will delete the cache if a different build is |
michael@0 | 254 | // present. However, we check that the version matches here to be safe. |
michael@0 | 255 | uint32_t version; |
michael@0 | 256 | rv = stream->Read32(&version); |
michael@0 | 257 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 258 | if (version != XBLBinding_Serialize_Version) { |
michael@0 | 259 | // The version that exists is different than expected, likely created with a |
michael@0 | 260 | // different build, so invalidate the cache. |
michael@0 | 261 | startupCache->InvalidateCache(); |
michael@0 | 262 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | nsCOMPtr<nsIPrincipal> principal; |
michael@0 | 266 | nsContentUtils::GetSecurityManager()-> |
michael@0 | 267 | GetSystemPrincipal(getter_AddRefs(principal)); |
michael@0 | 268 | |
michael@0 | 269 | nsCOMPtr<nsIDOMDocument> domdoc; |
michael@0 | 270 | rv = NS_NewXBLDocument(getter_AddRefs(domdoc), aURI, nullptr, principal); |
michael@0 | 271 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 272 | |
michael@0 | 273 | nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc); |
michael@0 | 274 | NS_ASSERTION(doc, "Must have a document!"); |
michael@0 | 275 | nsRefPtr<nsXBLDocumentInfo> docInfo = new nsXBLDocumentInfo(doc); |
michael@0 | 276 | |
michael@0 | 277 | while (1) { |
michael@0 | 278 | uint8_t flags; |
michael@0 | 279 | nsresult rv = stream->Read8(&flags); |
michael@0 | 280 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 281 | if (flags == XBLBinding_Serialize_NoMoreBindings) |
michael@0 | 282 | break; |
michael@0 | 283 | |
michael@0 | 284 | rv = nsXBLPrototypeBinding::ReadNewBinding(stream, docInfo, doc, flags); |
michael@0 | 285 | if (NS_FAILED(rv)) { |
michael@0 | 286 | return rv; |
michael@0 | 287 | } |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | docInfo.swap(*aDocInfo); |
michael@0 | 291 | return NS_OK; |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | nsresult |
michael@0 | 295 | nsXBLDocumentInfo::WritePrototypeBindings() |
michael@0 | 296 | { |
michael@0 | 297 | // Only write out bindings with the system principal |
michael@0 | 298 | if (!nsContentUtils::IsSystemPrincipal(mDocument->NodePrincipal())) |
michael@0 | 299 | return NS_OK; |
michael@0 | 300 | |
michael@0 | 301 | nsAutoCString spec(kXBLCachePrefix); |
michael@0 | 302 | nsresult rv = PathifyURI(DocumentURI(), spec); |
michael@0 | 303 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 304 | |
michael@0 | 305 | StartupCache* startupCache = StartupCache::GetSingleton(); |
michael@0 | 306 | NS_ENSURE_TRUE(startupCache, rv); |
michael@0 | 307 | |
michael@0 | 308 | nsCOMPtr<nsIObjectOutputStream> stream; |
michael@0 | 309 | nsCOMPtr<nsIStorageStream> storageStream; |
michael@0 | 310 | rv = NewObjectOutputWrappedStorageStream(getter_AddRefs(stream), |
michael@0 | 311 | getter_AddRefs(storageStream), |
michael@0 | 312 | true); |
michael@0 | 313 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 314 | |
michael@0 | 315 | rv = stream->Write32(XBLBinding_Serialize_Version); |
michael@0 | 316 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 317 | |
michael@0 | 318 | if (mBindingTable) { |
michael@0 | 319 | mBindingTable->EnumerateRead(WriteBinding, stream); |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | // write a end marker at the end |
michael@0 | 323 | rv = stream->Write8(XBLBinding_Serialize_NoMoreBindings); |
michael@0 | 324 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 325 | |
michael@0 | 326 | stream->Close(); |
michael@0 | 327 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 328 | |
michael@0 | 329 | uint32_t len; |
michael@0 | 330 | nsAutoArrayPtr<char> buf; |
michael@0 | 331 | rv = NewBufferFromStorageStream(storageStream, getter_Transfers(buf), &len); |
michael@0 | 332 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 333 | |
michael@0 | 334 | return startupCache->PutBuffer(spec.get(), buf, len); |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | void |
michael@0 | 338 | nsXBLDocumentInfo::SetFirstPrototypeBinding(nsXBLPrototypeBinding* aBinding) |
michael@0 | 339 | { |
michael@0 | 340 | mFirstBinding = aBinding; |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | static PLDHashOperator |
michael@0 | 344 | FlushScopedSkinSheets(const nsACString &aKey, nsXBLPrototypeBinding *aProto, void* aClosure) |
michael@0 | 345 | { |
michael@0 | 346 | aProto->FlushSkinSheets(); |
michael@0 | 347 | return PL_DHASH_NEXT; |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | void |
michael@0 | 351 | nsXBLDocumentInfo::FlushSkinStylesheets() |
michael@0 | 352 | { |
michael@0 | 353 | if (mBindingTable) { |
michael@0 | 354 | mBindingTable->EnumerateRead(FlushScopedSkinSheets, nullptr); |
michael@0 | 355 | } |
michael@0 | 356 | } |
michael@0 | 357 | |
michael@0 | 358 | #ifdef DEBUG |
michael@0 | 359 | void |
michael@0 | 360 | AssertInCompilationScope() |
michael@0 | 361 | { |
michael@0 | 362 | AutoJSContext cx; |
michael@0 | 363 | // Note - Inverting the order of these operands is a rooting hazard. |
michael@0 | 364 | MOZ_ASSERT(xpc::GetCompilationScope() == JS::CurrentGlobalOrNull(cx)); |
michael@0 | 365 | } |
michael@0 | 366 | #endif |