Repository navigation
Origin isolation
Origin isolation puts the proxy on a second origin, so the browser itself keeps proxied pages away from your page. This page explains why you want it, how RemoteController sets it up, which pairs of origins work, and what to do when the browser refuses the proxy origin its storage.
StompController keeps everything on one origin. A proxied page is then same-origin with the page that embeds it, and browsers give same-origin frames full access to each other: parent.document, top.location, frameElement, your page's cookies and storage. STomp's hooks hide most of that from the proxied page's rewritten JavaScript, but those hooks run in the same realm as the page and are no security boundary. One gap in the rewriter and the page can read or drive yours.
With origin isolation, your page (the host) is on one origin, say https://app.example.com, and dist/, the transport and every proxied page are on another, https://proxy.example.com. The proxied frames are cross-origin to your page, so the same-origin policy stops them, whatever happens inside them.
Use same-origin mode while you develop. Use origin isolation for anything other people use.
RemoteController has the same shape as StompController: wait(), createFrame(), the navigate, open and request reports, cookies, route(), setTransport(). Underneath it does this:
- It adds a hidden
<iframe>to your page that loadscontrollerFrame.htmlfrom<proxyOrigin><directory>. The URL carries the hash of the file and the name of its script, likecontrollerFrame.html?dca767db9734bb9e&script=controllerFrame.js%3Ff05ec7548d9c7d72, so you can serve both withimmutablecaching. - That controller frame registers the service worker on the proxy origin, loads the transport module in a worker there, and runs the real
StompController. - Your page talks to the controller frame with
postMessage. Your calls (go(),back(),cookies.list()) are relayed to it, and its reports are relayed back. - Each frame from
createFrame()getssandbox(the list below) andreferrerpolicy="no-referrer", and shows routed URLs on the proxy origin.back(),forward()andreload()are relayed through the controller frame, which is same-origin with the proxied pages. - The service worker sends every proxied document with
Content-Security-Policy: frame-ancestors 'self' <host origin>, so no other site can frame your proxy origin's pages.
The default sandbox is DEFAULT_SANDBOX, exported from the package:
allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox
allow-modals allow-downloads allow-pointer-lock allow-presentation allow-orientation-lock
allow-storage-access-by-user-activation
allow-same-origin is needed so the proxy origin's service worker controls the frame. allow-top-navigation is left out on purpose: top.location = ... and <a target="_top"> in a proxied page cannot navigate your page.
On top of the browser's checks, the inject makes each proxied page believe it is a top-level window: top, parent, frameElement, window.name, document.referrer, location.ancestorOrigins, the window's size and position answer as they would for a real top window, and message events from outside the proxied world never reach the page.
Keep the two origins on one site where you can. A site is the scheme plus the registrable domain: https://app.example.com and https://proxy.example.com are two origins of one site, example.com.
The reason is third-party cookie blocking. A browser that blocks third-party cookies (Brave and incognito windows by default, a setting in Chrome) gives a frame of another site no service worker and no storage. A proxy origin on another site then cannot run at all, and wait() rejects with a StorageBlockedError. A proxy origin on the same site is first-party and works in those browsers too.
| Host page | Proxy origin | Same site? |
|---|---|---|
https://app.example.com |
https://proxy.example.com |
Yes |
https://example.com |
https://proxy.example.com |
Yes |
https://app.example.com |
https://example-proxy.net |
No |
http://localhost:8080 |
http://127.0.0.1:8080 |
No |
http://app.localhost:8080 |
http://proxy.localhost:8080 |
No: localhost is not a registrable domain, so each *.localhost name is a site of its own |
The demo and the test suites use the localhost and 127.0.0.1 pair: open the demo (npm run demo) at http://localhost:8080 and it runs isolated with the proxy on http://127.0.0.1:8080; open it at http://127.0.0.1:8080 and it runs same-origin. That pair is convenient because it needs no DNS and both names are secure contexts, but it is cross-site. It works in a default Chrome profile and fails where third-party cookies are blocked. In the demo, that failure falls back to the same-origin controller (see Fall back when storage is blocked).
To test a same-site pair on your machine, map two names of one domain to 127.0.0.1 (a hosts file entry, or Chrome's --host-resolver-rules="MAP *.stomp.test 127.0.0.1"). Plain http:// on those names is not a secure context, so either serve HTTPS with a local certificate or start Chrome with --unsafely-treat-insecure-origin-as-secure=http://app.stomp.test:8080,http://proxy.stomp.test:8080 for the test.
If the proxy origin's name is a subdomain, keep it one level deep (proxy.example.com, not proxy.app.example.com) when your certificate is a wildcard for one level.
One server can answer for both names; it only has to serve the right files.
| Origin | Serves |
|---|---|
| Host | Your page, and the controller bundle your page imports (dist/controller.mjs, or dist/Controller.js as a classic script) |
| Proxy |
dist/ at directory (/stomp/ by default), the transport module, your error page, hook files given as { url }, and usually the wisp endpoint |
Import the controller bundle from your own origin, or bundle @sysce/stomp into your app. A module script from the proxy origin would need CORS headers.
The transport module must live on the proxy origin. A module URL on another origin makes wait() reject with Transport modules must live on the proxy origin. The wisp URL in its options can point anywhere, such as the public relay wss://holywisp.fracit.al/.
If your page is cross-origin isolated (it sends Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp), every file the proxy origin serves needs Cross-Origin-Resource-Policy: cross-origin, or the controller frame does not load in your page and nothing runs. Deploy STomp has the header setup.
By default controllerFrame.html trusts whichever page embeds it: it reads the embedder's origin from document.referrer once, at load. Any other site could then embed your proxy origin's controller frame and use your proxy and relay. For a real deployment, list your host origins in the frame's meta tag, space separated:
<meta name="stomp-allowed-origins" content="https://app.example.com" />Edit the copy of dist/controllerFrame.html that you serve, or have your server rewrite that line when it serves the file. A page whose origin is not listed gets a frame that says Embedding origin is not allowed., and its wait() never settles.
Both ways need a referrer: the frame learns your page's origin from document.referrer, with or without a list. If your page sends Referrer-Policy: no-referrer (or has <meta name="referrer" content="no-referrer">), the frame cannot tell who embedded it and never answers. Keep a policy that sends at least the origin; the browser default, strict-origin-when-cross-origin, does.
import { RemoteController } from '/stomp/controller.mjs';
const proxyOrigin = 'https://proxy.example.com';
const controller = new RemoteController({
proxyOrigin,
directory: '/stomp/',
transport: {
module: `${proxyOrigin}/epoxy/index.mjs`,
options: [{ wisp: 'wss://holywisp.fracit.al/' }],
},
codec: 'xor',
errorPage: '/error.html', // a path on the proxy origin
onOpen(url, frame) {},
onRequest(info) {},
});
await controller.wait();
const frame = controller.createFrame();
document.querySelector('#view').append(frame.element);
await frame.go('https://en.wikipedia.org/');There is no register() call and no serviceworker option: the controller frame registers the worker on the proxy origin. Create the controller once document.body exists, since it appends its hidden frame there. With classic scripts, dist/Controller.js defines the class as window.StompRemoteController.
Options that differ from StompController:
| Option | Default | Notes |
|---|---|---|
proxyOrigin |
required | Must differ from your page's origin, or the constructor throws RemoteController needs a proxy origin different from the host origin
|
directory |
'/stomp/' |
The path of dist/ on the proxy origin |
transport |
required | A descriptor only ({ module, options, host? }), with module on the proxy origin. Not an instance. |
sandbox |
DEFAULT_SANDBOX |
The sandbox attribute for proxied frames |
pollInterval |
500 |
Milliseconds between refreshes of each frame's url, title, canGoBack and canGoForward from the controller frame, a fallback to the page's reports; 0 turns it off |
errorPage |
built-in page | A path on the proxy origin |
hooks |
none | A { url } is fetched by the controller frame, from the proxy origin |
onHookError |
none | Called with info only; the hookerror event's detail has the frame |
Most other options (id, codec, cache, rules, adblock, assets, names, membrane, serviceWorkers, rewriteWorkers, onNavigate, onOpen, onRequest, passkeys, sitePool) pass through to the inner controller unchanged. Configure STomp has the full list. carrierHost and frameAncestors are not passed through: the controller frame keeps the carrier host on and sets frameAncestors to your page's origin itself.
createFrame() returns a RemoteFrame. It has go(), back(), forward(), reload(), element, controller and the navigate event, like a Frame. The differences:
On a Frame
|
On a RemoteFrame
|
|---|---|
url reads the page's real location; null on an error page |
url is the last known URL, from reports and the poll; on an error page, the URL that failed |
title, lastNavigate
|
title only, the last known one |
canGoBack, canGoForward are getters |
Plain fields, last known |
| The page's document is reachable (same origin) | Cross-origin: you cannot read it, by design |
| No such method |
refresh() asks the controller frame for the current values now |
Code that only uses the navigate reports, go(), back(), forward() and reload() runs unchanged on both.
When the browser denies the controller frame its storage, wait() rejects with an error whose name is StorageBlockedError. Its message names both origins and the fix:
the browser blocks storage for http://127.0.0.1:8080 inside http://localhost:8080 (third-party
cookies are blocked), so the proxy cannot register its service worker. Allow third-party cookies
for localhost:8080, or serve the host page and the proxy from one site.
The real fix is a same-site pair of origins. If you cannot have one, decide what your page does. You can tell the user, or fall back to the same-origin controller. Falling back gives up the isolation, so only do it where same-origin mode is acceptable, such as a local development default. The demo does exactly that: it falls back only when isolation was its own local default (opened as localhost), and treats the error as fatal when the deployment asked for isolation.
import { RemoteController, StompController } from '/stomp/controller.mjs';
// proxyOrigin and transport as in the section above
// only where same-origin mode is acceptable: here, a local development run
const allowSameOriginFallback = location.hostname === 'localhost';
async function startController() {
const remote = new RemoteController({ proxyOrigin, transport, codec: 'xor' });
try {
await remote.wait();
return remote;
} catch (error) {
if (error.name !== 'StorageBlockedError' || !allowSameOriginFallback) throw error;
remote.element.remove(); // the hidden controller frame
const registration = await StompController.register('/stomp/');
const local = new StompController({
serviceworker: registration,
transport: { module: '/epoxy/index.mjs', options: transport.options },
directory: '/stomp/',
codec: 'xor',
});
await local.wait();
return local;
}
}The fallback needs dist/ and the transport on your page's origin too.
A refused controller frame (an origin not in stomp-allowed-origins, or no referrer) does not reject wait(). If you want to show an error in that case, race wait() against a timer:
await Promise.race([
controller.wait(),
new Promise((_, reject) => setTimeout(() => reject(new Error('the proxy did not answer')), 30000)),
]);The worked example on the embedding page runs with StompController. Its server already answers for any hostname, so only public/browser.js changes. For a local try, set proxyOrigin to 'http://127.0.0.1:8080' and open http://localhost:8080/ in a browser that allows third-party cookies. In production, use your two names.
The top of the file names the proxy origin and puts the transport there:
import { RemoteController, SearchBuilder } from '/stomp/controller.mjs';
// the second origin: it serves dist/, the transport, wisp and the proxied pages
const proxyOrigin = 'https://proxy.example.com';
const settings = {
// where dist/ is served on the proxy origin
directory: '/stomp/',
// the transport module must be on the proxy origin too
transport: {
module: `${proxyOrigin}/epoxy/index.mjs`,
options: [{ wisp: `${proxyOrigin.replace(/^http/, 'ws')}/wisp/` }],
},
search: 'https://duckduckgo.com/?q=%s',
home: 'https://duckduckgo.com/',
};The register() call goes away, the constructor changes, and wait() gets a catch:
status('Starting the controller frame');
const controller = new RemoteController({
proxyOrigin,
transport: settings.transport,
directory: settings.directory,
codec: 'xor',
// a path on the proxy origin
errorPage: '/error.html',
// ...hooks, onHookError, onOpen and onRequest as before
});
status('Waiting for the service worker');
try {
await controller.wait();
} catch (error) {
// the browser gives a frame of another site no storage
if (error.name === 'StorageBlockedError') status(error.message);
throw error;
}Everything else stays: the tabs, the navigate listeners, back() and forward(), onOpen, the request log, cookies.list(), the icon hook and the error page's message. The icon hook posts to top, which is your page in both modes, and error.html posts to parent, which is your page too.
The proxy origin is on another site and the browser blocks third-party cookies. Move the proxy to a subdomain of your site, or see Fall back when storage is blocked. For a local test, allow third-party cookies for the host origin in the browser's settings.
The controller frame refused your page, or did not load. Open the hidden frame (controller.element.src) in the network panel. Embedding origin is not allowed. means your origin is missing from stomp-allowed-origins. This page is the Stomp controller frame; it only works embedded by a host application., or an empty frame with an Invalid URL error on its console, means it got no referrer: check your page's Referrer-Policy. A 404 means proxyOrigin or directory is wrong. A blocked load in a cross-origin isolated page means the proxy origin's files lack Cross-Origin-Resource-Policy: cross-origin.
The descriptor's module points at your page's origin. Serve the transport on the proxy origin and use its full URL.
proxyOrigin equals your page's origin. Use StompController for one origin, or give the proxy its own name.
With RemoteController both are fetched from the proxy origin. Serve them there.
With a cross-origin isolated host page, Chrome gives proxied frames no window for their popups. Take popups with onOpen and open them as tabs.
More symptoms are in Troubleshooting.
STomp is licensed under the GPL-3.0. Found a mistake in these pages? Open an issue.