File: privacy_sandbox_attestations_component_installer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (204 lines) | stat: -rw-r--r-- 7,574 bytes parent folder | download | duplicates (6)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/component_updater/privacy_sandbox_attestations_component_installer.h"

#include <cstdint>
#include <memory>
#include <utility>
#include <vector>

#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/path_service.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/values.h"
#include "base/version.h"
#include "chrome/common/chrome_paths.h"
#include "components/component_updater/component_installer.h"
#include "components/privacy_sandbox/privacy_sandbox_attestations/privacy_sandbox_attestations.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "components/update_client/update_client.h"

namespace {

constexpr base::FilePath::CharType kPrivacySandboxAttestationsFileName[] =
    FILE_PATH_LITERAL("privacy-sandbox-attestations.dat");

constexpr base::FilePath::CharType
    kPrivacySandboxAttestationsRelativeInstallDir[] =
        FILE_PATH_LITERAL("PrivacySandboxAttestationsPreloaded");

// The SHA256 of the SubjectPublicKeyInfo used to sign the extension.
// The extension id is: niikhdgajlphfehepabhhblakbdgeefj
constexpr uint8_t kPrivacySandboxAttestationsPublicKeySHA256[32] = {
    0xd8, 0x8a, 0x73, 0x60, 0x9b, 0xf7, 0x54, 0x74, 0xf0, 0x17, 0x71,
    0xb0, 0xa1, 0x36, 0x44, 0x59, 0xf6, 0x22, 0x38, 0xa8, 0x7c, 0xc9,
    0x7b, 0x7a, 0x43, 0x2c, 0x72, 0xee, 0x04, 0x01, 0xae, 0xc0};

const char kPrivacySandboxAttestationsManifestName[] =
    "Privacy Sandbox Attestations";

}  // namespace

namespace component_updater {

PrivacySandboxAttestationsComponentInstallerPolicy::
    PrivacySandboxAttestationsComponentInstallerPolicy(
        AttestationsReadyRepeatingCallback on_attestations_ready)
    : on_attestations_ready_(std::move(on_attestations_ready)) {}

PrivacySandboxAttestationsComponentInstallerPolicy::
    ~PrivacySandboxAttestationsComponentInstallerPolicy() = default;

bool PrivacySandboxAttestationsComponentInstallerPolicy::VerifyInstallation(
    const base::Value::Dict& manifest,
    const base::FilePath& install_dir) const {
  return base::PathExists(GetInstalledFilePath(install_dir));
}

bool PrivacySandboxAttestationsComponentInstallerPolicy::
    SupportsGroupPolicyEnabledComponentUpdates() const {
  return true;
}

bool PrivacySandboxAttestationsComponentInstallerPolicy::
    RequiresNetworkEncryption() const {
  // Privacy sandbox attestations are identical for all users.
  return false;
}

update_client::CrxInstaller::Result
PrivacySandboxAttestationsComponentInstallerPolicy::OnCustomInstall(
    const base::Value::Dict& manifest,
    const base::FilePath& install_dir) {
  // No custom install for privacy sandbox attestations.
  return update_client::CrxInstaller::Result(0);
}

void PrivacySandboxAttestationsComponentInstallerPolicy::OnCustomUninstall() {}

void PrivacySandboxAttestationsComponentInstallerPolicy::ComponentReady(
    const base::Version& version,
    const base::FilePath& install_dir,
    base::Value::Dict manifest) {
  if (!base::FeatureList::IsEnabled(
          privacy_sandbox::kEnforcePrivacySandboxAttestations)) {
    // Privacy Sandbox Enrollment is not enforced if the feature is disabled.
    return;
  }

  if (on_attestations_ready_.is_null()) {
    return;
  }

  if (install_dir.empty() || !version.IsValid()) {
    return;
  }

  // Record the time taken for the downloaded attestations file to be detected.
  startup_metric_utils::GetBrowser().RecordPrivacySandboxAttestationsFirstReady(
      base::TimeTicks::Now());

  VLOG(1) << "Privacy Sandbox Attestations Component ready, version "
          << version.GetString() << " in " << install_dir.value();

  on_attestations_ready_.Run(
      std::move(version),
      /*installed_file_path=*/
      PrivacySandboxAttestationsComponentInstallerPolicy::GetInstalledFilePath(
          install_dir),
      /*is_pre_installed=*/manifest.FindBool("pre_installed").value_or(false));
}

base::FilePath
PrivacySandboxAttestationsComponentInstallerPolicy::GetRelativeInstallDir()
    const {
  return base::FilePath(kPrivacySandboxAttestationsRelativeInstallDir);
}

void PrivacySandboxAttestationsComponentInstallerPolicy::GetHash(
    std::vector<uint8_t>* hash) const {
  hash->assign(std::begin(kPrivacySandboxAttestationsPublicKeySHA256),
               std::end(kPrivacySandboxAttestationsPublicKeySHA256));
}

std::string PrivacySandboxAttestationsComponentInstallerPolicy::GetName()
    const {
  return kPrivacySandboxAttestationsManifestName;
}

update_client::InstallerAttributes
PrivacySandboxAttestationsComponentInstallerPolicy::GetInstallerAttributes()
    const {
  return update_client::InstallerAttributes();
}

void PrivacySandboxAttestationsComponentInstallerPolicy::
    ComponentReadyForTesting(const base::Version& version,
                             const base::FilePath& install_dir,
                             base::Value::Dict manifest) {
  ComponentReady(version, install_dir, std::move(manifest));
}

// static
base::FilePath
PrivacySandboxAttestationsComponentInstallerPolicy::GetInstalledFilePath(
    const base::FilePath& base) {
  return base.Append(kPrivacySandboxAttestationsFileName);
}

// static
base::FilePath
PrivacySandboxAttestationsComponentInstallerPolicy::GetInstalledDirectory(
    const base::FilePath& base) {
  return base.Append(kPrivacySandboxAttestationsRelativeInstallDir);
}

void RegisterPrivacySandboxAttestationsComponent(ComponentUpdateService* cus) {
  if (!base::FeatureList::IsEnabled(
          privacy_sandbox::kEnforcePrivacySandboxAttestations)) {
    // Privacy sandbox enrollment is not enforced if the feature is disabled.
    // Any existing version of this component is deleted.
    base::FilePath user_path;
    if (base::PathService::Get(chrome::DIR_USER_DATA, &user_path)) {
      base::ThreadPool::PostTask(
          FROM_HERE, {base::TaskPriority::LOWEST, base::MayBlock()},
          base::GetDeletePathRecursivelyCallback(
              user_path.Append(kPrivacySandboxAttestationsRelativeInstallDir)));
    }
    return;
  }

  VLOG(1) << "Registering Privacy Sandbox Attestations component";

  auto policy =
      std::make_unique<PrivacySandboxAttestationsComponentInstallerPolicy>(
          /*on_attestations_ready=*/base::BindRepeating(
              [](base::Version version, base::FilePath install_dir,
                 bool is_pre_installed) {
                VLOG(1) << "Received privacy sandbox attestations file";
                privacy_sandbox::PrivacySandboxAttestations::GetInstance()
                    ->LoadAttestations(std::move(version),
                                       std::move(install_dir),
                                       is_pre_installed);
              }));

  base::MakeRefCounted<ComponentInstaller>(std::move(policy),
                                           /*action_handler=*/nullptr,
                                           base::TaskPriority::USER_BLOCKING)
      ->Register(cus, base::BindOnce([]() {
                   privacy_sandbox::PrivacySandboxAttestations::GetInstance()
                       ->OnAttestationsFileCheckComplete();
                 }));
}

}  // namespace component_updater