diff --git a/browser/about_flags.cc b/browser/about_flags.cc index fb2fee807e08..7aec034337c4 100644 --- a/browser/about_flags.cc +++ b/browser/about_flags.cc @@ -205,6 +205,11 @@ constexpr char kBraveTorWindowsHttpsOnlyDescription[] = "Prevents Private Windows with Tor from making any insecure HTTP " "connections without warning the user first."; +constexpr char kBraveRoundTimeStampsName[] = "Round time stamps"; +constexpr char kBraveRoundTimeStampsDescription[] = + "Prevents JavaScript from getting access to high-resolution clocks by " + "rounding all DOMHighResTimeStamps to the nearest millisecond."; + constexpr char kBraveSpeedreaderName[] = "Enable SpeedReader"; constexpr char kBraveSpeedreaderDescription[] = "Enables faster loading of simplified article-style web pages."; @@ -717,6 +722,11 @@ constexpr char kBraveBackgroundVideoPlaybackDescription[] = flag_descriptions::kBraveTorWindowsHttpsOnlyDescription, \ kOsAll, FEATURE_VALUE_TYPE( \ blink::features::kBraveTorWindowsHttpsOnly)}, \ + {"brave-round-time-stamps", \ + flag_descriptions::kBraveRoundTimeStampsName, \ + flag_descriptions::kBraveRoundTimeStampsDescription, \ + kOsAll, FEATURE_VALUE_TYPE( \ + blink::features::kBraveRoundTimeStamps)}, \ BRAVE_IPFS_FEATURE_ENTRIES \ BRAVE_NATIVE_WALLET_FEATURE_ENTRIES \ BRAVE_NEWS_FEATURE_ENTRIES \ diff --git a/browser/brave_shields/brave_timestamp_rounding_browsertest.cc b/browser/brave_shields/brave_timestamp_rounding_browsertest.cc new file mode 100644 index 000000000000..430c53cfeaa6 --- /dev/null +++ b/browser/brave_shields/brave_timestamp_rounding_browsertest.cc @@ -0,0 +1,72 @@ +/* Copyright (c) 2022 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 "base/test/scoped_feature_list.h" +#include "base/test/task_environment.h" +#include "base/time/time.h" +#include "content/public/renderer/render_frame.h" +#include "content/public/test/render_view_test.h" +#include "third_party/blink/public/common/features.h" + +using blink::features::kBraveRoundTimeStamps; + +class BraveTimeStampRoundingRenderViewTest + : public content::RenderViewTest, + public ::testing::WithParamInterface { + public: + BraveTimeStampRoundingRenderViewTest() {} + + ~BraveTimeStampRoundingRenderViewTest() override = default; + + bool IsBraveRoundTimeStampsEnabled() { return GetParam(); } + + void SetUp() override { + if (IsBraveRoundTimeStampsEnabled()) { + scoped_feature_list_.InitAndEnableFeature(kBraveRoundTimeStamps); + } else { + scoped_feature_list_.InitAndDisableFeature(kBraveRoundTimeStamps); + } + RenderViewTest::SetUp(); + } + + double ExecuteJSAndReturnDouble(const std::u16string& script) { + double result; + EXPECT_TRUE(ExecuteJavaScriptAndReturnNumberValue(script, &result)); + return result; + } + + void CheckRounded(const std::u16string& script, bool expect_rounded) { + double result = ExecuteJSAndReturnDouble(script); + if (expect_rounded) { + EXPECT_DOUBLE_EQ(round(result) - result, 0); + } else { + EXPECT_NE(round(result) - result, 0); + } + } + + void Advance100Microseconds() { + task_environment_.AdvanceClock(base::Microseconds(100)); + } + + protected: + base::test::ScopedFeatureList scoped_feature_list_; +}; + +TEST_P(BraveTimeStampRoundingRenderViewTest, SynchronousApisRounded) { + bool expect_rounded = IsBraveRoundTimeStampsEnabled(); + LoadHTML("hi"); + Advance100Microseconds(); + CheckRounded(u"performance.now()", expect_rounded); + Advance100Microseconds(); + CheckRounded(u"performance.mark('test').startTime", expect_rounded); + Advance100Microseconds(); + if (expect_rounded) { + CheckRounded(u"performance.timeOrigin", true); + } +} + +INSTANTIATE_TEST_SUITE_P(BraveTimeStampRoundingRenderViewTest, + BraveTimeStampRoundingRenderViewTest, + ::testing::Bool()); diff --git a/chromium_src/third_party/blink/common/features.cc b/chromium_src/third_party/blink/common/features.cc index 5d7ccdd5067d..233e827a55d6 100644 --- a/chromium_src/third_party/blink/common/features.cc +++ b/chromium_src/third_party/blink/common/features.cc @@ -54,13 +54,17 @@ const base::Feature kPartitionBlinkMemoryCache{ const base::Feature kRestrictWebSocketsPool{"RestrictWebSocketsPool", base::FEATURE_ENABLED_BY_DEFAULT}; -// Disable protection against fingerprinting on screen dimensions by default. +// Enables protection against fingerprinting on screen dimensions. const base::Feature kBraveBlockScreenFingerprinting{ "BraveBlockScreenFingerprinting", base::FEATURE_DISABLED_BY_DEFAULT}; -// Enable HTTPS-Only Mode in Private Windows with Tor by default. +// Enables HTTPS-Only Mode in Private Windows with Tor by default. const base::Feature kBraveTorWindowsHttpsOnly{"BraveTorWindowsHttpsOnly", base::FEATURE_ENABLED_BY_DEFAULT}; +// Enables protection against fingerprinting via high-resolution time stamps. +const base::Feature kBraveRoundTimeStamps{"BraveRoundTimeStamps", + base::FEATURE_ENABLED_BY_DEFAULT}; + } // namespace features } // namespace blink diff --git a/chromium_src/third_party/blink/public/common/features.h b/chromium_src/third_party/blink/public/common/features.h index 6698648a474e..19e55a610360 100644 --- a/chromium_src/third_party/blink/public/common/features.h +++ b/chromium_src/third_party/blink/public/common/features.h @@ -18,6 +18,7 @@ BLINK_COMMON_EXPORT extern const base::Feature kPartitionBlinkMemoryCache; BLINK_COMMON_EXPORT extern const base::Feature kRestrictWebSocketsPool; BLINK_COMMON_EXPORT extern const base::Feature kBraveBlockScreenFingerprinting; BLINK_COMMON_EXPORT extern const base::Feature kBraveTorWindowsHttpsOnly; +BLINK_COMMON_EXPORT extern const base::Feature kBraveRoundTimeStamps; } // namespace features } // namespace blink diff --git a/chromium_src/third_party/blink/renderer/core/loader/document_load_timing.cc b/chromium_src/third_party/blink/renderer/core/loader/document_load_timing.cc new file mode 100644 index 000000000000..6a2dfc6aeb8d --- /dev/null +++ b/chromium_src/third_party/blink/renderer/core/loader/document_load_timing.cc @@ -0,0 +1,25 @@ +/* Copyright (c) 2022 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 "third_party/blink/renderer/core/loader/document_load_timing.h" + +#define MonotonicTimeToZeroBasedDocumentTime \ + MonotonicTimeToZeroBasedDocumentTime_ChromiumImpl + +#include "src/third_party/blink/renderer/core/loader/document_load_timing.cc" + +#undef MonotonicTimeToZeroBasedDocumentTime + +#include "third_party/blink/renderer/core/timing/time_clamper.h" + +namespace blink { + +base::TimeDelta DocumentLoadTiming::MonotonicTimeToZeroBasedDocumentTime( + base::TimeTicks monotonic_time) const { + return TimeClamper::MaybeRoundTimeDelta( + MonotonicTimeToZeroBasedDocumentTime_ChromiumImpl(monotonic_time)); +} + +} // namespace blink diff --git a/chromium_src/third_party/blink/renderer/core/loader/document_load_timing.h b/chromium_src/third_party/blink/renderer/core/loader/document_load_timing.h new file mode 100644 index 000000000000..7de7b4a22f80 --- /dev/null +++ b/chromium_src/third_party/blink/renderer/core/loader/document_load_timing.h @@ -0,0 +1,17 @@ +/* Copyright (c) 2022 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/. */ + +#ifndef BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_DOCUMENT_LOAD_TIMING_H_ +#define BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_DOCUMENT_LOAD_TIMING_H_ + +#define MonotonicTimeToZeroBasedDocumentTime \ + MonotonicTimeToZeroBasedDocumentTime(base::TimeTicks) const; \ + base::TimeDelta MonotonicTimeToZeroBasedDocumentTime_ChromiumImpl + +#include "src/third_party/blink/renderer/core/loader/document_load_timing.h" + +#undef MonotonicTimeToZeroBasedDocumentTime + +#endif // BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_DOCUMENT_LOAD_TIMING_H_ diff --git a/chromium_src/third_party/blink/renderer/core/timing/time_clamper.cc b/chromium_src/third_party/blink/renderer/core/timing/time_clamper.cc new file mode 100644 index 000000000000..9ba60248bec2 --- /dev/null +++ b/chromium_src/third_party/blink/renderer/core/timing/time_clamper.cc @@ -0,0 +1,56 @@ +/* Copyright (c) 2022 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 "third_party/blink/renderer/core/timing/time_clamper.h" + +#define kFineResolutionMicroseconds FineResolutionMicroseconds() +#define kCoarseResolutionMicroseconds CoarseResolutionMicroseconds() + +#include "src/third_party/blink/renderer/core/timing/time_clamper.cc" + +#undef FineResolutionMicroseconds +#undef CoarseResolutionMicroseconds + +#include "base/feature_list.h" +#include "base/time/time.h" +#include "third_party/blink/public/common/features.h" + +namespace blink { + +namespace { + +constexpr int kBraveTimerResolutionMicroseconds = 1000; + +bool ShouldRound() { + return base::FeatureList::IsEnabled(features::kBraveRoundTimeStamps); +} + +} // namespace + +// static +double TimeClamper::MaybeRoundMilliseconds(double value) { + return ShouldRound() ? round(value) : value; +} + +// static +base::TimeDelta TimeClamper::MaybeRoundTimeDelta(base::TimeDelta value) { + return ShouldRound() ? value.RoundToMultiple(base::Microseconds( + kBraveTimerResolutionMicroseconds)) + : value; +} + +// static +int TimeClamper::FineResolutionMicroseconds() { + return ShouldRound() ? kBraveTimerResolutionMicroseconds + : kFineResolutionMicroseconds_ChromiumImpl; +} + +// static +int TimeClamper::CoarseResolutionMicroseconds() { + return ShouldRound() ? kBraveTimerResolutionMicroseconds + : kCoarseResolutionMicroseconds_ChromiumImpl; +} + +} // namespace blink diff --git a/chromium_src/third_party/blink/renderer/core/timing/time_clamper.h b/chromium_src/third_party/blink/renderer/core/timing/time_clamper.h new file mode 100644 index 000000000000..21260661f255 --- /dev/null +++ b/chromium_src/third_party/blink/renderer/core/timing/time_clamper.h @@ -0,0 +1,28 @@ +/* Copyright (c) 2022 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/. */ + +#ifndef BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_TIMING_TIME_CLAMPER_H_ +#define BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_TIMING_TIME_CLAMPER_H_ + +#include "base/time/time.h" + +// Any future usages of kCoarseResolutionMicroseconds or +// kFineResolutionMicroseconds won't compile and will need to be fixed. +#define kCoarseResolutionMicroseconds kCoarseResolutionMicroseconds_ChromiumImpl + +#define kFineResolutionMicroseconds \ + kFineResolutionMicroseconds_ChromiumImpl = 5; \ + static int CoarseResolutionMicroseconds(); \ + static int FineResolutionMicroseconds(); \ + static double MaybeRoundMilliseconds(double value); \ + static base::TimeDelta MaybeRoundTimeDelta(base::TimeDelta value); \ + int dummy + +#include "src/third_party/blink/renderer/core/timing/time_clamper.h" + +#undef kCoarseResolutionMicroseconds +#undef kFineResolutionMicroseconds + +#endif // BRAVE_CHROMIUM_SRC_THIRD_PARTY_BLINK_RENDERER_CORE_TIMING_TIME_CLAMPER_H_ diff --git a/chromium_src/third_party/blink/renderer/modules/video_rvfc/video_frame_callback_requester_impl.cc b/chromium_src/third_party/blink/renderer/modules/video_rvfc/video_frame_callback_requester_impl.cc new file mode 100644 index 000000000000..79c50ead4221 --- /dev/null +++ b/chromium_src/third_party/blink/renderer/modules/video_rvfc/video_frame_callback_requester_impl.cc @@ -0,0 +1,20 @@ +/* Copyright (c) 2022 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 "third_party/blink/renderer/modules/video_rvfc/video_frame_callback_requester_impl.h" + +#define kCoarseResolution kCoarseResolution [[maybe_unused]] + +#define FloorToMultiple(X) \ + FloorToMultiple( \ + base::Microseconds(TimeClamper::CoarseResolutionMicroseconds())) + +#define static_assert(...) static_assert(true) + +#include "src/third_party/blink/renderer/modules/video_rvfc/video_frame_callback_requester_impl.cc" + +#undef kCoarseResolution +#undef FloorToMultiple +#undef static_assert diff --git a/test/BUILD.gn b/test/BUILD.gn index 7ce6980e3639..1a1779a534ce 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -724,6 +724,7 @@ test("brave_browser_tests") { "//brave/browser/brave_shields/ad_block_service_browsertest.cc", "//brave/browser/brave_shields/ad_block_service_browsertest.h", "//brave/browser/brave_shields/brave_shields_web_contents_observer_browsertest.cc", + "//brave/browser/brave_shields/brave_timestamp_rounding_browsertest.cc", "//brave/browser/brave_shields/cookie_expiry_browsertest.cc", "//brave/browser/brave_shields/cookie_pref_service_browsertest.cc", "//brave/browser/brave_shields/domain_block_page_browsertest.cc", diff --git a/test/filters/browser_tests-linux.filter b/test/filters/browser_tests-linux.filter index 8b8e4f38d7fc..7899eabe361c 100644 --- a/test/filters/browser_tests-linux.filter +++ b/test/filters/browser_tests-linux.filter @@ -17,6 +17,7 @@ -ExternalProtocolHandlerSandboxBrowserTest.* -IndividualNetworkContextsPrefetchProxyBrowserTest.* -LayoutInstabilityTest.* +-MetricIntegrationTest.FirstInputDelay -MultiscreenWindowPlacementPermissionContextTest.IsExtendedCrossOriginAllow -MultiscreenWindowPlacementPermissionContextTest.IsExtendedCrossOriginDeny -MultiscreenWindowPlacementPermissionContextTest.IsExtendedSameOriginAllow @@ -24,6 +25,7 @@ -NewTabUIProcessPerTabTest.* -OzonePlatformTest.* -PolicyTestPrefetchProxyBrowserTest.* +-PolicyTestSetTimeoutWithout1MsClamp.DisablePolicy -PPAPINaClPNaClNonSfiTest.* -PrefetchProxyBrowserTest.* -PrefetchProxyWithDecoyRequestsBrowserTest.*