# How a spoofed API is detected with an instanceof check

> A spoofed getBattery that returns an object literal fails instanceof, prints the wrong toString tag and carries own keys a real BatteryManager lacks.

Source: https://mosaicbrowser.com/blog/platform-objects-not-literals

## Return the platform's own object, never a stand-in

A spoofed getBattery that returns an object literal fails instanceof, prints the wrong toString tag and carries own keys a real BatteryManager lacks.

![3: Three tells in one object literal, beside a screenshot: a profile fingerprint in Mosaic: GPU, screen, fonts, timezone and language matched to the proxy’s IP](https://mosaicbrowser.com/covers/platform-objects-not-literals.webp)

A spoofed browser API is caught by an `instanceof` check when the spoof hands back an object it built itself instead of the one the browser makes. Ask a real Chrome for `navigator.getBattery()` and the promise resolves with a `BatteryManager`; ask a script that returns `{ charging: true, level: 0.8 }` and `result instanceof BatteryManager` is `false`. We shipped four of these in our JavaScript fallback layer, and every one of them failed three checks at once.

## How a spoofed API is detected: instanceof and two more checks

A page does not need a fingerprinting library to run these. It needs three lines:

```
Object.prototype.toString.call(x)   // "[object BatteryManager]" or "[object Object]"
x instanceof BatteryManager          // true or false
Object.keys(x)                       // [] or ["charging", "level", ...]

```

The first reads the object's tag, which a real platform object carries on its prototype. The second walks the prototype chain. The third lists own enumerable properties, and this is the one people forget: on a real `BatteryManager`, `charging` and `level` are getters on `BatteryManager.prototype`, so the object itself has no own keys at all. A literal has every value as its own key, because that is what a literal is.

You can work your way round the first two with more overrides. Set the prototype and add a `Symbol.toStringTag` and the tag and `instanceof` come out right. The keys are still own properties, though, and the getters a page can pull off the prototype with `Object.getOwnPropertyDescriptor` still belong to the real class and still refuse your object. Each fix to a stand-in uncovers the next difference, because the stand-in is a different object.

Mosaic is a desktop app

Email yourself the link and open it on your computer.

[How it works: Machines that exist](https://mosaicbrowser.com/features/real-fingerprints)

## Four APIs that handed back a stand-in

Our engine moves these values inside the browser itself. Beside it sits a script that does the same job by injection, for a stock Chromium without our changes. When we audited it, four APIs were returning objects we had made ourselves:

- **`getClientRects`** returned a plain `Array` with an arrow function called `item` hung on it. `toString` said `[object Array]` where every browser says `[object DOMRectList]`, `instanceof DOMRectList` was false, and the array had an own enumerable `item` that no `DOMRectList` has.
- **`getBattery`** resolved with a literal carrying `charging`, `level`, the two times, three no-op event methods and four `null` handlers: eleven own keys against the real object's none. `toString` said `[object Object]`.
- **`enumerateDevices`** built `{ deviceId, kind, label, groupId, toJSON }` objects instead of `MediaDeviceInfo` (and `InputDeviceInfo` for inputs). Four own enumerable keys that no real entry has, on a call that fingerprinting scripts commonly make.
- **`navigator.plugins`** rebuilt `Plugin` objects with no `MimeType` entries in them, so a page walking `plugin[i]` read `undefined` for every field.

None of these values was wrong as a number. The battery level was plausible and the device list had the right kinds. A detector did not have to know what the true values were. It only had to notice that the object was not the kind of object Chrome makes.

## What replaced each one

The rule we wrote down afterwards: **if the engine already produces the object, change what it says, not what it is.** In practice that came to three moves.

**Override the prototype accessor.** The four battery numbers are now getters defined on `BatteryManager.prototype`. `getBattery()` resolves with the engine's own object, so its class, its `EventTarget` behaviour and its empty key list are Chrome's, and only `charging`, `level`, `chargingTime` and `dischargingTime` read differently. Where a build does not expose `BatteryManager` at all, there is nothing safe to override and the real API is left alone.

**Proxy the real instance.** `getClientRects` now returns a `Proxy` whose target is the real `DOMRectList`. The class, brand and property shape come from the target. Only the rectangles read out of it are the adjusted ones. One detail cost us a broken page before we found it: `Reflect.get` has to pass the **target** as the receiver, not the proxy. `length` and the indexed getters are brand-checked, and handed a proxy as `this` they throw `Illegal invocation` on every element of the page.

**Use the real list, or delete the spoof.** `enumerateDevices` first went to filtering the engine's own list, because before a permission is granted Chrome already returns one unlabelled placeholder per kind and there is nothing to hide. Then the filter went too. It could only ever remove a kind, so its one effect was to drop the camera from the list on a draw that claimed a cameraless desktop, while `getUserMedia` still opened the real one. Our stock-Chrome comparison caught it, intermittently, since it showed only on some draws:

```
stock  audioinput,videoinput,audiooutput
ours   audioinput,audiooutput

```

The list is the engine's, untouched, now. The `plugins` rebuild was removed outright: Chrome's real list is already the five PDF viewer entries we were imitating, which is the story in [why every Chrome lists the same five PDF plugins](https://mosaicbrowser.com/blog/five-pdf-plugins).

## Why a Proxy is not the end of it

A `Proxy` keeps the shape, but the functions you install still have to look native when a page prints them, and they have to look native from a second realm too. A fresh iframe has its own `Function.prototype.toString`, and on one profile it read the real source of 33 of our replaced functions. That measurement, and the fix of making each replacement a `Proxy` over the real native, is in [why we change fingerprints inside the engine, not JavaScript](https://mosaicbrowser.com/blog/cpp-not-javascript). The four-line version of the iframe check a page runs is in [how a page checks that a function is really native](https://mosaicbrowser.com/blog/native-code-tostring-check).

The same reasoning sits under [half a spoof](https://mosaicbrowser.com/blog/half-a-spoof), where we cut spoofs that changed one route to a value and left another. A stand-in object is a contradiction of the same kind. The value says one machine and the object's class says no browser at all.

## Where this leaves our engine

Tesseract Chrome, the Chromium that ships in Mosaic, runs no injected fingerprint script. Its changes set values inside the engine, where `getBattery` builds its `BatteryManager` and Blink builds its `DOMRectList`, so every frame and worker gets the engine's own object carrying the profile's numbers. The JavaScript fallback is where we learned the rule, and the engine is where we could follow it all the way down: there is no stand-in to find, because nothing above the engine builds one.

## Questions

### How does instanceof detect a spoofed browser API?

A real getBattery resolves with a BatteryManager, so the result instanceof BatteryManager is true. A spoof that resolves with a plain object literal answers false, because the literal's prototype is Object.prototype, not BatteryManager.prototype.

### Can a spoof fix instanceof by setting the prototype?

It fixes that one check and leaves the others. The literal still has its values as own enumerable properties, where the real object has none and reads them through getters on the prototype. Object.keys on the result gives it away.

### Why use a Proxy over the real object instead of a copy?

A Proxy keeps the target's class, brand and property shape, so toString, instanceof and Object.keys all answer as they would for the real object. Only the values the trap changes are different. Brand-checked getters still need the real target as their receiver.

### Does Mosaic's engine use JavaScript overrides like these?

No. Tesseract Chrome changes fingerprint values inside the engine, so there is no injected script for a page to inspect. The JavaScript layer described here is the fallback for a stock Chromium, and the rule came out of fixing it.

## Related

[Why we change fingerprints in the engine, not in JavaScript](https://mosaicbrowser.com/blog/cpp-not-javascript): A JavaScript spoof is one realm deep. An iframe read the real source of 33 of our replaced functions; the same values set inside the engine agree everywhere.

[Half a spoof is a contradiction](https://mosaicbrowser.com/blog/half-a-spoof): Partial fingerprint spoofing detection is simple: ask one question two ways. We cut five spoofs that changed one route to a value and left the other.

[How a page checks that a browser function is really native](https://mosaicbrowser.com/blog/native-code-tostring-check): A page calls toString from a fresh iframe to catch a replaced getter, then brand-checks the objects it returns. Neither takes more than four lines.

[Engine-level fingerprint spoofing, and what it fixes](https://mosaicbrowser.com/glossary/engine-level-fingerprint-spoofing): Engine-level fingerprint spoofing changes fingerprint values inside the browser's own source code, so every page, frame and worker reads the same answer.









[outerHeight minus innerHeight is the browser chrome a page can measure](https://mosaicbrowser.com/blog/outer-minus-inner-height): innerHeight is the page's viewport, outerHeight the whole window. Subtract one from the other and a page has the height of your tabs and toolbar.

[screen.availTop is where your menu bar is](https://mosaicbrowser.com/blog/screen-availtop-menu-bar): screen.availTop is the height of OS chrome at the top of the screen: 25 on most Macs, 0 on Windows. It names a platform before navigator.platform does.

## Mosaic, on your own computer

The browser this blog is about: its own Chromium, fingerprints from real machines and a proxy IP per profile. Free covers 3 profiles with no card, and Pro is $25 a month for any number of them.

Mosaic is a desktop app

Email yourself the link and open it on your computer.
