Chromium Code Reviews| 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 "content/common/content_security_policy/csp_context.h" | 5 #include "content/common/content_security_policy/csp_context.h" |
| 6 #include "content/common/content_security_policy_header.h" | 6 #include "content/common/content_security_policy_header.h" |
| 7 #include "content/common/navigation_params.h" | 7 #include "content/common/navigation_params.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace content { | 10 namespace content { |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 class CSPContextTest : public CSPContext { | 13 class CSPContextTest : public CSPContext { |
| 14 public: | 14 public: |
| 15 const std::string& LastConsoleMessage() { return console_message_; } | 15 const std::string& LastConsoleMessage() { return console_message_; } |
| 16 | 16 |
| 17 void AddSchemeToBypassCSP(const std::string& scheme) { | |
| 18 scheme_to_bypass_.push_back(scheme); | |
| 19 } | |
| 20 | |
| 21 bool SchemeShouldBypassCSP(const base::StringPiece& scheme) override { | |
| 22 return std::find(scheme_to_bypass_.begin(), scheme_to_bypass_.end(), | |
| 23 scheme) != scheme_to_bypass_.end(); | |
| 24 } | |
| 25 | |
| 17 private: | 26 private: |
| 18 void ReportContentSecurityPolicyViolation( | 27 void ReportContentSecurityPolicyViolation( |
| 19 const CSPViolationParams& violation_params) override { | 28 const CSPViolationParams& violation_params) override { |
| 20 console_message_ = violation_params.console_message; | 29 console_message_ = violation_params.console_message; |
| 21 } | 30 } |
| 22 std::string console_message_; | 31 std::string console_message_; |
| 32 std::vector<std::string> scheme_to_bypass_; | |
| 23 }; | 33 }; |
| 24 | 34 |
| 25 ContentSecurityPolicyHeader EmptyCspHeader() { | 35 ContentSecurityPolicyHeader EmptyCspHeader() { |
| 26 return ContentSecurityPolicyHeader(std::string(), | 36 return ContentSecurityPolicyHeader(std::string(), |
| 27 blink::WebContentSecurityPolicyTypeEnforce, | 37 blink::WebContentSecurityPolicyTypeEnforce, |
| 28 blink::WebContentSecurityPolicySourceHTTP); | 38 blink::WebContentSecurityPolicySourceHTTP); |
| 29 } | 39 } |
| 30 | 40 |
| 31 } // namespace | 41 } // namespace |
| 32 | 42 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 GURL("http://b.com"), false, | 135 GURL("http://b.com"), false, |
| 126 &context, SourceLocation())); | 136 &context, SourceLocation())); |
| 127 const char console_message[] = | 137 const char console_message[] = |
| 128 "Refused to frame 'http://b.com/' because it violates " | 138 "Refused to frame 'http://b.com/' because it violates " |
| 129 "the following Content Security Policy directive: \"frame-src " | 139 "the following Content Security Policy directive: \"frame-src " |
| 130 "http://a.com\".\n"; | 140 "http://a.com\".\n"; |
| 131 EXPECT_EQ(console_message, context.LastConsoleMessage()); | 141 EXPECT_EQ(console_message, context.LastConsoleMessage()); |
| 132 } | 142 } |
| 133 } | 143 } |
| 134 | 144 |
| 145 TEST(ContentSecurityPolicy, RequestsAllowedWhenBypassingCSP) { | |
| 146 CSPContextTest context; | |
| 147 std::vector<std::string> report_end_points; // empty | |
| 148 CSPSource source("https", "example.com", false, | |
| 149 url::PORT_UNSPECIFIED, false, ""); | |
| 150 CSPSourceList source_list(false, false, {source}); | |
| 151 ContentSecurityPolicy policy( | |
| 152 EmptyCspHeader(), | |
| 153 {CSPDirective(CSPDirective::DefaultSrc, source_list)}, | |
| 154 report_end_points); | |
| 155 | |
| 156 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 157 GURL("https://example.com/"), | |
| 158 false, &context, SourceLocation())); | |
| 159 EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 160 GURL("https://not-example.com/"), | |
| 161 false, &context, SourceLocation())); | |
| 162 | |
| 163 | |
| 164 // Register 'https' as bypassing CSP, which should now bypass is entirely | |
|
arthursonzogni
2017/04/04 11:54:34
Nit: missing dot at the end of the comment.
Same t
andypaicu
2017/04/04 15:10:07
Done
| |
| 165 context.AddSchemeToBypassCSP("https"); | |
| 166 | |
| 167 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 168 GURL("https://example.com/"), | |
| 169 false, &context, SourceLocation())); | |
| 170 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 171 GURL("https://not-example.com/"), | |
| 172 false, &context, SourceLocation())); | |
| 173 } | |
| 174 | |
| 175 TEST(ContentSecurityPolicy, FilesystemAllowedWhenBypassingCSP) { | |
| 176 CSPContextTest context; | |
| 177 std::vector<std::string> report_end_points; // empty | |
| 178 CSPSource source("https", "example.com", false, | |
| 179 url::PORT_UNSPECIFIED, false, ""); | |
| 180 CSPSourceList source_list(false, false, {source}); | |
| 181 ContentSecurityPolicy policy( | |
| 182 EmptyCspHeader(), | |
| 183 {CSPDirective(CSPDirective::DefaultSrc, source_list)}, | |
| 184 report_end_points); | |
| 185 | |
| 186 EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 187 GURL("filesystem:https://example.com/ file.txt/"), | |
|
arthursonzogni
2017/04/04 11:54:34
This line exceeds 80 character.
Please run git cl
arthursonzogni
2017/04/04 11:54:34
Why is there a slash at the end of the URL?
There
andypaicu
2017/04/04 15:10:07
Urgh... git cl format was failing locally but the
| |
| 188 false, &context, SourceLocation())); | |
| 189 EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 190 GURL("filesystem:https://not-example .com/file.txt"), | |
| 191 false, &context, SourceLocation())); | |
| 192 | |
| 193 | |
| 194 // Register 'https' as bypassing CSP, which should now bypass is entirely | |
| 195 context.AddSchemeToBypassCSP("https"); | |
| 196 | |
| 197 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 198 GURL("filesystem:https://example.com /file.txt/"), | |
| 199 false, &context, SourceLocation())); | |
| 200 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 201 GURL("filesystem:https://not-example .com/file.txt"), | |
| 202 false, &context, SourceLocation())); | |
| 203 } | |
| 204 | |
| 205 TEST(ContentSecurityPolicy, BlobAllowedWhenBypassingCSP) { | |
| 206 CSPContextTest context; | |
| 207 std::vector<std::string> report_end_points; // empty | |
| 208 CSPSource source("https", "example.com", false, | |
| 209 url::PORT_UNSPECIFIED, false, ""); | |
| 210 CSPSourceList source_list(false, false, {source}); | |
| 211 ContentSecurityPolicy policy( | |
| 212 EmptyCspHeader(), | |
| 213 {CSPDirective(CSPDirective::DefaultSrc, source_list)}, | |
| 214 report_end_points); | |
| 215 | |
| 216 EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 217 GURL("blob:https://example.com/file.t xt/"), | |
| 218 false, &context, SourceLocation())); | |
| 219 EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 220 GURL("blob:https://not-example.com/f ile.txt"), | |
| 221 false, &context, SourceLocation())); | |
| 222 | |
| 223 | |
| 224 // Register 'https' as bypassing CSP, which should now bypass is entirely | |
| 225 context.AddSchemeToBypassCSP("https"); | |
| 226 | |
| 227 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 228 GURL("blob:https://example.com/file. txt/"), | |
| 229 false, &context, SourceLocation())); | |
| 230 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, | |
| 231 GURL("blob:https://not-example.com/f ile.txt"), | |
| 232 false, &context, SourceLocation())); | |
| 233 } | |
| 234 | |
| 135 } // namespace content | 235 } // namespace content |
| OLD | NEW |