| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "core/frame/csp/CSPDirectiveList.h" | 6 #include "core/frame/csp/CSPDirectiveList.h" |
| 7 | 7 |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
| 10 #include "core/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 return directives.release(); | 43 return directives.release(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void CSPDirectiveList::reportViolation(const String& directiveText, const String
& effectiveDirective, const String& consoleMessage, const KURL& blockedURL) cons
t | 46 void CSPDirectiveList::reportViolation(const String& directiveText, const String
& effectiveDirective, const String& consoleMessage, const KURL& blockedURL) cons
t |
| 47 { | 47 { |
| 48 String message = m_reportOnly ? "[Report Only] " + consoleMessage : consoleM
essage; | 48 String message = m_reportOnly ? "[Report Only] " + consoleMessage : consoleM
essage; |
| 49 m_policy->logToConsole(ConsoleMessage::create(SecurityMessageSource, ErrorMe
ssageLevel, message)); | 49 m_policy->logToConsole(ConsoleMessage::create(SecurityMessageSource, ErrorMe
ssageLevel, message)); |
| 50 m_policy->reportViolation(directiveText, effectiveDirective, message, blocke
dURL, m_reportEndpoints, m_header); | 50 m_policy->reportViolation(directiveText, effectiveDirective, message, blocke
dURL, m_reportEndpoints, m_header); |
| 51 } | 51 } |
| 52 | 52 |
| 53 void CSPDirectiveList::reportViolationWithFrame(const String& directiveText, con
st String& effectiveDirective, const String& consoleMessage, const KURL& blocked
URL, LocalFrame* frame) const |
| 54 { |
| 55 String message = m_reportOnly ? "[Report Only] " + consoleMessage : consoleM
essage; |
| 56 m_policy->logToConsole(ConsoleMessage::create(SecurityMessageSource, ErrorMe
ssageLevel, message), frame); |
| 57 m_policy->reportViolation(directiveText, effectiveDirective, message, blocke
dURL, m_reportEndpoints, m_header, frame); |
| 58 } |
| 59 |
| 53 void CSPDirectiveList::reportViolationWithLocation(const String& directiveText,
const String& effectiveDirective, const String& consoleMessage, const KURL& bloc
kedURL, const String& contextURL, const WTF::OrdinalNumber& contextLine) const | 60 void CSPDirectiveList::reportViolationWithLocation(const String& directiveText,
const String& effectiveDirective, const String& consoleMessage, const KURL& bloc
kedURL, const String& contextURL, const WTF::OrdinalNumber& contextLine) const |
| 54 { | 61 { |
| 55 String message = m_reportOnly ? "[Report Only] " + consoleMessage : consoleM
essage; | 62 String message = m_reportOnly ? "[Report Only] " + consoleMessage : consoleM
essage; |
| 56 m_policy->logToConsole(ConsoleMessage::create(SecurityMessageSource, ErrorMe
ssageLevel, message, contextURL, contextLine.oneBasedInt())); | 63 m_policy->logToConsole(ConsoleMessage::create(SecurityMessageSource, ErrorMe
ssageLevel, message, contextURL, contextLine.oneBasedInt())); |
| 57 m_policy->reportViolation(directiveText, effectiveDirective, message, blocke
dURL, m_reportEndpoints, m_header); | 64 m_policy->reportViolation(directiveText, effectiveDirective, message, blocke
dURL, m_reportEndpoints, m_header); |
| 58 } | 65 } |
| 59 | 66 |
| 60 void CSPDirectiveList::reportViolationWithState(const String& directiveText, con
st String& effectiveDirective, const String& message, const KURL& blockedURL, Sc
riptState* scriptState) const | 67 void CSPDirectiveList::reportViolationWithState(const String& directiveText, con
st String& effectiveDirective, const String& message, const KURL& blockedURL, Sc
riptState* scriptState) const |
| 61 { | 68 { |
| 62 String reportMessage = m_reportOnly ? "[Report Only] " + message : message; | 69 String reportMessage = m_reportOnly ? "[Report Only] " + message : message; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 prefix = "Refused to load the stylesheet '"; | 215 prefix = "Refused to load the stylesheet '"; |
| 209 | 216 |
| 210 String suffix = String(); | 217 String suffix = String(); |
| 211 if (directive == m_defaultSrc) | 218 if (directive == m_defaultSrc) |
| 212 suffix = " Note that '" + effectiveDirective + "' was not explicitly set
, so 'default-src' is used as a fallback."; | 219 suffix = " Note that '" + effectiveDirective + "' was not explicitly set
, so 'default-src' is used as a fallback."; |
| 213 | 220 |
| 214 reportViolation(directive->text(), effectiveDirective, prefix + url.elidedSt
ring() + "' because it violates the following Content Security Policy directive:
\"" + directive->text() + "\"." + suffix + "\n", url); | 221 reportViolation(directive->text(), effectiveDirective, prefix + url.elidedSt
ring() + "' because it violates the following Content Security Policy directive:
\"" + directive->text() + "\"." + suffix + "\n", url); |
| 215 return denyIfEnforcingPolicy(); | 222 return denyIfEnforcingPolicy(); |
| 216 } | 223 } |
| 217 | 224 |
| 218 bool CSPDirectiveList::checkAncestorsAndReportViolation(SourceListDirective* dir
ective, LocalFrame* frame) const | 225 bool CSPDirectiveList::checkAncestorsAndReportViolation(SourceListDirective* dir
ective, LocalFrame* frame, const KURL& url) const |
| 219 { | 226 { |
| 220 if (checkAncestors(directive, frame)) | 227 if (checkAncestors(directive, frame)) |
| 221 return true; | 228 return true; |
| 222 | 229 |
| 223 reportViolation(directive->text(), "frame-ancestors", "Refused to display '"
+ frame->document()->url().elidedString() + " in a frame because an ancestor vi
olates the following Content Security Policy directive: \"" + directive->text()
+ "\".", frame->document()->url()); | 230 reportViolationWithFrame(directive->text(), "frame-ancestors", "Refused to d
isplay '" + url.elidedString() + "' in a frame because an ancestor violates the
following Content Security Policy directive: \"" + directive->text() + "\".", ur
l, frame); |
| 224 return denyIfEnforcingPolicy(); | 231 return denyIfEnforcingPolicy(); |
| 225 } | 232 } |
| 226 | 233 |
| 227 bool CSPDirectiveList::allowJavaScriptURLs(const String& contextURL, const WTF::
OrdinalNumber& contextLine, ContentSecurityPolicy::ReportingStatus reportingStat
us) const | 234 bool CSPDirectiveList::allowJavaScriptURLs(const String& contextURL, const WTF::
OrdinalNumber& contextLine, ContentSecurityPolicy::ReportingStatus reportingStat
us) const |
| 228 { | 235 { |
| 229 DEFINE_STATIC_LOCAL(String, consoleMessage, ("Refused to execute JavaScript
URL because it violates the following Content Security Policy directive: ")); | 236 DEFINE_STATIC_LOCAL(String, consoleMessage, ("Refused to execute JavaScript
URL because it violates the following Content Security Policy directive: ")); |
| 230 if (reportingStatus == ContentSecurityPolicy::SendReport) | 237 if (reportingStatus == ContentSecurityPolicy::SendReport) |
| 231 return checkInlineAndReportViolation(operativeDirective(m_scriptSrc.get(
)), consoleMessage, contextURL, contextLine, true); | 238 return checkInlineAndReportViolation(operativeDirective(m_scriptSrc.get(
)), consoleMessage, contextURL, contextLine, true); |
| 232 | 239 |
| 233 return checkInline(operativeDirective(m_scriptSrc.get())); | 240 return checkInline(operativeDirective(m_scriptSrc.get())); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 checkSource(m_formAction.get(), url); | 359 checkSource(m_formAction.get(), url); |
| 353 } | 360 } |
| 354 | 361 |
| 355 bool CSPDirectiveList::allowBaseURI(const KURL& url, ContentSecurityPolicy::Repo
rtingStatus reportingStatus) const | 362 bool CSPDirectiveList::allowBaseURI(const KURL& url, ContentSecurityPolicy::Repo
rtingStatus reportingStatus) const |
| 356 { | 363 { |
| 357 return reportingStatus == ContentSecurityPolicy::SendReport ? | 364 return reportingStatus == ContentSecurityPolicy::SendReport ? |
| 358 checkSourceAndReportViolation(m_baseURI.get(), url, ContentSecurityPolic
y::BaseURI) : | 365 checkSourceAndReportViolation(m_baseURI.get(), url, ContentSecurityPolic
y::BaseURI) : |
| 359 checkSource(m_baseURI.get(), url); | 366 checkSource(m_baseURI.get(), url); |
| 360 } | 367 } |
| 361 | 368 |
| 362 bool CSPDirectiveList::allowAncestors(LocalFrame* frame, ContentSecurityPolicy::
ReportingStatus reportingStatus) const | 369 bool CSPDirectiveList::allowAncestors(LocalFrame* frame, const KURL& url, Conten
tSecurityPolicy::ReportingStatus reportingStatus) const |
| 363 { | 370 { |
| 364 return reportingStatus == ContentSecurityPolicy::SendReport ? | 371 return reportingStatus == ContentSecurityPolicy::SendReport ? |
| 365 checkAncestorsAndReportViolation(m_frameAncestors.get(), frame) : | 372 checkAncestorsAndReportViolation(m_frameAncestors.get(), frame, url) : |
| 366 checkAncestors(m_frameAncestors.get(), frame); | 373 checkAncestors(m_frameAncestors.get(), frame); |
| 367 } | 374 } |
| 368 | 375 |
| 369 bool CSPDirectiveList::allowChildContextFromSource(const KURL& url, ContentSecur
ityPolicy::ReportingStatus reportingStatus) const | 376 bool CSPDirectiveList::allowChildContextFromSource(const KURL& url, ContentSecur
ityPolicy::ReportingStatus reportingStatus) const |
| 370 { | 377 { |
| 371 return reportingStatus == ContentSecurityPolicy::SendReport ? | 378 return reportingStatus == ContentSecurityPolicy::SendReport ? |
| 372 checkSourceAndReportViolation(operativeDirective(m_childSrc.get()), url,
ContentSecurityPolicy::ChildSrc) : | 379 checkSourceAndReportViolation(operativeDirective(m_childSrc.get()), url,
ContentSecurityPolicy::ChildSrc) : |
| 373 checkSource(operativeDirective(m_childSrc.get()), url); | 380 checkSource(operativeDirective(m_childSrc.get()), url); |
| 374 } | 381 } |
| 375 | 382 |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 parseReferrer(name, value); | 681 parseReferrer(name, value); |
| 675 else | 682 else |
| 676 m_policy->reportUnsupportedDirective(name); | 683 m_policy->reportUnsupportedDirective(name); |
| 677 } else { | 684 } else { |
| 678 m_policy->reportUnsupportedDirective(name); | 685 m_policy->reportUnsupportedDirective(name); |
| 679 } | 686 } |
| 680 } | 687 } |
| 681 | 688 |
| 682 | 689 |
| 683 } // namespace blink | 690 } // namespace blink |
| OLD | NEW |