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

content / browser / interest_group / subresource_url_authorizations_unittest.cc [blame]

// Copyright 2022 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/interest_group/subresource_url_authorizations.h"

#include <tuple>

#include "content/browser/interest_group/auction_worklet_manager.h"
#include "content/browser/interest_group/subresource_url_builder.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/interest_group/auction_config.h"
#include "url/gurl.h"

namespace content {

namespace {

using WorkletHandle = AuctionWorkletManager::WorkletHandle;
using BundleSubresourceInfo = SubresourceUrlBuilder::BundleSubresourceInfo;

// We don't need to actually make real WorkletHandles --
// SubresourceUrlAuthorizations only cares about the addresses of
// WorkletHandles, so we can use hard-coded pointers.
const WorkletHandle* const kWorkletHandle1 =
    reinterpret_cast<const WorkletHandle*>(0xABC123);

const WorkletHandle* const kWorkletHandle2 =
    reinterpret_cast<const WorkletHandle*>(0xDEF456);

constexpr char kBundleUrl[] = "https://seller.test/bundle";

constexpr char kSubresourceUrl1[] = "https://seller.test/signals1";
constexpr char kSubresourceUrl2[] = "https://seller.test/signals2";

// NOTE: MakeBlinkSubresource() does *not* yield the same results each
// time it is called, since it generates a random token in the
// DirectFromSellerSignalsSubresource constructor.

blink::DirectFromSellerSignalsSubresource MakeBlinkSubresource() {
  blink::DirectFromSellerSignalsSubresource subresource;
  subresource.bundle_url = GURL(kBundleUrl);
  return subresource;
}

}  // namespace

class SubresourceUrlAuthorizationsTest : public ::testing::Test {
 protected:
  void AuthorizeSubresourceUrls(
      const AuctionWorkletManager::WorkletHandle* worklet_handle,
      const std::vector<SubresourceUrlBuilder::BundleSubresourceInfo>&
          authorized_subresource_urls,
      SubresourceUrlAuthorizations& authorizations) {
    authorizations.AuthorizeSubresourceUrls(worklet_handle,
                                            authorized_subresource_urls);
  }

  void OnWorkletHandleDestruction(
      const AuctionWorkletManager::WorkletHandle* worklet_handle,
      SubresourceUrlAuthorizations& authorizations) {
    authorizations.OnWorkletHandleDestruction(worklet_handle);
  }
};

TEST_F(SubresourceUrlAuthorizationsTest, Construct) {
  SubresourceUrlAuthorizations authorizations;
}

TEST_F(SubresourceUrlAuthorizationsTest, InitiallyNotAuthorized) {
  SubresourceUrlAuthorizations authorizations;

  GURL subresource_url1(kSubresourceUrl1);
  GURL subresource_url2(kSubresourceUrl2);

  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url1));
  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url2));
}

TEST_F(SubresourceUrlAuthorizationsTest, Simple) {
  SubresourceUrlAuthorizations authorizations;

  GURL subresource_url1(kSubresourceUrl1);
  GURL subresource_url2(kSubresourceUrl2);

  const BundleSubresourceInfo full_info1(subresource_url1,
                                         MakeBlinkSubresource());
  const BundleSubresourceInfo full_info2(subresource_url2,
                                         MakeBlinkSubresource());

  AuthorizeSubresourceUrls(kWorkletHandle1, {full_info1, full_info2},
                           authorizations);

  EXPECT_EQ(full_info1, *authorizations.GetAuthorizationInfo(subresource_url1));
  EXPECT_EQ(full_info2, *authorizations.GetAuthorizationInfo(subresource_url2));

  OnWorkletHandleDestruction(kWorkletHandle1, authorizations);

  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url1));
  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url2));
}

TEST_F(SubresourceUrlAuthorizationsTest, NotAuthorized) {
  SubresourceUrlAuthorizations authorizations;

  GURL subresource_url1(kSubresourceUrl1);
  GURL subresource_url2(kSubresourceUrl2);

  const BundleSubresourceInfo full_info1(subresource_url1,
                                         MakeBlinkSubresource());

  AuthorizeSubresourceUrls(kWorkletHandle1, {full_info1}, authorizations);

  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url2));

  OnWorkletHandleDestruction(kWorkletHandle1, authorizations);

  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url1));
  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url2));
}

TEST_F(SubresourceUrlAuthorizationsTest, MultipleHandles) {
  SubresourceUrlAuthorizations authorizations;

  GURL subresource_url1(kSubresourceUrl1);
  GURL subresource_url2(kSubresourceUrl2);

  const BundleSubresourceInfo full_info1(subresource_url1,
                                         MakeBlinkSubresource());
  const BundleSubresourceInfo full_info2(subresource_url2,
                                         MakeBlinkSubresource());

  // `full_info1` is authorized by both `kWorkletHandle1` and `kWorkletHandle2`.
  // `full_info1` should only de-authorize when both `kWorkletHandle1` and
  // `kWorkletHandle2` are both destroyed.
  AuthorizeSubresourceUrls(kWorkletHandle1, {full_info1, full_info2},
                           authorizations);
  AuthorizeSubresourceUrls(kWorkletHandle2, {full_info1}, authorizations);

  EXPECT_EQ(full_info1, *authorizations.GetAuthorizationInfo(subresource_url1));
  EXPECT_EQ(full_info2, *authorizations.GetAuthorizationInfo(subresource_url2));

  OnWorkletHandleDestruction(kWorkletHandle1, authorizations);

  EXPECT_EQ(full_info1, *authorizations.GetAuthorizationInfo(subresource_url1));
  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url2));

  OnWorkletHandleDestruction(kWorkletHandle2, authorizations);

  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url1));
  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url2));
}

TEST_F(SubresourceUrlAuthorizationsTest, BadScriptAltersSubresource) {
  SubresourceUrlAuthorizations authorizations;

  GURL subresource_url1(kSubresourceUrl1);

  const BundleSubresourceInfo full_info1(subresource_url1,
                                         MakeBlinkSubresource());
  // Try to authorize with a different token, but the same `subresource_url1`
  // subresource URL. This mimics the behavior of a misbehaving script that
  // alters the <script type="webbundle"> definition of a subresource while an
  // auction is still running.
  const BundleSubresourceInfo full_info2(subresource_url1,
                                         MakeBlinkSubresource());

  // When kWorkletHandle2 is authorized, `subresource_url1` will still map to
  // the full info from `full_info1`, and no crash should occur.
  AuthorizeSubresourceUrls(kWorkletHandle1, {full_info1}, authorizations);
  AuthorizeSubresourceUrls(kWorkletHandle2, {full_info2}, authorizations);

  EXPECT_EQ(full_info1, *authorizations.GetAuthorizationInfo(subresource_url1));

  OnWorkletHandleDestruction(kWorkletHandle1, authorizations);

  EXPECT_EQ(full_info1, *authorizations.GetAuthorizationInfo(subresource_url1));

  OnWorkletHandleDestruction(kWorkletHandle2, authorizations);

  EXPECT_EQ(nullptr, authorizations.GetAuthorizationInfo(subresource_url1));
}

}  // namespace content