1 | /* |
2 | * Copyright (C) 2013 Google, Inc. All Rights Reserved. |
3 | * Copyright (C) 2017 Apple Inc. 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 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 "XSSAuditorDelegate.h" |
29 | |
30 | #include "Document.h" |
31 | #include "DocumentLoader.h" |
32 | #include "FormData.h" |
33 | #include "Frame.h" |
34 | #include "FrameLoader.h" |
35 | #include "FrameLoaderClient.h" |
36 | #include "HTMLParserIdioms.h" |
37 | #include "NavigationScheduler.h" |
38 | #include "PingLoader.h" |
39 | #include <wtf/JSONValues.h> |
40 | #include <wtf/text/StringBuilder.h> |
41 | #include <wtf/text/CString.h> |
42 | |
43 | |
44 | namespace WebCore { |
45 | using namespace Inspector; |
46 | |
47 | XSSAuditorDelegate::XSSAuditorDelegate(Document& document) |
48 | : m_document(document) |
49 | { |
50 | ASSERT(isMainThread()); |
51 | } |
52 | |
53 | static inline String buildConsoleError(const XSSInfo& xssInfo) |
54 | { |
55 | StringBuilder message; |
56 | message.appendLiteral("The XSS Auditor " ); |
57 | message.append(xssInfo.m_didBlockEntirePage ? "blocked access to" : "refused to execute a script in" ); |
58 | message.appendLiteral(" '" ); |
59 | message.append(xssInfo.m_originalURL); |
60 | message.appendLiteral("' because " ); |
61 | message.append(xssInfo.m_didBlockEntirePage ? "the source code of a script" : "its source code" ); |
62 | message.appendLiteral(" was found within the request." ); |
63 | |
64 | if (xssInfo.m_didSendXSSProtectionHeader) |
65 | message.appendLiteral(" The server sent an 'X-XSS-Protection' header requesting this behavior." ); |
66 | else |
67 | message.appendLiteral(" The auditor was enabled because the server did not send an 'X-XSS-Protection' header." ); |
68 | |
69 | return message.toString(); |
70 | } |
71 | |
72 | Ref<FormData> XSSAuditorDelegate::generateViolationReport(const XSSInfo& xssInfo) |
73 | { |
74 | ASSERT(isMainThread()); |
75 | |
76 | auto& frameLoader = m_document.frame()->loader(); |
77 | String httpBody; |
78 | if (frameLoader.documentLoader()) { |
79 | if (auto formData = makeRefPtr(frameLoader.documentLoader()->originalRequest().httpBody())) |
80 | httpBody = formData->flattenToString(); |
81 | } |
82 | |
83 | auto reportDetails = JSON::Object::create(); |
84 | reportDetails->setString("request-url" , xssInfo.m_originalURL); |
85 | reportDetails->setString("request-body" , httpBody); |
86 | |
87 | auto reportObject = JSON::Object::create(); |
88 | reportObject->setObject("xss-report" , WTFMove(reportDetails)); |
89 | |
90 | return FormData::create(reportObject->toJSONString().utf8().data()); |
91 | } |
92 | |
93 | void XSSAuditorDelegate::didBlockScript(const XSSInfo& xssInfo) |
94 | { |
95 | ASSERT(isMainThread()); |
96 | |
97 | m_document.addConsoleMessage(MessageSource::JS, MessageLevel::Error, buildConsoleError(xssInfo)); |
98 | |
99 | FrameLoader& frameLoader = m_document.frame()->loader(); |
100 | if (xssInfo.m_didBlockEntirePage) |
101 | frameLoader.stopAllLoaders(); |
102 | |
103 | if (!m_didSendNotifications) { |
104 | m_didSendNotifications = true; |
105 | |
106 | frameLoader.client().didDetectXSS(m_document.url(), xssInfo.m_didBlockEntirePage); |
107 | |
108 | if (!m_reportURL.isEmpty()) |
109 | PingLoader::sendViolationReport(*m_document.frame(), m_reportURL, generateViolationReport(xssInfo), ViolationReportType::XSSAuditor); |
110 | } |
111 | |
112 | if (xssInfo.m_didBlockEntirePage) |
113 | m_document.frame()->navigationScheduler().schedulePageBlock(m_document); |
114 | } |
115 | |
116 | } // namespace WebCore |
117 | |