| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Adam Barth. All Rights Reserved. | 2 * Copyright (C) 2011 Adam Barth. All Rights Reserved. |
| 3 * Copyright (C) 2011 Daniel Bates (dbates@intudata.com). | 3 * Copyright (C) 2011 Daniel Bates (dbates@intudata.com). |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #include "config.h" | 27 #include "config.h" |
| 28 #include "core/html/parser/XSSAuditor.h" | 28 #include "core/html/parser/XSSAuditor.h" |
| 29 | 29 |
| 30 #include "HTMLNames.h" | 30 #include "HTMLNames.h" |
| 31 #include "SVGNames.h" | 31 #include "SVGNames.h" |
| 32 #include "XLinkNames.h" | 32 #include "XLinkNames.h" |
| 33 #include "core/dom/Document.h" | 33 #include "core/dom/Document.h" |
| 34 #include "core/fetch/TextResourceDecoder.h" |
| 34 #include "core/frame/ContentSecurityPolicy.h" | 35 #include "core/frame/ContentSecurityPolicy.h" |
| 35 #include "core/frame/Frame.h" | 36 #include "core/frame/Frame.h" |
| 36 #include "core/html/HTMLParamElement.h" | 37 #include "core/html/HTMLParamElement.h" |
| 37 #include "core/html/parser/HTMLDocumentParser.h" | 38 #include "core/html/parser/HTMLDocumentParser.h" |
| 38 #include "core/html/parser/HTMLParserIdioms.h" | 39 #include "core/html/parser/HTMLParserIdioms.h" |
| 39 #include "core/html/parser/TextResourceDecoder.h" | |
| 40 #include "core/html/parser/XSSAuditorDelegate.h" | 40 #include "core/html/parser/XSSAuditorDelegate.h" |
| 41 #include "core/loader/DocumentLoader.h" | 41 #include "core/loader/DocumentLoader.h" |
| 42 #include "core/frame/Settings.h" | 42 #include "core/frame/Settings.h" |
| 43 #include "platform/JSONValues.h" | 43 #include "platform/JSONValues.h" |
| 44 #include "platform/network/FormData.h" | 44 #include "platform/network/FormData.h" |
| 45 #include "platform/text/DecodeEscapeSequences.h" | 45 #include "platform/text/DecodeEscapeSequences.h" |
| 46 #include "wtf/MainThread.h" | 46 #include "wtf/MainThread.h" |
| 47 | 47 |
| 48 namespace { | 48 namespace { |
| 49 | 49 |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 ASSERT(isMainThread()); | 220 ASSERT(isMainThread()); |
| 221 ASSERT(m_state == Uninitialized); | 221 ASSERT(m_state == Uninitialized); |
| 222 m_state = FilteringTokens; | 222 m_state = FilteringTokens; |
| 223 // When parsing a fragment, we don't enable the XSS auditor because it's | 223 // When parsing a fragment, we don't enable the XSS auditor because it's |
| 224 // too much overhead. | 224 // too much overhead. |
| 225 ASSERT(!m_isEnabled); | 225 ASSERT(!m_isEnabled); |
| 226 } | 226 } |
| 227 | 227 |
| 228 void XSSAuditor::init(Document* document, XSSAuditorDelegate* auditorDelegate) | 228 void XSSAuditor::init(Document* document, XSSAuditorDelegate* auditorDelegate) |
| 229 { | 229 { |
| 230 const size_t miniumLengthForSuffixTree = 512; // FIXME: Tune this parameter. |
| 231 const int suffixTreeDepth = 5; |
| 232 |
| 230 ASSERT(isMainThread()); | 233 ASSERT(isMainThread()); |
| 231 if (m_state != Uninitialized) | 234 if (m_state != Uninitialized) |
| 232 return; | 235 return; |
| 233 m_state = FilteringTokens; | 236 m_state = FilteringTokens; |
| 234 | 237 |
| 235 if (Settings* settings = document->settings()) | 238 if (Settings* settings = document->settings()) |
| 236 m_isEnabled = settings->xssAuditorEnabled(); | 239 m_isEnabled = settings->xssAuditorEnabled(); |
| 237 | 240 |
| 238 if (!m_isEnabled) | 241 if (!m_isEnabled) |
| 239 return; | 242 return; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 254 } | 257 } |
| 255 | 258 |
| 256 if (m_documentURL.protocolIsData()) { | 259 if (m_documentURL.protocolIsData()) { |
| 257 m_isEnabled = false; | 260 m_isEnabled = false; |
| 258 return; | 261 return; |
| 259 } | 262 } |
| 260 | 263 |
| 261 if (document->encoding().isValid()) | 264 if (document->encoding().isValid()) |
| 262 m_encoding = document->encoding(); | 265 m_encoding = document->encoding(); |
| 263 | 266 |
| 267 m_decodedURL = fullyDecodeString(m_documentURL.string(), m_encoding); |
| 268 if (m_decodedURL.find(isRequiredForInjection) == kNotFound) |
| 269 m_decodedURL = String(); |
| 270 |
| 271 String httpBodyAsString; |
| 264 if (DocumentLoader* documentLoader = document->frame()->loader().documentLoa
der()) { | 272 if (DocumentLoader* documentLoader = document->frame()->loader().documentLoa
der()) { |
| 265 DEFINE_STATIC_LOCAL(const AtomicString, XSSProtectionHeader, ("X-XSS-Pro
tection", AtomicString::ConstructFromLiteral)); | 273 DEFINE_STATIC_LOCAL(const AtomicString, XSSProtectionHeader, ("X-XSS-Pro
tection", AtomicString::ConstructFromLiteral)); |
| 266 const AtomicString& headerValue = documentLoader->response().httpHeaderF
ield(XSSProtectionHeader); | 274 const AtomicString& headerValue = documentLoader->response().httpHeaderF
ield(XSSProtectionHeader); |
| 267 String errorDetails; | 275 String errorDetails; |
| 268 unsigned errorPosition = 0; | 276 unsigned errorPosition = 0; |
| 269 String reportURL; | 277 String reportURL; |
| 270 KURL xssProtectionReportURL; | 278 KURL xssProtectionReportURL; |
| 271 | 279 |
| 272 // Process the X-XSS-Protection header, then mix in the CSP header's val
ue. | 280 // Process the X-XSS-Protection header, then mix in the CSP header's val
ue. |
| 273 ReflectedXSSDisposition xssProtectionHeader = parseXSSProtectionHeader(h
eaderValue, errorDetails, errorPosition, reportURL); | 281 ReflectedXSSDisposition xssProtectionHeader = parseXSSProtectionHeader(h
eaderValue, errorDetails, errorPosition, reportURL); |
| 274 m_didSendValidXSSProtectionHeader = xssProtectionHeader != ReflectedXSSU
nset && xssProtectionHeader != ReflectedXSSInvalid; | 282 m_didSendValidXSSProtectionHeader = xssProtectionHeader != ReflectedXSSU
nset && xssProtectionHeader != ReflectedXSSInvalid; |
| 275 if ((xssProtectionHeader == FilterReflectedXSS || xssProtectionHeader ==
BlockReflectedXSS) && !reportURL.isEmpty()) { | 283 if ((xssProtectionHeader == FilterReflectedXSS || xssProtectionHeader ==
BlockReflectedXSS) && !reportURL.isEmpty()) { |
| 276 xssProtectionReportURL = document->completeURL(reportURL); | 284 xssProtectionReportURL = document->completeURL(reportURL); |
| 277 if (MixedContentChecker::isMixedContent(document->securityOrigin(),
xssProtectionReportURL)) { | 285 if (MixedContentChecker::isMixedContent(document->securityOrigin(),
xssProtectionReportURL)) { |
| 278 errorDetails = "insecure reporting URL for secure page"; | 286 errorDetails = "insecure reporting URL for secure page"; |
| 279 xssProtectionHeader = ReflectedXSSInvalid; | 287 xssProtectionHeader = ReflectedXSSInvalid; |
| 280 xssProtectionReportURL = KURL(); | 288 xssProtectionReportURL = KURL(); |
| 281 } | 289 } |
| 282 } | 290 } |
| 283 if (xssProtectionHeader == ReflectedXSSInvalid) | 291 if (xssProtectionHeader == ReflectedXSSInvalid) |
| 284 document->addConsoleMessage(SecurityMessageSource, ErrorMessageLevel
, "Error parsing header X-XSS-Protection: " + headerValue + ": " + errorDetails
+ " at character position " + String::format("%u", errorPosition) + ". The defa
ult protections will be applied."); | 292 document->addConsoleMessage(SecurityMessageSource, ErrorMessageLevel
, "Error parsing header X-XSS-Protection: " + headerValue + ": " + errorDetails
+ " at character position " + String::format("%u", errorPosition) + ". The defa
ult protections will be applied."); |
| 285 | 293 |
| 286 ReflectedXSSDisposition cspHeader = document->contentSecurityPolicy()->r
eflectedXSSDisposition(); | 294 ReflectedXSSDisposition cspHeader = document->contentSecurityPolicy()->r
eflectedXSSDisposition(); |
| 287 m_didSendValidCSPHeader = cspHeader != ReflectedXSSUnset && cspHeader !=
ReflectedXSSInvalid; | 295 m_didSendValidCSPHeader = cspHeader != ReflectedXSSUnset && cspHeader !=
ReflectedXSSInvalid; |
| 288 | 296 |
| 289 m_xssProtection = combineXSSProtectionHeaderAndCSP(xssProtectionHeader,
cspHeader); | 297 m_xssProtection = combineXSSProtectionHeaderAndCSP(xssProtectionHeader,
cspHeader); |
| 290 // FIXME: Combine the two report URLs in some reasonable way. | 298 // FIXME: Combine the two report URLs in some reasonable way. |
| 291 if (auditorDelegate) | 299 if (auditorDelegate) |
| 292 auditorDelegate->setReportURL(xssProtectionReportURL.copy()); | 300 auditorDelegate->setReportURL(xssProtectionReportURL.copy()); |
| 293 | |
| 294 FormData* httpBody = documentLoader->request().httpBody(); | 301 FormData* httpBody = documentLoader->request().httpBody(); |
| 295 if (httpBody && !httpBody->isEmpty()) | 302 if (httpBody && !httpBody->isEmpty()) { |
| 296 m_httpBodyAsString = httpBody->flattenToString(); | 303 httpBodyAsString = httpBody->flattenToString(); |
| 304 if (!httpBodyAsString.isEmpty()) { |
| 305 m_decodedHTTPBody = fullyDecodeString(httpBodyAsString, m_encodi
ng); |
| 306 if (m_decodedHTTPBody.find(isRequiredForInjection) == kNotFound) |
| 307 m_decodedHTTPBody = String(); |
| 308 if (m_decodedHTTPBody.length() >= miniumLengthForSuffixTree) |
| 309 m_decodedHTTPBodySuffixTree = adoptPtr(new SuffixTree<ASCIIC
odebook>(m_decodedHTTPBody, suffixTreeDepth)); |
| 310 } |
| 311 } |
| 297 } | 312 } |
| 298 | 313 |
| 299 setEncoding(m_encoding); | 314 if (m_decodedURL.isEmpty() && m_decodedHTTPBody.isEmpty()) { |
| 300 } | 315 m_isEnabled = false; |
| 301 | |
| 302 void XSSAuditor::setEncoding(const WTF::TextEncoding& encoding) | |
| 303 { | |
| 304 const size_t miniumLengthForSuffixTree = 512; // FIXME: Tune this parameter. | |
| 305 const int suffixTreeDepth = 5; | |
| 306 | |
| 307 if (!encoding.isValid()) | |
| 308 return; | 316 return; |
| 309 | |
| 310 m_encoding = encoding; | |
| 311 | |
| 312 m_decodedURL = fullyDecodeString(m_documentURL.string(), m_encoding); | |
| 313 if (m_decodedURL.find(isRequiredForInjection) == kNotFound) | |
| 314 m_decodedURL = String(); | |
| 315 | |
| 316 if (!m_httpBodyAsString.isEmpty()) { | |
| 317 m_decodedHTTPBody = fullyDecodeString(m_httpBodyAsString, m_encoding); | |
| 318 m_httpBodyAsString = String(); | |
| 319 if (m_decodedHTTPBody.find(isRequiredForInjection) == kNotFound) | |
| 320 m_decodedHTTPBody = String(); | |
| 321 if (m_decodedHTTPBody.length() >= miniumLengthForSuffixTree) | |
| 322 m_decodedHTTPBodySuffixTree = adoptPtr(new SuffixTree<ASCIICodeb
ook>(m_decodedHTTPBody, suffixTreeDepth)); | |
| 323 } | 317 } |
| 324 | |
| 325 if (m_decodedURL.isEmpty() && m_decodedHTTPBody.isEmpty()) | |
| 326 m_isEnabled = false; | |
| 327 } | 318 } |
| 328 | 319 |
| 329 PassOwnPtr<XSSInfo> XSSAuditor::filterToken(const FilterTokenRequest& request) | 320 PassOwnPtr<XSSInfo> XSSAuditor::filterToken(const FilterTokenRequest& request) |
| 330 { | 321 { |
| 331 ASSERT(m_state != Uninitialized); | 322 ASSERT(m_state != Uninitialized); |
| 332 if (!m_isEnabled || m_xssProtection == AllowReflectedXSS) | 323 if (!m_isEnabled || m_xssProtection == AllowReflectedXSS) |
| 333 return nullptr; | 324 return nullptr; |
| 334 | 325 |
| 335 bool didBlockScript = false; | 326 bool didBlockScript = false; |
| 336 if (request.token.type() == HTMLToken::StartTag) | 327 if (request.token.type() == HTMLToken::StartTag) |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 return false; | 725 return false; |
| 735 | 726 |
| 736 KURL resourceURL(m_documentURL, url); | 727 KURL resourceURL(m_documentURL, url); |
| 737 return (m_documentURL.host() == resourceURL.host() && resourceURL.query().is
Empty()); | 728 return (m_documentURL.host() == resourceURL.host() && resourceURL.query().is
Empty()); |
| 738 } | 729 } |
| 739 | 730 |
| 740 bool XSSAuditor::isSafeToSendToAnotherThread() const | 731 bool XSSAuditor::isSafeToSendToAnotherThread() const |
| 741 { | 732 { |
| 742 return m_documentURL.isSafeToSendToAnotherThread() | 733 return m_documentURL.isSafeToSendToAnotherThread() |
| 743 && m_decodedURL.isSafeToSendToAnotherThread() | 734 && m_decodedURL.isSafeToSendToAnotherThread() |
| 744 && m_decodedHTTPBody.isSafeToSendToAnotherThread() | 735 && m_decodedHTTPBody.isSafeToSendToAnotherThread(); |
| 745 && m_httpBodyAsString.isSafeToSendToAnotherThread(); | |
| 746 } | 736 } |
| 747 | 737 |
| 748 } // namespace WebCore | 738 } // namespace WebCore |
| OLD | NEW |