LibWeb: Implement basic high resolution time coarsening

Several interfaces that return a high resolution time require that
time to be coarsened, in order to prevent timing attacks. This
implementation simply reduces the resolution of the returned timestamp
to the minimum values given in the specification. Further work may be
needed to make our implementation more robust to the kind of attacks
that this mechanism is designed to prevent.
This commit is contained in:
Tim Ledbetter 2025-01-28 10:47:32 +00:00 committed by Alexander Kalenik
commit 39445d6dd6
Notes: github-actions[bot] 2025-01-30 17:39:14 +00:00
7 changed files with 100 additions and 5 deletions

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Math.h>
#include <AK/Time.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
@ -46,8 +47,19 @@ DOMHighResTimeStamp get_time_origin_timestamp(JS::Object const& global)
// https://w3c.github.io/hr-time/#dfn-coarsen-time
DOMHighResTimeStamp coarsen_time(DOMHighResTimeStamp timestamp, bool cross_origin_isolated_capability)
{
// FIXME: Implement this.
(void)cross_origin_isolated_capability;
// 1. Let time resolution be 100 microseconds, or a higher implementation-defined value.
auto time_resolution_milliseconds = 0.1;
// 2. If crossOriginIsolatedCapability is true, set time resolution to be 5 microseconds, or a higher implementation-defined value.
if (cross_origin_isolated_capability)
time_resolution_milliseconds = 0.005;
// 3. In an implementation-defined manner, coarsen and potentially jitter timestamp such that its resolution will not exceed time resolution
timestamp = floor(timestamp / time_resolution_milliseconds) * time_resolution_milliseconds;
// FIXME: Applying jitter to the coarsened timestamp here may decrease our susceptibility to timing attacks.
// 4. Return timestamp as a moment
return timestamp;
}