How we sandbox HTML5 game builds on a browser game platform with two CSP headers
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…
WEBQ3 Gaming offers free browser games, such as Power Surge: Planet Breakers and works by independent developers. The company faces a common challenge: how to run third-party game builds without granting them access to other parts of their site. The solution they found is two Content-Security-Policy (CSP) headers. One is placed on the game page, and the other on the separate origin that delivers the game build.
The game page, served from webq3.games, starts with the default-src policy set to 'self.' It then allows the game build to be embedded by framing it from the build host, webq3-games.netlify.app, using frame-src. The game page also prevents other sites from embedding its page through frame-ancestors 'none' and blocks any outside scripts from executing using script-src 'self' with a per-request nonce and 'strict-dynamic.'
The build host, webq3-games.netlify.app, sends a much stricter policy. It begins with the sandbox directive, which creates an opaque origin where the game can run JavaScript without navigation, popups, or form submissions. The build can only load assets from its host, and it can only be embedded by WEBQ3 Gaming's site thanks to the frame-ancestors 'none' setting on the build host.
While this setup comes with trade-offs, it ensures the security of WEBQ3 Gaming's site. Many browser game platforms embed games in an iframe on their domain, but with the current restrictions, WEBQ3 Gaming can only be listed on directories that link to the game page. If they want to work with a specific portal, they can add that portal's exact origin to the frame-ancestors on the build host.
To serve untrusted or third-party game builds, WEBQ3 Gaming recommends using a separate origin, applying the sandbox directive to the build, and locking frame-ancestors on both the game page and build host. You can experience the results by playing Power Surge: Planet Breakers at https://webq3.games/games/power-surge-planet-breakers-e9db6900. For more information on publishing HTML5 games, visit the creator page at https://webq3.games/for-creators.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to