From f7a686456c8debd005b5ed7eb79797576cc48357 Mon Sep 17 00:00:00 2001 From: Daniel Holbert Date: Fri, 27 May 2011 21:55:20 -0700 Subject: [PATCH] Bug 660066 patch 2: Prevent nsFileDataURI from QI'ing to nsSimpleURI. r=bz --- .../base/src/nsFileDataProtocolHandler.cpp | 13 +++++- netwerk/test/unit/test_bug660066.js | 43 +++++++++++++++++++ netwerk/test/unit/xpcshell.ini | 1 + 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 netwerk/test/unit/test_bug660066.js diff --git a/content/base/src/nsFileDataProtocolHandler.cpp b/content/base/src/nsFileDataProtocolHandler.cpp index 394e702f33df..b0d06a51447f 100644 --- a/content/base/src/nsFileDataProtocolHandler.cpp +++ b/content/base/src/nsFileDataProtocolHandler.cpp @@ -161,12 +161,23 @@ public: nsCOMPtr mPrincipal; }; +static NS_DEFINE_CID(kThisSimpleURIImplementationCID, + NS_THIS_SIMPLEURI_IMPLEMENTATION_CID); + NS_IMPL_ADDREF_INHERITED(nsFileDataURI, nsSimpleURI) NS_IMPL_RELEASE_INHERITED(nsFileDataURI, nsSimpleURI) + NS_INTERFACE_MAP_BEGIN(nsFileDataURI) NS_INTERFACE_MAP_ENTRY(nsIURIWithPrincipal) if (aIID.Equals(kFILEDATAURICID)) - foundInterface = static_cast(this); + foundInterface = static_cast(this); + else if (aIID.Equals(kThisSimpleURIImplementationCID)) { + // Need to return explicitly here, because if we just set foundInterface + // to null the NS_INTERFACE_MAP_END_INHERITING will end up calling into + // nsSimplURI::QueryInterface and finding something for this CID. + *aInstancePtr = nsnull; + return NS_NOINTERFACE; + } else NS_INTERFACE_MAP_END_INHERITING(nsSimpleURI) diff --git a/netwerk/test/unit/test_bug660066.js b/netwerk/test/unit/test_bug660066.js new file mode 100644 index 000000000000..abfc391ddf29 --- /dev/null +++ b/netwerk/test/unit/test_bug660066.js @@ -0,0 +1,43 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +Components.utils.import("resource://gre/modules/NetUtil.jsm"); +const Ci = Components.interfaces; +const SIMPLEURI_SPEC = "data:text/plain,hello world"; +const FILEDATA_SPEC = "moz-filedata:123456"; + +function do_info(text, stack) { + if (!stack) + stack = Components.stack.caller; + + dump( "\n" + + "TEST-INFO | " + stack.filename + " | [" + stack.name + " : " + + stack.lineNumber + "] " + text + "\n"); +} + +function do_check_uri_neq(uri1, uri2) +{ + do_info("Checking equality in forward direction..."); + do_check_false(uri1.equals(uri2)); + do_check_false(uri1.equalsExceptRef(uri2)); + + do_info("Checking equality in reverse direction..."); + do_check_false(uri2.equals(uri1)); + do_check_false(uri2.equalsExceptRef(uri1)); +} + +function run_test() +{ + var simpleURI = NetUtil.newURI(SIMPLEURI_SPEC); + var fileDataURI = NetUtil.newURI(FILEDATA_SPEC); + + do_info("Checking that " + SIMPLEURI_SPEC + " != " + FILEDATA_SPEC); + do_check_uri_neq(simpleURI, fileDataURI); + + do_info("Changing the nsSimpleURI spec to match the nsFileDataURI"); + simpleURI.spec = FILEDATA_SPEC; + + do_info("Verifying that .spec matches"); + do_check_eq(simpleURI.spec, fileDataURI.spec); + + do_info("Checking that nsSimpleURI != nsFileDataURI despite their .spec matching") + do_check_uri_neq(simpleURI, fileDataURI); +} diff --git a/netwerk/test/unit/xpcshell.ini b/netwerk/test/unit/xpcshell.ini index 9d0387f15fbc..8e11bae64429 100644 --- a/netwerk/test/unit/xpcshell.ini +++ b/netwerk/test/unit/xpcshell.ini @@ -60,6 +60,7 @@ tail = [test_bug652761.js] [test_bug651100.js] [test_bug659569.js] +[test_bug660066.js] [test_cacheflags.js] [test_channel_close.js] [test_compareURIs.js]