WEBQ3 Gaming hosts free browser games, including our own arena fighter Power Surge: Planet Breakers and builds from independent creators. Every game runs in the page with no download and no account. That raises a practical question for any site that runs game code it didn't fully write: how do you let a build run at full speed without giving it access to the rest of the site?

Our answer is two Content-Security-Policy headers, one on the game page and one on the separate origin that serves the build.

The page and the build live on different origins

The game page is served from webq3.games. The build itself is served from a separate host, webq3-games.netlify.app, and is loaded into the page in an iframe. Because the build sits on its own origin, the browser's same-origin policy already keeps it away from cookies, storage and scripts on the main site.

Header one: the game page

The page's policy starts from default-src 'self' and only opens what it needs. The key lines for games are these:

frame-src https://webq3-games.netlify.app

frame-ancestors 'none'

object-src 'none'

script-src 'self' with a per-request nonce and 'strict-dynamic'

frame-src says the page may only frame the build host and nothing else. frame-ancestors 'none' says no other site may frame the game page itself, which stops clickjacking and stops other sites from wrapping our pages in theirs.

Header two: the build host

The build host sends a much stricter policy, and it starts with the sandbox directive:

sandbox allow-scripts

default-src 'none'

connect-src https://webq3-games.netlify.app

frame-src 'none'

form-action 'none'

frame-ancestors https://webq3.games

sandbox allow-scripts lets the game run JavaScript but puts it in an opaque origin with no top-level navigation, no popups and no form submission. default-src 'none' with connect-src limited to the build host means a build can load its own assets but cannot call out to arbitrary servers. frame-ancestors https://webq3.games means the build can only be embedded by our own site.

The trade-off we live with

This setup has a real cost. Many browser game portals list games by embedding them in an iframe on their own domain. With frame-ancestors locked to our site, those portals cannot embed our games, so we can only be listed on directories that link out to the game page. If we ever partner with a specific portal, the fix is narrow: add that portal's exact origin to frame-ancestors on the build host, rather than opening it to everyone.

What we would tell another small team

Serve untrusted or third-party game builds from a separate origin. Put the sandbox directive on the build, not just the sandbox attribute on the iframe, so the protection travels with the response. Lock frame-ancestors on both sides, and loosen it one origin at a time when you have a reason.

You can see the result in action by playing Power Surge: Planet Breakers in your browser: https://webq3.games/games/power-surge-planet-breakers-e9db6900

If you build HTML5 games and want a place to publish them, the creator page is here: https://webq3.games/for-creators

This post was written with AI assistance and checked against our live response headers.

Posted by the WEBQ3 team.