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

Side by Side Diff: content/common/content_security_policy/content_security_policy_unittest.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 "content/common/content_security_policy/csp_context.h"
6 #include "content/common/content_security_policy_header.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace content {
10
11 namespace {
12 class CSPContextTest : public CSPContext {
13 public:
14 const std::string& LastConsoleMessage() { return console_message_; }
15
16 private:
17 void LogToConsole(const std::string& message) override {
18 console_message_ = message;
19 }
20 std::string console_message_;
21 };
22
23 } // namespace
24
25 TEST(ContentSecurityPolicy, NoDirective) {
26 CSPContextTest context;
27 std::vector<std::string> report_end_points; // empty
28 ContentSecurityPolicy policy(blink::WebContentSecurityPolicyTypeEnforce,
29 blink::WebContentSecurityPolicySourceHTTP,
30 std::vector<CSPDirective>(), report_end_points);
31
32 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FormAction,
33 GURL("http://www.example.com"),
34 &context));
35 EXPECT_EQ("", context.LastConsoleMessage());
36 }
37
38 TEST(ContentSecurityPolicy, ReportViolation) {
39 CSPContextTest context;
40
41 // source = "www.example.com"
42 CSPSource source("", "www.example.com", false, url::PORT_UNSPECIFIED, false,
43 "");
44 CSPSourceList source_list(false, false, {source});
45 CSPDirective directive(CSPDirective::FormAction, source_list);
46 std::vector<std::string> report_end_points; // empty
47 ContentSecurityPolicy policy(blink::WebContentSecurityPolicyTypeEnforce,
48 blink::WebContentSecurityPolicySourceHTTP,
49 {directive}, report_end_points);
50
51 EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FormAction,
52 GURL("http://www.not-example.com"),
53 &context));
54
55 const char console_message[] =
56 "Refused to send form data to 'http://www.not-example.com/' because it "
57 "violates the following Content Security Policy directive: \"form-action "
58 "www.example.com\".\n";
59 EXPECT_EQ(console_message, context.LastConsoleMessage());
60 }
61
62 TEST(ContentSecurityPolicy, DirectiveFallback) {
63 CSPSource source_a("http", "a.com", false, url::PORT_UNSPECIFIED, false, "");
64 CSPSource source_b("http", "b.com", false, url::PORT_UNSPECIFIED, false, "");
65 CSPSourceList source_list_a(false, false, {source_a});
66 CSPSourceList source_list_b(false, false, {source_b});
67
68 std::vector<std::string> report_end_points; // Empty.
69
70 {
71 CSPContextTest context;
72 ContentSecurityPolicy policy(
73 blink::WebContentSecurityPolicyTypeEnforce,
74 blink::WebContentSecurityPolicySourceHTTP,
75 {CSPDirective(CSPDirective::DefaultSrc, source_list_a)},
76 report_end_points);
77 EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc,
78 GURL("http://b.com"), &context));
79 const char console_message[] =
80 "Refused to frame 'http://b.com/' because it violates "
81 "the following Content Security Policy directive: \"default-src "
82 "http://a.com\". Note that 'frame-src' was not explicitly "
83 "set, so 'default-src' is used as a fallback.\n";
84 EXPECT_EQ(console_message, context.LastConsoleMessage());
85 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc,
86 GURL("http://a.com"), &context));
87 }
88 {
89 CSPContextTest context;
90 ContentSecurityPolicy policy(
91 blink::WebContentSecurityPolicyTypeEnforce,
92 blink::WebContentSecurityPolicySourceHTTP,
93 {CSPDirective(CSPDirective::ChildSrc, source_list_a)},
94 report_end_points);
95 EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc,
96 GURL("http://b.com"), &context));
97 const char console_message[] =
98 "Refused to frame 'http://b.com/' because it violates "
99 "the following Content Security Policy directive: \"child-src "
100 "http://a.com\". Note that 'frame-src' was not explicitly "
101 "set, so 'child-src' is used as a fallback.\n";
102 EXPECT_EQ(console_message, context.LastConsoleMessage());
103 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc,
104 GURL("http://a.com"), &context));
105 }
106 {
107 CSPContextTest context;
108 CSPSourceList source_list(false, false, {source_a, source_b});
109 ContentSecurityPolicy policy(
110 blink::WebContentSecurityPolicyTypeEnforce,
111 blink::WebContentSecurityPolicySourceHTTP,
112 {CSPDirective(CSPDirective::FrameSrc, {source_list_a}),
113 CSPDirective(CSPDirective::ChildSrc, {source_list_b})},
114 report_end_points);
115 EXPECT_TRUE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc,
116 GURL("http://a.com"), &context));
117 EXPECT_FALSE(ContentSecurityPolicy::Allow(policy, CSPDirective::FrameSrc,
118 GURL("http://b.com"), &context));
119 const char console_message[] =
120 "Refused to frame 'http://b.com/' because it violates "
121 "the following Content Security Policy directive: \"frame-src "
122 "http://a.com\".\n";
123 EXPECT_EQ(console_message, context.LastConsoleMessage());
124 }
125 }
126
127 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698