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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/Logging.h"
#include "mozilla/Preferences.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/media/MediaUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsNetUtil.h"
#include "nsIFile.h"
#include "nsIObserverService.h"
#include "nsIURI.h"
#include "nsIURIMutator.h"
#include "ContentAnalysis.h"
#include "SpecialSystemDirectory.h"
#include "TestContentAnalysisUtils.h"
#include <processenv.h>
#include <synchapi.h>
#include <vector>
const char* kAllowUrlPref = "browser.contentanalysis.allow_url_regex_list";
const char* kDenyUrlPref = "browser.contentanalysis.deny_url_regex_list";
const char* kPipePathNamePref = "browser.contentanalysis.pipe_path_name";
const char* kIsDLPEnabledPref = "browser.contentanalysis.enabled";
const char* kTimeoutPref = "browser.contentanalysis.agent_timeout";
using namespace mozilla;
using namespace mozilla::contentanalysis;
class ContentAnalysisTest : public testing::Test {
protected:
ContentAnalysisTest() {
auto* logmodule = LogModule::Get("contentanalysis");
logmodule->SetLevel(LogLevel::Verbose);
MOZ_ALWAYS_SUCCEEDS(
Preferences::SetString(kPipePathNamePref, mPipeName.get()));
MOZ_ALWAYS_SUCCEEDS(Preferences::SetBool(kIsDLPEnabledPref, true));
nsCOMPtr<nsIContentAnalysis> caSvc =
do_GetService("@mozilla.org/contentanalysis;1");
MOZ_ASSERT(caSvc);
mContentAnalysis = static_cast<ContentAnalysis*>(caSvc.get());
// Tests run earlier could have altered these values
mContentAnalysis->mParsedUrlLists = false;
mContentAnalysis->mAllowUrlList = {};
mContentAnalysis->mDenyUrlList = {};
MOZ_ALWAYS_SUCCEEDS(mContentAnalysis->TestOnlySetCACmdLineArg(true));
MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(kAllowUrlPref, ""));
MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(kDenyUrlPref, ""));
bool isActive = false;
MOZ_ALWAYS_SUCCEEDS(mContentAnalysis->GetIsActive(&isActive));
EXPECT_TRUE(isActive);
}
// Note that the constructor (and SetUp() method) get called once per test,
// not once for the whole fixture. Because Firefox does not currently
// reconnect to an agent after the DLP pipe is closed (bug 1888293), we only
// want to create the agent once and make sure the same process stays alive
// through all of these tests.
static void SetUpTestSuite() {
GeneratePipeName(L"contentanalysissdk-gtest-", mPipeName);
mAgentInfo = LaunchAgentNormal(L"block", mPipeName);
}
static void TearDownTestSuite() { mAgentInfo.TerminateProcess(); }
void TearDown() override {
mContentAnalysis->mParsedUrlLists = false;
mContentAnalysis->mAllowUrlList = {};
mContentAnalysis->mDenyUrlList = {};
mContentAnalysis->ResetCachedDataTimeoutForTesting();
MOZ_ALWAYS_SUCCEEDS(mContentAnalysis->TestOnlySetCACmdLineArg(false));
MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(kAllowUrlPref, ""));
MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(kDenyUrlPref, ""));
MOZ_ALWAYS_SUCCEEDS(Preferences::ClearUser(kPipePathNamePref));
MOZ_ALWAYS_SUCCEEDS(Preferences::ClearUser(kIsDLPEnabledPref));
}
already_AddRefed<nsIContentAnalysisRequest> CreateRequest(const char* aUrl) {
nsCOMPtr<nsIURI> uri;
MOZ_ALWAYS_SUCCEEDS(NS_NewURI(getter_AddRefs(uri), aUrl));
// We will only use the URL and, implicitly, the analysisType
// (behavior differs for download vs other types).
return RefPtr(new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eFileTransfer,
EmptyString(), false, EmptyCString(), uri,
nsIContentAnalysisRequest::OperationType::eDroppedText,
nullptr))
.forget();
}
RefPtr<ContentAnalysis> mContentAnalysis;
static nsString mPipeName;
static MozAgentInfo mAgentInfo;
// Proxies for private members of ContentAnalysis. TEST_F
// creates new subclasses -- they do not inherit `friend`s.
// (FRIEND_TEST is another more verbose solution.)
using UrlFilterResult = ContentAnalysis::UrlFilterResult;
UrlFilterResult FilterByUrlLists(nsIContentAnalysisRequest* aReq) {
return mContentAnalysis->FilterByUrlLists(aReq);
}
};
nsString ContentAnalysisTest::mPipeName;
MozAgentInfo ContentAnalysisTest::mAgentInfo;
TEST_F(ContentAnalysisTest, AllowUrlList) {
MOZ_ALWAYS_SUCCEEDS(
Preferences::SetCString(kAllowUrlPref, ".*\\.org/match.*"));
RefPtr<nsIContentAnalysisRequest> car =
CreateRequest("https://example.org/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eAllow);
car = CreateRequest("https://example.com/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck);
}
TEST_F(ContentAnalysisTest, DefaultAllowUrlList) {
MOZ_ALWAYS_SUCCEEDS(Preferences::ClearUser(kAllowUrlPref));
RefPtr<nsIContentAnalysisRequest> car = CreateRequest("about:home");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eAllow);
car = CreateRequest("about:blank");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck);
car = CreateRequest("about:srcdoc");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck);
car = CreateRequest("https://example.com/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck);
}
TEST_F(ContentAnalysisTest, MultipleAllowUrlList) {
MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(
kAllowUrlPref, ".*\\.org/match.* .*\\.net/match.*"));
RefPtr<nsIContentAnalysisRequest> car =
CreateRequest("https://example.org/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eAllow);
car = CreateRequest("https://example.net/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eAllow);
car = CreateRequest("https://example.com/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck);
}
TEST_F(ContentAnalysisTest, DenyUrlList) {
MOZ_ALWAYS_SUCCEEDS(
Preferences::SetCString(kDenyUrlPref, ".*\\.com/match.*"));
RefPtr<nsIContentAnalysisRequest> car =
CreateRequest("https://example.org/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck);
car = CreateRequest("https://example.com/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eDeny);
}
TEST_F(ContentAnalysisTest, MultipleDenyUrlList) {
MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(
kDenyUrlPref, ".*\\.com/match.* .*\\.biz/match.*"));
RefPtr<nsIContentAnalysisRequest> car =
CreateRequest("https://example.org/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eCheck);
car = CreateRequest("https://example.com/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eDeny);
car = CreateRequest("https://example.biz/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eDeny);
}
TEST_F(ContentAnalysisTest, DenyOverridesAllowUrlList) {
MOZ_ALWAYS_SUCCEEDS(
Preferences::SetCString(kAllowUrlPref, ".*\\.org/match.*"));
MOZ_ALWAYS_SUCCEEDS(Preferences::SetCString(kDenyUrlPref, ".*.org/match.*"));
RefPtr<nsIContentAnalysisRequest> car =
CreateRequest("https://example.org/matchme/");
ASSERT_EQ(FilterByUrlLists(car), UrlFilterResult::eDeny);
}
nsCOMPtr<nsIURI> GetExampleDotComURI() {
nsCOMPtr<nsIURI> uri;
MOZ_ALWAYS_SUCCEEDS(NS_NewURI(getter_AddRefs(uri), "https://example.com"));
return uri;
}
nsCOMPtr<nsIURI> GetExampleDotComWithPathURI() {
nsCOMPtr<nsIURI> uri;
MOZ_ALWAYS_SUCCEEDS(
NS_NewURI(getter_AddRefs(uri), "https://example.com/path"));
return uri;
}
struct BoolStruct {
bool mValue = false;
};
void SendRequestAndExpectResponse(
RefPtr<ContentAnalysis> contentAnalysis,
const nsCOMPtr<nsIContentAnalysisRequest>& request,
Maybe<bool> expectedShouldAllow,
Maybe<nsIContentAnalysisResponse::Action> expectedAction,
Maybe<bool> expectedIsCached) {
std::atomic<bool> gotResponse = false;
// Make timedOut a RefPtr so if we get a response from content analysis
// after this function has finished we can safely check that (and don't
// start accessing stack values that don't exist anymore)
RefPtr timedOut = MakeRefPtr<media::Refcountable<BoolStruct>>();
auto callback = MakeRefPtr<ContentAnalysisCallback>(
[&, timedOut](nsIContentAnalysisResponse* response) {
if (timedOut->mValue) {
return;
}
if (expectedShouldAllow.isSome()) {
bool shouldAllow = false;
MOZ_ALWAYS_SUCCEEDS(response->GetShouldAllowContent(&shouldAllow));
EXPECT_EQ(*expectedShouldAllow, shouldAllow);
}
if (expectedAction.isSome()) {
nsIContentAnalysisResponse::Action action;
MOZ_ALWAYS_SUCCEEDS(response->GetAction(&action));
EXPECT_EQ(*expectedAction, action);
}
if (expectedIsCached.isSome()) {
bool isCached;
MOZ_ALWAYS_SUCCEEDS(response->GetIsCachedResponse(&isCached));
EXPECT_EQ(*expectedIsCached, isCached);
}
nsCString requestToken, originalRequestToken;
MOZ_ALWAYS_SUCCEEDS(response->GetRequestToken(requestToken));
MOZ_ALWAYS_SUCCEEDS(request->GetRequestToken(originalRequestToken));
EXPECT_EQ(originalRequestToken, requestToken);
gotResponse = true;
},
[&gotResponse, timedOut](nsresult error) {
if (timedOut->mValue) {
return;
}
EXPECT_EQ(NS_OK, error);
gotResponse = true;
// Make sure that we didn't somehow get passed NS_OK
FAIL() << "Got error response";
});
MOZ_ALWAYS_SUCCEEDS(
contentAnalysis->AnalyzeContentRequestCallback(request, false, callback));
RefPtr<CancelableRunnable> timer =
NS_NewCancelableRunnableFunction("Content Analysis timeout", [&] {
if (!gotResponse.load()) {
timedOut->mValue = true;
}
});
#if defined(MOZ_ASAN)
// This can be pretty slow on ASAN builds (bug 1895256)
constexpr uint32_t kCATimeout = 25000;
#else
constexpr uint32_t kCATimeout = 10000;
#endif
NS_DelayedDispatchToCurrentThread(do_AddRef(timer), kCATimeout);
mozilla::SpinEventLoopUntil(
"Waiting for ContentAnalysis result"_ns,
[&, timedOut]() { return gotResponse.load() || timedOut->mValue; });
timer->Cancel();
EXPECT_TRUE(gotResponse);
EXPECT_FALSE(timedOut->mValue);
}
void YieldMainThread(uint32_t timeInMs) {
std::atomic<bool> timeExpired = false;
// The timer gets cleared on the main thread, so we need to yield the main
// thread for this to work
RefPtr<CancelableRunnable> timer = NS_NewCancelableRunnableFunction(
"Content Analysis yielding", [&] { timeExpired = true; });
// Wait for longer than the cache timeout
NS_DelayedDispatchToCurrentThread(do_AddRef(timer), timeInMs);
mozilla::SpinEventLoopUntil("Waiting for Content Analysis yielding"_ns,
[&]() { return timeExpired.load(); });
timer->Cancel();
}
TEST_F(ContentAnalysisTest, SendAllowedTextToAgent_GetAllowedResponse) {
nsCOMPtr<nsIURI> uri = GetExampleDotComURI();
nsString allow(L"allow");
nsCOMPtr<nsIContentAnalysisRequest> request = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, std::move(allow),
false, EmptyCString(), uri,
nsIContentAnalysisRequest::OperationType::eClipboard, nullptr);
SendRequestAndExpectResponse(mContentAnalysis, request, Some(true),
Some(nsIContentAnalysisResponse::eAllow),
Some(false));
}
TEST_F(ContentAnalysisTest, SendBlockedTextToAgent_GetBlockResponse) {
nsCOMPtr<nsIURI> uri = GetExampleDotComURI();
nsString block(L"block");
nsCOMPtr<nsIContentAnalysisRequest> request = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, std::move(block),
false, EmptyCString(), uri,
nsIContentAnalysisRequest::OperationType::eClipboard, nullptr);
SendRequestAndExpectResponse(mContentAnalysis, request, Some(false),
Some(nsIContentAnalysisResponse::eBlock),
Some(false));
}
class RawRequestObserver final : public nsIObserver {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
RawRequestObserver() {}
const std::vector<content_analysis::sdk::ContentAnalysisRequest>&
GetRequests() {
return mRequests;
}
private:
~RawRequestObserver() = default;
std::vector<content_analysis::sdk::ContentAnalysisRequest> mRequests;
};
NS_IMPL_ISUPPORTS(RawRequestObserver, nsIObserver);
NS_IMETHODIMP RawRequestObserver::Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aData) {
std::wstring dataWideString(reinterpret_cast<const wchar_t*>(aData));
std::vector<uint8_t> dataVector(dataWideString.size());
for (size_t i = 0; i < dataWideString.size(); ++i) {
// Since this data is really bytes and not a null-terminated string, the
// calling code adds 0xFF00 to every member to ensure there are no 0 values.
dataVector[i] = static_cast<uint8_t>(dataWideString[i] - 0xFF00);
}
content_analysis::sdk::ContentAnalysisRequest request;
EXPECT_TRUE(request.ParseFromArray(dataVector.data(), dataVector.size()));
mRequests.push_back(std::move(request));
return NS_OK;
}
TEST_F(ContentAnalysisTest, CheckRawRequestWithText) {
MOZ_ALWAYS_SUCCEEDS(Preferences::SetInt(kTimeoutPref, 65));
nsCOMPtr<nsIURI> uri = GetExampleDotComURI();
nsString allow(L"allow");
nsCOMPtr<nsIContentAnalysisRequest> request = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, std::move(allow),
false, EmptyCString(), uri,
nsIContentAnalysisRequest::OperationType::eClipboard, nullptr);
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
auto rawRequestObserver = MakeRefPtr<RawRequestObserver>();
MOZ_ALWAYS_SUCCEEDS(
obsServ->AddObserver(rawRequestObserver, "dlp-request-sent-raw", false));
time_t now = time(nullptr);
SendRequestAndExpectResponse(mContentAnalysis, request, Nothing(), Nothing(),
Some(false));
auto requests = rawRequestObserver->GetRequests();
EXPECT_EQ(static_cast<size_t>(1), requests.size());
time_t t = requests[0].expires_at();
time_t secs_remaining = t - now;
// There should be around 65 seconds remaining
EXPECT_LE(abs(secs_remaining - 65), 2);
const auto& request_url = requests[0].request_data().url();
EXPECT_EQ(uri->GetSpecOrDefault(),
nsCString(request_url.data(), request_url.size()));
nsCString request_user_action_id(requests[0].user_action_id().data(),
requests[0].user_action_id().size());
// The user_action_id has a GUID appended to the end, just make sure the
// beginning is right.
request_user_action_id.Truncate(8);
EXPECT_EQ(nsCString("Firefox "), request_user_action_id);
const auto& request_text = requests[0].text_content();
EXPECT_EQ(nsCString("allow"),
nsCString(request_text.data(), request_text.size()));
MOZ_ALWAYS_SUCCEEDS(
obsServ->RemoveObserver(rawRequestObserver, "dlp-request-sent-raw"));
MOZ_ALWAYS_SUCCEEDS(Preferences::ClearUser(kTimeoutPref));
}
TEST_F(ContentAnalysisTest, CheckRawRequestWithFile) {
nsCOMPtr<nsIURI> uri = GetExampleDotComURI();
nsCOMPtr<nsIFile> file;
MOZ_ALWAYS_SUCCEEDS(GetSpecialSystemDirectory(OS_CurrentWorkingDirectory,
getter_AddRefs(file)));
nsString allowRelativePath(L"allowedFile.txt");
MOZ_ALWAYS_SUCCEEDS(file->AppendRelativePath(allowRelativePath));
nsString allowPath;
MOZ_ALWAYS_SUCCEEDS(file->GetPath(allowPath));
nsCOMPtr<nsIContentAnalysisRequest> request = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, allowPath, true,
EmptyCString(), uri, nsIContentAnalysisRequest::OperationType::eClipboard,
nullptr);
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
auto rawRequestObserver = MakeRefPtr<RawRequestObserver>();
MOZ_ALWAYS_SUCCEEDS(
obsServ->AddObserver(rawRequestObserver, "dlp-request-sent-raw", false));
SendRequestAndExpectResponse(mContentAnalysis, request, Nothing(), Nothing(),
Some(false));
auto requests = rawRequestObserver->GetRequests();
EXPECT_EQ(static_cast<size_t>(1), requests.size());
const auto& request_url = requests[0].request_data().url();
EXPECT_EQ(uri->GetSpecOrDefault(),
nsCString(request_url.data(), request_url.size()));
nsCString request_user_action_id(requests[0].user_action_id().data(),
requests[0].user_action_id().size());
// The user_action_id has a GUID appended to the end, just make sure the
// beginning is right.
request_user_action_id.Truncate(8);
EXPECT_EQ(nsCString("Firefox "), request_user_action_id);
const auto& request_file_path = requests[0].file_path();
EXPECT_EQ(NS_ConvertUTF16toUTF8(allowPath),
nsCString(request_file_path.data(), request_file_path.size()));
MOZ_ALWAYS_SUCCEEDS(
obsServ->RemoveObserver(rawRequestObserver, "dlp-request-sent-raw"));
}
TEST_F(ContentAnalysisTest, CheckTwoRequestsHaveSameUserActionId) {
nsCOMPtr<nsIURI> uri = GetExampleDotComURI();
nsString allow1(L"allowMe");
nsCOMPtr<nsIContentAnalysisRequest> request1 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry,
std::move(allow1), false, EmptyCString(), uri,
nsIContentAnalysisRequest::OperationType::eClipboard, nullptr);
// Use different text so the request doesn't match the cache
nsString allow2(L"allowMeAgain");
nsCOMPtr<nsIContentAnalysisRequest> request2 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry,
std::move(allow2), false, EmptyCString(), uri,
nsIContentAnalysisRequest::OperationType::eClipboard, nullptr);
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
auto rawRequestObserver = MakeRefPtr<RawRequestObserver>();
MOZ_ALWAYS_SUCCEEDS(
obsServ->AddObserver(rawRequestObserver, "dlp-request-sent-raw", false));
SendRequestAndExpectResponse(mContentAnalysis, request1, Nothing(), Nothing(),
Some(false));
SendRequestAndExpectResponse(mContentAnalysis, request2, Nothing(), Nothing(),
Some(false));
auto requests = rawRequestObserver->GetRequests();
EXPECT_EQ(static_cast<size_t>(2), requests.size());
EXPECT_EQ(requests[0].user_action_id(), requests[1].user_action_id());
MOZ_ALWAYS_SUCCEEDS(
obsServ->RemoveObserver(rawRequestObserver, "dlp-request-sent-raw"));
}
TEST_F(ContentAnalysisTest, CheckNoCachedResultWhenDifferentText) {
nsCOMPtr<nsIURI> uri = GetExampleDotComURI();
nsString allow1(L"allowMe");
nsCOMPtr<nsIContentAnalysisRequest> request1 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry,
std::move(allow1), false, EmptyCString(), uri,
nsIContentAnalysisRequest::OperationType::eClipboard, nullptr);
// Use different text so the request doesn't match the cache
nsString allow2(L"allowMeAgain");
nsCOMPtr<nsIContentAnalysisRequest> request2 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry,
std::move(allow2), false, EmptyCString(), uri,
nsIContentAnalysisRequest::OperationType::eClipboard, nullptr);
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
auto rawRequestObserver = MakeRefPtr<RawRequestObserver>();
MOZ_ALWAYS_SUCCEEDS(
obsServ->AddObserver(rawRequestObserver, "dlp-request-sent-raw", false));
SendRequestAndExpectResponse(mContentAnalysis, request1, Nothing(), Nothing(),
Some(false));
SendRequestAndExpectResponse(mContentAnalysis, request2, Nothing(), Nothing(),
Some(false));
auto requests = rawRequestObserver->GetRequests();
EXPECT_EQ(static_cast<size_t>(2), requests.size());
MOZ_ALWAYS_SUCCEEDS(
obsServ->RemoveObserver(rawRequestObserver, "dlp-request-sent-raw"));
}
TEST_F(ContentAnalysisTest, CheckNoCachedResultWhenDifferentUrl) {
nsCOMPtr<nsIURI> uri1 = GetExampleDotComURI();
nsCOMPtr<nsIURI> uri2 = GetExampleDotComWithPathURI();
nsString allow(L"allowMe");
nsCOMPtr<nsIContentAnalysisRequest> request1 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, allow, false,
EmptyCString(), uri1,
nsIContentAnalysisRequest::OperationType::eClipboard, nullptr);
nsCOMPtr<nsIContentAnalysisRequest> request2 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, allow, false,
EmptyCString(), uri2,
nsIContentAnalysisRequest::OperationType::eClipboard, nullptr);
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
auto rawRequestObserver = MakeRefPtr<RawRequestObserver>();
MOZ_ALWAYS_SUCCEEDS(
obsServ->AddObserver(rawRequestObserver, "dlp-request-sent-raw", false));
SendRequestAndExpectResponse(mContentAnalysis, request1, Nothing(), Nothing(),
Some(false));
SendRequestAndExpectResponse(mContentAnalysis, request2, Nothing(), Nothing(),
Some(false));
auto requests = rawRequestObserver->GetRequests();
EXPECT_EQ(static_cast<size_t>(2), requests.size());
MOZ_ALWAYS_SUCCEEDS(
obsServ->RemoveObserver(rawRequestObserver, "dlp-request-sent-raw"));
}
TEST_F(ContentAnalysisTest, CheckNoCachedResultWhenFilePath) {
nsCOMPtr<nsIURI> uri = GetExampleDotComURI();
nsCOMPtr<nsIFile> file;
MOZ_ALWAYS_SUCCEEDS(GetSpecialSystemDirectory(OS_CurrentWorkingDirectory,
getter_AddRefs(file)));
nsString allowRelativePath(L"allowedFile.txt");
MOZ_ALWAYS_SUCCEEDS(file->AppendRelativePath(allowRelativePath));
nsString allowPath;
MOZ_ALWAYS_SUCCEEDS(file->GetPath(allowPath));
nsCOMPtr<nsIContentAnalysisRequest> request1 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, allowPath, true,
EmptyCString(), uri, nsIContentAnalysisRequest::OperationType::eClipboard,
nullptr);
nsCOMPtr<nsIContentAnalysisRequest> request2 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, allowPath, true,
EmptyCString(), uri, nsIContentAnalysisRequest::OperationType::eClipboard,
nullptr);
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
auto rawRequestObserver = MakeRefPtr<RawRequestObserver>();
MOZ_ALWAYS_SUCCEEDS(
obsServ->AddObserver(rawRequestObserver, "dlp-request-sent-raw", false));
SendRequestAndExpectResponse(mContentAnalysis, request1, Nothing(), Nothing(),
Some(false));
SendRequestAndExpectResponse(mContentAnalysis, request2, Nothing(), Nothing(),
Some(false));
auto requests = rawRequestObserver->GetRequests();
EXPECT_EQ(static_cast<size_t>(2), requests.size());
MOZ_ALWAYS_SUCCEEDS(
obsServ->RemoveObserver(rawRequestObserver, "dlp-request-sent-raw"));
}
TEST_F(ContentAnalysisTest, CheckCachedResultForAllow) {
nsCOMPtr<nsIURI> uri = GetExampleDotComURI();
nsString allow(L"allowMeText");
nsCOMPtr<nsIContentAnalysisRequest> request1 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, allow, false,
EmptyCString(), uri, nsIContentAnalysisRequest::OperationType::eClipboard,
nullptr);
nsCOMPtr<nsIContentAnalysisRequest> request2 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, allow, false,
EmptyCString(), uri, nsIContentAnalysisRequest::OperationType::eClipboard,
nullptr);
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
auto rawRequestObserver = MakeRefPtr<RawRequestObserver>();
MOZ_ALWAYS_SUCCEEDS(
obsServ->AddObserver(rawRequestObserver, "dlp-request-sent-raw", false));
SendRequestAndExpectResponse(mContentAnalysis, request1, Some(true),
Some(nsIContentAnalysisResponse::eAllow),
Some(false));
// The timer gets cleared on the main thread, so yield for a short time
// to make sure it doesn't get cleared
YieldMainThread(50);
SendRequestAndExpectResponse(mContentAnalysis, request2, Some(true),
Some(nsIContentAnalysisResponse::eAllow),
Some(true));
auto requests = rawRequestObserver->GetRequests();
// Only the first request should be analyzed since the second would match the
// cache.
EXPECT_EQ(static_cast<size_t>(1), requests.size());
MOZ_ALWAYS_SUCCEEDS(
obsServ->RemoveObserver(rawRequestObserver, "dlp-request-sent-raw"));
}
TEST_F(ContentAnalysisTest, CheckCachedResultForBlock) {
nsCOMPtr<nsIURI> uri = GetExampleDotComURI();
nsString block(L"blockMeText");
nsCOMPtr<nsIContentAnalysisRequest> request1 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, block, false,
EmptyCString(), uri, nsIContentAnalysisRequest::OperationType::eClipboard,
nullptr);
nsCOMPtr<nsIContentAnalysisRequest> request2 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, block, false,
EmptyCString(), uri, nsIContentAnalysisRequest::OperationType::eClipboard,
nullptr);
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
auto rawRequestObserver = MakeRefPtr<RawRequestObserver>();
MOZ_ALWAYS_SUCCEEDS(
obsServ->AddObserver(rawRequestObserver, "dlp-request-sent-raw", false));
SendRequestAndExpectResponse(mContentAnalysis, request1, Some(false),
Some(nsIContentAnalysisResponse::eBlock),
Some(false));
// The timer gets cleared on the main thread, so yield for a short time
// to make sure it doesn't get cleared
YieldMainThread(50);
SendRequestAndExpectResponse(mContentAnalysis, request2, Some(false),
Some(nsIContentAnalysisResponse::eBlock),
Some(true));
auto requests = rawRequestObserver->GetRequests();
// Only the first request should be analyzed since the second would match the
// cache.
EXPECT_EQ(static_cast<size_t>(1), requests.size());
MOZ_ALWAYS_SUCCEEDS(
obsServ->RemoveObserver(rawRequestObserver, "dlp-request-sent-raw"));
}
TEST_F(ContentAnalysisTest, CheckCachedExpiration) {
nsCOMPtr<nsIURI> uri = GetExampleDotComURI();
nsString allow(L"allowMeTextWithExpiration");
constexpr uint32_t kShortCacheTimeout =
ContentAnalysis::kDefaultCachedDataTimeoutInMs / 50;
mContentAnalysis->SetCachedDataTimeoutForTesting(kShortCacheTimeout);
nsCOMPtr<nsIContentAnalysisRequest> request1 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, allow, false,
EmptyCString(), uri, nsIContentAnalysisRequest::OperationType::eClipboard,
nullptr);
nsCOMPtr<nsIContentAnalysisRequest> request2 = new ContentAnalysisRequest(
nsIContentAnalysisRequest::AnalysisType::eBulkDataEntry, allow, false,
EmptyCString(), uri, nsIContentAnalysisRequest::OperationType::eClipboard,
nullptr);
nsCOMPtr<nsIObserverService> obsServ =
mozilla::services::GetObserverService();
auto rawRequestObserver = MakeRefPtr<RawRequestObserver>();
MOZ_ALWAYS_SUCCEEDS(
obsServ->AddObserver(rawRequestObserver, "dlp-request-sent-raw", false));
SendRequestAndExpectResponse(mContentAnalysis, request1, Some(true),
Some(nsIContentAnalysisResponse::eAllow),
Some(false));
// The timer gets cleared on the main thread, so we need to yield the main
// thread for this to work
YieldMainThread(kShortCacheTimeout * 2);
SendRequestAndExpectResponse(mContentAnalysis, request2, Some(true),
Some(nsIContentAnalysisResponse::eAllow),
Some(false));
mContentAnalysis->ResetCachedDataTimeoutForTesting();
auto requests = rawRequestObserver->GetRequests();
// Both requests should be analyzed since the second arrived after the
// timeout.
EXPECT_EQ(static_cast<size_t>(2), requests.size());
MOZ_ALWAYS_SUCCEEDS(
obsServ->RemoveObserver(rawRequestObserver, "dlp-request-sent-raw"));
}
|