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

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

Issue 2612793002: Implement ContentSecurityPolicy on the browser-side. (Closed)
Patch Set: Rebase from master. Created 3 years, 10 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <sstream>
6 #include "base/strings/string_split.h"
7 #include "base/strings/string_util.h"
8 #include "content/common/content_security_policy/csp_context.h"
9
10 namespace content {
11
12 namespace {
13
14 static CSPDirective::Name CSPFallback(CSPDirective::Name directive) {
15 switch (directive) {
16 case CSPDirective::DefaultSrc:
17 case CSPDirective::FormAction:
18 return CSPDirective::Unknown;
19
20 case CSPDirective::FrameSrc:
21 return CSPDirective::ChildSrc;
22
23 case CSPDirective::ChildSrc:
24 return CSPDirective::DefaultSrc;
25
26 case CSPDirective::Unknown:
27 NOTREACHED();
28 return CSPDirective::Unknown;
29 }
30 NOTREACHED();
31 return CSPDirective::Unknown;
32 }
33
34 std::string ElideURLForReportViolation(const GURL& url) {
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.
37 return url.spec();
38 }
39
40 void ReportViolation(CSPContext* context,
41 const ContentSecurityPolicy& policy,
42 const CSPDirective& directive,
43 const CSPDirective::Name directive_name,
44 const GURL& url) {
45 // We should never have a violation against `child-src` or `default-src`
46 // directly; the effective directive should always be one of the explicit
47 // fetch directives.
48 DCHECK_NE(directive_name, CSPDirective::DefaultSrc);
49 DCHECK_NE(directive_name, CSPDirective::ChildSrc);
50
51 std::stringstream message;
52
53 if (policy.disposition == blink::WebContentSecurityPolicyTypeReport)
54 message << "[Report Only] ";
55
56 if (directive_name == CSPDirective::FormAction)
57 message << "Refused to send form data to '";
58 else if (directive_name == CSPDirective::FrameSrc)
59 message << "Refused to frame '";
60
61 message << ElideURLForReportViolation(url)
62 << "' because it violates the following Content Security Policy "
63 "directive: \""
64 << directive.ToString() << "\".";
65
66 if (directive.name != directive_name)
67 message << " Note that '" << CSPDirective::NameToString(directive_name)
68 << "' was not explicitly set, so '"
69 << CSPDirective::NameToString(directive.name)
70 << "' is used as a fallback.";
71
72 message << "\n";
73
74 context->LogToConsole(message.str());
75 context->ReportViolation(CSPDirective::NameToString(directive.name),
76 CSPDirective::NameToString(directive_name),
77 message.str(), url, policy.report_endpoints,
78 // TODO(arthursonzogni): consider passing the
79 // original header
80 "", policy.disposition);
81 }
82
83 bool AllowDirective(CSPContext* context,
84 const ContentSecurityPolicy& policy,
85 const CSPDirective& directive,
86 CSPDirective::Name directive_name,
87 const GURL& url,
88 bool is_redirect) {
89 if (CSPSourceList::Allow(directive.source_list, url, context, is_redirect))
90 return true;
91
92 ReportViolation(context, policy, directive, directive_name, url);
93 return false;
94 }
95
96 } // namespace
97
98 ContentSecurityPolicy::ContentSecurityPolicy()
99 : disposition(blink::WebContentSecurityPolicyTypeEnforce),
100 source(blink::WebContentSecurityPolicySourceHTTP),
101 directives(),
102 report_endpoints() {}
103
104 ContentSecurityPolicy::ContentSecurityPolicy(
105 blink::WebContentSecurityPolicyType disposition,
106 blink::WebContentSecurityPolicySource source,
107 const std::vector<CSPDirective>& directives,
108 const std::vector<std::string>& report_endpoints)
109 : disposition(disposition),
110 source(source),
111 directives(directives),
112 report_endpoints(report_endpoints) {}
113
114 ContentSecurityPolicy::ContentSecurityPolicy(const ContentSecurityPolicy&) =
115 default;
116 ContentSecurityPolicy::~ContentSecurityPolicy() = default;
117
118 // static
119 bool ContentSecurityPolicy::Allow(const ContentSecurityPolicy& policy,
120 CSPDirective::Name directive_name,
121 const GURL& url,
122 CSPContext* context,
123 bool is_redirect) {
124 CSPDirective::Name current_directive_name = directive_name;
125 do {
126 for (const CSPDirective& directive : policy.directives) {
127 if (directive.name == current_directive_name) {
128 bool allowed = AllowDirective(context, policy, directive,
129 directive_name, url, is_redirect);
130 return allowed ||
131 policy.disposition == blink::WebContentSecurityPolicyTypeReport;
132 }
133 }
134 current_directive_name = CSPFallback(current_directive_name);
135 } while (current_directive_name != CSPDirective::Unknown);
136 return true;
137 }
138
139 std::string ContentSecurityPolicy::ToString() const {
140 std::stringstream text;
141 bool is_first_policy = true;
142 for (const CSPDirective& directive : directives) {
143 if (!is_first_policy)
144 text << "; ";
145 is_first_policy = false;
146 text << directive.ToString();
147 }
148
149 if (!report_endpoints.empty()) {
150 if (!is_first_policy)
151 text << "; ";
152 is_first_policy = false;
153 text << "report-uri";
154 for (const std::string& endpoint : report_endpoints)
155 text << " " << endpoint;
156 }
157
158 return text.str();
159 }
160
161 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698