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 "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 | 7 |
| 8 namespace content { | 8 namespace content { |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 } | 39 } |
| 40 | 40 |
| 41 TEST(CSPSourceTest, AllowScheme) { | 41 TEST(CSPSourceTest, AllowScheme) { |
| 42 CSPContext context; | 42 CSPContext context; |
| 43 | 43 |
| 44 // http -> {http, https}. | 44 // http -> {http, https}. |
| 45 { | 45 { |
| 46 CSPSource source("http", "", false, url::PORT_UNSPECIFIED, false, ""); | 46 CSPSource source("http", "", false, url::PORT_UNSPECIFIED, false, ""); |
| 47 EXPECT_TRUE(Allow(source, GURL("http://a.com"), &context)); | 47 EXPECT_TRUE(Allow(source, GURL("http://a.com"), &context)); |
| 48 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); | 48 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); |
| 49 // TODO(mkwst, arthursonzogni): It is weird to upgrade the scheme without | 49 // This passes because the source is "scheme only" so the upgrade is allowed |
|
arthursonzogni
2017/04/07 09:20:27
Nit: a dot is missing at the end of this comment.
andypaicu
2017/04/07 11:34:24
Done
| |
| 50 // the port. See http://crbug.com/692499 | |
| 51 EXPECT_TRUE(Allow(source, GURL("https://a.com:80"), &context)); | 50 EXPECT_TRUE(Allow(source, GURL("https://a.com:80"), &context)); |
|
arthursonzogni
2017/04/05 12:14:55
The example I made have the opposite test expectat
andypaicu
2017/04/06 09:05:51
Yeah this should be EXPECT_FALSE regardless of the
andypaicu
2017/04/06 09:05:51
Yeah you're right it should not pass here, regardl
| |
| 52 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); | 51 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); |
| 53 EXPECT_FALSE(Allow(source, GURL("ws://a.com"), &context)); | 52 EXPECT_FALSE(Allow(source, GURL("ws://a.com"), &context)); |
| 54 EXPECT_FALSE(Allow(source, GURL("wss://a.com"), &context)); | 53 EXPECT_FALSE(Allow(source, GURL("wss://a.com"), &context)); |
| 55 } | 54 } |
| 56 | 55 |
| 57 // ws -> {ws, wss}. | 56 // ws -> {ws, wss}. |
| 58 { | 57 { |
| 59 CSPSource source("ws", "", false, url::PORT_UNSPECIFIED, false, ""); | 58 CSPSource source("ws", "", false, url::PORT_UNSPECIFIED, false, ""); |
| 60 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); | 59 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); |
| 61 EXPECT_FALSE(Allow(source, GURL("https://a.com"), &context)); | 60 EXPECT_FALSE(Allow(source, GURL("https://a.com"), &context)); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); | 95 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); |
| 97 EXPECT_TRUE(Allow(source, GURL("http-so://a.com"), &context)); | 96 EXPECT_TRUE(Allow(source, GURL("http-so://a.com"), &context)); |
| 98 EXPECT_TRUE(Allow(source, GURL("https-so://a.com"), &context)); | 97 EXPECT_TRUE(Allow(source, GURL("https-so://a.com"), &context)); |
| 99 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); | 98 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); |
| 100 | 99 |
| 101 // Self's is https. | 100 // Self's is https. |
| 102 context.SetSelf(url::Origin(GURL("https://a.com"))); | 101 context.SetSelf(url::Origin(GURL("https://a.com"))); |
| 103 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); | 102 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); |
| 104 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); | 103 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); |
| 105 EXPECT_FALSE(Allow(source, GURL("http-so://a.com"), &context)); | 104 EXPECT_FALSE(Allow(source, GURL("http-so://a.com"), &context)); |
| 106 // TODO(mkwst, arthursonzogni): Maybe it should return true. | 105 // TODO(jochen): Maybe it should return false? |
| 107 // See http://crbug.com/692442: | 106 EXPECT_TRUE(Allow(source, GURL("https-so://a.com"), &context)); |
| 108 EXPECT_FALSE(Allow(source, GURL("https-so://a.com"), &context)); | |
| 109 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); | 107 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); |
| 110 | 108 |
| 111 // Self's scheme is not in the http familly. | 109 // Self's scheme is not in the http familly. |
| 112 context.SetSelf(url::Origin(GURL("ftp://a.com/"))); | 110 context.SetSelf(url::Origin(GURL("ftp://a.com/"))); |
| 113 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); | 111 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); |
| 114 EXPECT_TRUE(Allow(source, GURL("ftp://a.com"), &context)); | 112 EXPECT_TRUE(Allow(source, GURL("ftp://a.com"), &context)); |
| 115 | 113 |
| 116 // Self's scheme is unique. | 114 // Self's scheme is unique. |
| 117 context.SetSelf(url::Origin(GURL("non-standard-scheme://a.com"))); | 115 context.SetSelf(url::Origin(GURL("non-standard-scheme://a.com"))); |
| 118 // TODO(mkwst, arthursonzogni): This result might be wrong. | 116 // TODO(mkwst, arthursonzogni): This result might be wrong. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 EXPECT_TRUE(Allow(source, GURL("http://a.com"), &context)); | 176 EXPECT_TRUE(Allow(source, GURL("http://a.com"), &context)); |
| 179 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); | 177 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); |
| 180 } | 178 } |
| 181 | 179 |
| 182 // Source's port is "*". | 180 // Source's port is "*". |
| 183 { | 181 { |
| 184 CSPSource source("", "a.com", false, url::PORT_UNSPECIFIED, true, ""); | 182 CSPSource source("", "a.com", false, url::PORT_UNSPECIFIED, true, ""); |
| 185 EXPECT_TRUE(Allow(source, GURL("http://a.com"), &context)); | 183 EXPECT_TRUE(Allow(source, GURL("http://a.com"), &context)); |
| 186 EXPECT_TRUE(Allow(source, GURL("http://a.com:80"), &context)); | 184 EXPECT_TRUE(Allow(source, GURL("http://a.com:80"), &context)); |
| 187 EXPECT_TRUE(Allow(source, GURL("http://a.com:8080"), &context)); | 185 EXPECT_TRUE(Allow(source, GURL("http://a.com:8080"), &context)); |
| 188 EXPECT_TRUE(Allow(source, GURL("https://a.com:8080"), &context)); | 186 EXPECT_TRUE(Allow(source, GURL("https://a.com:8080"), &context)); |
|
arthursonzogni
2017/04/05 12:14:55
What about this test expectation?
Source's scheme
andypaicu
2017/04/06 09:05:51
This is a wildcard port though so it should pass.
arthursonzogni
2017/04/06 15:05:00
I think it should no pass for the reasons I have e
| |
| 189 EXPECT_TRUE(Allow(source, GURL("https://a.com:0"), &context)); | 187 EXPECT_TRUE(Allow(source, GURL("https://a.com:0"), &context)); |
| 190 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); | 188 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); |
| 191 } | 189 } |
| 192 | 190 |
| 193 // Source has a port. | 191 // Source has a port. |
| 194 { | 192 { |
| 195 CSPSource source("", "a.com", false, 80, false, ""); | 193 CSPSource source("", "a.com", false, 80, false, ""); |
| 196 EXPECT_TRUE(Allow(source, GURL("http://a.com:80"), &context)); | 194 EXPECT_TRUE(Allow(source, GURL("http://a.com:80"), &context)); |
| 197 EXPECT_TRUE(Allow(source, GURL("http://a.com"), &context)); | 195 EXPECT_TRUE(Allow(source, GURL("http://a.com"), &context)); |
| 198 EXPECT_FALSE(Allow(source, GURL("http://a.com:8080"), &context)); | 196 EXPECT_FALSE(Allow(source, GURL("http://a.com:8080"), &context)); |
| 199 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); | 197 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); |
| 200 } | 198 } |
| 201 | 199 |
| 202 // Allow upgrade from :80 to :443 | 200 // Allow upgrade from :80 to :443 |
| 203 { | 201 { |
| 204 CSPSource source("", "a.com", false, 80, false, ""); | 202 CSPSource source("", "a.com", false, 80, false, ""); |
| 205 EXPECT_TRUE(Allow(source, GURL("https://a.com:443"), &context)); | 203 EXPECT_TRUE(Allow(source, GURL("https://a.com:443"), &context)); |
| 206 // TODO(mkwst, arthursonzogni): It is weird to upgrade the port without the | 204 // Should not allow scheme upgrades unless both port and scheme are upgraded |
|
arthursonzogni
2017/04/07 09:20:27
Nit: A dot is missing at the end of this comment.
andypaicu
2017/04/07 11:34:24
Done
| |
| 207 // sheme. See http://crbug.com/692499 | 205 EXPECT_FALSE(Allow(source, GURL("http://a.com:443"), &context)); |
| 208 EXPECT_TRUE(Allow(source, GURL("http://a.com:443"), &context)); | |
| 209 } | 206 } |
| 210 | 207 |
| 211 // Host is * but port is specified | 208 // Host is * but port is specified |
| 212 { | 209 { |
| 213 CSPSource source("http", "", true, 111, false, ""); | 210 CSPSource source("http", "", true, 111, false, ""); |
| 214 EXPECT_TRUE(Allow(source, GURL("http://a.com:111"), &context)); | 211 EXPECT_TRUE(Allow(source, GURL("http://a.com:111"), &context)); |
| 215 EXPECT_FALSE(Allow(source, GURL("http://a.com:222"), &context)); | 212 EXPECT_FALSE(Allow(source, GURL("http://a.com:222"), &context)); |
| 216 } | 213 } |
| 217 } | 214 } |
| 218 | 215 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 EXPECT_TRUE(Allow(source, GURL("http://a.com/allowed-path"), &context)); | 274 EXPECT_TRUE(Allow(source, GURL("http://a.com/allowed-path"), &context)); |
| 278 EXPECT_FALSE(Allow(source, GURL("http://a.com/disallowed-path"), &context)); | 275 EXPECT_FALSE(Allow(source, GURL("http://a.com/disallowed-path"), &context)); |
| 279 } | 276 } |
| 280 } | 277 } |
| 281 | 278 |
| 282 TEST(CSPSourceTest, RedirectMatching) { | 279 TEST(CSPSourceTest, RedirectMatching) { |
| 283 CSPContext context; | 280 CSPContext context; |
| 284 CSPSource source("http", "a.com", false, 8000, false, "/bar/"); | 281 CSPSource source("http", "a.com", false, 8000, false, "/bar/"); |
| 285 EXPECT_TRUE(Allow(source, GURL("http://a.com:8000/"), &context, true)); | 282 EXPECT_TRUE(Allow(source, GURL("http://a.com:8000/"), &context, true)); |
| 286 EXPECT_TRUE(Allow(source, GURL("http://a.com:8000/foo"), &context, true)); | 283 EXPECT_TRUE(Allow(source, GURL("http://a.com:8000/foo"), &context, true)); |
| 287 EXPECT_TRUE(Allow(source, GURL("https://a.com:8000/foo"), &context, true)); | 284 EXPECT_FALSE(Allow(source, GURL("https://a.com:8000/foo"), &context, true)); |
| 288 EXPECT_FALSE( | 285 EXPECT_FALSE( |
| 289 Allow(source, GURL("http://not-a.com:8000/foo"), &context, true)); | 286 Allow(source, GURL("http://not-a.com:8000/foo"), &context, true)); |
| 290 EXPECT_FALSE(Allow(source, GURL("http://a.com:9000/foo/"), &context, false)); | 287 EXPECT_FALSE(Allow(source, GURL("http://a.com:9000/foo/"), &context, false)); |
| 291 } | 288 } |
| 292 | 289 |
| 293 TEST(CSPSourceTest, ToString) { | 290 TEST(CSPSourceTest, ToString) { |
| 294 { | 291 { |
| 295 CSPSource source("http", "", false, url::PORT_UNSPECIFIED, false, ""); | 292 CSPSource source("http", "", false, url::PORT_UNSPECIFIED, false, ""); |
| 296 EXPECT_EQ("http:", source.ToString()); | 293 EXPECT_EQ("http:", source.ToString()); |
| 297 } | 294 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 318 { | 315 { |
| 319 CSPSource source("", "a.com", false, url::PORT_UNSPECIFIED, true, ""); | 316 CSPSource source("", "a.com", false, url::PORT_UNSPECIFIED, true, ""); |
| 320 EXPECT_EQ("a.com:*", source.ToString()); | 317 EXPECT_EQ("a.com:*", source.ToString()); |
| 321 } | 318 } |
| 322 { | 319 { |
| 323 CSPSource source("", "a.com", false, url::PORT_UNSPECIFIED, false, "/path"); | 320 CSPSource source("", "a.com", false, url::PORT_UNSPECIFIED, false, "/path"); |
| 324 EXPECT_EQ("a.com/path", source.ToString()); | 321 EXPECT_EQ("a.com/path", source.ToString()); |
| 325 } | 322 } |
| 326 } | 323 } |
| 327 | 324 |
| 325 TEST(CSPSourceTest, UpgradeRequests) { | |
| 326 CSPContext context; | |
| 327 CSPSource source("http", "a.com", false, 80, false, ""); | |
| 328 EXPECT_TRUE(Allow(source, GURL("http://a.com:80"), &context, true)); | |
| 329 EXPECT_FALSE(Allow(source, GURL("https://a.com:80"), &context, true)); | |
| 330 EXPECT_FALSE(Allow(source, GURL("http://a.com:443"), &context, true)); | |
| 331 EXPECT_TRUE(Allow(source, GURL("https://a.com:443"), &context, true)); | |
| 332 | |
|
arthursonzogni
2017/04/07 09:20:27
Nit: I think you can probably remove this empty li
andypaicu
2017/04/07 11:34:24
Done
| |
| 333 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context, true)); | |
| 334 } | |
| 335 | |
| 336 | |
| 337 | |
| 328 } // namespace content | 338 } // namespace content |
| OLD | NEW |