1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Mozilla Corporation.
18 : * Portions created by the Initial Developer are Copyright (C) 2007
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Dave Camp <dcamp@mozilla.com>
23 : * Michal Novotny <michal.novotny@gmail.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsChannelClassifier.h"
40 :
41 : #include "nsNetUtil.h"
42 : #include "nsIChannel.h"
43 : #include "nsIProtocolHandler.h"
44 : #include "nsICachingChannel.h"
45 : #include "nsICacheEntryDescriptor.h"
46 : #include "prlog.h"
47 :
48 : #if defined(PR_LOGGING)
49 : //
50 : // NSPR_LOG_MODULES=nsChannelClassifier:5
51 : //
52 : static PRLogModuleInfo *gChannelClassifierLog;
53 : #endif
54 : #define LOG(args) PR_LOG(gChannelClassifierLog, PR_LOG_DEBUG, args)
55 :
56 0 : NS_IMPL_ISUPPORTS1(nsChannelClassifier,
57 : nsIURIClassifierCallback)
58 :
59 0 : nsChannelClassifier::nsChannelClassifier()
60 : {
61 : #if defined(PR_LOGGING)
62 0 : if (!gChannelClassifierLog)
63 0 : gChannelClassifierLog = PR_NewLogModule("nsChannelClassifier");
64 : #endif
65 0 : }
66 :
67 : nsresult
68 0 : nsChannelClassifier::Start(nsIChannel *aChannel)
69 : {
70 : // Don't bother to run the classifier on a load that has already failed.
71 : // (this might happen after a redirect)
72 : PRUint32 status;
73 0 : aChannel->GetStatus(&status);
74 0 : if (NS_FAILED(status))
75 0 : return NS_OK;
76 :
77 : // Don't bother to run the classifier on a cached load that was
78 : // previously classified.
79 0 : if (HasBeenClassified(aChannel)) {
80 0 : return NS_OK;
81 : }
82 :
83 0 : nsCOMPtr<nsIURI> uri;
84 0 : nsresult rv = aChannel->GetURI(getter_AddRefs(uri));
85 0 : NS_ENSURE_SUCCESS(rv, rv);
86 :
87 : // Don't bother checking certain types of URIs.
88 : bool hasFlags;
89 : rv = NS_URIChainHasFlags(uri,
90 : nsIProtocolHandler::URI_DANGEROUS_TO_LOAD,
91 0 : &hasFlags);
92 0 : NS_ENSURE_SUCCESS(rv, rv);
93 0 : if (hasFlags) return NS_OK;
94 :
95 : rv = NS_URIChainHasFlags(uri,
96 : nsIProtocolHandler::URI_IS_LOCAL_FILE,
97 0 : &hasFlags);
98 0 : NS_ENSURE_SUCCESS(rv, rv);
99 0 : if (hasFlags) return NS_OK;
100 :
101 : rv = NS_URIChainHasFlags(uri,
102 : nsIProtocolHandler::URI_IS_UI_RESOURCE,
103 0 : &hasFlags);
104 0 : NS_ENSURE_SUCCESS(rv, rv);
105 0 : if (hasFlags) return NS_OK;
106 :
107 : rv = NS_URIChainHasFlags(uri,
108 : nsIProtocolHandler::URI_IS_LOCAL_RESOURCE,
109 0 : &hasFlags);
110 0 : NS_ENSURE_SUCCESS(rv, rv);
111 0 : if (hasFlags) return NS_OK;
112 :
113 : nsCOMPtr<nsIURIClassifier> uriClassifier =
114 0 : do_GetService(NS_URICLASSIFIERSERVICE_CONTRACTID, &rv);
115 0 : if (rv == NS_ERROR_FACTORY_NOT_REGISTERED ||
116 : rv == NS_ERROR_NOT_AVAILABLE) {
117 : // no URI classifier, ignore this failure.
118 0 : return NS_OK;
119 : }
120 0 : NS_ENSURE_SUCCESS(rv, rv);
121 :
122 : bool expectCallback;
123 0 : rv = uriClassifier->Classify(uri, this, &expectCallback);
124 0 : if (NS_FAILED(rv)) return rv;
125 :
126 0 : if (expectCallback) {
127 : // Suspend the channel, it will be resumed when we get the classifier
128 : // callback.
129 0 : rv = aChannel->Suspend();
130 0 : if (NS_FAILED(rv)) {
131 : // Some channels (including nsJSChannel) fail on Suspend. This
132 : // shouldn't be fatal, but will prevent malware from being
133 : // blocked on these channels.
134 0 : return NS_OK;
135 : }
136 :
137 0 : mSuspendedChannel = aChannel;
138 : #ifdef DEBUG
139 0 : LOG(("nsChannelClassifier[%p]: suspended channel %p",
140 : this, mSuspendedChannel.get()));
141 : #endif
142 : }
143 :
144 0 : return NS_OK;
145 : }
146 :
147 : // Note in the cache entry that this URL was classified, so that future
148 : // cached loads don't need to be checked.
149 : void
150 0 : nsChannelClassifier::MarkEntryClassified(nsresult status)
151 : {
152 : nsCOMPtr<nsICachingChannel> cachingChannel =
153 0 : do_QueryInterface(mSuspendedChannel);
154 0 : if (!cachingChannel) {
155 : return;
156 : }
157 :
158 0 : nsCOMPtr<nsISupports> cacheToken;
159 0 : cachingChannel->GetCacheToken(getter_AddRefs(cacheToken));
160 0 : if (!cacheToken) {
161 : return;
162 : }
163 :
164 : nsCOMPtr<nsICacheEntryDescriptor> cacheEntry =
165 0 : do_QueryInterface(cacheToken);
166 0 : if (!cacheEntry) {
167 : return;
168 : }
169 :
170 0 : cacheEntry->SetMetaDataElement("necko:classified",
171 0 : NS_SUCCEEDED(status) ? "1" : nsnull);
172 : }
173 :
174 : bool
175 0 : nsChannelClassifier::HasBeenClassified(nsIChannel *aChannel)
176 : {
177 : nsCOMPtr<nsICachingChannel> cachingChannel =
178 0 : do_QueryInterface(aChannel);
179 0 : if (!cachingChannel) {
180 0 : return false;
181 : }
182 :
183 : // Only check the tag if we are loading from the cache without
184 : // validation.
185 : bool fromCache;
186 0 : if (NS_FAILED(cachingChannel->IsFromCache(&fromCache)) || !fromCache) {
187 0 : return false;
188 : }
189 :
190 0 : nsCOMPtr<nsISupports> cacheToken;
191 0 : cachingChannel->GetCacheToken(getter_AddRefs(cacheToken));
192 0 : if (!cacheToken) {
193 0 : return false;
194 : }
195 :
196 : nsCOMPtr<nsICacheEntryDescriptor> cacheEntry =
197 0 : do_QueryInterface(cacheToken);
198 0 : if (!cacheEntry) {
199 0 : return false;
200 : }
201 :
202 0 : nsXPIDLCString tag;
203 0 : cacheEntry->GetMetaDataElement("necko:classified", getter_Copies(tag));
204 0 : return tag.EqualsLiteral("1");
205 : }
206 :
207 : NS_IMETHODIMP
208 0 : nsChannelClassifier::OnClassifyComplete(nsresult aErrorCode)
209 : {
210 0 : if (mSuspendedChannel) {
211 0 : MarkEntryClassified(aErrorCode);
212 :
213 0 : if (NS_FAILED(aErrorCode)) {
214 : #ifdef DEBUG
215 0 : LOG(("nsChannelClassifier[%p]: cancelling channel %p with error "
216 : "code: %x", this, mSuspendedChannel.get(), aErrorCode));
217 : #endif
218 0 : mSuspendedChannel->Cancel(aErrorCode);
219 : }
220 : #ifdef DEBUG
221 0 : LOG(("nsChannelClassifier[%p]: resuming channel %p from "
222 : "OnClassifyComplete", this, mSuspendedChannel.get()));
223 : #endif
224 0 : mSuspendedChannel->Resume();
225 0 : mSuspendedChannel = nsnull;
226 : }
227 :
228 0 : return NS_OK;
229 : }
|