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

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

Issue 2612793002: Implement ContentSecurityPolicy on the browser-side. (Closed)
Patch Set: Nit. 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_source_list.h"
6 #include "content/common/content_security_policy/csp_context.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace content {
10
11 namespace {
12
13 class CSPContextTest : public CSPContext {
14 public:
15 void AddSchemeToBypassCSP(const std::string& scheme) {
16 scheme_to_bypass_.push_back(scheme);
17 }
18
19 bool SchemeShouldBypassCSP(const base::StringPiece& scheme) override {
20 return std::find(scheme_to_bypass_.begin(), scheme_to_bypass_.end(),
21 scheme) != scheme_to_bypass_.end();
22 }
23
24 private:
25 std::vector<std::string> scheme_to_bypass_;
26 };
27 }
28
29 TEST(CSPSourceListTest, MultipleSource) {
30 CSPContextTest context;
31 context.SetSelf(url::Origin(GURL("http://example.com")));
32 CSPSourceList source_list(
33 false, // allow_self
34 false, // allow_star:
35 {CSPSource("", "a.com", false, url::PORT_UNSPECIFIED, false, ""),
36 CSPSource("", "b.com", false, url::PORT_UNSPECIFIED, false, "")});
37 EXPECT_TRUE(source_list.Allow(&context, GURL("http://a.com")));
38 EXPECT_TRUE(source_list.Allow(&context, GURL("http://b.com")));
39 EXPECT_FALSE(source_list.Allow(&context, GURL("http://c.com")));
40 }
41
42 TEST(CSPSourceList, AllowStar) {
43 CSPContextTest context;
44 context.SetSelf(url::Origin(GURL("http://example.com")));
45 CSPSourceList source_list(false, // allow_self
46 true, // allow_star:
47 std::vector<CSPSource>()); // source_list
48 EXPECT_TRUE(source_list.Allow(&context, GURL("http://not-example.com")));
49 EXPECT_TRUE(source_list.Allow(&context, GURL("https://not-example.com")));
50 EXPECT_TRUE(source_list.Allow(&context, GURL("http-so://not-example.com")));
51 EXPECT_TRUE(source_list.Allow(&context, GURL("https-so://not-example.com")));
52 EXPECT_TRUE(source_list.Allow(&context, GURL("ws://not-example.com")));
53 EXPECT_TRUE(source_list.Allow(&context, GURL("wss://not-example.com")));
54 EXPECT_TRUE(source_list.Allow(&context, GURL("ftp://not-example.com")));
55
56 EXPECT_FALSE(source_list.Allow(&context, GURL("file://not-example.com")));
57 EXPECT_FALSE(source_list.Allow(&context, GURL("applewebdata://a.test")));
58
59 // With a protocol of 'file', '*' allow 'file:'
60 context.SetSelf(url::Origin(GURL("file://example.com")));
61 EXPECT_TRUE(source_list.Allow(&context, GURL("file://not-example.com")));
62 EXPECT_FALSE(source_list.Allow(&context, GURL("applewebdata://a.test")));
63 }
64
65 TEST(CSPSourceList, AllowSelf) {
66 CSPContextTest context;
67 context.SetSelf(url::Origin(GURL("http://example.com")));
68 CSPSourceList source_list(true, // allow_self
69 false, // allow_star:
70 std::vector<CSPSource>()); // source_list
71 EXPECT_TRUE(source_list.Allow(&context, GURL("http://example.com")));
72 EXPECT_FALSE(source_list.Allow(&context, GURL("http://not-example.com")));
73 EXPECT_TRUE(source_list.Allow(&context, GURL("https://example.com")));
74 EXPECT_FALSE(source_list.Allow(&context, GURL("ws://example.com")));
75 }
76
77 TEST(CSPSourceList, AllowSelfWithFilesystem) {
78 CSPContextTest context;
79 context.SetSelf(url::Origin(GURL("https://a.test")));
80 CSPSourceList source_list(true, // allow_self
81 false, // allow_star:
82 std::vector<CSPSource>()); // source_list
83
84 GURL filesystem_url("filesystem:https://a.test/file.txt");
85
86 EXPECT_TRUE(source_list.Allow(&context, GURL("https://a.test/")));
87 EXPECT_FALSE(source_list.Allow(&context, filesystem_url));
88
89 // Register 'https' as bypassing CSP, which should trigger the inner URL
90 // behavior.
91 context.AddSchemeToBypassCSP("https");
92
93 EXPECT_TRUE(source_list.Allow(&context, GURL("https://a.test/")));
94 EXPECT_TRUE(source_list.Allow(&context, filesystem_url));
95 }
96
97 TEST(CSPSourceList, BlobDisallowedWhenBypassingSelfScheme) {
98 CSPContextTest context;
99 context.SetSelf(url::Origin(GURL("https://a.test")));
100 CSPSource blob(
101 CSPSource("blob", "", false, url::PORT_UNSPECIFIED, false, ""));
102 CSPSourceList source_list(true, // allow_self
103 false, // allow_star:
104 {blob}); // source_list
105
106 GURL blob_url_self("blob:https://a.test/1be95204-93d6-4GUID");
107 GURL blob_url_not_self("blob:https://b.test/1be95204-93d6-4GUID");
108
109 EXPECT_TRUE(source_list.Allow(&context, blob_url_self));
110 EXPECT_TRUE(source_list.Allow(&context, blob_url_not_self));
111
112 // Register 'https' as bypassing CSP, which should trigger the inner URL
113 // behavior.
114 context.AddSchemeToBypassCSP("https");
115
116 EXPECT_TRUE(source_list.Allow(&context, blob_url_self));
117 // TODO(arthursonzogni, mkwst): This should be true
118 // see http://crbug.com/692046
119 EXPECT_FALSE(source_list.Allow(&context, blob_url_not_self));
120 }
121
122 TEST(CSPSourceList, FilesystemDisallowedWhenBypassingSelfScheme) {
123 CSPContextTest context;
124 context.SetSelf(url::Origin(GURL("https://a.test")));
125 CSPSource filesystem(
126 CSPSource("filesystem", "", false, url::PORT_UNSPECIFIED, false, ""));
127 CSPSourceList source_list(true, // allow_self
128 false, // allow_star:
129 {filesystem}); // source_list
130
131 GURL filesystem_url_self("filesystem:https://a.test/file.txt");
132 GURL filesystem_url_not_self("filesystem:https://b.test/file.txt");
133
134 EXPECT_TRUE(source_list.Allow(&context, filesystem_url_self));
135 EXPECT_TRUE(source_list.Allow(&context, filesystem_url_not_self));
136
137 // Register 'https' as bypassing CSP, which should trigger the inner URL
138 // behavior.
139 context.AddSchemeToBypassCSP("https");
140
141 EXPECT_TRUE(source_list.Allow(&context, filesystem_url_self));
142 // TODO(arthursonzogni, mkwst): This should be true
143 // see http://crbug.com/692046
144 EXPECT_FALSE(source_list.Allow(&context, filesystem_url_not_self));
145 }
146
147 TEST(CSPSourceList, AllowSelfWithUnspecifiedPort) {
148 CSPContext context;
149 context.SetSelf(url::Origin(GURL("chrome://print")));
150 CSPSourceList source_list(true, // allow_self
151 false, // allow_star:
152 std::vector<CSPSource>()); // source_list
153
154 EXPECT_TRUE(source_list.Allow(
155 &context,
156 GURL("chrome://print/pdf_preview.html?chrome://print/1/0/print.pdf")));
157 }
158
159 TEST(CSPSourceList, AllowNone) {
160 CSPContextTest context;
161 context.SetSelf(url::Origin(GURL("http://example.com")));
162 CSPSourceList source_list(false, // allow_self
163 false, // allow_star:
164 std::vector<CSPSource>()); // source_list
165 EXPECT_FALSE(source_list.Allow(&context, GURL("http://example.com")));
166 EXPECT_FALSE(source_list.Allow(&context, GURL("https://example.test/")));
167 }
168
169 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698