# Maximum call stack size exceeded: a Proxy over toString

> A Proxy over the real Function.prototype.toString overflowed the stack on one bot-check page and killed its script. The cause was the target, not the trap.

Source: https://mosaicbrowser.com/blog/tostring-proxy-stack-overflow

## The one function that must not be a Proxy over itself

A Proxy over the real Function.prototype.toString overflowed the stack on one bot-check page and killed its script. The cause was the target, not the trap.

![Four stack overflows and a verdict box that never filled](https://mosaicbrowser.com/covers/tostring-proxy-stack-overflow.webp)

A `Proxy` over `Function.prototype.toString` that throws "Maximum call stack size exceeded" usually has one of two causes. The common one is a trap that calls `Function.prototype.toString`, which by then is the proxy, so it calls itself. The other is the one we had, and it survived for months because the trap was fine: the proxy's **target** was the real `toString`, and that alone made it re-enter itself once a page started wrapping `Function.prototype` the way detectors do. A different target fixed it.

## Where the toString proxy hit "Maximum call stack size exceeded"

This is the JavaScript payload from Mosaic's old stock-Chromium fallback, where nothing below JavaScript changed any values. (Mosaic now runs only on Tesseract Chrome, our own engine, which changes the values inside the engine, so none of this code runs on a profile.) Every overridden getter in that payload is a `Proxy` whose target is the real native function. V8 prints a callable proxy as native code in every realm, so an iframe and its parent agree without sharing anything. Why we moved to proxies, after a fresh iframe pulled 33 real function bodies out of the old version, is in our toString override and the iframe check.

We built `Function.prototype.toString` the same way, as a proxy over the real `toString`. On `deviceandbrowserinfo.com/are_you_a_bot` that produced four `Maximum call stack size exceeded` rejections. The site's detection script died and its verdict box never filled in.

Our detector runner recorded the row as `unread`, with this hint:

```
the raw detection JSON had not rendered. It arrives a few seconds
after load — raise settleMs if this persists.

```

It persisted, for months. The longer the hint sat there, the more it read like the site's fault. Waiting longer could never have helped, because there was nothing left running on the page to render a result.

Mosaic is a desktop app

Email yourself the link and open it on your computer.

[How it works: Leak tests and detectors](https://mosaicbrowser.com/features/detection-tests)

## The control, then one change per run

The first useful step was a stock Chrome with only our payload injected: no Mosaic, no proxy, no other overrides. If the page broke there too, the payload was the whole cause. It broke the same way. An untouched Chrome on the same page answered in about five seconds.

Then we changed one thing per run:

```
as shipped (target = the real toString)      4 rejections, no verdict
without the ANON_NATIVE name fallback        4 rejections, no verdict
Reflect.apply instead of .call throughout    4 rejections, no verdict
installed as a plain function, no proxy      clean, verdict renders
proxy over any OTHER function as target      clean, verdict renders

```

The name fallback was not it. The way the trap invoked the native was not it either. (The trap calls a reference to the native `toString` taken before anything is installed, which is the usual fix for the common cause.) A plain function worked, and so did a proxy, as long as its target was some other function. The variable was the identity of the target. A proxy whose target is the very function it is installed in place of re-enters itself when a page wraps `Function.prototype` around it.

## The fix, and the shape it has to match

A plain function would have stopped the recursion and lost what the proxy gives every other override. So the target is now a different function with the same visible shape:

```
const nativeToString = Function.prototype.toString  // captured first
const toStringShim = { toString() {} }.toString      // the new target

Function.prototype.toString = nativeProxy(
  toStringShim,
  (target, thisArg) => toStringImpl.call(thisArg),
  'toString'
)

```

The method shorthand is the only shape we found that matches the native in every way a page reads it:

- its `name` is `toString` and its `length` is 0
- it has no `prototype` own property; a function declaration would have one, and stock Chrome answers `false` to `'prototype' in Function.prototype.toString`
- it prints as native code through the proxy, in the page and through an iframe

Measured on a neutral page against the old version, own property names, `name`, `length`, the function printing itself and the cross-realm answers through an iframe came out byte-identical. The fix cost nothing a page could read, and `deviceandbrowserinfo.com` renders its verdict again.

If you hit the same error in your own code, check the common cause first: capture `Function.prototype.toString` into a variable before installing anything, and call only that variable inside the trap. If the stack still overflows, change the target before you change anything else.

## What the runner had been telling us

Two rules came out of this, and both are about the runner more than the proxy.

A stubborn `unread` is a bug report. The catalogue in [Mosaic's detection tests](https://mosaicbrowser.com/features/detection-tests) records `unread` when a page renders no verdict where the extractor expects one. That status exists so a test that did not run is never counted as a pass. It was doing its job; we were reading it as a slow site.

The control is a stock browser, not a longer timeout. One run of the same page in an untouched Chrome turned "flaky third-party site" into "our script kills their script" in about a minute. It is the same lesson as [using Google's own build as the control](https://mosaicbrowser.com/blog/the-control-is-googles-build): before bisecting your own code, ask whether a clean browser does the same thing. The checks a page runs against `toString` in the first place are in [how a page checks that a function is native](https://mosaicbrowser.com/blog/native-code-tostring-check).

## Questions

### Why does a Proxy over Function.prototype.toString cause Maximum call stack size exceeded?

There are two common causes. A trap that calls Function.prototype.toString after the proxy has been installed there calls itself. And in our case, with the trap fixed, a proxy whose target was the real toString still re-entered itself once the page wrapped Function.prototype, so the target had to be a different function.

### How do you fix a recursive toString proxy?

Keep a reference to the native toString before you install anything, call that reference inside the trap, and make the proxy's target a separate function with the same shape as the real one. A method shorthand such as { toString() {} }.toString has no prototype property, the name toString and length 0, like the native.

### Can a page tell that toString has been replaced?

It can try. Pages compare name, length, own property names, whether 'prototype' is in the function and what it prints for itself and through an iframe. Measured on a neutral page, our fixed replacement gave byte-identical answers to the version it replaced on every one of those.

### Does Mosaic's own engine use this JavaScript?

No. Mosaic runs only on Tesseract Chrome, which changes fingerprint values inside the engine, so the getters a page reads are Chromium's own and nothing is replaced in script. This payload belongs to the stock-Chromium fallback Mosaic used to have.

## Related



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

[The control that settles "is it our changes?"](https://mosaicbrowser.com/blog/the-control-is-googles-build): An unchanged Chrome for Testing got the same IPHey verdict as our engine, so our changes were not the cause. Its voice list still differs from Chrome's.

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

[Detection tests: check a profile against 49 public detectors](https://mosaicbrowser.com/features/detection-tests): Test your anti-detect browser against detectors: Mosaic lists 49 public ones in five groups, from Pixelscan to a reCAPTCHA v3 score, and opens any in a profile.







[A detector that never answered because its tab was in the background](https://mosaicbrowser.com/blog/requestanimationframe-background-tab): Background tab throttling stops requestAnimationFrame. Our test tab was in the background, two detectors never answered, and Page.bringToFront fixed both.

[When the FingerprintJS demo can't name your browser](https://mosaicbrowser.com/blog/fingerprintjs-cannot-name-browser): FingerprintJS read 'Chromium-Based Browser Not Available' for our engine. We blamed the version; the real variable was Google's branding.

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