Skip to main content
Be Bitwise

Side-Channel Attacks on the Web

Web Application Exploitation (Low-Level Perspective) · Advanced Exploitation track

9 min read

In this module

Information Leaks Without Direct Access

A side-channel attack extracts information not by breaking a system's logic, but by watching how it behaves: how long it takes to respond, what it caches, how it handles errors. In web applications, this is especially inconvenient — these attacks can be launched remotely, often from nothing more than a browser tab.

A buffer overflow hands the attacker direct memory access. A side-channel attack is quieter; it measures some observable property — typically timing — and infers secrets from the statistics. The system is operating correctly from a logical standpoint. It's just inadvertently narrating its internal state.

Timing Attacks on Web Applications

The most practical side-channel attack against web applications is the timing attack. The core idea is simple: if the time a server takes to process a request varies depending on secret data, an attacker can deduce the secret by measuring response times.

The classic example is string comparison. Consider how a server might verify an API token:

c
/* Vulnerable: early-exit comparison */
int verify_token(const char *provided, const char *expected) {
for (int i = 0; i < TOKEN_LENGTH; i++) {
if (provided[i] != expected[i]) {
return 0; /* Mismatch - return immediately */
}
}
return 1; /* All bytes match */
}

This function returns as soon as it finds a mismatched byte. If the first byte is wrong, it returns almost immediately. If the first byte is correct but the second is wrong, it takes slightly longer. An attacker can exploit this:

  1. Try all 256 possible values for the first byte, measuring response time for each
  2. The value that produces the longest response time is correct (it matched, so the comparison continued to the second byte)
  3. Fix the first byte and repeat for the second byte
  4. Continue until the entire token is recovered
Constant-Time Comparison

A constant-time comparison always examines every byte of both strings, regardless of where mismatches occur. It uses bitwise operations to accumulate a difference value without branching. This prevents timing side channels because the execution time is the same whether the strings match on zero bytes or all bytes.

The defence is a constant-time comparison function that always processes all bytes:

c
int verify_token_safe(const char *provided, const char *expected) {
int result = 0;
for (int i = 0; i < TOKEN_LENGTH; i++) {
result |= provided[i] ^ expected[i];
}
return result == 0;
}

This function XORs every byte pair and ORs the result into an accumulator. It always processes all bytes, taking the same amount of time regardless of how many bytes match.

Timing Attacks in Practice

You might wonder whether network jitter would make timing differences too small to measure. In local network conditions, the difference between matching one byte and matching two bytes might be just a few nanoseconds - far less than typical network latency variation of milliseconds.

The key insight is statistical amplification. By sending thousands or millions of requests for each candidate byte value and averaging the response times, the attacker can reduce noise to reveal the underlying timing difference. With enough samples, even nanosecond differences become statistically significant.

Researchers have demonstrated successful timing attacks over the public internet, not just local networks. Crosby, Wallach and Riedi ("Opportunities and Limits of Remote Timing Attacks", ACM TISSEC, 2009) found that with careful filtering of samples an attacker could reliably tell apart processing differences of tens of microseconds across the internet, and of a few hundred nanoseconds or less on a LAN. Differences smaller than that need more measurements, often hundreds of thousands of requests, but the principle works because the timing difference is systematic while network jitter is random.

Spectre in Web Browsers

The Spectre vulnerability, disclosed in 2018, brought CPU-level side channels into the web security world. Spectre exploits speculative execution - a CPU optimisation where the processor guesses which branch will be taken and executes instructions ahead of time, rolling back if the guess was wrong.

The problem is that speculative execution leaves traces in the CPU cache. Even though the results are architecturally rolled back, the cache changes persist. An attacker can observe these cache changes to determine what data was accessed speculatively.

In a web browser, this is particularly dangerous because JavaScript from one website runs in the same process as JavaScript from other websites (or at least, it did before mitigations were deployed). A malicious page could use Spectre to read memory belonging to other origins.

The attack works in three phases:

  1. Mistrain the branch predictor: The attacker executes a pattern of branches to make the CPU predict that a bounds check will pass
  2. Trigger speculative access: The attacker provides an out-of-bounds index. The CPU speculatively reads the secret byte at the out-of-bounds location before the bounds check retires
  3. Observe the cache: The speculative read was used to index into an array, bringing a specific cache line into the cache. The attacker probes which cache line was loaded, revealing the secret byte's value
c
// Vulnerable pattern (simplified):
if (index < array_length) { // Branch predictor: "usually true"
value = array[index]; // Speculative: reads secret byte
temp = probe_array[value * 4096]; // Speculative: loads cache line
}
// Even after rollback, the cache line for probe_array[secret * 4096]
// is still in the cache. Timing which line is cached reveals 'secret'.

The 4096-byte stride puts each candidate value on its own page, so the hardware prefetcher (which doesn't cross page boundaries) can't pull in neighbouring lines and blur the result. It's the same layout the speculative execution lesson uses.

To make this work in JavaScript, the attacker needs a high-resolution timer to measure cache access times. Browsers initially provided performance.now() with microsecond resolution, which was sufficient. Browser vendors responded by coarsening the timer (the exact figure differs by browser and by whether the page is cross-origin isolated, from around 5 microseconds up to 100 microseconds or more) and adding jitter, but researchers demonstrated that alternative timing sources (SharedArrayBuffer used as a counter, etc.) could substitute.

Browser Mitigations for Spectre

The browser industry deployed several layers of defence:

  • Site isolation: Each origin gets its own operating system process. This means a Spectre attack in one process cannot read memory belonging to a different origin's process.
  • Cross-Origin Read Blocking (CORB), now Opaque Response Blocking (ORB): Prevents cross-origin responses (HTML, JSON, XML) from entering the renderer process of the requesting origin. If the data never enters the process, Spectre cannot read it. ORB is the successor specified in the Fetch standard: it allowlists the response types a no-cors request may legitimately load and blocks the rest, and Chrome and Firefox have been replacing CORB with it since 2022-2023.
  • Timer degradation: Reducing the resolution of performance.now() and similar timing APIs makes cache timing measurements harder.
  • SharedArrayBuffer restrictions: SharedArrayBuffer can be used as a high-resolution timer by having a web worker increment a counter in shared memory. Browsers disabled SharedArrayBuffer by default and re-enabled it only when cross-origin isolation headers are present.

These mitigations are defence in depth. Site isolation is the strongest defence because it prevents cross-origin data from being in the same address space. The timer mitigations raise the cost of the attack but do not eliminate it.

Cache-Based Side Channels

Beyond Spectre, caches create side channels at many levels. In web applications, caching behaviour can leak information:

HTTP cache timing: If a resource is in the browser cache, loading it is faster than fetching it from the network. A malicious page can measure whether a cross-origin resource is cached, revealing whether the user has visited a particular site or loaded a particular resource. This is a privacy leak.

DNS cache timing: Similarly, measuring DNS resolution time reveals whether a domain name is in the DNS cache, indicating recent visits to that domain.

Application-level caching: If a web application caches database query results, the response time for a cached query is shorter than an uncached one. An attacker who can craft queries that may or may not hit the cache can infer information about what other users have queried.

Cache Timing Side Channel

A cache timing side channel exploits the fact that accessing data already in a cache is faster than accessing data not in the cache. By measuring access times, an attacker can determine what data has been recently accessed. This applies at every level: CPU L1/L2/L3 caches, browser HTTP caches, DNS caches, and application-level caches.

Error-Based Side Channels

Web applications sometimes leak information through error handling. Different error types may produce different HTTP status codes, different response sizes, or different response times:

  • A login form that says "invalid username" vs "invalid password" reveals which usernames exist
  • An API that returns 403 (forbidden) vs 404 (not found) reveals whether a resource exists even if the user cannot access it
  • A search endpoint that takes longer for partial matches than no matches reveals whether specific data exists in the database

These are not memory-level side channels, but they follow the same principle: observable differences in behaviour leak information that should be secret. The defence is to make all error responses indistinguishable: same status code, same response size, same timing.

The Broader Lesson

Side-channel attacks demonstrate that security isn't reducible to logical correctness. A system can produce perfectly correct outputs and still leak secrets through its timing, the resources it touches, or the errors it generates. Defending against this requires thinking about every observable property of the system — not just the data that comes out.