Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(573)

Side by Side Diff: content/common/content_security_policy/content_security_policy.cc

Issue 2764993002: CSP: group policies in didAddContentSecurityPolicy. (Closed)
Patch Set: Rebase. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 const GURL& url, 44 const GURL& url,
45 bool is_redirect) { 45 bool is_redirect) {
46 // We should never have a violation against `child-src` or `default-src` 46 // We should never have a violation against `child-src` or `default-src`
47 // directly; the effective directive should always be one of the explicit 47 // directly; the effective directive should always be one of the explicit
48 // fetch directives. 48 // fetch directives.
49 DCHECK_NE(directive_name, CSPDirective::DefaultSrc); 49 DCHECK_NE(directive_name, CSPDirective::DefaultSrc);
50 DCHECK_NE(directive_name, CSPDirective::ChildSrc); 50 DCHECK_NE(directive_name, CSPDirective::ChildSrc);
51 51
52 std::stringstream message; 52 std::stringstream message;
53 53
54 if (policy.disposition == blink::WebContentSecurityPolicyTypeReport) 54 if (policy.header.type == blink::WebContentSecurityPolicyTypeReport)
55 message << "[Report Only] "; 55 message << "[Report Only] ";
56 56
57 if (directive_name == CSPDirective::FormAction) 57 if (directive_name == CSPDirective::FormAction)
58 message << "Refused to send form data to '"; 58 message << "Refused to send form data to '";
59 else if (directive_name == CSPDirective::FrameSrc) 59 else if (directive_name == CSPDirective::FrameSrc)
60 message << "Refused to frame '"; 60 message << "Refused to frame '";
61 61
62 message << ElideURLForReportViolation(url) 62 message << ElideURLForReportViolation(url)
63 << "' because it violates the following Content Security Policy " 63 << "' because it violates the following Content Security Policy "
64 "directive: \"" 64 "directive: \""
65 << directive.ToString() << "\"."; 65 << directive.ToString() << "\".";
66 66
67 if (directive.name != directive_name) 67 if (directive.name != directive_name)
68 message << " Note that '" << CSPDirective::NameToString(directive_name) 68 message << " Note that '" << CSPDirective::NameToString(directive_name)
69 << "' was not explicitly set, so '" 69 << "' was not explicitly set, so '"
70 << CSPDirective::NameToString(directive.name) 70 << CSPDirective::NameToString(directive.name)
71 << "' is used as a fallback."; 71 << "' is used as a fallback.";
72 72
73 message << "\n"; 73 message << "\n";
74 74
75 context->LogToConsole(message.str()); 75 context->LogToConsole(message.str());
76 76
77 context->ReportContentSecurityPolicyViolation(CSPViolationParams( 77 context->ReportContentSecurityPolicyViolation(CSPViolationParams(
78 CSPDirective::NameToString(directive.name), 78 CSPDirective::NameToString(directive.name),
79 CSPDirective::NameToString(directive_name), message.str(), url, 79 CSPDirective::NameToString(directive_name), message.str(), url,
80 policy.report_endpoints, policy.header, policy.disposition, is_redirect)); 80 policy.report_endpoints, policy.header.header_value, policy.header.type,
81 is_redirect));
81 } 82 }
82 83
83 bool AllowDirective(CSPContext* context, 84 bool AllowDirective(CSPContext* context,
84 const ContentSecurityPolicy& policy, 85 const ContentSecurityPolicy& policy,
85 const CSPDirective& directive, 86 const CSPDirective& directive,
86 CSPDirective::Name directive_name, 87 CSPDirective::Name directive_name,
87 const GURL& url, 88 const GURL& url,
88 bool is_redirect) { 89 bool is_redirect) {
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);
93 return false; 94 return false;
94 } 95 }
95 96
96 } // namespace 97 } // namespace
97 98
98 ContentSecurityPolicy::ContentSecurityPolicy() 99 ContentSecurityPolicy::ContentSecurityPolicy()
99 : disposition(blink::WebContentSecurityPolicyTypeEnforce), 100 : header(std::string(),
100 source(blink::WebContentSecurityPolicySourceHTTP) {} 101 blink::WebContentSecurityPolicyTypeEnforce,
102 blink::WebContentSecurityPolicySourceHTTP) {}
101 103
102 ContentSecurityPolicy::ContentSecurityPolicy( 104 ContentSecurityPolicy::ContentSecurityPolicy(
103 blink::WebContentSecurityPolicyType disposition, 105 const ContentSecurityPolicyHeader& header,
104 blink::WebContentSecurityPolicySource source,
105 const std::vector<CSPDirective>& directives, 106 const std::vector<CSPDirective>& directives,
106 const std::vector<std::string>& report_endpoints, 107 const std::vector<std::string>& report_endpoints)
107 const std::string& header) 108 : header(header),
108 : disposition(disposition),
109 source(source),
110 directives(directives), 109 directives(directives),
111 report_endpoints(report_endpoints), 110 report_endpoints(report_endpoints) {}
112 header(header) {}
113 111
114 ContentSecurityPolicy::ContentSecurityPolicy(const ContentSecurityPolicy&) = 112 ContentSecurityPolicy::ContentSecurityPolicy(const ContentSecurityPolicy&) =
115 default; 113 default;
116 ContentSecurityPolicy::~ContentSecurityPolicy() = default; 114 ContentSecurityPolicy::~ContentSecurityPolicy() = default;
117 115
118 // static 116 // static
119 bool ContentSecurityPolicy::Allow(const ContentSecurityPolicy& policy, 117 bool ContentSecurityPolicy::Allow(const ContentSecurityPolicy& policy,
120 CSPDirective::Name directive_name, 118 CSPDirective::Name directive_name,
121 const GURL& url, 119 const GURL& url,
122 CSPContext* context, 120 CSPContext* context,
123 bool is_redirect) { 121 bool is_redirect) {
124 CSPDirective::Name current_directive_name = directive_name; 122 CSPDirective::Name current_directive_name = directive_name;
125 do { 123 do {
126 for (const CSPDirective& directive : policy.directives) { 124 for (const CSPDirective& directive : policy.directives) {
127 if (directive.name == current_directive_name) { 125 if (directive.name == current_directive_name) {
128 bool allowed = AllowDirective(context, policy, directive, 126 bool allowed = AllowDirective(context, policy, directive,
129 directive_name, url, is_redirect); 127 directive_name, url, is_redirect);
130 return allowed || 128 return allowed ||
131 policy.disposition == blink::WebContentSecurityPolicyTypeReport; 129 policy.header.type == blink::WebContentSecurityPolicyTypeReport;
132 } 130 }
133 } 131 }
134 current_directive_name = CSPFallback(current_directive_name); 132 current_directive_name = CSPFallback(current_directive_name);
135 } while (current_directive_name != CSPDirective::Unknown); 133 } while (current_directive_name != CSPDirective::Unknown);
136 return true; 134 return true;
137 } 135 }
138 136
139 std::string ContentSecurityPolicy::ToString() const { 137 std::string ContentSecurityPolicy::ToString() const {
140 std::stringstream text; 138 std::stringstream text;
141 bool is_first_policy = true; 139 bool is_first_policy = true;
(...skipping 10 matching lines...) Expand all
152 is_first_policy = false; 150 is_first_policy = false;
153 text << "report-uri"; 151 text << "report-uri";
154 for (const std::string& endpoint : report_endpoints) 152 for (const std::string& endpoint : report_endpoints)
155 text << " " << endpoint; 153 text << " " << endpoint;
156 } 154 }
157 155
158 return text.str(); 156 return text.str();
159 } 157 }
160 158
161 } // namespace content 159 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698