Question presented to candidate: "If you needed to run a plugin's untrusted JavaScript code without letting it touch your application's own global objects, could you use ShadowRealm today? What would you actually reach for instead?"
What a strong answer should cover:
- π Interview term:
ShadowRealmβ a proposed constructor creating a genuinely separate, isolated realm (its OWN global object and built-ins, covered in more depth in this bank's own dedicated Error.isError question's realm coverage) specifically FOR running untrusted or plugin code, with a real.evaluate()method to run code inside it and.importValue()to pull a specific export back across the boundary. - π Interview term: the real, direct answer to the prompt β verified directly, in BOTH Node v24 and a real, current browser:
ShadowRealmis genuinely unavailable everywhere today βtypeof ShadowRealmis genuinely"undefined"in both. It remains a Stage 3 TC39 proposal with, as of this verification, zero shipped native runtime support anywhere. - π Interview term: what "true sandboxing" would mean β a precise answer names that
ShadowRealm's real, intended value over an iframe or a Worker (today's real, PARTIAL alternatives) is running untrusted code with its own genuinely separate global environment WITHOUT the overhead of a full browsing context or a separate thread β a lighter-weight, same-thread isolation primitive. - π Interview term: today's real, honest alternatives β since
ShadowRealmis not shippable, real applications needing this kind of isolation today reach for a real, dynamically-created<iframe>(the identical real cross-realm mechanism verified in this bank's own dedicated Error.isError question) or a Web Worker β genuinely different real trade-offs (a full browsing context, or a separate thread) from whatShadowRealmis specifically designed to provide. - A precise answer names that
ShadowRealmgenuinely does NOT provide full security isolation on its own β the proposal's own documentation is explicit that it shares the same memory/CPU as the surrounding code, so it is a real ENCAPSULATION primitive (separate globals, no accidental interference), not a substitute for a genuine security sandbox against malicious, resource-abusive code.
Clarifying questions expected:
- None β this is a definitional/technical question; honestly answering "could you use it today" (no) is the strong signal, not describing a hypothetical API as if it were shipped.
Code / implementation expected: The example below shows real, accurate proposed ShadowRealm syntax, honestly marked non-runnable, since it is confirmed genuinely unavailable in every current environment.