1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
content / browser / shared_storage / shared_storage_url_loader_factory_proxy_unittest.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/shared_storage/shared_storage_url_loader_factory_proxy.h"
#include <stdint.h>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/system/functions.h"
#include "net/base/isolation_info.h"
#include "net/cookies/site_for_cookies.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
namespace {
const char kContextUrl[] = "https://a.host.test";
const char kSameOriginScriptUrl[] = "https://a.host.test/script";
const char kCrossOriginScriptUrl[] = "https://b.host.test/script";
// Values for the Accept header.
const char kAcceptJavascript[] = "application/javascript";
enum class DataOriginCase {
kSameOriginScript = 0,
kCrossOriginScriptUseContextDataOrigin = 1,
kCrossOriginScriptUseScriptDataOrigin = 2,
};
} // namespace
class SharedStorageURLLoaderFactoryProxyTest
: public testing::TestWithParam<DataOriginCase> {
public:
// Ways the proxy can behave in response to a request.
enum class ExpectedResponse {
kReject,
kAllow,
};
SharedStorageURLLoaderFactoryProxyTest()
: script_url_(IsSameOriginScript() ? GURL(kSameOriginScriptUrl)
: GURL(kCrossOriginScriptUrl)),
frame_origin_(url::Origin::Create(GURL(kContextUrl))),
data_origin_(IsContextOriginDataOrigin()
? url::Origin::Create(GURL(kContextUrl))
: url::Origin::Create(script_url_)) {
CreateUrlLoaderFactoryProxy();
}
~SharedStorageURLLoaderFactoryProxyTest() override {
mojo::SetDefaultProcessErrorHandler(base::NullCallback());
}
bool IsSameOriginScript() const {
return GetParam() == DataOriginCase::kSameOriginScript;
}
bool IsContextOriginDataOrigin() const {
return GetParam() != DataOriginCase::kCrossOriginScriptUseScriptDataOrigin;
}
void CreateUrlLoaderFactoryProxy() {
// The SharedStorageURLLoaderFactoryProxy should only be created if there is
// no old one, or the old one's pipe was closed.
DCHECK(!remote_url_loader_factory_ ||
!remote_url_loader_factory_.is_connected());
remote_url_loader_factory_.reset();
mojo::Remote<network::mojom::URLLoaderFactory> factory;
proxied_url_loader_factory_.Clone(factory.BindNewPipeAndPassReceiver());
url_loader_factory_proxy_ =
std::make_unique<SharedStorageURLLoaderFactoryProxy>(
factory.Unbind(),
remote_url_loader_factory_.BindNewPipeAndPassReceiver(),
frame_origin_, data_origin_, script_url_,
network::mojom::CredentialsMode::kSameOrigin,
net::SiteForCookies::FromOrigin(frame_origin_));
}
// Attempts to make a request for `request`.
void TryMakeRequest(const network::ResourceRequest& request,
ExpectedResponse expected_response) {
// Create a new factory if the last test case closed the pipe.
if (!remote_url_loader_factory_.is_connected())
CreateUrlLoaderFactoryProxy();
EXPECT_EQ(0, proxied_url_loader_factory_.NumPending());
// Try to send a request. Requests are never run to completion, instead,
// requests that make it to the nested `url_loader_factory_` are tracked in
// its vector of pending requests.
mojo::PendingRemote<network::mojom::URLLoader> receiver;
mojo::PendingReceiver<network::mojom::URLLoaderClient> client;
// Set a couple random options, which should be ignored.
int options = network::mojom::kURLLoadOptionSendSSLInfoWithResponse |
network::mojom::kURLLoadOptionSniffMimeType;
remote_url_loader_factory_->CreateLoaderAndStart(
receiver.InitWithNewPipeAndPassReceiver(), 0 /* request_id_ */, options,
request, client.InitWithNewPipeAndPassRemote(),
net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS));
// Wait until requests have made it through the Mojo pipe. NumPending()
// actually spins the message loop already, but seems best to be safe.
remote_url_loader_factory_.FlushForTesting();
network::TestURLLoaderFactory::PendingRequest* pending_request;
switch (expected_response) {
case ExpectedResponse::kReject:
EXPECT_EQ(0, proxied_url_loader_factory_.NumPending());
return;
case ExpectedResponse::kAllow:
EXPECT_EQ(1, proxied_url_loader_factory_.NumPending());
pending_request =
&proxied_url_loader_factory_.pending_requests()->back();
break;
}
// The pipe will be closed after the first request regardless of its
// response type.
EXPECT_FALSE(remote_url_loader_factory_.is_connected());
// These should always be the same for all requests.
EXPECT_EQ(0u, pending_request->options);
EXPECT_EQ(
net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS),
pending_request->traffic_annotation);
// Each request should be assigned a unique ID. These should actually be
// unique within the browser process, not just among requests using the
// SharedStorageURLLoaderFactoryProxy.
for (const auto& other_pending_request :
*proxied_url_loader_factory_.pending_requests()) {
if (&other_pending_request == pending_request)
continue;
EXPECT_NE(other_pending_request.request_id, pending_request->request_id);
}
const auto& observed_request = pending_request->request;
// The URL should be unaltered.
EXPECT_EQ(request.url, observed_request.url);
// Verify the accept kAcceptJavascript header.
EXPECT_THAT(
observed_request.headers.GetHeader(net::HttpRequestHeaders::kAccept),
testing::Optional(std::string(kAcceptJavascript)));
if (IsContextOriginDataOrigin()) {
// The accept kAcceptJavascript header should be the only header.
EXPECT_EQ(1u, observed_request.headers.GetHeaderVector().size());
} else {
// In addition to the accept kAcceptJavascript header, the
// kSecSharedStorageDataOriginHeader should be present.
EXPECT_EQ(2u, observed_request.headers.GetHeaderVector().size());
EXPECT_THAT(
observed_request.headers.GetHeader(kSecSharedStorageDataOriginHeader),
testing::Optional(
url::Origin::Create(GURL(kCrossOriginScriptUrl)).Serialize()));
}
// The request should include credentials, and should not follow redirects.
EXPECT_EQ(network::mojom::CredentialsMode::kSameOrigin,
observed_request.credentials_mode);
EXPECT_EQ(network::mojom::RedirectMode::kError,
observed_request.redirect_mode);
// The initiator should be set.
EXPECT_EQ(frame_origin_, observed_request.request_initiator);
EXPECT_EQ(network::mojom::RequestMode::kCors, observed_request.mode);
ASSERT_FALSE(observed_request.trusted_params);
}
void TryMakeRequest(const GURL& url, ExpectedResponse expected_response) {
network::ResourceRequest request;
request.url = url;
TryMakeRequest(request, expected_response);
}
void TryMakeRequest(const std::string& url,
ExpectedResponse expected_response) {
TryMakeRequest(GURL(url), expected_response);
}
protected:
base::test::TaskEnvironment task_environment_;
const GURL script_url_;
const url::Origin frame_origin_;
const url::Origin data_origin_;
network::TestURLLoaderFactory proxied_url_loader_factory_;
std::unique_ptr<SharedStorageURLLoaderFactoryProxy> url_loader_factory_proxy_;
mojo::Remote<network::mojom::URLLoaderFactory> remote_url_loader_factory_;
};
namespace {
std::string DescribeTestParam(
const testing::TestParamInfo<
SharedStorageURLLoaderFactoryProxyTest::ParamType>& info) {
return base::StrCat(
{(info.param == DataOriginCase::kSameOriginScript ? "Same" : "Cross"),
"OriginScript",
(info.param == DataOriginCase::kSameOriginScript
? ""
: (info.param ==
DataOriginCase::kCrossOriginScriptUseContextDataOrigin
? "_ContextDataOrigin"
: "_ScriptDataOrigin"))});
}
} // namespace
INSTANTIATE_TEST_SUITE_P(
All,
SharedStorageURLLoaderFactoryProxyTest,
testing::Values(DataOriginCase::kSameOriginScript,
DataOriginCase::kCrossOriginScriptUseContextDataOrigin,
DataOriginCase::kCrossOriginScriptUseScriptDataOrigin),
DescribeTestParam);
TEST_P(SharedStorageURLLoaderFactoryProxyTest, Basic) {
TryMakeRequest(script_url_, ExpectedResponse::kAllow);
TryMakeRequest(kContextUrl, ExpectedResponse::kReject);
TryMakeRequest("https://b.host.test/", ExpectedResponse::kReject);
TryMakeRequest(script_url_, ExpectedResponse::kAllow);
}
} // namespace content