0
|
1 |
/*
|
|
2 |
* Copyright (C) 2008, 2009 Daniel Bates (dbates@intudata.com)
|
|
3 |
* All rights reserved.
|
|
4 |
*
|
|
5 |
* Redistribution and use in source and binary forms, with or without
|
|
6 |
* modification, are permitted provided that the following conditions
|
|
7 |
* are met:
|
|
8 |
* 1. Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
11 |
* notice, this list of conditions and the following disclaimer in the
|
|
12 |
* documentation and/or other materials provided with the distribution.
|
|
13 |
*
|
|
14 |
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
|
15 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
17 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
|
|
18 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
19 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
20 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
21 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
22 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
23 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
24 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
25 |
*/
|
|
26 |
|
|
27 |
#include "config.h"
|
|
28 |
#include "XSSAuditor.h"
|
|
29 |
|
|
30 |
#include <wtf/StdLibExtras.h>
|
|
31 |
#include <wtf/Vector.h>
|
|
32 |
|
|
33 |
#include "Console.h"
|
|
34 |
#include "CString.h"
|
|
35 |
#include "DocumentLoader.h"
|
|
36 |
#include "DOMWindow.h"
|
|
37 |
#include "Frame.h"
|
|
38 |
#include "KURL.h"
|
|
39 |
#include "PreloadScanner.h"
|
|
40 |
#include "ResourceResponseBase.h"
|
|
41 |
#include "ScriptSourceCode.h"
|
|
42 |
#include "Settings.h"
|
|
43 |
#include "TextResourceDecoder.h"
|
|
44 |
|
|
45 |
using namespace WTF;
|
|
46 |
|
|
47 |
namespace WebCore {
|
|
48 |
|
|
49 |
static bool isNonCanonicalCharacter(UChar c)
|
|
50 |
{
|
|
51 |
// We remove all non-ASCII characters, including non-printable ASCII characters.
|
|
52 |
//
|
|
53 |
// Note, we don't remove backslashes like PHP stripslashes(), which among other things converts "\\0" to the \0 character.
|
|
54 |
// Instead, we remove backslashes and zeros (since the string "\\0" =(remove backslashes)=> "0"). However, this has the
|
|
55 |
// adverse effect that we remove any legitimate zeros from a string.
|
|
56 |
//
|
|
57 |
// For instance: new String("http://localhost:8000") => new String("http://localhost:8").
|
|
58 |
return (c == '\\' || c == '0' || c < ' ' || c >= 127);
|
|
59 |
}
|
|
60 |
|
|
61 |
static bool isIllegalURICharacter(UChar c)
|
|
62 |
{
|
|
63 |
// The characters described in section 2.4.3 of RFC 2396 <http://www.faqs.org/rfcs/rfc2396.html> in addition to the
|
|
64 |
// single quote character "'" are considered illegal URI characters. That is, the following characters cannot appear
|
|
65 |
// in a valid URI: ', ", <, >
|
|
66 |
//
|
|
67 |
// If the request does not contain these characters then we can assume that no inline scripts have been injected
|
|
68 |
// into the response page, because it is impossible to write an inline script of the form <script>...</script>
|
|
69 |
// without "<", ">".
|
|
70 |
return (c == '\'' || c == '"' || c == '<' || c == '>');
|
|
71 |
}
|
|
72 |
|
|
73 |
String XSSAuditor::CachingURLCanonicalizer::canonicalizeURL(const String& url, const TextEncoding& encoding, bool decodeEntities,
|
|
74 |
bool decodeURLEscapeSequencesTwice)
|
|
75 |
{
|
|
76 |
if (decodeEntities == m_decodeEntities && decodeURLEscapeSequencesTwice == m_decodeURLEscapeSequencesTwice
|
|
77 |
&& encoding == m_encoding && url == m_inputURL)
|
|
78 |
return m_cachedCanonicalizedURL;
|
|
79 |
|
|
80 |
m_cachedCanonicalizedURL = canonicalize(decodeURL(url, encoding, decodeEntities, decodeURLEscapeSequencesTwice));
|
|
81 |
m_inputURL = url;
|
|
82 |
m_encoding = encoding;
|
|
83 |
m_decodeEntities = decodeEntities;
|
|
84 |
m_decodeURLEscapeSequencesTwice = decodeURLEscapeSequencesTwice;
|
|
85 |
return m_cachedCanonicalizedURL;
|
|
86 |
}
|
|
87 |
|
|
88 |
XSSAuditor::XSSAuditor(Frame* frame)
|
|
89 |
: m_frame(frame)
|
|
90 |
{
|
|
91 |
}
|
|
92 |
|
|
93 |
XSSAuditor::~XSSAuditor()
|
|
94 |
{
|
|
95 |
}
|
|
96 |
|
|
97 |
bool XSSAuditor::isEnabled() const
|
|
98 |
{
|
|
99 |
Settings* settings = m_frame->settings();
|
|
100 |
return (settings && settings->xssAuditorEnabled());
|
|
101 |
}
|
|
102 |
|
|
103 |
bool XSSAuditor::canEvaluate(const String& code) const
|
|
104 |
{
|
|
105 |
if (!isEnabled())
|
|
106 |
return true;
|
|
107 |
|
|
108 |
if (findInRequest(code, false, true)) {
|
|
109 |
DEFINE_STATIC_LOCAL(String, consoleMessage, ("Refused to execute a JavaScript script. Source code of script found within request.\n"));
|
|
110 |
m_frame->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, consoleMessage, 1, String());
|
|
111 |
return false;
|
|
112 |
}
|
|
113 |
return true;
|
|
114 |
}
|
|
115 |
|
|
116 |
bool XSSAuditor::canEvaluateJavaScriptURL(const String& code) const
|
|
117 |
{
|
|
118 |
if (!isEnabled())
|
|
119 |
return true;
|
|
120 |
|
|
121 |
if (findInRequest(code, true, false, true)) {
|
|
122 |
DEFINE_STATIC_LOCAL(String, consoleMessage, ("Refused to execute a JavaScript script. Source code of script found within request.\n"));
|
|
123 |
m_frame->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, consoleMessage, 1, String());
|
|
124 |
return false;
|
|
125 |
}
|
|
126 |
return true;
|
|
127 |
}
|
|
128 |
|
|
129 |
bool XSSAuditor::canCreateInlineEventListener(const String&, const String& code) const
|
|
130 |
{
|
|
131 |
if (!isEnabled())
|
|
132 |
return true;
|
|
133 |
|
|
134 |
if (findInRequest(code, true, true)) {
|
|
135 |
DEFINE_STATIC_LOCAL(String, consoleMessage, ("Refused to execute a JavaScript script. Source code of script found within request.\n"));
|
|
136 |
m_frame->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, consoleMessage, 1, String());
|
|
137 |
return false;
|
|
138 |
}
|
|
139 |
return true;
|
|
140 |
}
|
|
141 |
|
|
142 |
bool XSSAuditor::canLoadExternalScriptFromSrc(const String& context, const String& url) const
|
|
143 |
{
|
|
144 |
if (!isEnabled())
|
|
145 |
return true;
|
|
146 |
|
|
147 |
// If the script is loaded from the same URL as the enclosing page, it's
|
|
148 |
// probably not an XSS attack, so we reduce false positives by allowing the
|
|
149 |
// script. If the script has a query string, we're more suspicious,
|
|
150 |
// however, because that's pretty rare and the attacker might be able to
|
|
151 |
// trick a server-side script into doing something dangerous with the query
|
|
152 |
// string.
|
|
153 |
KURL scriptURL(m_frame->document()->url(), url);
|
|
154 |
if (m_frame->document()->url().host() == scriptURL.host() && scriptURL.query().isEmpty())
|
|
155 |
return true;
|
|
156 |
|
|
157 |
if (findInRequest(context + url)) {
|
|
158 |
DEFINE_STATIC_LOCAL(String, consoleMessage, ("Refused to execute a JavaScript script. Source code of script found within request.\n"));
|
|
159 |
m_frame->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, consoleMessage, 1, String());
|
|
160 |
return false;
|
|
161 |
}
|
|
162 |
return true;
|
|
163 |
}
|
|
164 |
|
|
165 |
bool XSSAuditor::canLoadObject(const String& url) const
|
|
166 |
{
|
|
167 |
if (!isEnabled())
|
|
168 |
return true;
|
|
169 |
|
|
170 |
if (findInRequest(url)) {
|
|
171 |
DEFINE_STATIC_LOCAL(String, consoleMessage, ("Refused to execute a JavaScript script. Source code of script found within request"));
|
|
172 |
m_frame->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, consoleMessage, 1, String());
|
|
173 |
return false;
|
|
174 |
}
|
|
175 |
return true;
|
|
176 |
}
|
|
177 |
|
|
178 |
bool XSSAuditor::canSetBaseElementURL(const String& url) const
|
|
179 |
{
|
|
180 |
if (!isEnabled())
|
|
181 |
return true;
|
|
182 |
|
|
183 |
KURL baseElementURL(m_frame->document()->url(), url);
|
|
184 |
if (m_frame->document()->url().host() != baseElementURL.host() && findInRequest(url)) {
|
|
185 |
DEFINE_STATIC_LOCAL(String, consoleMessage, ("Refused to execute a JavaScript script. Source code of script found within request"));
|
|
186 |
m_frame->domWindow()->console()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, consoleMessage, 1, String());
|
|
187 |
return false;
|
|
188 |
}
|
|
189 |
return true;
|
|
190 |
}
|
|
191 |
|
|
192 |
String XSSAuditor::canonicalize(const String& string)
|
|
193 |
{
|
|
194 |
String result = decodeHTMLEntities(string);
|
|
195 |
return result.removeCharacters(&isNonCanonicalCharacter);
|
|
196 |
}
|
|
197 |
|
|
198 |
String XSSAuditor::decodeURL(const String& string, const TextEncoding& encoding, bool decodeEntities, bool decodeURLEscapeSequencesTwice)
|
|
199 |
{
|
|
200 |
String result;
|
|
201 |
String url = string;
|
|
202 |
|
|
203 |
url.replace('+', ' ');
|
|
204 |
result = decodeURLEscapeSequences(url);
|
|
205 |
CString utf8Url = result.utf8();
|
|
206 |
String decodedResult = encoding.decode(utf8Url.data(), utf8Url.length());
|
|
207 |
if (!decodedResult.isEmpty())
|
|
208 |
result = decodedResult;
|
|
209 |
if (decodeURLEscapeSequencesTwice) {
|
|
210 |
result = decodeURLEscapeSequences(result);
|
|
211 |
utf8Url = result.utf8();
|
|
212 |
decodedResult = encoding.decode(utf8Url.data(), utf8Url.length());
|
|
213 |
if (!decodedResult.isEmpty())
|
|
214 |
result = decodedResult;
|
|
215 |
}
|
|
216 |
if (decodeEntities)
|
|
217 |
result = decodeHTMLEntities(result);
|
|
218 |
return result;
|
|
219 |
}
|
|
220 |
|
|
221 |
String XSSAuditor::decodeHTMLEntities(const String& string, bool leaveUndecodableEntitiesUntouched)
|
|
222 |
{
|
|
223 |
SegmentedString source(string);
|
|
224 |
SegmentedString sourceShadow;
|
|
225 |
Vector<UChar> result;
|
|
226 |
|
|
227 |
while (!source.isEmpty()) {
|
|
228 |
UChar cc = *source;
|
|
229 |
source.advance();
|
|
230 |
|
|
231 |
if (cc != '&') {
|
|
232 |
result.append(cc);
|
|
233 |
continue;
|
|
234 |
}
|
|
235 |
|
|
236 |
if (leaveUndecodableEntitiesUntouched)
|
|
237 |
sourceShadow = source;
|
|
238 |
bool notEnoughCharacters = false;
|
|
239 |
unsigned entity = PreloadScanner::consumeEntity(source, notEnoughCharacters);
|
|
240 |
// We ignore notEnoughCharacters because we might as well use this loop
|
|
241 |
// to copy the remaining characters into |result|.
|
|
242 |
|
|
243 |
if (entity > 0xFFFF) {
|
|
244 |
result.append(U16_LEAD(entity));
|
|
245 |
result.append(U16_TRAIL(entity));
|
|
246 |
} else if (entity && (!leaveUndecodableEntitiesUntouched || entity != 0xFFFD)){
|
|
247 |
result.append(entity);
|
|
248 |
} else {
|
|
249 |
result.append('&');
|
|
250 |
if (leaveUndecodableEntitiesUntouched)
|
|
251 |
source = sourceShadow;
|
|
252 |
}
|
|
253 |
}
|
|
254 |
|
|
255 |
return String::adopt(result);
|
|
256 |
}
|
|
257 |
|
|
258 |
bool XSSAuditor::findInRequest(const String& string, bool decodeEntities, bool allowRequestIfNoIllegalURICharacters,
|
|
259 |
bool decodeURLEscapeSequencesTwice) const
|
|
260 |
{
|
|
261 |
bool result = false;
|
|
262 |
Frame* parentFrame = m_frame->tree()->parent();
|
|
263 |
if (parentFrame && m_frame->document()->url() == blankURL())
|
|
264 |
result = findInRequest(parentFrame, string, decodeEntities, allowRequestIfNoIllegalURICharacters, decodeURLEscapeSequencesTwice);
|
|
265 |
if (!result)
|
|
266 |
result = findInRequest(m_frame, string, decodeEntities, allowRequestIfNoIllegalURICharacters, decodeURLEscapeSequencesTwice);
|
|
267 |
return result;
|
|
268 |
}
|
|
269 |
|
|
270 |
bool XSSAuditor::findInRequest(Frame* frame, const String& string, bool decodeEntities, bool allowRequestIfNoIllegalURICharacters,
|
|
271 |
bool decodeURLEscapeSequencesTwice) const
|
|
272 |
{
|
|
273 |
ASSERT(frame->document());
|
|
274 |
|
|
275 |
if (!frame->document()->decoder()) {
|
|
276 |
// Note, JavaScript URLs do not have a charset.
|
|
277 |
return false;
|
|
278 |
}
|
|
279 |
|
|
280 |
if (string.isEmpty())
|
|
281 |
return false;
|
|
282 |
|
|
283 |
FormData* formDataObj = frame->loader()->documentLoader()->originalRequest().httpBody();
|
|
284 |
String pageURL = frame->document()->url().string();
|
|
285 |
|
|
286 |
if (!formDataObj && string.length() >= 2 * pageURL.length()) {
|
|
287 |
// Q: Why do we bother to do this check at all?
|
|
288 |
// A: Canonicalizing large inline scripts can be expensive. We want to
|
|
289 |
// bail out before the call to canonicalize below, which could
|
|
290 |
// result in an unneeded allocation and memcpy.
|
|
291 |
//
|
|
292 |
// Q: Why do we multiply by two here?
|
|
293 |
// A: We attempt to detect reflected XSS even when the server
|
|
294 |
// transforms the attacker's input with addSlashes. The best the
|
|
295 |
// attacker can do get the server to inflate his/her input by a
|
|
296 |
// factor of two by sending " characters, which the server
|
|
297 |
// transforms to \".
|
|
298 |
return false;
|
|
299 |
}
|
|
300 |
|
|
301 |
if (frame->document()->url().protocolIs("data"))
|
|
302 |
return false;
|
|
303 |
|
|
304 |
String canonicalizedString = canonicalize(string);
|
|
305 |
if (canonicalizedString.isEmpty())
|
|
306 |
return false;
|
|
307 |
|
|
308 |
if (string.length() < pageURL.length()) {
|
|
309 |
// The string can actually fit inside the pageURL.
|
|
310 |
String decodedPageURL = m_cache.canonicalizeURL(pageURL, frame->document()->decoder()->encoding(), decodeEntities, decodeURLEscapeSequencesTwice);
|
|
311 |
|
|
312 |
if (allowRequestIfNoIllegalURICharacters && (!formDataObj || formDataObj->isEmpty())
|
|
313 |
&& decodedPageURL.find(&isIllegalURICharacter, 0) == -1)
|
|
314 |
return false; // Injection is impossible because the request does not contain any illegal URI characters.
|
|
315 |
|
|
316 |
if (decodedPageURL.find(canonicalizedString, 0, false) != -1)
|
|
317 |
return true; // We've found the smoking gun.
|
|
318 |
}
|
|
319 |
|
|
320 |
if (formDataObj && !formDataObj->isEmpty()) {
|
|
321 |
String formData = formDataObj->flattenToString();
|
|
322 |
if (string.length() < formData.length()) {
|
|
323 |
// Notice it is sufficient to compare the length of the string to
|
|
324 |
// the url-encoded POST data because the length of the url-decoded
|
|
325 |
// code is less than or equal to the length of the url-encoded
|
|
326 |
// string.
|
|
327 |
String decodedFormData = m_cache.canonicalizeURL(formData, frame->document()->decoder()->encoding(), decodeEntities, decodeURLEscapeSequencesTwice);
|
|
328 |
if (decodedFormData.find(canonicalizedString, 0, false) != -1)
|
|
329 |
return true; // We found the string in the POST data.
|
|
330 |
}
|
|
331 |
}
|
|
332 |
|
|
333 |
return false;
|
|
334 |
}
|
|
335 |
|
|
336 |
} // namespace WebCore
|
|
337 |
|