Skip to content

Commit

Permalink
Implement navigator.plugins farbling
Browse files Browse the repository at this point in the history
  • Loading branch information
pilgrim-brave committed Jul 1, 2020
1 parent e80c245 commit 141bb28
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
9 changes: 7 additions & 2 deletions chromium_src/third_party/blink/renderer/core/dom/document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ WTF::String BraveSessionCache::GenerateRandomString(std::string seed,
wtf_size_t length) {
uint8_t key[32];
crypto::HMAC h(crypto::HMAC::SHA256);
CHECK(h.Init(reinterpret_cast<const unsigned char*>(&session_key_),
sizeof session_key_));
CHECK(h.Init(reinterpret_cast<const unsigned char*>(&domain_key_),
sizeof domain_key_));
CHECK(h.Sign(seed, key, sizeof key));
// initial PRNG seed based on session key and passed-in seed string
uint64_t v = *reinterpret_cast<uint64_t*>(key);
Expand All @@ -241,6 +241,11 @@ WTF::String BraveSessionCache::GenerateRandomString(std::string seed,
return value;
}

std::mt19937_64 BraveSessionCache::MakePseudoRandomGenerator() {
uint64_t seed = *reinterpret_cast<uint64_t*>(domain_key_);
return std::mt19937_64(seed);
}

} // namespace brave

#include "../../../../../../third_party/blink/renderer/core/dom/document.cc"
2 changes: 2 additions & 0 deletions chromium_src/third_party/blink/renderer/core/dom/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "../../../../../../../third_party/blink/renderer/core/dom/document.h"

#include <random>
#include "base/callback.h"

using blink::Document;
Expand Down Expand Up @@ -46,6 +47,7 @@ class CORE_EXPORT BraveSessionCache final
blink::LocalFrame* frame,
scoped_refptr<blink::StaticBitmapImage> image_bitmap);
WTF::String GenerateRandomString(std::string seed, wtf_size_t length);
std::mt19937_64 MakePseudoRandomGenerator();

private:
bool farbling_enabled_;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright (c) 2020 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 http://mozilla.org/MPL/2.0/. */

#include "brave/third_party/blink/renderer/brave_farbling_constants.h"
#include "third_party/blink/public/platform/web_content_settings_client.h"
#include <random>

#define BRAVE_DOM_PLUGINS_UPDATE_PLUGIN_DATA \
if (GetFrame() && GetFrame()->GetContentSettingsClient()) { \
switch (GetFrame()->GetContentSettingsClient()->GetBraveFarblingLevel()) { \
case BraveFarblingLevel::OFF: { \
break; \
} \
case BraveFarblingLevel::MAXIMUM: { \
dom_plugins_.clear(); \
U_FALLTHROUGH; \
} \
case BraveFarblingLevel::BALANCED: { \
for (unsigned index = 0; index < length(); index++) { \
dom_plugins_[index] = item(index); \
} \
auto* fake_plugin_info_1 = MakeGarbageCollected<PluginInfo>( \
brave::BraveSessionCache::From(*(GetFrame()->GetDocument())) \
.GenerateRandomString("PLUGIN_1_NAME", 8), \
brave::BraveSessionCache::From(*(GetFrame()->GetDocument())) \
.GenerateRandomString("PLUGIN_1_FILENAME", 16), \
brave::BraveSessionCache::From(*(GetFrame()->GetDocument())) \
.GenerateRandomString("PLUGIN_1_DESCRIPTION", 32), \
0, false); \
auto* fake_dom_plugin_1 = \
MakeGarbageCollected<DOMPlugin>(GetFrame(), *fake_plugin_info_1); \
dom_plugins_.push_back(fake_dom_plugin_1); \
auto* fake_plugin_info_2 = MakeGarbageCollected<PluginInfo>( \
brave::BraveSessionCache::From(*(GetFrame()->GetDocument())) \
.GenerateRandomString("PLUGIN_2_NAME", 7), \
brave::BraveSessionCache::From(*(GetFrame()->GetDocument())) \
.GenerateRandomString("PLUGIN_2_FILENAME", 15), \
brave::BraveSessionCache::From(*(GetFrame()->GetDocument())) \
.GenerateRandomString("PLUGIN_2_DESCRIPTION", 31), \
0, false); \
auto* fake_dom_plugin_2 = \
MakeGarbageCollected<DOMPlugin>(GetFrame(), *fake_plugin_info_2); \
dom_plugins_.push_back(fake_dom_plugin_2); \
std::mt19937_64 prng = \
brave::BraveSessionCache::From(*(GetFrame()->GetDocument())) \
.MakePseudoRandomGenerator(); \
std::shuffle(dom_plugins_.begin(), dom_plugins_.end(), prng); \
break; \
} \
default: \
NOTREACHED(); \
} \
}

#include "../../../../../../third_party/blink/renderer/modules/plugins/dom_plugin_array.cc"

#undef BRAVE_DOM_PLUGIN_ARRAY_GET_PLUGIN_DATA
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/third_party/blink/renderer/modules/plugins/dom_plugin_array.cc b/third_party/blink/renderer/modules/plugins/dom_plugin_array.cc
index 322e74880ddaa6c6c54f80f4f5f7ab0264631ffd..0c13152cb2f9ebd5bbf86955f50eab6909f05443 100644
--- a/third_party/blink/renderer/modules/plugins/dom_plugin_array.cc
+++ b/third_party/blink/renderer/modules/plugins/dom_plugin_array.cc
@@ -146,6 +146,7 @@ void DOMPluginArray::UpdatePluginData() {
}
}
}
+ BRAVE_DOM_PLUGINS_UPDATE_PLUGIN_DATA
}

void DOMPluginArray::ContextDestroyed() {

0 comments on commit 141bb28

Please sign in to comment.