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

Side by Side Diff: content/browser/service_worker/service_worker_provider_host_unittest.cc

Issue 2061203002: WIP Reland: service worker: Don't control a subframe of an insecure context Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: maybe fix drmemory Created 4 years, 6 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/browser/service_worker/service_worker_provider_host.h" 5 #include "content/browser/service_worker/service_worker_provider_host.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/memory/weak_ptr.h" 9 #include "base/memory/weak_ptr.h"
10 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/threading/thread_task_runner_handle.h"
11 #include "content/browser/service_worker/embedded_worker_test_helper.h" 11 #include "content/browser/service_worker/embedded_worker_test_helper.h"
12 #include "content/browser/service_worker/service_worker_context_core.h" 12 #include "content/browser/service_worker/service_worker_context_core.h"
13 #include "content/browser/service_worker/service_worker_register_job.h" 13 #include "content/browser/service_worker/service_worker_register_job.h"
14 #include "content/browser/service_worker/service_worker_registration.h" 14 #include "content/browser/service_worker/service_worker_registration.h"
15 #include "content/browser/service_worker/service_worker_version.h" 15 #include "content/browser/service_worker/service_worker_version.h"
16 #include "content/public/common/origin_util.h"
16 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "content/test/test_content_browser_client.h"
19 #include "content/test/test_content_client.h"
17 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
18 21
19 namespace content { 22 namespace content {
20 23
24 const char kServiceWorkerScheme[] = "i-can-use-service-worker";
25
26 class ServiceWorkerTestContentClient : public TestContentClient {
27 public:
28 void AddServiceWorkerSchemes(std::set<std::string>* schemes) override {
29 schemes->insert(kServiceWorkerScheme);
30 }
31 };
32
21 class ServiceWorkerProviderHostTest : public testing::Test { 33 class ServiceWorkerProviderHostTest : public testing::Test {
22 protected: 34 protected:
23 ServiceWorkerProviderHostTest() 35 ServiceWorkerProviderHostTest()
24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} 36 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {
37 SetContentClient(&test_content_client_);
38 }
25 ~ServiceWorkerProviderHostTest() override {} 39 ~ServiceWorkerProviderHostTest() override {}
26 40
27 void SetUp() override { 41 void SetUp() override {
42 old_content_browser_client_ =
43 SetBrowserClientForTesting(&test_content_browser_client_);
44
28 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); 45 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
29 context_ = helper_->context(); 46 context_ = helper_->context();
30 script_url_ = GURL("http://www.example.com/service_worker.js"); 47 script_url_ = GURL("https://www.example.com/service_worker.js");
31 registration1_ = new ServiceWorkerRegistration( 48 registration1_ = new ServiceWorkerRegistration(
32 GURL("http://www.example.com/"), 1L, context_->AsWeakPtr()); 49 GURL("https://www.example.com/"), 1L, context_->AsWeakPtr());
33 registration2_ = new ServiceWorkerRegistration( 50 registration2_ = new ServiceWorkerRegistration(
34 GURL("http://www.example.com/example"), 2L, context_->AsWeakPtr()); 51 GURL("https://www.example.com/example"), 2L, context_->AsWeakPtr());
35 52
36 // Prepare provider hosts (for the same process). 53 // Prepare provider hosts (for the same process).
37 std::unique_ptr<ServiceWorkerProviderHost> host1( 54 std::unique_ptr<ServiceWorkerProviderHost> host1(
38 new ServiceWorkerProviderHost(helper_->mock_render_process_id(), 55 new ServiceWorkerProviderHost(
39 MSG_ROUTING_NONE, 1 /* provider_id */, 56 helper_->mock_render_process_id(), MSG_ROUTING_NONE,
40 SERVICE_WORKER_PROVIDER_FOR_WINDOW, 57 1 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
41 context_->AsWeakPtr(), NULL)); 58 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE,
42 host1->SetDocumentUrl(GURL("http://www.example.com/example1.html")); 59 context_->AsWeakPtr(), NULL));
60 host1->SetDocumentUrl(GURL("https://www.example.com/example1.html"));
43 std::unique_ptr<ServiceWorkerProviderHost> host2( 61 std::unique_ptr<ServiceWorkerProviderHost> host2(
44 new ServiceWorkerProviderHost(helper_->mock_render_process_id(), 62 new ServiceWorkerProviderHost(
45 MSG_ROUTING_NONE, 2 /* provider_id */, 63 helper_->mock_render_process_id(), MSG_ROUTING_NONE,
46 SERVICE_WORKER_PROVIDER_FOR_WINDOW, 64 2 /* provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
47 context_->AsWeakPtr(), NULL)); 65 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE,
48 host2->SetDocumentUrl(GURL("http://www.example.com/example2.html")); 66 context_->AsWeakPtr(), NULL));
67 host2->SetDocumentUrl(GURL("https://www.example.com/example2.html"));
49 provider_host1_ = host1->AsWeakPtr(); 68 provider_host1_ = host1->AsWeakPtr();
50 provider_host2_ = host2->AsWeakPtr(); 69 provider_host2_ = host2->AsWeakPtr();
51 context_->AddProviderHost(base::WrapUnique(host1.release())); 70 context_->AddProviderHost(base::WrapUnique(host1.release()));
52 context_->AddProviderHost(base::WrapUnique(host2.release())); 71 context_->AddProviderHost(base::WrapUnique(host2.release()));
53 } 72 }
54 73
55 void TearDown() override { 74 void TearDown() override {
56 registration1_ = 0; 75 registration1_ = 0;
57 registration2_ = 0; 76 registration2_ = 0;
58 helper_.reset(); 77 helper_.reset();
78 SetBrowserClientForTesting(old_content_browser_client_);
59 } 79 }
60 80
61 bool PatternHasProcessToRun(const GURL& pattern) const { 81 bool PatternHasProcessToRun(const GURL& pattern) const {
62 return context_->process_manager()->PatternHasProcessToRun(pattern); 82 return context_->process_manager()->PatternHasProcessToRun(pattern);
63 } 83 }
64 84
65 content::TestBrowserThreadBundle thread_bundle_; 85 content::TestBrowserThreadBundle thread_bundle_;
66 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; 86 std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
67 ServiceWorkerContextCore* context_; 87 ServiceWorkerContextCore* context_;
68 scoped_refptr<ServiceWorkerRegistration> registration1_; 88 scoped_refptr<ServiceWorkerRegistration> registration1_;
69 scoped_refptr<ServiceWorkerRegistration> registration2_; 89 scoped_refptr<ServiceWorkerRegistration> registration2_;
70 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; 90 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_;
71 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; 91 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_;
72 GURL script_url_; 92 GURL script_url_;
93 ServiceWorkerTestContentClient test_content_client_;
94 TestContentBrowserClient test_content_browser_client_;
95 ContentBrowserClient* old_content_browser_client_;
73 96
74 private: 97 private:
75 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); 98 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest);
76 }; 99 };
77 100
78 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { 101 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) {
79 provider_host1_->AddMatchingRegistration(registration1_.get()); 102 provider_host1_->AddMatchingRegistration(registration1_.get());
80 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); 103 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern()));
81 104
82 // Adding the same registration twice has no effect. 105 // Adding the same registration twice has no effect.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 // Match registration should return the longest matching one. 144 // Match registration should return the longest matching one.
122 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_); 145 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_);
123 provider_host1_->RemoveMatchingRegistration(registration2_.get()); 146 provider_host1_->RemoveMatchingRegistration(registration2_.get());
124 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_); 147 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_);
125 148
126 // Should return nullptr after removing all matching registrations. 149 // Should return nullptr after removing all matching registrations.
127 provider_host1_->RemoveMatchingRegistration(registration1_.get()); 150 provider_host1_->RemoveMatchingRegistration(registration1_.get());
128 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr); 151 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr);
129 } 152 }
130 153
154 TEST_F(ServiceWorkerProviderHostTest, ContextSecurity) {
155 using FrameSecurityLevel = ServiceWorkerProviderHost::FrameSecurityLevel;
156 content::ResetSchemesAndOriginsWhitelistForTesting();
157
158 // Insecure document URL.
159 provider_host1_->SetDocumentUrl(GURL("http://host"));
160 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE;
161 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker());
162
163 // Insecure parent frame.
164 provider_host1_->SetDocumentUrl(GURL("https://host"));
165 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE;
166 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker());
167
168 // Secure URL and parent frame.
169 provider_host1_->SetDocumentUrl(GURL("https://host"));
170 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE;
171 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker());
172
173 // Exceptional service worker scheme.
174 GURL url(std::string(kServiceWorkerScheme) + "://host");
175 EXPECT_TRUE(url.is_valid());
176 provider_host1_->SetDocumentUrl(url);
177 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::SECURE;
178 EXPECT_FALSE(IsOriginSecure(url));
179 EXPECT_TRUE(OriginCanAccessServiceWorkers(url));
180 EXPECT_TRUE(provider_host1_->IsContextSecureForServiceWorker());
181
182 // Exceptional service worker scheme with insecure parent frame.
183 provider_host1_->parent_frame_security_level_ = FrameSecurityLevel::INSECURE;
184 EXPECT_FALSE(provider_host1_->IsContextSecureForServiceWorker());
185 }
186
131 } // namespace content 187 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698