DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Untracked file

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * Contributor(s):
 *   Scott MacGregor <scott@scott-macgregor.org>
 *
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "nsMsgContentPolicy.h"
#include "nsIServiceManager.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsIPrefBranchInternal.h"
#include "nsIURI.h"
#include "nsCOMPtr.h"
#include "nsCRT.h"
#include "nsIContentPolicy.h"
#include "nsIRDFService.h"
#include "nsIRDFResource.h"
#include "nsIMsgHeaderParser.h"
#include "nsIAbMDBDirectory.h"

#include "nsIMsgMailNewsUrl.h"
#include "nsIMsgWindow.h"
#include "nsIMimeMiscStatus.h"
#include "nsIMsgMessageService.h"
#include "nsIMsgIncomingServer.h"
#include "nsIRssIncomingServer.h"
#include "nsIMsgHdr.h"
#include "nsMsgUtils.h"

// needed by the content load policy manager
#include "nsIExternalProtocolService.h"
#include "nsCExternalHandlerService.h"

static const char kBlockRemoteImages[] = "mailnews.message_display.disable_remote_image";
static const char kRemoteImagesUseWhiteList[] = "mailnews.message_display.disable_remote_images.useWhitelist";
static const char kRemoteImagesWhiteListURI[] = "mailnews.message_display.disable_remote_images.whiteListAbURI";
static const char kAllowPlugins[] = "mailnews.message_display.allow.plugins";

// Per message headder flags to keep track of whether the user is allowing remote
// content for a particular message. 
// if you change or add more values to these constants, be sure to modify
// the corresponding definitions in mailWindowOverlay.js
#define kNoRemoteContentPolicy 0
#define kBlockRemoteContent 1
#define kAllowRemoteContent 2

NS_IMPL_ADDREF(nsMsgContentPolicy)
NS_IMPL_RELEASE(nsMsgContentPolicy)

NS_INTERFACE_MAP_BEGIN(nsMsgContentPolicy)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentPolicy)
   NS_INTERFACE_MAP_ENTRY(nsIContentPolicy)
   NS_INTERFACE_MAP_ENTRY(nsIObserver)
   NS_INTERFACE_MAP_ENTRY(nsSupportsWeakReference)
NS_INTERFACE_MAP_END

nsMsgContentPolicy::nsMsgContentPolicy()
{
  mAllowPlugins = PR_FALSE;
  mUseRemoteImageWhiteList = PR_TRUE;
  mBlockRemoteImages = PR_TRUE;
}

nsMsgContentPolicy::~nsMsgContentPolicy()
{
  // hey, we are going away...clean up after ourself....unregister our observer
  nsresult rv = NS_OK;
  nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
  if (NS_SUCCEEDED(rv))
  {
    nsCOMPtr<nsIPrefBranchInternal> prefInternal = do_QueryInterface(prefBranch, &rv);
    if (NS_SUCCEEDED(rv))
    {
      prefInternal->RemoveObserver(kBlockRemoteImages, this);
      prefInternal->RemoveObserver(kRemoteImagesUseWhiteList, this);
      prefInternal->RemoveObserver(kRemoteImagesWhiteListURI, this);
      prefInternal->RemoveObserver(kAllowPlugins, this);
    }
  }
}

nsresult nsMsgContentPolicy::Init()
{
  nsresult rv;

  // register ourself as an observer on the mail preference to block remote images
  nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIPrefBranchInternal> prefInternal = do_QueryInterface(prefBranch, &rv);
  NS_ENSURE_SUCCESS(rv, rv);
  prefInternal->AddObserver(kBlockRemoteImages, this, PR_TRUE);
  prefInternal->AddObserver(kRemoteImagesUseWhiteList, this, PR_TRUE);
  prefInternal->AddObserver(kRemoteImagesWhiteListURI, this, PR_TRUE);
  prefInternal->AddObserver(kAllowPlugins, this, PR_TRUE);

  prefBranch->GetBoolPref(kAllowPlugins, &mAllowPlugins);
  prefBranch->GetBoolPref(kRemoteImagesUseWhiteList, &mUseRemoteImageWhiteList);
  prefBranch->GetCharPref(kRemoteImagesWhiteListURI, getter_Copies(mRemoteImageWhiteListURI));
  return prefBranch->GetBoolPref(kBlockRemoteImages, &mBlockRemoteImages);
}

nsresult nsMsgContentPolicy::IsSenderInWhiteList(nsIMsgDBHdr * aMsgHdr, PRBool * aWhiteListed)
{
  *aWhiteListed = PR_FALSE;
  NS_ENSURE_ARG_POINTER(aMsgHdr); 
  nsresult rv = NS_OK;

  if (mBlockRemoteImages && mUseRemoteImageWhiteList && !mRemoteImageWhiteListURI.IsEmpty())
  {
    nsXPIDLCString author;
    rv = aMsgHdr->GetAuthor(getter_Copies(author));
    NS_ENSURE_SUCCESS(rv, rv);

    nsCOMPtr<nsIRDFService> rdfService = do_GetService("@mozilla.org/rdf/rdf-service;1", &rv);
    NS_ENSURE_SUCCESS(rv, rv);
    
    nsCOMPtr <nsIRDFResource> resource;
    rv = rdfService->GetResource(mRemoteImageWhiteListURI, getter_AddRefs(resource));
    NS_ENSURE_SUCCESS(rv, rv);

    nsCOMPtr <nsIAbMDBDirectory> addressBook = do_QueryInterface(resource, &rv);
    NS_ENSURE_SUCCESS(rv, rv);

    nsCOMPtr<nsIMsgHeaderParser> headerParser = do_GetService("@mozilla.org/messenger/headerparser;1", &rv);
    NS_ENSURE_SUCCESS(rv, rv);

    nsXPIDLCString emailAddress; 
    rv = headerParser->ExtractHeaderAddressMailboxes(nsnull, author, getter_Copies(emailAddress));
    NS_ENSURE_SUCCESS(rv, rv);

    rv = addressBook->HasCardForEmailAddress(emailAddress, aWhiteListed);
  }

  return rv;
}

NS_IMETHODIMP
nsMsgContentPolicy::ShouldLoad(PRUint32          aContentType,
                               nsIURI           *aContentLocation,
                               nsIURI           *aRequestingLocation,
                               nsISupports      *aRequestingContext,
                               const nsACString &aMimeGuess,
                               nsISupports      *aExtra,
                               PRInt16          *aDecision)
{
  nsresult rv = NS_OK;
  *aDecision = nsIContentPolicy::REJECT_REQUEST;

  NS_ENSURE_ARG_POINTER(aContentLocation);
  NS_ENSURE_ARG_POINTER(aRequestingLocation);

  if (aContentType == nsIContentPolicy::TYPE_OBJECT)
  {
      // only allow the plugin to load if the allow plugins pref has been set
      *aDecision = mAllowPlugins ? nsIContentPolicy::ACCEPT :
                                   nsIContentPolicy::REJECT_TYPE;
  }
  else 
  {
    // if aRequestingLocation is chrome, about or resource, allow aContentLocation to load
    PRBool isChrome = PR_FALSE;
    PRBool isRes = PR_FALSE;
    PRBool isAbout = PR_FALSE;

    rv = aRequestingLocation->SchemeIs("chrome", &isChrome);
    rv |= aRequestingLocation->SchemeIs("resource", &isRes);
    rv |= aRequestingLocation->SchemeIs("about", &isAbout);

    if (NS_SUCCEEDED(rv) && (isChrome || isRes || isAbout))
    {
      *aDecision = nsIContentPolicy::ACCEPT;
      return rv;
    }

    // if aContentLocation is a protocol we handle (imap, pop3, mailbox, etc) or is a chrome url, then allowe the load
    nsCAutoString contentScheme;
    PRBool isExposedProtocol = PR_FALSE;
    rv = aContentLocation->GetScheme(contentScheme);
      NS_ENSURE_SUCCESS(rv,rv);

    nsCOMPtr<nsIExternalProtocolService> extProtService = do_GetService(NS_EXTERNALPROTOCOLSERVICE_CONTRACTID);
    rv = extProtService->IsExposedProtocol(contentScheme.get(), &isExposedProtocol);
        NS_ENSURE_SUCCESS(rv,rv);
    aContentLocation->SchemeIs("chrome", &isChrome);    
    
    if (isExposedProtocol || isChrome)
    {
      *aDecision = nsIContentPolicy::ACCEPT;
      return rv;
      }

    // for unexposed protocols, we never try to load any of them with the exception of http and https. 
    // this means we never even try to load urls that we don't handle ourselves like ftp and gopher.
    PRBool isHttp = PR_FALSE;
    PRBool isHttps = PR_FALSE;

    rv = aContentLocation->SchemeIs("http", &isHttp);
    rv |= aContentLocation->SchemeIs("https", &isHttps);

    // Look into http and https more closely to determine if the load should be allowed
    if (NS_SUCCEEDED(rv) && (isHttp || isHttps)) 
      {
        // default to blocking remote content 
        *aDecision = mBlockRemoteImages ? nsIContentPolicy::REJECT_REQUEST : nsIContentPolicy::ACCEPT;

        // now do some more detective work to better refine our decision...
        // (1) examine the msg hdr value for the remote content policy on this particular message to
        //     see if this particular message has special rights to bypass the remote content check
        // (2) special case RSS urls, always allow them to load remote images since the user explicitly
        //     subscribed to the feed.
        // (3) Check the personal address book and use it as a white list for senders 
        //     who are allowed to send us remote images 

        // get the msg hdr for the message URI we are actually loading
        NS_ENSURE_TRUE(aRequestingLocation, NS_OK);
        nsCOMPtr<nsIMsgMessageUrl> msgUrl = do_QueryInterface(aRequestingLocation, &rv);
        NS_ENSURE_SUCCESS(rv, rv);

        nsXPIDLCString resourceURI;
        msgUrl->GetUri(getter_Copies(resourceURI));
        
        // get the msg service for this URI
        nsCOMPtr<nsIMsgMessageService> msgService;
        rv = GetMessageServiceFromURI(resourceURI.get(), getter_AddRefs(msgService));
        NS_ENSURE_SUCCESS(rv, rv);
        
        nsCOMPtr<nsIMsgDBHdr> msgHdr;
        rv = msgService->MessageURIToMsgHdr(resourceURI, getter_AddRefs(msgHdr));
        NS_ENSURE_SUCCESS(rv, rv);

        nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(aRequestingLocation, &rv);
        NS_ENSURE_SUCCESS(rv, rv);

        // Case #1, check the db hdr for the remote content policy on this particular message
        PRUint32 remoteContentPolicy = kNoRemoteContentPolicy;
        msgHdr->GetUint32Property("remoteContentPolicy", &remoteContentPolicy); 

        // get the folder and the server from the msghdr
        // an error trying to get the folder shouldn't make us quit...
        nsCOMPtr<nsIRssIncomingServer> rssServer;
        nsCOMPtr<nsIMsgFolder> folder;
        rv = msgHdr->GetFolder(getter_AddRefs(folder));
        if (NS_SUCCEEDED(rv) && folder)
        {
          nsCOMPtr<nsIMsgIncomingServer> server;
          folder->GetServer(getter_AddRefs(server));
          rssServer = do_QueryInterface(server);
        }

        // Case #3, author is in our white list..
        PRBool authorInWhiteList = PR_FALSE;
        IsSenderInWhiteList(msgHdr, &authorInWhiteList);
        
        // Case #1 and #2: special case RSS. Allow urls that are RSS feeds to show remote image (Bug #250246)
        // Honor the message specific remote content policy
        if (rssServer || remoteContentPolicy == kAllowRemoteContent || authorInWhiteList)
          *aDecision = nsIContentPolicy::ACCEPT;
        else if (mBlockRemoteImages) 
        {
          if (!remoteContentPolicy) // kNoRemoteContentPolicy means we have never set a value on the message
            msgHdr->SetUint32Property("remoteContentPolicy", kBlockRemoteContent); 

          // now we need to call out the msg sink informing it that this message has remote content
          nsCOMPtr<nsIMsgWindow> msgWindow;
          rv = mailnewsUrl->GetMsgWindow(getter_AddRefs(msgWindow)); // it's not an error for the msg window to be null
          NS_ENSURE_TRUE(msgWindow, NS_OK);

          nsCOMPtr<nsIMsgHeaderSink> msgHdrSink;
          rv = msgWindow->GetMsgHeaderSink(getter_AddRefs(msgHdrSink));
          NS_ENSURE_SUCCESS(rv, rv);

          msgHdrSink->OnMsgHasRemoteContent(msgHdr); // notify the UI to show the remote content hdr bar so the user can overide
      } // if need to check the url for a remote image policy
    }  // if isHttp
  }

  return rv;
}

NS_IMETHODIMP
nsMsgContentPolicy::ShouldProcess(PRUint32          aContentType,
                                  nsIURI           *aContentLocation,
                                  nsIURI           *aRequestingLocation,
                                  nsISupports      *aRequestingContext,
                                  const nsACString &aMimeGuess,
                                  nsISupports      *aExtra,
                                  PRInt16          *aDecision)
{
  *aDecision = nsIContentPolicy::ACCEPT;
  return NS_OK;
}

NS_IMETHODIMP nsMsgContentPolicy::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
{
  nsresult rv;
   
  if (!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic)) 
  {
    nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
    NS_ENSURE_SUCCESS(rv, rv);

    NS_LossyConvertUCS2toASCII pref(aData);

    if (pref.Equals(kBlockRemoteImages))
      rv = prefBranch->GetBoolPref(kBlockRemoteImages, &mBlockRemoteImages);
    else if (pref.Equals(kRemoteImagesUseWhiteList))
      prefBranch->GetBoolPref(kRemoteImagesUseWhiteList, &mUseRemoteImageWhiteList);
    else if (pref.Equals(kRemoteImagesWhiteListURI))
      prefBranch->GetCharPref(kRemoteImagesWhiteListURI, getter_Copies(mRemoteImageWhiteListURI));
  }
  
  return NS_OK;
}