Chromium Code Reviews| Index: content/common/content_security_policy/content_security_policy_unittest.cc |
| diff --git a/content/common/content_security_policy/content_security_policy_unittest.cc b/content/common/content_security_policy/content_security_policy_unittest.cc |
| index 7a0a0b9a633043490ed00fc3391591a8459d92d6..9cd0fa2f240b912ed413d7751e1f1d934a0475a1 100644 |
| --- a/content/common/content_security_policy/content_security_policy_unittest.cc |
| +++ b/content/common/content_security_policy/content_security_policy_unittest.cc |
| @@ -14,12 +14,22 @@ class CSPContextTest : public CSPContext { |
| public: |
| const std::string& LastConsoleMessage() { return console_message_; } |
| + void AddSchemeToBypassCSP(const std::string& scheme) { |
| + scheme_to_bypass_.push_back(scheme); |
| + } |
| + |
| + bool SchemeShouldBypassCSP(const base::StringPiece& scheme) override { |
| + return std::find(scheme_to_bypass_.begin(), scheme_to_bypass_.end(), |
| + scheme) != scheme_to_bypass_.end(); |
| + } |
| + |
| private: |
| void ReportContentSecurityPolicyViolation( |
| const CSPViolationParams& violation_params) override { |
| console_message_ = violation_params.console_message; |
| } |
| std::string console_message_; |
| + std::vector<std::string> scheme_to_bypass_; |
| }; |
| ContentSecurityPolicyHeader EmptyCspHeader() { |
| @@ -132,4 +142,94 @@ TEST(ContentSecurityPolicy, DirectiveFallback) { |
| } |
| } |
| +TEST(ContentSecurityPolicy, RequestsAllowedWhenBypassingCSP) { |
| + CSPContextTest context; |
| + std::vector<std::string> report_end_points; // empty |
| + CSPSource source("https", "example.com", false, |
| + url::PORT_UNSPECIFIED, false, ""); |
| + CSPSourceList source_list(false, false, {source}); |
| + ContentSecurityPolicy policy( |
| + EmptyCspHeader(), |
| + {CSPDirective(CSPDirective::DefaultSrc, source_list)}, |
| + report_end_points); |
| + |
| + EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("https://example.com/"), |
| + false, &context, SourceLocation())); |
| + EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("https://not-example.com/"), |
| + false, &context, SourceLocation())); |
| + |
| + |
| + // 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
|
| + context.AddSchemeToBypassCSP("https"); |
| + |
| + EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("https://example.com/"), |
| + false, &context, SourceLocation())); |
| + EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("https://not-example.com/"), |
| + false, &context, SourceLocation())); |
| +} |
| + |
| +TEST(ContentSecurityPolicy, FilesystemAllowedWhenBypassingCSP) { |
| + CSPContextTest context; |
| + std::vector<std::string> report_end_points; // empty |
| + CSPSource source("https", "example.com", false, |
| + url::PORT_UNSPECIFIED, false, ""); |
| + CSPSourceList source_list(false, false, {source}); |
| + ContentSecurityPolicy policy( |
| + EmptyCspHeader(), |
| + {CSPDirective(CSPDirective::DefaultSrc, source_list)}, |
| + report_end_points); |
| + |
| + EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + 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
|
| + false, &context, SourceLocation())); |
| + EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("filesystem:https://not-example.com/file.txt"), |
| + false, &context, SourceLocation())); |
| + |
| + |
| + // Register 'https' as bypassing CSP, which should now bypass is entirely |
| + context.AddSchemeToBypassCSP("https"); |
| + |
| + EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("filesystem:https://example.com/file.txt/"), |
| + false, &context, SourceLocation())); |
| + EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("filesystem:https://not-example.com/file.txt"), |
| + false, &context, SourceLocation())); |
| +} |
| + |
| +TEST(ContentSecurityPolicy, BlobAllowedWhenBypassingCSP) { |
| + CSPContextTest context; |
| + std::vector<std::string> report_end_points; // empty |
| + CSPSource source("https", "example.com", false, |
| + url::PORT_UNSPECIFIED, false, ""); |
| + CSPSourceList source_list(false, false, {source}); |
| + ContentSecurityPolicy policy( |
| + EmptyCspHeader(), |
| + {CSPDirective(CSPDirective::DefaultSrc, source_list)}, |
| + report_end_points); |
| + |
| + EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("blob:https://example.com/file.txt/"), |
| + false, &context, SourceLocation())); |
| + EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("blob:https://not-example.com/file.txt"), |
| + false, &context, SourceLocation())); |
| + |
| + |
| + // Register 'https' as bypassing CSP, which should now bypass is entirely |
| + context.AddSchemeToBypassCSP("https"); |
| + |
| + EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("blob:https://example.com/file.txt/"), |
| + false, &context, SourceLocation())); |
| + EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc, |
| + GURL("blob:https://not-example.com/file.txt"), |
| + false, &context, SourceLocation())); |
| +} |
| + |
| } // namespace content |