Ticket #13033: 0001-Bug-13033-Backport-for-Redirects-should-go-through-M.patch

File 0001-Bug-13033-Backport-for-Redirects-should-go-through-M.patch, 14.4 KB (added by arthuredelstein, 4 years ago)
  • browser/base/content/test/general/browser.ini

    From 35480af43b3999982567de29f884dd6623dc8f6d Mon Sep 17 00:00:00 2001
    From: Tanvi Vyas <tvyas@mozilla.com>
    Date: Sat, 18 Oct 2014 13:21:06 -0700
    Subject: [PATCH] Bug #13033: Backport for "Redirects should go through Mixed
     Content Blocker"
    
    Mozilla patches (modified for Tor Browser) are:
    - Bug 418354 - Redirects should go through Mixed Content Blocker. r=smaug, ckerschb
    - Bug 418354 - Allow Mixed Content Blocker to handle redirects - testcases. r=smaug
    - Bug 418354 - Disable mixed content blocker redirect test for e10s. r=jduell
    - Bug 418354 - update test for bug 455367. Insecure image loads should be considered mixed display content regardless of whether image data was actually returned. r=honzab
    ---
     browser/base/content/test/general/browser.ini      |   5 +
     .../content/test/general/browser_mcb_redirect.js   | 110 +++++++++++++++++++++
     .../content/test/general/test_mcb_redirect.html    |  15 +++
     .../base/content/test/general/test_mcb_redirect.js |   5 +
     .../content/test/general/test_mcb_redirect.sjs     |  11 +++
     content/base/src/nsMixedContentBlocker.cpp         |  87 +++++++++++++++-
     content/base/src/nsMixedContentBlocker.h           |   6 +-
     layout/build/nsLayoutModule.cpp                    |   1 +
     .../mochitest/mixedcontent/test_bug455367.html     |   4 +-
     9 files changed, 240 insertions(+), 4 deletions(-)
     create mode 100644 browser/base/content/test/general/browser_mcb_redirect.js
     create mode 100644 browser/base/content/test/general/test_mcb_redirect.html
     create mode 100644 browser/base/content/test/general/test_mcb_redirect.js
     create mode 100644 browser/base/content/test/general/test_mcb_redirect.sjs
    
    diff --git a/browser/base/content/test/general/browser.ini b/browser/base/content/test/general/browser.ini
    index 11fa874..8ec0a62 100644
    a b support-files = 
    7878  test_no_mcb_on_http_site_font.css
    7979  test_no_mcb_on_http_site_font2.html
    8080  test_no_mcb_on_http_site_font2.css
     81  test_mcb_redirect.html
     82  test_mcb_redirect.js
     83  test_mcb_redirect.sjs
    8184  xul_tooltiptext.xhtml
    8285
    8386[browser_URLBarSetURI.js]
    skip-if = e10s # Bug 940206 - nsIWebContentHandlerRegistrar::registerProtocolHan 
    422425[browser_no_mcb_on_http_site.js]
    423426skip-if = e10s # Bug 516755 - SessionStore disabled for e10s
    424427[browser_bug1003461-switchtab-override.js]
     428[browser_mcb_redirect.js]
     429skip-if = e10s # bug 1084504 - [e10s] Mixed content detection does not take redirection into account
  • new file rowser/base/content/test/general/browser_mcb_redirect.js

    diff --git a/browser/base/content/test/general/browser_mcb_redirect.js b/browser/base/content/test/general/browser_mcb_redirect.js
    new file mode 100644
    index 0000000..f457d8f
    - +  
     1/*
     2 * Description of the Tests for
     3 *  - Bug 418354 - Call Mixed content blocking on redirects
     4 *
     5 * 1. Load a script over https inside an https page
     6 *    - the server responds with a 302 redirect to a >> HTTP << script
     7 *    - the doorhanger should appear!
     8 *
     9 * 2. Load a script over https inside an http page
     10 *    - the server responds with a 302 redirect to a >> HTTP << script
     11 *    - the doorhanger should not appear!
     12 */
     13
     14const PREF_ACTIVE = "security.mixed_content.block_active_content";
     15const gHttpsTestRoot = "https://example.com/browser/browser/base/content/test/general/";
     16const gHttpTestRoot = "http://example.com/browser/browser/base/content/test/general/";
     17
     18let origBlockActive;
     19var gTestBrowser = null;
     20
     21//------------------------ Helper Functions ---------------------
     22
     23registerCleanupFunction(function() {
     24  // Set preferences back to their original values
     25  Services.prefs.setBoolPref(PREF_ACTIVE, origBlockActive);
     26});
     27
     28function cleanUpAfterTests() {
     29  gBrowser.removeCurrentTab();
     30  window.focus();
     31  finish();
     32}
     33
     34function waitForCondition(condition, nextTest, errorMsg, okMsg) {
     35  var tries = 0;
     36  var interval = setInterval(function() {
     37    if (tries >= 30) {
     38      ok(false, errorMsg);
     39      moveOn();
     40    }
     41    if (condition()) {
     42      ok(true, okMsg)
     43      moveOn();
     44    }
     45    tries++;
     46  }, 100);
     47  var moveOn = function() {
     48    clearInterval(interval); nextTest();
     49  };
     50}
     51
     52//------------------------ Test 1 ------------------------------
     53
     54function test1() {
     55  gTestBrowser.addEventListener("load", checkPopUpNotificationsForTest1, true);
     56  var url = gHttpsTestRoot + "test_mcb_redirect.html"
     57  gTestBrowser.contentWindow.location = url;
     58}
     59
     60function checkPopUpNotificationsForTest1() {
     61  gTestBrowser.removeEventListener("load", checkPopUpNotificationsForTest1, true);
     62
     63  var notification = PopupNotifications.getNotification("bad-content", gTestBrowser.selectedBrowser);
     64  ok(notification, "OK: Mixed Content Doorhanger appeared in Test1!");
     65
     66  var expected = "script blocked";
     67  waitForCondition(
     68    function() content.document.getElementById('mctestdiv').innerHTML == expected,
     69    test2, "Error: Waited too long for status in Test 1!",
     70    "OK: Expected result in innerHTML for Test1!");
     71}
     72
     73//------------------------ Test 2 ------------------------------
     74
     75function test2() {
     76  gTestBrowser.addEventListener("load", checkPopUpNotificationsForTest2, true);
     77  var url = gHttpTestRoot + "test_mcb_redirect.html"
     78  gTestBrowser.contentWindow.location = url;
     79}
     80
     81function checkPopUpNotificationsForTest2() {
     82  gTestBrowser.removeEventListener("load", checkPopUpNotificationsForTest2, true);
     83
     84  var notification = PopupNotifications.getNotification("bad-content", gTestBrowser.selectedBrowser);
     85  ok(!notification, "OK: Mixed Content Doorhanger did not appear in 2!");
     86
     87  var expected = "script executed";
     88  waitForCondition(
     89    function() content.document.getElementById('mctestdiv').innerHTML == expected,
     90    cleanUpAfterTests, "Error: Waited too long for status in Test 2!",
     91    "OK: Expected result in innerHTML for Test2!");
     92}
     93
     94//------------------------ SETUP ------------------------------
     95
     96function test() {
     97  // Performing async calls, e.g. 'onload', we have to wait till all of them finished
     98  waitForExplicitFinish();
     99
     100  // Store original preferences so we can restore settings after testing
     101  origBlockActive = Services.prefs.getBoolPref(PREF_ACTIVE);
     102  Services.prefs.setBoolPref(PREF_ACTIVE, true);
     103
     104  var newTab = gBrowser.addTab();
     105  gBrowser.selectedTab = newTab;
     106  gTestBrowser = gBrowser.selectedBrowser;
     107  newTab.linkedBrowser.stop();
     108
     109  executeSoon(test1);
     110}
  • new file rowser/base/content/test/general/test_mcb_redirect.html

    diff --git a/browser/base/content/test/general/test_mcb_redirect.html b/browser/base/content/test/general/test_mcb_redirect.html
    new file mode 100644
    index 0000000..fcc30ee
    - +  
     1<!DOCTYPE HTML>
     2<html>
     3<!--
     4  Test 1 for Bug 418354 - See file browser_mcb_redirect.js for description.
     5  https://bugzilla.mozilla.org/show_bug.cgi?id=418354
     6-->
     7<head>
     8  <meta charset="utf-8">
     9  <title>Bug 418354</title>
     10</head>
     11<body>
     12  <div id="mctestdiv">script blocked</div>
     13  <script src="https://example.com/browser/browser/base/content/test/general/test_mcb_redirect.sjs" ></script>
     14</body>
     15</html>
  • new file rowser/base/content/test/general/test_mcb_redirect.js

    diff --git a/browser/base/content/test/general/test_mcb_redirect.js b/browser/base/content/test/general/test_mcb_redirect.js
    new file mode 100644
    index 0000000..48538c9
    - +  
     1/*
     2 * Once the mixed content blocker is disabled for the page, this scripts loads
     3 * and updates the text inside the div container.
     4 */
     5document.getElementById("mctestdiv").innerHTML = "script executed";
  • new file rowser/base/content/test/general/test_mcb_redirect.sjs

    diff --git a/browser/base/content/test/general/test_mcb_redirect.sjs b/browser/base/content/test/general/test_mcb_redirect.sjs
    new file mode 100644
    index 0000000..0ccda99
    - +  
     1function handleRequest(request, response) {
     2  var page = "<!DOCTYPE html><html><body>bug 418354</body></html>";
     3
     4  var redirect = "http://example.com/browser/browser/base/content/test/general/test_mcb_redirect.js";
     5
     6  response.setHeader("Cache-Control", "no-cache", false);
     7  response.setHeader("Content-Type", "text/html", false);
     8  response.setStatusLine(request.httpVersion, "302", "Found");
     9  response.setHeader("Location", redirect, false);
     10  response.write(page);
     11}
  • content/base/src/nsMixedContentBlocker.cpp

    diff --git a/content/base/src/nsMixedContentBlocker.cpp b/content/base/src/nsMixedContentBlocker.cpp
    index 40fed3c..f7b586d 100644
    a b  
    2626#include "nsIWebNavigation.h"
    2727#include "nsLoadGroup.h"
    2828#include "nsIScriptError.h"
     29#include "nsIURI.h"
     30#include "nsIChannelEventSink.h"
     31#include "nsAsyncRedirectVerifyHelper.h"
     32#include "mozilla/LoadInfo.h"
    2933
    3034#include "prlog.h"
    3135
    nsMixedContentBlocker::~nsMixedContentBlocker() 
    150154{
    151155}
    152156
    153 NS_IMPL_ISUPPORTS(nsMixedContentBlocker, nsIContentPolicy)
     157NS_IMPL_ISUPPORTS(nsMixedContentBlocker, nsIContentPolicy, nsIChannelEventSink)
    154158
    155159static void
    156160LogMixedContentMessage(MixedContentTypes aClassification,
    LogMixedContentMessage(MixedContentTypes aClassification, 
    190194                                  messageLookupKey.get(), strings, ArrayLength(strings));
    191195}
    192196
     197
     198
     199/* nsIChannelEventSink implementation
     200 * This code is called when a request is redirected.
     201 * We check the channel associated with the new uri is allowed to load
     202 * in the current context
     203 */
     204NS_IMETHODIMP
     205nsMixedContentBlocker::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
     206                                              nsIChannel* aNewChannel,
     207                                              uint32_t aFlags,
     208                                              nsIAsyncVerifyRedirectCallback* aCallback)
     209{
     210  nsAsyncRedirectAutoCallback autoCallback(aCallback);
     211
     212  if (!aOldChannel) {
     213    NS_ERROR("No channel when evaluating mixed content!");
     214    return NS_ERROR_FAILURE;
     215  }
     216
     217  nsresult rv;
     218  nsCOMPtr<nsIURI> oldUri;
     219  rv = aOldChannel->GetURI(getter_AddRefs(oldUri));
     220  NS_ENSURE_SUCCESS(rv, rv);
     221
     222  nsCOMPtr<nsIURI> newUri;
     223  rv = aNewChannel->GetURI(getter_AddRefs(newUri));
     224  NS_ENSURE_SUCCESS(rv, rv);
     225
     226  // Get the loading Info from the old channel
     227  nsCOMPtr<nsILoadInfo> loadInfo;
     228  rv = aOldChannel->GetLoadInfo(getter_AddRefs(loadInfo));
     229  NS_ENSURE_SUCCESS(rv, rv);
     230  if (!loadInfo) {
     231    // XXX: We want to have a loadInfo on all channels, but we don't yet.
     232    // If an addon creates a channel, they may not set loadinfo. If that
     233    // channel redirects from one page to another page, we would get caught
     234    // in this code path. Hence, we have to return NS_OK. Once we have more
     235    // confidence that all channels have loadinfo, we can change this to
     236    // a failure. See bug 1077201.
     237    return NS_OK;
     238  }
     239
     240  uint32_t contentPolicyType = loadInfo->GetContentPolicyType();
     241  nsCOMPtr<nsIPrincipal> requestingPrincipal = loadInfo->LoadingPrincipal();
     242
     243  // Since we are calling shouldLoad() directly on redirects, we don't go through the code
     244  // in nsContentPolicyUtils::NS_CheckContentLoadPolicy(). Hence, we have to
     245  // duplicate parts of it here.
     246  nsCOMPtr<nsIURI> requestingLocation;
     247  if (requestingPrincipal) {
     248    // We check to see if the loadingPrincipal is systemPrincipal and return
     249    // early if it is
     250    if (nsContentUtils::IsSystemPrincipal(requestingPrincipal)) {
     251      return NS_OK;
     252    }
     253    // We set the requestingLocation from the RequestingPrincipal.
     254    rv = requestingPrincipal->GetURI(getter_AddRefs(requestingLocation));
     255    NS_ENSURE_SUCCESS(rv, rv);
     256  }
     257
     258  int16_t decision = REJECT_REQUEST;
     259  rv = ShouldLoad(contentPolicyType,
     260                  newUri,
     261                  requestingLocation,
     262                  loadInfo->LoadingNode(),
     263                  EmptyCString(),       // aMimeGuess
     264                  nullptr,              // aExtra
     265                  requestingPrincipal,
     266                  &decision);
     267  NS_ENSURE_SUCCESS(rv, rv);
     268
     269  // If the channel is about to load mixed content, abort the channel
     270  if (!NS_CP_ACCEPTED(decision)) {
     271    autoCallback.DontCallback();
     272    return NS_BINDING_FAILED;
     273  }
     274
     275  return NS_OK;
     276}
     277
    193278NS_IMETHODIMP
    194279nsMixedContentBlocker::ShouldLoad(uint32_t aContentType,
    195280                                  nsIURI* aContentLocation,
  • content/base/src/nsMixedContentBlocker.h

    diff --git a/content/base/src/nsMixedContentBlocker.h b/content/base/src/nsMixedContentBlocker.h
    index 789783e..d1ebb31 100644
    a b enum MixedContentTypes { 
    2323};
    2424
    2525#include "nsIContentPolicy.h"
     26#include "nsIChannel.h"
     27#include "nsIChannelEventSink.h"
    2628
    27 class nsMixedContentBlocker : public nsIContentPolicy
     29class nsMixedContentBlocker : public nsIContentPolicy,
     30                              public nsIChannelEventSink
    2831{
    2932public:
    3033  NS_DECL_ISUPPORTS
    3134  NS_DECL_NSICONTENTPOLICY
     35  NS_DECL_NSICHANNELEVENTSINK
    3236
    3337  nsMixedContentBlocker();
    3438  virtual ~nsMixedContentBlocker();
  • layout/build/nsLayoutModule.cpp

    diff --git a/layout/build/nsLayoutModule.cpp b/layout/build/nsLayoutModule.cpp
    index 1985b54..813b8ea 100644
    a b static const mozilla::Module::CategoryEntry kLayoutCategories[] = { 
    12291229  { "content-policy", "CSPService", CSPSERVICE_CONTRACTID },
    12301230  { "content-policy", NS_MIXEDCONTENTBLOCKER_CONTRACTID, NS_MIXEDCONTENTBLOCKER_CONTRACTID },
    12311231  { "net-channel-event-sinks", "CSPService", CSPSERVICE_CONTRACTID },
     1232  { "net-channel-event-sinks", NS_MIXEDCONTENTBLOCKER_CONTRACTID, NS_MIXEDCONTENTBLOCKER_CONTRACTID },
    12321233  { JAVASCRIPT_GLOBAL_STATIC_NAMESET_CATEGORY, "PrivilegeManager", NS_SECURITYNAMESET_CONTRACTID },
    12331234  { "app-startup", "Script Security Manager", "service," NS_SCRIPTSECURITYMANAGER_CONTRACTID },
    12341235  { TOPIC_WEB_APP_CLEAR_DATA, "QuotaManager", "service," QUOTA_MANAGER_CONTRACTID },
  • security/manager/ssl/tests/mochitest/mixedcontent/test_bug455367.html

    diff --git a/security/manager/ssl/tests/mochitest/mixedcontent/test_bug455367.html b/security/manager/ssl/tests/mochitest/mixedcontent/test_bug455367.html
    index c0b6e3e..99631f0 100644
    a b  
    1414
    1515  function runTest()
    1616  {
    17     isSecurityState("secure", "secure");
     17    isSecurityState("broken", "broken");
    1818    finish();
    1919  }
    2020
    2121  function afterNavigationTest()
    2222  {
    23     isSecurityState("secure", "secure after navigation");
     23    isSecurityState("broken", "broken after navigation");
    2424    finish();
    2525  }
    2626