# Function toString native code detection, explained

> 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.

Source: https://mosaicbrowser.com/blog/native-code-tostring-check

## How a page checks that a browser function is really native

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.

![4 lines: A fresh iframe asks the question the page cannot fake](https://mosaicbrowser.com/covers/native-code-tostring-check.webp)

A page checks that a browser function is native by printing it. `Function.prototype.toString` returns the source of a function written in JavaScript and `function name() { [native code] }` for one built into the browser. A spoof that replaced a getter has to hide that difference, and the usual way of hiding it lasts exactly one realm. A page then checks the objects that functions return, because a stand-in object fails a brand check three ways at once.

## The first check: Function toString and native code

Every getter a fingerprint is read from has a native implementation. Take the one behind `navigator.userAgent`:

```
const get = Object.getOwnPropertyDescriptor(Navigator.prototype, 'userAgent').get
Function.prototype.toString.call(get)
// "function get userAgent() { [native code] }"

```

If a script has replaced that getter with its own function, the same call prints the script's code. So a spoofing script also replaces `Function.prototype.toString`, with a version that checks a list of its own functions and answers `[native code]` for them. Then it has to make the new `toString` print as native too, because the next probe is `toString.call(toString)`.

That holds up in the page. It does not hold up anywhere else.

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)

## The second check: ask from a fresh realm

An iframe is a new realm: its own globals, its own `Function.prototype`, its own `toString`. A page can make one in a line and borrow its `toString`:

```
const f = document.createElement('iframe')
document.documentElement.appendChild(f)
f.contentWindow.Function.prototype.toString.call(
  Object.getOwnPropertyDescriptor(Navigator.prototype, 'userAgent').get)

```

The iframe's `toString` has never heard of the spoof's list. It sees an ordinary script function and prints its body.

We measured this on ourselves, in the JavaScript fallback layer we wrote for a stock Chromium, the path our own engine replaces. Its `toString` answered from a `WeakMap` of our own functions. On one profile, that four-line probe made **thirty-three** replaced functions across Navigator, Screen, canvas, WebGL, audio and client rects print their real source.

The repair was to stop substituting functions. Each replacement became a JavaScript `Proxy` whose target is the real native function. V8 prints a proxy over a native function as native code, in any realm, without anything being shared between realms, and the target's `name`, `length` and own property names come along. One function had to be the exception: `Function.prototype.toString` itself, installed as a proxy over the real `toString`, recursed until the stack ran out. On one bot-check page that meant four `Maximum call stack size exceeded` errors, their detection script dead, and a verdict box that never filled in. Bisecting on a stock Chrome with only our script injected pinned it to the proxy's target, not its trap or its name handling. The target is now a method shorthand, which, like the real one, has no `prototype` property.

## The third check: is the object what it says it is

Functions are half of it. The other half is what they return. Chrome hands back platform objects with an internal brand, and a page can test that brand three ways:

```
const r = document.body.getClientRects()
Object.prototype.toString.call(r)   // "[object DOMRectList]"
r instanceof DOMRectList            // true
Object.keys(r)                      // own enumerable keys: a real list has only indices

```

That same layer's old `getClientRects` returned a plain `Array` with an `item` function hung on it. It said `[object Array]`, failed `instanceof`, and carried an own enumerable property no `DOMRectList` has. `getBattery` resolved with an object literal holding eleven own keys, where a real `BatteryManager` has none because its values sit on prototype getters. `enumerateDevices` built plain objects instead of `MediaDeviceInfo`. Each one failed all three tests together.

The fix in each case was to keep the platform's own object. The rect list became a `Proxy` over the real `DOMRectList`. The battery numbers moved onto `BatteryManager.prototype`, so the object stayed the engine's. The device list became a filter over the engine's own list. There is a detail in the first one that catches people: the indexed getters and `length` are brand-checked too, so the proxy has to pass the real list, not itself, as the receiver in `Reflect.get`. Passed the proxy, Chrome throws `Illegal invocation`.

The rule we took from it: if the engine already produces the object, change what it says, not what it is.

## Why the engine is the place to do it

Every check above asks whether something is the browser's own. A value changed inside the browser passes by construction. On Tesseract Chrome, Mosaic's own Chromium, `navigator.userAgent` is still Chromium's getter. It returns a different string because the browser's own code behind it was changed, so `toString` prints native code in every realm, the returned objects carry their real brands, and an iframe asks the same code and gets the same answer. That is why Mosaic's engine has no fingerprint script: one in the page on top of a change inside the engine would bring back exactly the functions these checks are written to find.

CreepJS runs these tests across dozens of prototypes, and [what its prototype page reports](https://mosaicbrowser.com/blog/creepjs-prototype-page) maps each failure name to the test that produced it. The longer argument, with the realms a page can open besides an iframe, is in [why we change fingerprints inside the engine](https://mosaicbrowser.com/blog/cpp-not-javascript), and [what a CreepJS lie is](https://mosaicbrowser.com/blog/what-a-creepjs-lie-is) explains why a stock Chrome scores none.

## Questions

### What does [native code] mean in toString?

It is how a JavaScript engine prints a function implemented in the engine itself rather than in script. Calling Function.prototype.toString on a built-in such as the userAgent getter returns its name followed by { [native code] } instead of a body.

### Why does a detector use an iframe for the toString check?

A new iframe has its own copy of Function.prototype.toString. A script that overrode the page's toString to hide its replacements has usually not touched that copy, so the iframe's toString prints the replacement's real source.

### Is a Proxy over a native function detectable?

V8 prints a Proxy whose target is a native function as native code, in every realm, so the toString check passes. Proxies have other tells, such as how a trap shows up in a stack trace, and some detectors test those separately.

### What is a brand check?

A test that an object really is the platform class it claims to be: its Object.prototype.toString tag, instanceof against the class, and whether its values are own properties or prototype getters. A plain object fails all three.

## Related

[What CreepJS's prototype page tells you about JavaScript spoofing](https://mosaicbrowser.com/blog/creepjs-prototype-page): CreepJS's prototype page checks each native function's keys, source and errors. A value faked in JavaScript trips it; one set inside the engine leaves nothing.

[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.

[What a CreepJS lie is, and why stock Chrome has none](https://mosaicbrowser.com/blog/what-a-creepjs-lie-is): CreepJS lies detected means an API behaved unlike the native one: wrong toString, wrong descriptor, or a worker that disagrees. Stock Chrome has none.

[Browser fingerprint: what a site can read about your machine](https://mosaicbrowser.com/glossary/browser-fingerprint): A browser fingerprint is the set of values a site reads about your browser and computer, which together recognize you without cookies. What goes into one.

[Why we built our own Chromium instead of injecting JavaScript](https://mosaicbrowser.com/blog/why-we-built-our-own-chromium): Mosaic changes fingerprint values inside its own engine. Three measured differences between Chrome and plain Chromium explain why that matters.







[screen.width said 1728 and a media query said 1512](https://mosaicbrowser.com/blog/device-width-media-query): A binary search over max-device-width finds the real screen whatever screen.width claims. Fixing that route took IPHey from 90 to 100 on one profile.

## 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.
