| 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 |
| 50 // the port. See http://crbug.com/692499 | 50 // allowed. |
| 51 EXPECT_TRUE(Allow(source, GURL("https://a.com:80"), &context)); | 51 EXPECT_TRUE(Allow(source, GURL("https://a.com:80"), &context)); |
| 52 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); | 52 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); |
| 53 EXPECT_FALSE(Allow(source, GURL("ws://a.com"), &context)); | 53 EXPECT_FALSE(Allow(source, GURL("ws://a.com"), &context)); |
| 54 EXPECT_FALSE(Allow(source, GURL("wss://a.com"), &context)); | 54 EXPECT_FALSE(Allow(source, GURL("wss://a.com"), &context)); |
| 55 } | 55 } |
| 56 | 56 |
| 57 // ws -> {ws, wss}. | 57 // ws -> {ws, wss}. |
| 58 { | 58 { |
| 59 CSPSource source("ws", "", false, url::PORT_UNSPECIFIED, false, ""); | 59 CSPSource source("ws", "", false, url::PORT_UNSPECIFIED, false, ""); |
| 60 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); | 60 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); | 96 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); |
| 97 EXPECT_TRUE(Allow(source, GURL("http-so://a.com"), &context)); | 97 EXPECT_TRUE(Allow(source, GURL("http-so://a.com"), &context)); |
| 98 EXPECT_TRUE(Allow(source, GURL("https-so://a.com"), &context)); | 98 EXPECT_TRUE(Allow(source, GURL("https-so://a.com"), &context)); |
| 99 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); | 99 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); |
| 100 | 100 |
| 101 // Self's is https. | 101 // Self's is https. |
| 102 context.SetSelf(url::Origin(GURL("https://a.com"))); | 102 context.SetSelf(url::Origin(GURL("https://a.com"))); |
| 103 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); | 103 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); |
| 104 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); | 104 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); |
| 105 EXPECT_FALSE(Allow(source, GURL("http-so://a.com"), &context)); | 105 EXPECT_FALSE(Allow(source, GURL("http-so://a.com"), &context)); |
| 106 // TODO(mkwst, arthursonzogni): Maybe it should return true. | 106 // TODO(jochen): Maybe it should return false? |
| 107 // See http://crbug.com/692442: | 107 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)); | 108 EXPECT_FALSE(Allow(source, GURL("ftp://a.com"), &context)); |
| 110 | 109 |
| 111 // Self's scheme is not in the http familly. | 110 // Self's scheme is not in the http familly. |
| 112 context.SetSelf(url::Origin(GURL("ftp://a.com/"))); | 111 context.SetSelf(url::Origin(GURL("ftp://a.com/"))); |
| 113 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); | 112 EXPECT_FALSE(Allow(source, GURL("http://a.com"), &context)); |
| 114 EXPECT_TRUE(Allow(source, GURL("ftp://a.com"), &context)); | 113 EXPECT_TRUE(Allow(source, GURL("ftp://a.com"), &context)); |
| 115 | 114 |
| 116 // Self's scheme is unique. | 115 // Self's scheme is unique. |
| 117 context.SetSelf(url::Origin(GURL("non-standard-scheme://a.com"))); | 116 context.SetSelf(url::Origin(GURL("non-standard-scheme://a.com"))); |
| 118 // TODO(mkwst, arthursonzogni): This result might be wrong. | 117 // TODO(mkwst, arthursonzogni): This result might be wrong. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 EXPECT_TRUE(Allow(source, GURL("http://a.com:80"), &context)); | 195 EXPECT_TRUE(Allow(source, GURL("http://a.com:80"), &context)); |
| 197 EXPECT_TRUE(Allow(source, GURL("http://a.com"), &context)); | 196 EXPECT_TRUE(Allow(source, GURL("http://a.com"), &context)); |
| 198 EXPECT_FALSE(Allow(source, GURL("http://a.com:8080"), &context)); | 197 EXPECT_FALSE(Allow(source, GURL("http://a.com:8080"), &context)); |
| 199 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); | 198 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context)); |
| 200 } | 199 } |
| 201 | 200 |
| 202 // Allow upgrade from :80 to :443 | 201 // Allow upgrade from :80 to :443 |
| 203 { | 202 { |
| 204 CSPSource source("", "a.com", false, 80, false, ""); | 203 CSPSource source("", "a.com", false, 80, false, ""); |
| 205 EXPECT_TRUE(Allow(source, GURL("https://a.com:443"), &context)); | 204 EXPECT_TRUE(Allow(source, GURL("https://a.com:443"), &context)); |
| 206 // TODO(mkwst, arthursonzogni): It is weird to upgrade the port without the | 205 // Should not allow scheme upgrades unless both port and scheme are |
| 207 // sheme. See http://crbug.com/692499 | 206 // upgraded. |
| 208 EXPECT_TRUE(Allow(source, GURL("http://a.com:443"), &context)); | 207 EXPECT_FALSE(Allow(source, GURL("http://a.com:443"), &context)); |
| 209 } | 208 } |
| 210 | 209 |
| 211 // Host is * but port is specified | 210 // Host is * but port is specified |
| 212 { | 211 { |
| 213 CSPSource source("http", "", true, 111, false, ""); | 212 CSPSource source("http", "", true, 111, false, ""); |
| 214 EXPECT_TRUE(Allow(source, GURL("http://a.com:111"), &context)); | 213 EXPECT_TRUE(Allow(source, GURL("http://a.com:111"), &context)); |
| 215 EXPECT_FALSE(Allow(source, GURL("http://a.com:222"), &context)); | 214 EXPECT_FALSE(Allow(source, GURL("http://a.com:222"), &context)); |
| 216 } | 215 } |
| 217 } | 216 } |
| 218 | 217 |
| (...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)); | 276 EXPECT_TRUE(Allow(source, GURL("http://a.com/allowed-path"), &context)); |
| 278 EXPECT_FALSE(Allow(source, GURL("http://a.com/disallowed-path"), &context)); | 277 EXPECT_FALSE(Allow(source, GURL("http://a.com/disallowed-path"), &context)); |
| 279 } | 278 } |
| 280 } | 279 } |
| 281 | 280 |
| 282 TEST(CSPSourceTest, RedirectMatching) { | 281 TEST(CSPSourceTest, RedirectMatching) { |
| 283 CSPContext context; | 282 CSPContext context; |
| 284 CSPSource source("http", "a.com", false, 8000, false, "/bar/"); | 283 CSPSource source("http", "a.com", false, 8000, false, "/bar/"); |
| 285 EXPECT_TRUE(Allow(source, GURL("http://a.com:8000/"), &context, true)); | 284 EXPECT_TRUE(Allow(source, GURL("http://a.com:8000/"), &context, true)); |
| 286 EXPECT_TRUE(Allow(source, GURL("http://a.com:8000/foo"), &context, true)); | 285 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)); | 286 EXPECT_FALSE(Allow(source, GURL("https://a.com:8000/foo"), &context, true)); |
| 288 EXPECT_FALSE( | 287 EXPECT_FALSE( |
| 289 Allow(source, GURL("http://not-a.com:8000/foo"), &context, true)); | 288 Allow(source, GURL("http://not-a.com:8000/foo"), &context, true)); |
| 290 EXPECT_FALSE(Allow(source, GURL("http://a.com:9000/foo/"), &context, false)); | 289 EXPECT_FALSE(Allow(source, GURL("http://a.com:9000/foo/"), &context, false)); |
| 291 } | 290 } |
| 292 | 291 |
| 293 TEST(CSPSourceTest, ToString) { | 292 TEST(CSPSourceTest, ToString) { |
| 294 { | 293 { |
| 295 CSPSource source("http", "", false, url::PORT_UNSPECIFIED, false, ""); | 294 CSPSource source("http", "", false, url::PORT_UNSPECIFIED, false, ""); |
| 296 EXPECT_EQ("http:", source.ToString()); | 295 EXPECT_EQ("http:", source.ToString()); |
| 297 } | 296 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 318 { | 317 { |
| 319 CSPSource source("", "a.com", false, url::PORT_UNSPECIFIED, true, ""); | 318 CSPSource source("", "a.com", false, url::PORT_UNSPECIFIED, true, ""); |
| 320 EXPECT_EQ("a.com:*", source.ToString()); | 319 EXPECT_EQ("a.com:*", source.ToString()); |
| 321 } | 320 } |
| 322 { | 321 { |
| 323 CSPSource source("", "a.com", false, url::PORT_UNSPECIFIED, false, "/path"); | 322 CSPSource source("", "a.com", false, url::PORT_UNSPECIFIED, false, "/path"); |
| 324 EXPECT_EQ("a.com/path", source.ToString()); | 323 EXPECT_EQ("a.com/path", source.ToString()); |
| 325 } | 324 } |
| 326 } | 325 } |
| 327 | 326 |
| 327 TEST(CSPSourceTest, UpgradeRequests) { |
| 328 CSPContext context; |
| 329 CSPSource source("http", "a.com", false, 80, false, ""); |
| 330 EXPECT_TRUE(Allow(source, GURL("http://a.com:80"), &context, true)); |
| 331 EXPECT_FALSE(Allow(source, GURL("https://a.com:80"), &context, true)); |
| 332 EXPECT_FALSE(Allow(source, GURL("http://a.com:443"), &context, true)); |
| 333 EXPECT_TRUE(Allow(source, GURL("https://a.com:443"), &context, true)); |
| 334 EXPECT_TRUE(Allow(source, GURL("https://a.com"), &context, true)); |
| 335 } |
| 336 |
| 328 } // namespace content | 337 } // namespace content |
| OLD | NEW |