-
Notifications
You must be signed in to change notification settings - Fork 893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added ComponentContentsVerifier that checks verified_contents.json shipped with a component. #26660
Open
boocmp
wants to merge
14
commits into
master
Choose a base branch
from
components_verify
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+591
−118
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
df8a9e8
Added ComponentContentsVerifier that checks verified_contents.json sh…
boocmp 1cd84a9
Tests compilation.
boocmp b2cc1c4
Added test.
boocmp 9935d8f
Some review issues are addressed.
boocmp e7bce5d
Returning optional instead of passing default value.
boocmp 29d238b
Removed ComponentNoChecksContentsAccessorImpl.
boocmp 8a7e230
Check integrigity of all ad-block components.
boocmp e43bc74
Added bypassing mechanism.
boocmp 678e074
Moved accsessor creation to VerifyIntalltion stage.
boocmp e5d0620
Review issues are addressed.
boocmp 4aca6a9
Review.
boocmp 616de9b
Review.
boocmp 83bafe1
Feature moved to features.h.
boocmp b5a2e4a
Added const modifier.
boocmp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
browser/component_updater/brave_component_contents_verifier.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// 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 "brave/browser/component_updater/brave_component_contents_verifier.h" | ||
|
||
#include <algorithm> | ||
#include <memory> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "base/command_line.h" | ||
#include "base/containers/span_reader.h" | ||
#include "base/files/file_path.h" | ||
#include "base/files/file_util.h" | ||
#include "base/functional/bind.h" | ||
#include "brave/components/brave_component_updater/browser/component_contents_verifier.h" | ||
#include "brave/components/brave_component_updater/browser/features.h" | ||
#include "crypto/sha2.h" | ||
#include "extensions/buildflags/buildflags.h" | ||
|
||
#if BUILDFLAG(ENABLE_EXTENSIONS) | ||
|
||
#include "extensions/browser/content_hash_tree.h" | ||
#include "extensions/browser/verified_contents.h" | ||
|
||
namespace { | ||
|
||
constexpr uint8_t kComponentContentsVerifierPublicKey[] = { | ||
0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, | ||
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, | ||
0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc3, 0x8a, 0x2d, | ||
0x68, 0x10, 0xb3, 0x52, 0xf1, 0xcc, 0xae, 0xd7, 0x70, 0xb7, 0xc9, 0xc7, | ||
0xe3, 0x1c, 0x91, 0x4c, 0x1a, 0x73, 0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0x94, 0x48, 0x0a, 0x54, 0x05, 0x52, 0x01, 0x04, 0xd2, 0xef, 0x57, | ||
0x89, 0x47, 0xd3, 0xae, 0xd9, 0xc1, 0xf5, 0xa9, 0xbf, 0x60, 0x96, 0x4b, | ||
0xa0, 0x12, 0x9c, 0xf3, 0x00, 0xe6, 0x32, 0x40, 0xc7, 0x6e, 0x29, 0xa8, | ||
0x81, 0xfd, 0x2f, 0x1e, 0x92, 0x4a, 0x5e, 0xed, 0xef, 0x13, 0x9f, 0xed, | ||
0x88, 0x77, 0x2c, 0x84, 0xdd, 0x00, 0x87, 0x03, 0x49, 0x09, 0xb7, 0x4b, | ||
0xc7, 0x02, 0x03, 0x01, 0x00, 0x01}; | ||
|
||
constexpr char kVerifiedContentsPath[] = "_metadata/verified_contents.json"; | ||
constexpr char kBraveVerifiedContentsPath[] = | ||
"brave_metadata/verified_contents.json"; | ||
|
||
std::string GetRootHashForContent(base::span<const uint8_t> contents, | ||
size_t block_size) { | ||
CHECK(block_size % crypto::kSHA256Length == 0); | ||
|
||
std::vector<std::string> hashes; | ||
// Even when the contents is empty, we want to output at least one hash | ||
// block (the hash of the empty string). | ||
base::SpanReader reader(contents); | ||
do { | ||
const auto chunk = reader.Read(std::min(block_size, reader.remaining())); | ||
const auto hash = crypto::SHA256Hash(chunk.value()); | ||
hashes.emplace_back(hash.begin(), hash.end()); | ||
} while (reader.remaining() > 0); | ||
|
||
return extensions::ComputeTreeHashRoot(hashes, | ||
block_size / crypto::kSHA256Length); | ||
} | ||
|
||
class ExtensionsTreeHashContentsVerifier | ||
: public component_updater::ContentsVerifier { | ||
public: | ||
explicit ExtensionsTreeHashContentsVerifier( | ||
const base::FilePath& component_root) { | ||
if (base::PathExists( | ||
component_root.AppendASCII(kBraveVerifiedContentsPath))) { | ||
verified_contents_ = extensions::VerifiedContents::CreateFromFile( | ||
kComponentContentsVerifierPublicKey, | ||
component_root.AppendASCII(kBraveVerifiedContentsPath)); | ||
} else { | ||
verified_contents_ = extensions::VerifiedContents::CreateFromFile( | ||
kComponentContentsVerifierPublicKey, | ||
component_root.AppendASCII(kVerifiedContentsPath)); | ||
} | ||
if (verified_contents_ && | ||
verified_contents_->block_size() % crypto::kSHA256Length != 0) { | ||
// Unsupported block size. | ||
verified_contents_.reset(); | ||
} | ||
} | ||
|
||
bool VerifyContents(const base::FilePath& relative_path, | ||
base::span<const uint8_t> contents) const override { | ||
if (!verified_contents_ || | ||
!verified_contents_->HasTreeHashRoot(relative_path)) { | ||
return false; | ||
} | ||
|
||
const auto root_hash = | ||
GetRootHashForContent(contents, verified_contents_->block_size()); | ||
return verified_contents_->TreeHashRootEquals(relative_path, root_hash); | ||
} | ||
|
||
private: | ||
std::unique_ptr<extensions::VerifiedContents> verified_contents_; | ||
cdesouza-chromium marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
bool ShouldBypassSignature() { | ||
static const bool kBypass = | ||
!base::FeatureList::IsEnabled( | ||
brave_component_updater::kComponentContentsVerifier) || | ||
base::CommandLine::ForCurrentProcess()->HasSwitch( | ||
component_updater::kBypassComponentContentsVerifier); | ||
return kBypass; | ||
} | ||
|
||
} // namespace | ||
#endif // BUILDFLAG(ENABLE_EXTENSIONS) | ||
|
||
namespace component_updater { | ||
|
||
namespace { | ||
std::unique_ptr<component_updater::ContentsVerifier> | ||
CreateExtensionsTreeHashContentsVerifier(const base::FilePath& component_root) { | ||
#if BUILDFLAG(ENABLE_EXTENSIONS) | ||
if (!ShouldBypassSignature()) { | ||
return std::make_unique<ExtensionsTreeHashContentsVerifier>(component_root); | ||
} | ||
#endif | ||
// if there is no extensions enabled then we expect that on these platforms | ||
// the component files are protected by the OS. | ||
return nullptr; | ||
} | ||
} // namespace | ||
|
||
void SetupComponentContentsVerifier() { | ||
auto factory = base::BindRepeating(CreateExtensionsTreeHashContentsVerifier); | ||
SetContentsVerifierFactory(std::move(factory)); | ||
} | ||
|
||
} // namespace component_updater |
18 changes: 18 additions & 0 deletions
18
browser/component_updater/brave_component_contents_verifier.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// 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/. | ||
|
||
#ifndef BRAVE_BROWSER_COMPONENT_UPDATER_BRAVE_COMPONENT_CONTENTS_VERIFIER_H_ | ||
#define BRAVE_BROWSER_COMPONENT_UPDATER_BRAVE_COMPONENT_CONTENTS_VERIFIER_H_ | ||
|
||
namespace component_updater { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this filename doesn't make sense to me with the contents. The feature should be in features.h and this also seems like a strange filename for |
||
|
||
inline constexpr char kBypassComponentContentsVerifier[] = | ||
"bypass-component-contents-verifier"; | ||
|
||
void SetupComponentContentsVerifier(); | ||
|
||
} // namespace component_updater | ||
|
||
#endif // BRAVE_BROWSER_COMPONENT_UPDATER_BRAVE_COMPONENT_CONTENTS_VERIFIER_H_ |
14 changes: 14 additions & 0 deletions
14
chromium_src/chrome/browser/ui/startup/bad_flags_prompt.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// 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 "brave/browser/component_updater/brave_component_contents_verifier.h" | ||
#include "services/network/public/cpp/network_switches.h" | ||
|
||
#define kHostResolverRules \ | ||
kHostResolverRules, component_updater::kBypassComponentContentsVerifier | ||
|
||
#include "src/chrome/browser/ui/startup/bad_flags_prompt.cc" | ||
|
||
#undef kHostResolverRules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does it matter if extensions are enabled for adblock? It's not an extension
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it uses the chromium code which placed under this flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this have its own buildflag then and use that here?