Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "base/basictypes.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsAboutProtocolHandler.h" |
michael@0 | 9 | #include "nsIURI.h" |
michael@0 | 10 | #include "nsIAboutModule.h" |
michael@0 | 11 | #include "nsString.h" |
michael@0 | 12 | #include "nsNetCID.h" |
michael@0 | 13 | #include "nsAboutProtocolUtils.h" |
michael@0 | 14 | #include "nsError.h" |
michael@0 | 15 | #include "nsNetUtil.h" |
michael@0 | 16 | #include "nsIObjectInputStream.h" |
michael@0 | 17 | #include "nsIObjectOutputStream.h" |
michael@0 | 18 | #include "nsAutoPtr.h" |
michael@0 | 19 | #include "nsIWritablePropertyBag2.h" |
michael@0 | 20 | |
michael@0 | 21 | static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID); |
michael@0 | 22 | static NS_DEFINE_CID(kNestedAboutURICID, NS_NESTEDABOUTURI_CID); |
michael@0 | 23 | |
michael@0 | 24 | static bool IsSafeForUntrustedContent(nsIAboutModule *aModule, nsIURI *aURI) { |
michael@0 | 25 | uint32_t flags; |
michael@0 | 26 | nsresult rv = aModule->GetURIFlags(aURI, &flags); |
michael@0 | 27 | NS_ENSURE_SUCCESS(rv, false); |
michael@0 | 28 | |
michael@0 | 29 | return (flags & nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT) != 0; |
michael@0 | 30 | } |
michael@0 | 31 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 32 | |
michael@0 | 33 | NS_IMPL_ISUPPORTS(nsAboutProtocolHandler, nsIProtocolHandler) |
michael@0 | 34 | |
michael@0 | 35 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 36 | // nsIProtocolHandler methods: |
michael@0 | 37 | |
michael@0 | 38 | NS_IMETHODIMP |
michael@0 | 39 | nsAboutProtocolHandler::GetScheme(nsACString &result) |
michael@0 | 40 | { |
michael@0 | 41 | result.AssignLiteral("about"); |
michael@0 | 42 | return NS_OK; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | NS_IMETHODIMP |
michael@0 | 46 | nsAboutProtocolHandler::GetDefaultPort(int32_t *result) |
michael@0 | 47 | { |
michael@0 | 48 | *result = -1; // no port for about: URLs |
michael@0 | 49 | return NS_OK; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | NS_IMETHODIMP |
michael@0 | 53 | nsAboutProtocolHandler::GetProtocolFlags(uint32_t *result) |
michael@0 | 54 | { |
michael@0 | 55 | *result = URI_NORELATIVE | URI_NOAUTH | URI_DANGEROUS_TO_LOAD; |
michael@0 | 56 | return NS_OK; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | NS_IMETHODIMP |
michael@0 | 60 | nsAboutProtocolHandler::NewURI(const nsACString &aSpec, |
michael@0 | 61 | const char *aCharset, // ignore charset info |
michael@0 | 62 | nsIURI *aBaseURI, |
michael@0 | 63 | nsIURI **result) |
michael@0 | 64 | { |
michael@0 | 65 | *result = nullptr; |
michael@0 | 66 | nsresult rv; |
michael@0 | 67 | |
michael@0 | 68 | // Use a simple URI to parse out some stuff first |
michael@0 | 69 | nsCOMPtr<nsIURI> url = do_CreateInstance(kSimpleURICID, &rv); |
michael@0 | 70 | if (NS_FAILED(rv)) return rv; |
michael@0 | 71 | |
michael@0 | 72 | rv = url->SetSpec(aSpec); |
michael@0 | 73 | if (NS_FAILED(rv)) { |
michael@0 | 74 | return rv; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | // Unfortunately, people create random about: URIs that don't correspond to |
michael@0 | 78 | // about: modules... Since those URIs will never open a channel, might as |
michael@0 | 79 | // well consider them unsafe for better perf, and just in case. |
michael@0 | 80 | bool isSafe = false; |
michael@0 | 81 | |
michael@0 | 82 | nsCOMPtr<nsIAboutModule> aboutMod; |
michael@0 | 83 | rv = NS_GetAboutModule(url, getter_AddRefs(aboutMod)); |
michael@0 | 84 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 85 | isSafe = IsSafeForUntrustedContent(aboutMod, url); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | if (isSafe) { |
michael@0 | 89 | // We need to indicate that this baby is safe. Use an inner URI that |
michael@0 | 90 | // no one but the security manager will see. Make sure to preserve our |
michael@0 | 91 | // path, in case someone decides to hardcode checks for particular |
michael@0 | 92 | // about: URIs somewhere. |
michael@0 | 93 | nsAutoCString spec; |
michael@0 | 94 | rv = url->GetPath(spec); |
michael@0 | 95 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 96 | |
michael@0 | 97 | spec.Insert("moz-safe-about:", 0); |
michael@0 | 98 | |
michael@0 | 99 | nsCOMPtr<nsIURI> inner; |
michael@0 | 100 | rv = NS_NewURI(getter_AddRefs(inner), spec); |
michael@0 | 101 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 102 | |
michael@0 | 103 | nsSimpleNestedURI* outer = new nsNestedAboutURI(inner, aBaseURI); |
michael@0 | 104 | NS_ENSURE_TRUE(outer, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 105 | |
michael@0 | 106 | // Take a ref to it in the COMPtr we plan to return |
michael@0 | 107 | url = outer; |
michael@0 | 108 | |
michael@0 | 109 | rv = outer->SetSpec(aSpec); |
michael@0 | 110 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | // We don't want to allow mutation, since it would allow safe and |
michael@0 | 114 | // unsafe URIs to change into each other... |
michael@0 | 115 | NS_TryToSetImmutable(url); |
michael@0 | 116 | url.swap(*result); |
michael@0 | 117 | return NS_OK; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | NS_IMETHODIMP |
michael@0 | 121 | nsAboutProtocolHandler::NewChannel(nsIURI* uri, nsIChannel* *result) |
michael@0 | 122 | { |
michael@0 | 123 | NS_ENSURE_ARG_POINTER(uri); |
michael@0 | 124 | |
michael@0 | 125 | // about:what you ask? |
michael@0 | 126 | nsCOMPtr<nsIAboutModule> aboutMod; |
michael@0 | 127 | nsresult rv = NS_GetAboutModule(uri, getter_AddRefs(aboutMod)); |
michael@0 | 128 | |
michael@0 | 129 | nsAutoCString path; |
michael@0 | 130 | nsresult rv2 = NS_GetAboutModuleName(uri, path); |
michael@0 | 131 | if (NS_SUCCEEDED(rv2) && path.EqualsLiteral("srcdoc")) { |
michael@0 | 132 | // about:srcdoc is meant to be unresolvable, yet is included in the |
michael@0 | 133 | // about lookup tables so that it can pass security checks when used in |
michael@0 | 134 | // a srcdoc iframe. To ensure that it stays unresolvable, we pretend |
michael@0 | 135 | // that it doesn't exist. |
michael@0 | 136 | rv = NS_ERROR_FACTORY_NOT_REGISTERED; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 140 | // The standard return case: |
michael@0 | 141 | rv = aboutMod->NewChannel(uri, result); |
michael@0 | 142 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 143 | // If this URI is safe for untrusted content, enforce that its |
michael@0 | 144 | // principal be based on the channel's originalURI by setting the |
michael@0 | 145 | // owner to null. |
michael@0 | 146 | // Note: this relies on aboutMod's newChannel implementation |
michael@0 | 147 | // having set the proper originalURI, which probably isn't ideal. |
michael@0 | 148 | if (IsSafeForUntrustedContent(aboutMod, uri)) { |
michael@0 | 149 | (*result)->SetOwner(nullptr); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | nsRefPtr<nsNestedAboutURI> aboutURI; |
michael@0 | 153 | nsresult rv2 = uri->QueryInterface(kNestedAboutURICID, |
michael@0 | 154 | getter_AddRefs(aboutURI)); |
michael@0 | 155 | if (NS_SUCCEEDED(rv2) && aboutURI->GetBaseURI()) { |
michael@0 | 156 | nsCOMPtr<nsIWritablePropertyBag2> writableBag = |
michael@0 | 157 | do_QueryInterface(*result); |
michael@0 | 158 | if (writableBag) { |
michael@0 | 159 | writableBag-> |
michael@0 | 160 | SetPropertyAsInterface(NS_LITERAL_STRING("baseURI"), |
michael@0 | 161 | aboutURI->GetBaseURI()); |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | } |
michael@0 | 165 | return rv; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | // mumble... |
michael@0 | 169 | |
michael@0 | 170 | if (rv == NS_ERROR_FACTORY_NOT_REGISTERED) { |
michael@0 | 171 | // This looks like an about: we don't know about. Convert |
michael@0 | 172 | // this to an invalid URI error. |
michael@0 | 173 | rv = NS_ERROR_MALFORMED_URI; |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | return rv; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | NS_IMETHODIMP |
michael@0 | 180 | nsAboutProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval) |
michael@0 | 181 | { |
michael@0 | 182 | // don't override anything. |
michael@0 | 183 | *_retval = false; |
michael@0 | 184 | return NS_OK; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 188 | // Safe about protocol handler impl |
michael@0 | 189 | |
michael@0 | 190 | NS_IMPL_ISUPPORTS(nsSafeAboutProtocolHandler, nsIProtocolHandler) |
michael@0 | 191 | |
michael@0 | 192 | // nsIProtocolHandler methods: |
michael@0 | 193 | |
michael@0 | 194 | NS_IMETHODIMP |
michael@0 | 195 | nsSafeAboutProtocolHandler::GetScheme(nsACString &result) |
michael@0 | 196 | { |
michael@0 | 197 | result.AssignLiteral("moz-safe-about"); |
michael@0 | 198 | return NS_OK; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | NS_IMETHODIMP |
michael@0 | 202 | nsSafeAboutProtocolHandler::GetDefaultPort(int32_t *result) |
michael@0 | 203 | { |
michael@0 | 204 | *result = -1; // no port for moz-safe-about: URLs |
michael@0 | 205 | return NS_OK; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | NS_IMETHODIMP |
michael@0 | 209 | nsSafeAboutProtocolHandler::GetProtocolFlags(uint32_t *result) |
michael@0 | 210 | { |
michael@0 | 211 | *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE | URI_SAFE_TO_LOAD_IN_SECURE_CONTEXT; |
michael@0 | 212 | return NS_OK; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | NS_IMETHODIMP |
michael@0 | 216 | nsSafeAboutProtocolHandler::NewURI(const nsACString &aSpec, |
michael@0 | 217 | const char *aCharset, // ignore charset info |
michael@0 | 218 | nsIURI *aBaseURI, |
michael@0 | 219 | nsIURI **result) |
michael@0 | 220 | { |
michael@0 | 221 | nsresult rv; |
michael@0 | 222 | |
michael@0 | 223 | nsCOMPtr<nsIURI> url = do_CreateInstance(kSimpleURICID, &rv); |
michael@0 | 224 | if (NS_FAILED(rv)) return rv; |
michael@0 | 225 | |
michael@0 | 226 | rv = url->SetSpec(aSpec); |
michael@0 | 227 | if (NS_FAILED(rv)) { |
michael@0 | 228 | return rv; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | NS_TryToSetImmutable(url); |
michael@0 | 232 | |
michael@0 | 233 | *result = nullptr; |
michael@0 | 234 | url.swap(*result); |
michael@0 | 235 | return rv; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | NS_IMETHODIMP |
michael@0 | 239 | nsSafeAboutProtocolHandler::NewChannel(nsIURI* uri, nsIChannel* *result) |
michael@0 | 240 | { |
michael@0 | 241 | *result = nullptr; |
michael@0 | 242 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | NS_IMETHODIMP |
michael@0 | 246 | nsSafeAboutProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval) |
michael@0 | 247 | { |
michael@0 | 248 | // don't override anything. |
michael@0 | 249 | *_retval = false; |
michael@0 | 250 | return NS_OK; |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | //////////////////////////////////////////////////////////// |
michael@0 | 254 | // nsNestedAboutURI implementation |
michael@0 | 255 | NS_INTERFACE_MAP_BEGIN(nsNestedAboutURI) |
michael@0 | 256 | if (aIID.Equals(kNestedAboutURICID)) |
michael@0 | 257 | foundInterface = static_cast<nsIURI*>(this); |
michael@0 | 258 | else |
michael@0 | 259 | NS_INTERFACE_MAP_END_INHERITING(nsSimpleNestedURI) |
michael@0 | 260 | |
michael@0 | 261 | // nsISerializable |
michael@0 | 262 | NS_IMETHODIMP |
michael@0 | 263 | nsNestedAboutURI::Read(nsIObjectInputStream* aStream) |
michael@0 | 264 | { |
michael@0 | 265 | nsresult rv = nsSimpleNestedURI::Read(aStream); |
michael@0 | 266 | if (NS_FAILED(rv)) return rv; |
michael@0 | 267 | |
michael@0 | 268 | bool haveBase; |
michael@0 | 269 | rv = aStream->ReadBoolean(&haveBase); |
michael@0 | 270 | if (NS_FAILED(rv)) return rv; |
michael@0 | 271 | |
michael@0 | 272 | if (haveBase) { |
michael@0 | 273 | nsCOMPtr<nsISupports> supports; |
michael@0 | 274 | rv = aStream->ReadObject(true, getter_AddRefs(supports)); |
michael@0 | 275 | if (NS_FAILED(rv)) return rv; |
michael@0 | 276 | |
michael@0 | 277 | mBaseURI = do_QueryInterface(supports, &rv); |
michael@0 | 278 | if (NS_FAILED(rv)) return rv; |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | return NS_OK; |
michael@0 | 282 | } |
michael@0 | 283 | |
michael@0 | 284 | NS_IMETHODIMP |
michael@0 | 285 | nsNestedAboutURI::Write(nsIObjectOutputStream* aStream) |
michael@0 | 286 | { |
michael@0 | 287 | nsresult rv = nsSimpleNestedURI::Write(aStream); |
michael@0 | 288 | if (NS_FAILED(rv)) return rv; |
michael@0 | 289 | |
michael@0 | 290 | rv = aStream->WriteBoolean(mBaseURI != nullptr); |
michael@0 | 291 | if (NS_FAILED(rv)) return rv; |
michael@0 | 292 | |
michael@0 | 293 | if (mBaseURI) { |
michael@0 | 294 | // A previous iteration of this code wrote out mBaseURI as nsISupports |
michael@0 | 295 | // and then read it in as nsIURI, which is non-kosher when mBaseURI |
michael@0 | 296 | // implements more than just a single line of interfaces and the |
michael@0 | 297 | // canonical nsISupports* isn't the one a static_cast<> of mBaseURI |
michael@0 | 298 | // would produce. For backwards compatibility with existing |
michael@0 | 299 | // serializations we continue to write mBaseURI as nsISupports but |
michael@0 | 300 | // switch to reading it as nsISupports, with a post-read QI to get to |
michael@0 | 301 | // nsIURI. |
michael@0 | 302 | rv = aStream->WriteCompoundObject(mBaseURI, NS_GET_IID(nsISupports), |
michael@0 | 303 | true); |
michael@0 | 304 | if (NS_FAILED(rv)) return rv; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | return NS_OK; |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | // nsSimpleURI |
michael@0 | 311 | /* virtual */ nsSimpleURI* |
michael@0 | 312 | nsNestedAboutURI::StartClone(nsSimpleURI::RefHandlingEnum aRefHandlingMode) |
michael@0 | 313 | { |
michael@0 | 314 | // Sadly, we can't make use of nsSimpleNestedURI::StartClone here. |
michael@0 | 315 | // However, this function is expected to exactly match that function, |
michael@0 | 316 | // aside from the "new ns***URI()" call. |
michael@0 | 317 | NS_ENSURE_TRUE(mInnerURI, nullptr); |
michael@0 | 318 | |
michael@0 | 319 | nsCOMPtr<nsIURI> innerClone; |
michael@0 | 320 | nsresult rv = aRefHandlingMode == eHonorRef ? |
michael@0 | 321 | mInnerURI->Clone(getter_AddRefs(innerClone)) : |
michael@0 | 322 | mInnerURI->CloneIgnoringRef(getter_AddRefs(innerClone)); |
michael@0 | 323 | |
michael@0 | 324 | if (NS_FAILED(rv)) { |
michael@0 | 325 | return nullptr; |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | nsNestedAboutURI* url = new nsNestedAboutURI(innerClone, mBaseURI); |
michael@0 | 329 | url->SetMutable(false); |
michael@0 | 330 | |
michael@0 | 331 | return url; |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | // nsIClassInfo |
michael@0 | 335 | NS_IMETHODIMP |
michael@0 | 336 | nsNestedAboutURI::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc) |
michael@0 | 337 | { |
michael@0 | 338 | *aClassIDNoAlloc = kNestedAboutURICID; |
michael@0 | 339 | return NS_OK; |
michael@0 | 340 | } |