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.
Test a profile the way a site would, before a site doesIn Mosaic: Leak tests and detectors. A leak test and 49 public detectors, read for you. 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.
How it works: Leak tests and detectors
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
nameistoStringand itslengthis 0 - it has no
prototypeown property; a function declaration would have one, and stock Chrome answersfalseto'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 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: 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.






