Canvas Fingerprinting: How a Few Lines of JavaScript Recognise Your Browser Without Cookies
Clearing your cookies or opening a private window doesn't make you a stranger to every website. One reason is canvas fingerprinting: a page draws a small, invisible image and reads the pixels back. The exact result depen
Clearing your cookies or opening a private window doesn't make you a stranger to every website. One reason is canvas fingerprinting: a page draws a small, invisible image and reads the pixels back. The exact result depends on your hardware and software, and it is stable enough to recognise the same browser again, without storing anything on your device.
This post shows the code, explains why the pixels differ from one device to another, and covers what browsers do about it.
Disclosure: I work on GhostSMS, which runs the free fingerprint test mentioned at the end.
The code
async function canvasFingerprint() {
const canvas = document.createElement('canvas');
canvas.width = 240;
canvas.height = 60;
const ctx = canvas.getContext('2d');
ctx.textBaseline = 'top';
ctx.font = '16px Arial';
ctx.fillStyle = '#f60';
ctx.fillRect(100, 1, 62, 20);
ctx.fillStyle = '#069';
ctx.fillText('Cwm fjordbank glyphs vext quiz π', 2, 15);
ctx.fillStyle = 'rgba(102, 204, 0, 0.7)';
ctx.fillText('Cwm fjordbank glyphs vext quiz π', 4, 17);
// Read the pixels back and hash them
const bytes = new TextEncoder().encode(canvas.toDataURL());
const digest = await crypto.subtle.digest('SHA-256', bytes);
return [...new Uint8Array(digest)]
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
.slice(0, 16);
}
canvasFingerprint().then(console.log); // e.g. "3f9c0a7e51d2b84c"
The canvas is never added to the page, so nothing appears on screen, and most browsers show no prompt. (crypto.subtle needs HTTPS or localhost.) Run it in two different browsers or on two different computers and you will usually get two different values. Run it again in the same browser and you will usually get the same one.
Why the pixels differ
Drawing text and a rectangle sounds deterministic, but the final pixels pass through a lot of machinery:
- Font rendering. Operating systems rasterise and smooth text differently, and which font actually draws "Arial" or the emoji depends on what is installed.
- Graphics hardware and drivers. When the canvas is GPU-accelerated, small differences in how a GPU and its driver blend colours and smooth edges end up in the pixels.
- The browser itself. Different engines, and different versions of the same engine, can draw the same instructions slightly differently.
The semi-transparent second line and the colour emoji are there on purpose: blending and emoji rendering make those differences easier to see.
How identifying is it?
A canvas hash on its own doesn't name anyone. People with the same phone model, system version and browser tend to get the same hash. Desktops vary more, because of different GPUs, drivers, screens and fonts.
Its power comes from combination. A script adds the canvas hash to other signals such as WebGL, audio processing, installed fonts, screen size, time zone and languages. Each one narrows the crowd, and together they are often enough to recognise one browser among many. Here is how a page reads your graphics card's name through WebGL:
const gl = document.createElement('canvas').getContext('webgl');
const info = gl && gl.getExtension('WEBGL_debug_renderer_info');
console.log(info ? gl.getParameter(info.UNMASKED_RENDERER_WEBGL) : 'not available');
Depending on the browser, this returns your exact graphics card or a deliberately vague name.
Canvas fingerprinting was first described by Mowery and Shacham in 2012 ("Pixel Perfect"). Two years later, the study "The Web Never Forgets" (Acar et al., CCS 2014) found canvas fingerprinting scripts on more than 5% of the home pages of the 100,000 most popular websites.
How browsers fight back
There are two strategies: make every user look the same, or make every reading look different.
- Tor Browser doesn't give pages the real canvas pixels unless you allow it; by default a page gets placeholder data instead. Mullvad Browser, made by the Tor Project with Mullvad, is likewise designed so that its users look alike.
- Brave adds small random noise ("farbling") to canvas and audio readouts, among other values. The noise is keyed to the site and the session, so the same browser shows a different hash on each site and in each new session.
-
Firefox blocks known fingerprinting scripts by default and adds stronger protections in private windows, in Strict mode and behind the
privacy.resistFingerprintingsetting. - Safari's advanced fingerprinting protection adds noise to canvas and audio readouts. It was introduced for Private Browsing.
Noise isn't a complete fix: researchers have shown that averaging many readings can strip some of it away.
What doesn't help: clearing cookies (nothing is stored), private mode on its own, a VPN on its own (it changes your IP address, not your pixels) and extensions that fake a single value, which often make the overall combination rarer.
If you build for the web
Fingerprinting also powers fraud and bot detection, which has legitimate uses. If you add it to a product:
- Say so in your privacy policy. In the EU and the UK, regulators have said that fingerprinting for tracking generally needs the same consent as cookies.
- Start with signals you already have on the server, such as rate limits and account history, before reading pixels from your users' devices.
- Keep raw values to yourself. A short hash is enough to answer "have I seen this device before?"
Test your own browser
We built a free browser fingerprint test that shows 21 components of your fingerprint, including canvas, WebGL, audio and installed fonts, and rates how identifying each one is. It runs only in your browser and sends nothing to our server. It also gives you a short comparison code that contains only hashes, so you can check whether two browsers, or a normal and a private window, look like the same device. For the bigger picture, see Browser Fingerprinting Explained.
If you also want to keep your phone number out of sign-up forms, that is what GhostSMS does: temporary numbers for one-time SMS codes, no KYC, paid with Monero or other crypto. ghostsms.org
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.