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,14 @@
bufferedMessages.push(message);
}
function synchronousWaitMicroseconds(microseconds) {
var start = performance.now() * 1000,
now = start;
while (now - start < microseconds) {
now = performance.now() * 1000;
}
}
const globalObserver = new PerformanceObserver((list, observer) => {
printlnBuffered(`observer === globalObserver: ${observer === globalObserver}`);
printlnBuffered(
@ -53,7 +61,11 @@
globalObserver.observe({ entryTypes: ["measure", "mark"] });
const startMark = performance.mark("start");
// The resolution of the clock used by the Performance interface is 100 microseconds, so we wait twice that time
// between calls to ensure they are ordered as we expect.
synchronousWaitMicroseconds(200);
const endMark = performance.mark("end");
synchronousWaitMicroseconds(200);
const measureMark = performance.measure("measure", "start", "end");
function printCatchedException(func) {