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 <set> |
| 6 |
5 #include "content/common/content_security_policy/csp_context.h" | 7 #include "content/common/content_security_policy/csp_context.h" |
6 #include "content/common/content_security_policy_header.h" | 8 #include "content/common/content_security_policy_header.h" |
7 #include "content/common/navigation_params.h" | 9 #include "content/common/navigation_params.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
9 | 11 |
10 namespace content { | 12 namespace content { |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 class CSPContextTest : public CSPContext { | 16 class CSPContextTest : public CSPContext { |
15 public: | 17 public: |
16 const std::string& LastConsoleMessage() { return console_message_; } | 18 const CSPViolationParams& last_violation() { return last_violation_; } |
17 | 19 |
18 void AddSchemeToBypassCSP(const std::string& scheme) { | 20 void AddSchemeToBypassCSP(const std::string& scheme) { |
19 scheme_to_bypass_.push_back(scheme); | 21 scheme_to_bypass_.insert(scheme); |
20 } | 22 } |
21 | 23 |
22 bool SchemeShouldBypassCSP(const base::StringPiece& scheme) override { | 24 bool SchemeShouldBypassCSP(const base::StringPiece& scheme) override { |
23 return std::find(scheme_to_bypass_.begin(), scheme_to_bypass_.end(), | 25 return scheme_to_bypass_.count(scheme.as_string()); |
24 scheme) != scheme_to_bypass_.end(); | 26 } |
| 27 |
| 28 void set_sanitize_data_for_use_in_csp_violation(bool value) { |
| 29 sanitize_data_for_use_in_csp_violation_ = value; |
| 30 } |
| 31 |
| 32 void SanitizeDataForUseInCspViolation( |
| 33 bool is_redirect, |
| 34 CSPDirective::Name directive, |
| 35 GURL* blocked_url, |
| 36 SourceLocation* source_location) const override { |
| 37 if (!sanitize_data_for_use_in_csp_violation_) |
| 38 return; |
| 39 *blocked_url = blocked_url->GetOrigin(); |
| 40 *source_location = |
| 41 SourceLocation(GURL(source_location->url).GetOrigin().spec(), 0u, 0u); |
25 } | 42 } |
26 | 43 |
27 private: | 44 private: |
28 void ReportContentSecurityPolicyViolation( | 45 void ReportContentSecurityPolicyViolation( |
29 const CSPViolationParams& violation_params) override { | 46 const CSPViolationParams& violation_params) override { |
30 console_message_ = violation_params.console_message; | 47 last_violation_ = violation_params; |
31 } | 48 } |
32 std::string console_message_; | 49 CSPViolationParams last_violation_; |
33 std::vector<std::string> scheme_to_bypass_; | 50 std::set<std::string> scheme_to_bypass_; |
| 51 bool sanitize_data_for_use_in_csp_violation_ = false; |
34 }; | 52 }; |
35 | 53 |
36 // Build a new policy made of only one directive and no report endpoints. | 54 // Build a new policy made of only one directive and no report endpoints. |
37 ContentSecurityPolicy BuildPolicy(CSPDirective::Name directive_name, | 55 ContentSecurityPolicy BuildPolicy(CSPDirective::Name directive_name, |
38 std::vector<CSPSource> sources) { | 56 std::vector<CSPSource> sources) { |
39 return ContentSecurityPolicy( | 57 return ContentSecurityPolicy( |
40 ContentSecurityPolicyHeader(std::string(), // header | 58 ContentSecurityPolicyHeader(std::string(), // header |
41 blink::kWebContentSecurityPolicyTypeEnforce, | 59 blink::kWebContentSecurityPolicyTypeEnforce, |
42 blink::kWebContentSecurityPolicySourceHTTP), | 60 blink::kWebContentSecurityPolicySourceHTTP), |
43 {CSPDirective(directive_name, CSPSourceList(false, false, sources))}, | 61 {CSPDirective(directive_name, CSPSourceList(false, false, sources))}, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 EXPECT_TRUE(context.IsAllowedByCsp( | 97 EXPECT_TRUE(context.IsAllowedByCsp( |
80 CSPDirective::FrameSrc, GURL("http://a.com"), false, SourceLocation())); | 98 CSPDirective::FrameSrc, GURL("http://a.com"), false, SourceLocation())); |
81 EXPECT_FALSE(context.IsAllowedByCsp( | 99 EXPECT_FALSE(context.IsAllowedByCsp( |
82 CSPDirective::FrameSrc, GURL("http://b.com"), false, SourceLocation())); | 100 CSPDirective::FrameSrc, GURL("http://b.com"), false, SourceLocation())); |
83 EXPECT_FALSE(context.IsAllowedByCsp( | 101 EXPECT_FALSE(context.IsAllowedByCsp( |
84 CSPDirective::FrameSrc, GURL("http://c.com"), false, SourceLocation())); | 102 CSPDirective::FrameSrc, GURL("http://c.com"), false, SourceLocation())); |
85 EXPECT_FALSE(context.IsAllowedByCsp( | 103 EXPECT_FALSE(context.IsAllowedByCsp( |
86 CSPDirective::FrameSrc, GURL("http://d.com"), false, SourceLocation())); | 104 CSPDirective::FrameSrc, GURL("http://d.com"), false, SourceLocation())); |
87 } | 105 } |
88 | 106 |
| 107 TEST(CSPContextTest, SanitizeDataForUseInCspViolation) { |
| 108 CSPContextTest context; |
| 109 context.SetSelf(url::Origin(GURL("http://a.com"))); |
| 110 |
| 111 // Content-Security-Policy: frame-src "a.com/iframe" |
| 112 context.AddContentSecurityPolicy( |
| 113 BuildPolicy(CSPDirective::FrameSrc, |
| 114 {CSPSource("", "a.com", false, url::PORT_UNSPECIFIED, false, |
| 115 "/iframe")})); |
| 116 |
| 117 GURL blocked_url("http://a.com/login?password=1234"); |
| 118 SourceLocation source_location("http://a.com/login", 10u, 20u); |
| 119 |
| 120 // When the |blocked_url| and |source_location| aren't sensitive information. |
| 121 { |
| 122 EXPECT_FALSE(context.IsAllowedByCsp(CSPDirective::FrameSrc, blocked_url, |
| 123 false, source_location)); |
| 124 EXPECT_EQ(context.last_violation().blocked_url, blocked_url); |
| 125 EXPECT_EQ(context.last_violation().source_location.url, |
| 126 "http://a.com/login"); |
| 127 EXPECT_EQ(context.last_violation().source_location.line_number, 10u); |
| 128 EXPECT_EQ(context.last_violation().source_location.column_number, 20u); |
| 129 EXPECT_EQ(context.last_violation().console_message, |
| 130 "Refused to frame 'http://a.com/login?password=1234' because it " |
| 131 "violates the following Content Security Policy directive: " |
| 132 "\"frame-src a.com/iframe\".\n"); |
| 133 } |
| 134 |
| 135 context.set_sanitize_data_for_use_in_csp_violation(true); |
| 136 |
| 137 // When the |blocked_url| and |source_location| are sensitive information. |
| 138 { |
| 139 EXPECT_FALSE(context.IsAllowedByCsp(CSPDirective::FrameSrc, blocked_url, |
| 140 false, source_location)); |
| 141 EXPECT_EQ(context.last_violation().blocked_url, blocked_url.GetOrigin()); |
| 142 EXPECT_EQ(context.last_violation().source_location.url, "http://a.com/"); |
| 143 EXPECT_EQ(context.last_violation().source_location.line_number, 0u); |
| 144 EXPECT_EQ(context.last_violation().source_location.column_number, 0u); |
| 145 EXPECT_EQ(context.last_violation().console_message, |
| 146 "Refused to frame 'http://a.com/' because it violates the " |
| 147 "following Content Security Policy directive: \"frame-src " |
| 148 "a.com/iframe\".\n"); |
| 149 } |
| 150 } |
| 151 |
89 } // namespace content | 152 } // namespace content |
OLD | NEW |