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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// Copyright 2014 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/public/browser/shared_worker_instance.h"
#include <memory>
#include <string>
#include <string_view>
#include "base/strings/utf_string_conversions.h"
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
namespace content {
class SharedWorkerInstanceTest : public testing::Test {
protected:
SharedWorkerInstanceTest() {}
SharedWorkerInstanceTest(const SharedWorkerInstanceTest&) = delete;
SharedWorkerInstanceTest& operator=(const SharedWorkerInstanceTest&) = delete;
SharedWorkerInstance CreateInstance(const GURL& script_url,
const std::string& name,
const blink::StorageKey& storage_key) {
return SharedWorkerInstance(
script_url, blink::mojom::ScriptType::kClassic,
network::mojom::CredentialsMode::kSameOrigin, name, storage_key,
blink::mojom::SharedWorkerCreationContextType::kNonsecure,
storage_key.IsFirstPartyContext()
? blink::mojom::SharedWorkerSameSiteCookies::kAll
: blink::mojom::SharedWorkerSameSiteCookies::kNone,
/*extended_lifetime=*/false);
}
bool Matches(const SharedWorkerInstance& instance,
const std::string& url,
const std::string_view& name) {
blink::StorageKey storage_key;
if (GURL(url).SchemeIs(url::kDataScheme)) {
storage_key =
blink::StorageKey::CreateFromStringForTesting("http://example.com/");
} else {
storage_key = blink::StorageKey::CreateFromStringForTesting(url);
}
return instance.Matches(GURL(url), std::string(name), storage_key,
blink::mojom::SharedWorkerSameSiteCookies::kAll);
}
};
TEST_F(SharedWorkerInstanceTest, MatchesTest) {
const std::string kDataURL("data:text/javascript;base64,Ly8gSGVsbG8h");
const std::string kFileURL("file:///w.js");
// SharedWorker that doesn't have a name option.
GURL script_url1("http://example.com/w.js");
std::string name1("");
const blink::StorageKey storage_key1 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(script_url1));
SharedWorkerInstance instance1 =
CreateInstance(script_url1, name1, storage_key1);
EXPECT_TRUE(Matches(instance1, "http://example.com/w.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.com/w2.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.net/w.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.net/w2.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.com/w.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.com/w2.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.net/w.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.net/w2.js", "name"));
EXPECT_FALSE(Matches(instance1, kDataURL, ""));
EXPECT_FALSE(Matches(instance1, kDataURL, "name"));
EXPECT_FALSE(Matches(instance1, kFileURL, ""));
EXPECT_FALSE(Matches(instance1, kFileURL, "name"));
// SharedWorker that has a name option.
GURL script_url2("http://example.com/w.js");
std::string name2("name");
const blink::StorageKey storage_key2 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(script_url2));
SharedWorkerInstance instance2 =
CreateInstance(script_url2, name2, storage_key2);
EXPECT_FALSE(Matches(instance2, "http://example.com/w.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", ""));
EXPECT_TRUE(Matches(instance2, "http://example.com/w.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.com/w.js", "name2"));
EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", "name2"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", "name2"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", "name2"));
EXPECT_FALSE(Matches(instance2, kDataURL, ""));
EXPECT_FALSE(Matches(instance2, kDataURL, "name"));
EXPECT_FALSE(Matches(instance2, kFileURL, ""));
EXPECT_FALSE(Matches(instance2, kFileURL, "name"));
}
TEST_F(SharedWorkerInstanceTest, MatchesTest_DataURLWorker) {
const std::string kDataURL("data:text/javascript;base64,Ly8gSGVsbG8h");
const std::string kFileURL("file:///w.js");
// SharedWorker created from a data: URL without a name option.
GURL script_url1(kDataURL);
std::string name1("");
const blink::StorageKey storage_key1 = blink::StorageKey::CreateFirstParty(
url::Origin::Create(GURL("http://example.com/")));
SharedWorkerInstance instance1 =
CreateInstance(script_url1, name1, storage_key1);
EXPECT_FALSE(Matches(instance1, "http://example.com/w.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.com/w2.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.net/w.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.net/w2.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.com/w.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.com/w2.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.net/w.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.net/w2.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.com/w.js", "name2"));
EXPECT_FALSE(Matches(instance1, "http://example.com/w2.js", "name2"));
EXPECT_FALSE(Matches(instance1, "http://example.net/w.js", "name2"));
EXPECT_FALSE(Matches(instance1, "http://example.net/w2.js", "name2"));
// This should match because the instance has the same data: URL, name, and
// constructor origin.
EXPECT_TRUE(Matches(instance1, kDataURL, ""));
EXPECT_FALSE(Matches(instance1, kDataURL, "name"));
EXPECT_FALSE(Matches(instance1, kFileURL, ""));
EXPECT_FALSE(Matches(instance1, kFileURL, "name"));
// SharedWorker created from a data: URL with a name option.
GURL script_url2(kDataURL);
std::string name2("name");
const blink::StorageKey storage_key2 = blink::StorageKey::CreateFirstParty(
url::Origin::Create(GURL("http://example.com/")));
SharedWorkerInstance instance2 =
CreateInstance(script_url2, name2, storage_key2);
EXPECT_FALSE(Matches(instance2, "http://example.com/w.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.com/w.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.com/w.js", "name2"));
EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", "name2"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", "name2"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", "name2"));
EXPECT_FALSE(Matches(instance2, kDataURL, ""));
// This should match because the instance has the same data: URL, name, and
// constructor origin.
EXPECT_TRUE(Matches(instance2, kDataURL, "name"));
EXPECT_FALSE(Matches(instance2, kFileURL, ""));
EXPECT_FALSE(Matches(instance2, kFileURL, "name"));
// SharedWorker created from a data: URL on a remote origin (i.e., example.net
// opposed to example.com) without a name option.
GURL script_url3(kDataURL);
std::string name3("");
const blink::StorageKey storage_key3 = blink::StorageKey::CreateFirstParty(
url::Origin::Create(GURL("http://example.net/")));
SharedWorkerInstance instance3 =
CreateInstance(script_url3, name3, storage_key3);
EXPECT_FALSE(Matches(instance3, "http://example.com/w.js", ""));
EXPECT_FALSE(Matches(instance3, "http://example.com/w2.js", ""));
EXPECT_FALSE(Matches(instance3, "http://example.net/w.js", ""));
EXPECT_FALSE(Matches(instance3, "http://example.net/w2.js", ""));
EXPECT_FALSE(Matches(instance3, "http://example.com/w.js", "name"));
EXPECT_FALSE(Matches(instance3, "http://example.com/w2.js", "name"));
EXPECT_FALSE(Matches(instance3, "http://example.net/w.js", "name"));
EXPECT_FALSE(Matches(instance3, "http://example.net/w2.js", "name"));
EXPECT_FALSE(Matches(instance3, "http://example.com/w.js", "name2"));
EXPECT_FALSE(Matches(instance3, "http://example.com/w2.js", "name2"));
EXPECT_FALSE(Matches(instance3, "http://example.net/w.js", "name2"));
EXPECT_FALSE(Matches(instance3, "http://example.net/w2.js", "name2"));
// This should not match because the instance has a different constructor
// origin.
EXPECT_FALSE(Matches(instance3, kDataURL, ""));
EXPECT_FALSE(Matches(instance3, kDataURL, "name"));
EXPECT_FALSE(Matches(instance3, kFileURL, ""));
EXPECT_FALSE(Matches(instance3, kFileURL, "name"));
// SharedWorker created from a data: URL on a remote origin (i.e., example.net
// opposed to example.com) with a name option.
GURL script_url4(kDataURL);
std::string name4("");
const blink::StorageKey storage_key4 = blink::StorageKey::CreateFirstParty(
url::Origin::Create(GURL("http://example.net/")));
SharedWorkerInstance instance4 =
CreateInstance(script_url4, name4, storage_key4);
EXPECT_FALSE(Matches(instance4, "http://example.com/w.js", ""));
EXPECT_FALSE(Matches(instance4, "http://example.com/w2.js", ""));
EXPECT_FALSE(Matches(instance4, "http://example.net/w.js", ""));
EXPECT_FALSE(Matches(instance4, "http://example.net/w2.js", ""));
EXPECT_FALSE(Matches(instance4, "http://example.com/w.js", "name"));
EXPECT_FALSE(Matches(instance4, "http://example.com/w2.js", "name"));
EXPECT_FALSE(Matches(instance4, "http://example.net/w.js", "name"));
EXPECT_FALSE(Matches(instance4, "http://example.net/w2.js", "name"));
EXPECT_FALSE(Matches(instance4, "http://example.com/w.js", "name2"));
EXPECT_FALSE(Matches(instance4, "http://example.com/w2.js", "name2"));
EXPECT_FALSE(Matches(instance4, "http://example.net/w.js", "name2"));
EXPECT_FALSE(Matches(instance4, "http://example.net/w2.js", "name2"));
EXPECT_FALSE(Matches(instance4, kDataURL, ""));
// This should not match because the instance has a different constructor
// origin.
EXPECT_FALSE(Matches(instance4, kDataURL, "name"));
EXPECT_FALSE(Matches(instance4, kFileURL, ""));
EXPECT_FALSE(Matches(instance4, kFileURL, "name"));
}
TEST_F(SharedWorkerInstanceTest, MatchesTest_FileURLWorker) {
const std::string kDataURL("data:text/javascript;base64,Ly8gSGVsbG8h");
const std::string kFileURL("file:///w.js");
// SharedWorker created from a file:// URL without a name option.
GURL script_url1(kFileURL);
std::string name1("");
const blink::StorageKey storage_key1 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(GURL(kFileURL)));
SharedWorkerInstance instance1 =
CreateInstance(script_url1, name1, storage_key1);
EXPECT_FALSE(Matches(instance1, "http://example.com/w.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.com/w2.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.net/w.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.net/w2.js", ""));
EXPECT_FALSE(Matches(instance1, "http://example.com/w.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.com/w2.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.net/w.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.net/w2.js", "name"));
EXPECT_FALSE(Matches(instance1, "http://example.com/w.js", "name2"));
EXPECT_FALSE(Matches(instance1, "http://example.com/w2.js", "name2"));
EXPECT_FALSE(Matches(instance1, "http://example.net/w.js", "name2"));
EXPECT_FALSE(Matches(instance1, "http://example.net/w2.js", "name2"));
EXPECT_FALSE(Matches(instance1, kDataURL, ""));
EXPECT_FALSE(Matches(instance1, kDataURL, "name"));
// This should not match because file:// URL is treated as an opaque origin.
EXPECT_FALSE(Matches(instance1, kFileURL, ""));
EXPECT_FALSE(Matches(instance1, kFileURL, "name"));
// SharedWorker created from a file:// URL with a name option.
GURL script_url2(kFileURL);
std::string name2("name");
const blink::StorageKey storage_key2 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(GURL(kFileURL)));
SharedWorkerInstance instance2 =
CreateInstance(script_url2, name2, storage_key2);
EXPECT_FALSE(Matches(instance2, "http://example.com/w.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", ""));
EXPECT_FALSE(Matches(instance2, "http://example.com/w.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", "name"));
EXPECT_FALSE(Matches(instance2, "http://example.com/w.js", "name2"));
EXPECT_FALSE(Matches(instance2, "http://example.com/w2.js", "name2"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w.js", "name2"));
EXPECT_FALSE(Matches(instance2, "http://example.net/w2.js", "name2"));
EXPECT_FALSE(Matches(instance2, kDataURL, ""));
EXPECT_FALSE(Matches(instance2, kDataURL, "name"));
EXPECT_FALSE(Matches(instance2, kFileURL, ""));
// This should not match because file:// URL is treated as an opaque origin.
EXPECT_FALSE(Matches(instance2, kFileURL, "name"));
}
} // namespace content
|