| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 <sstream> | 5 #include <sstream> |
| 6 #include "base/strings/string_split.h" | 6 #include "base/strings/string_split.h" |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "content/common/content_security_policy/csp_context.h" | 8 #include "content/common/content_security_policy/csp_context.h" |
| 9 | 9 |
| 10 namespace content { | 10 namespace content { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 // TODO(arthursonzogni): the url length should be limited to 1024 char. Find | 35 // TODO(arthursonzogni): the url length should be limited to 1024 char. Find |
| 36 // a function that will not break the utf8 encoding while eliding the string. | 36 // a function that will not break the utf8 encoding while eliding the string. |
| 37 return url.spec(); | 37 return url.spec(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void ReportViolation(CSPContext* context, | 40 void ReportViolation(CSPContext* context, |
| 41 const ContentSecurityPolicy& policy, | 41 const ContentSecurityPolicy& policy, |
| 42 const CSPDirective& directive, | 42 const CSPDirective& directive, |
| 43 const CSPDirective::Name directive_name, | 43 const CSPDirective::Name directive_name, |
| 44 const GURL& url, | 44 const GURL& url, |
| 45 bool is_redirect) { | 45 bool is_redirect, |
| 46 const SourceLocation& source_location) { |
| 46 // We should never have a violation against `child-src` or `default-src` | 47 // We should never have a violation against `child-src` or `default-src` |
| 47 // directly; the effective directive should always be one of the explicit | 48 // directly; the effective directive should always be one of the explicit |
| 48 // fetch directives. | 49 // fetch directives. |
| 49 DCHECK_NE(directive_name, CSPDirective::DefaultSrc); | 50 DCHECK_NE(directive_name, CSPDirective::DefaultSrc); |
| 50 DCHECK_NE(directive_name, CSPDirective::ChildSrc); | 51 DCHECK_NE(directive_name, CSPDirective::ChildSrc); |
| 51 | 52 |
| 52 std::stringstream message; | 53 std::stringstream message; |
| 53 | 54 |
| 54 if (policy.disposition == blink::WebContentSecurityPolicyTypeReport) | 55 if (policy.disposition == blink::WebContentSecurityPolicyTypeReport) |
| 55 message << "[Report Only] "; | 56 message << "[Report Only] "; |
| 56 | 57 |
| 57 if (directive_name == CSPDirective::FormAction) | 58 if (directive_name == CSPDirective::FormAction) |
| 58 message << "Refused to send form data to '"; | 59 message << "Refused to send form data to '"; |
| 59 else if (directive_name == CSPDirective::FrameSrc) | 60 else if (directive_name == CSPDirective::FrameSrc) |
| 60 message << "Refused to frame '"; | 61 message << "Refused to frame '"; |
| 61 | 62 |
| 62 message << ElideURLForReportViolation(url) | 63 message << ElideURLForReportViolation(url) |
| 63 << "' because it violates the following Content Security Policy " | 64 << "' because it violates the following Content Security Policy " |
| 64 "directive: \"" | 65 "directive: \"" |
| 65 << directive.ToString() << "\"."; | 66 << directive.ToString() << "\"."; |
| 66 | 67 |
| 67 if (directive.name != directive_name) | 68 if (directive.name != directive_name) |
| 68 message << " Note that '" << CSPDirective::NameToString(directive_name) | 69 message << " Note that '" << CSPDirective::NameToString(directive_name) |
| 69 << "' was not explicitly set, so '" | 70 << "' was not explicitly set, so '" |
| 70 << CSPDirective::NameToString(directive.name) | 71 << CSPDirective::NameToString(directive.name) |
| 71 << "' is used as a fallback."; | 72 << "' is used as a fallback."; |
| 72 | 73 |
| 73 message << "\n"; | 74 message << "\n"; |
| 74 | 75 |
| 75 context->LogToConsole(message.str()); | |
| 76 | |
| 77 context->ReportContentSecurityPolicyViolation(CSPViolationParams( | 76 context->ReportContentSecurityPolicyViolation(CSPViolationParams( |
| 78 CSPDirective::NameToString(directive.name), | 77 CSPDirective::NameToString(directive.name), |
| 79 CSPDirective::NameToString(directive_name), message.str(), url, | 78 CSPDirective::NameToString(directive_name), message.str(), url, |
| 80 policy.report_endpoints, policy.header, policy.disposition, is_redirect)); | 79 policy.report_endpoints, policy.header, policy.disposition, is_redirect, |
| 80 source_location)); |
| 81 } | 81 } |
| 82 | 82 |
| 83 bool AllowDirective(CSPContext* context, | 83 bool AllowDirective(CSPContext* context, |
| 84 const ContentSecurityPolicy& policy, | 84 const ContentSecurityPolicy& policy, |
| 85 const CSPDirective& directive, | 85 const CSPDirective& directive, |
| 86 CSPDirective::Name directive_name, | 86 CSPDirective::Name directive_name, |
| 87 const GURL& url, | 87 const GURL& url, |
| 88 bool is_redirect) { | 88 bool is_redirect, |
| 89 const SourceLocation& source_location) { |
| 89 if (CSPSourceList::Allow(directive.source_list, url, context, is_redirect)) | 90 if (CSPSourceList::Allow(directive.source_list, url, context, is_redirect)) |
| 90 return true; | 91 return true; |
| 91 | 92 |
| 92 ReportViolation(context, policy, directive, directive_name, url, is_redirect); | 93 ReportViolation(context, policy, directive, directive_name, url, is_redirect, |
| 94 source_location); |
| 93 return false; | 95 return false; |
| 94 } | 96 } |
| 95 | 97 |
| 96 } // namespace | 98 } // namespace |
| 97 | 99 |
| 98 ContentSecurityPolicy::ContentSecurityPolicy() | 100 ContentSecurityPolicy::ContentSecurityPolicy() |
| 99 : disposition(blink::WebContentSecurityPolicyTypeEnforce), | 101 : disposition(blink::WebContentSecurityPolicyTypeEnforce), |
| 100 source(blink::WebContentSecurityPolicySourceHTTP) {} | 102 source(blink::WebContentSecurityPolicySourceHTTP) {} |
| 101 | 103 |
| 102 ContentSecurityPolicy::ContentSecurityPolicy( | 104 ContentSecurityPolicy::ContentSecurityPolicy( |
| 103 blink::WebContentSecurityPolicyType disposition, | 105 blink::WebContentSecurityPolicyType disposition, |
| 104 blink::WebContentSecurityPolicySource source, | 106 blink::WebContentSecurityPolicySource source, |
| 105 const std::vector<CSPDirective>& directives, | 107 const std::vector<CSPDirective>& directives, |
| 106 const std::vector<std::string>& report_endpoints, | 108 const std::vector<std::string>& report_endpoints, |
| 107 const std::string& header) | 109 const std::string& header) |
| 108 : disposition(disposition), | 110 : disposition(disposition), |
| 109 source(source), | 111 source(source), |
| 110 directives(directives), | 112 directives(directives), |
| 111 report_endpoints(report_endpoints), | 113 report_endpoints(report_endpoints), |
| 112 header(header) {} | 114 header(header) {} |
| 113 | 115 |
| 114 ContentSecurityPolicy::ContentSecurityPolicy(const ContentSecurityPolicy&) = | 116 ContentSecurityPolicy::ContentSecurityPolicy(const ContentSecurityPolicy&) = |
| 115 default; | 117 default; |
| 116 ContentSecurityPolicy::~ContentSecurityPolicy() = default; | 118 ContentSecurityPolicy::~ContentSecurityPolicy() = default; |
| 117 | 119 |
| 118 // static | 120 // static |
| 119 bool ContentSecurityPolicy::Allow(const ContentSecurityPolicy& policy, | 121 bool ContentSecurityPolicy::Allow(const ContentSecurityPolicy& policy, |
| 120 CSPDirective::Name directive_name, | 122 CSPDirective::Name directive_name, |
| 121 const GURL& url, | 123 const GURL& url, |
| 124 bool is_redirect, |
| 122 CSPContext* context, | 125 CSPContext* context, |
| 123 bool is_redirect) { | 126 const SourceLocation& source_location) { |
| 124 CSPDirective::Name current_directive_name = directive_name; | 127 CSPDirective::Name current_directive_name = directive_name; |
| 125 do { | 128 do { |
| 126 for (const CSPDirective& directive : policy.directives) { | 129 for (const CSPDirective& directive : policy.directives) { |
| 127 if (directive.name == current_directive_name) { | 130 if (directive.name == current_directive_name) { |
| 128 bool allowed = AllowDirective(context, policy, directive, | 131 bool allowed = |
| 129 directive_name, url, is_redirect); | 132 AllowDirective(context, policy, directive, directive_name, url, |
| 133 is_redirect, source_location); |
| 130 return allowed || | 134 return allowed || |
| 131 policy.disposition == blink::WebContentSecurityPolicyTypeReport; | 135 policy.disposition == blink::WebContentSecurityPolicyTypeReport; |
| 132 } | 136 } |
| 133 } | 137 } |
| 134 current_directive_name = CSPFallback(current_directive_name); | 138 current_directive_name = CSPFallback(current_directive_name); |
| 135 } while (current_directive_name != CSPDirective::Unknown); | 139 } while (current_directive_name != CSPDirective::Unknown); |
| 136 return true; | 140 return true; |
| 137 } | 141 } |
| 138 | 142 |
| 139 std::string ContentSecurityPolicy::ToString() const { | 143 std::string ContentSecurityPolicy::ToString() const { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 152 is_first_policy = false; | 156 is_first_policy = false; |
| 153 text << "report-uri"; | 157 text << "report-uri"; |
| 154 for (const std::string& endpoint : report_endpoints) | 158 for (const std::string& endpoint : report_endpoints) |
| 155 text << " " << endpoint; | 159 text << " " << endpoint; |
| 156 } | 160 } |
| 157 | 161 |
| 158 return text.str(); | 162 return text.str(); |
| 159 } | 163 } |
| 160 | 164 |
| 161 } // namespace content | 165 } // namespace content |
| OLD | NEW |