Insights · Trend · Web

How to safely embed games or widgets: iframe and CSP rules

Topic: Websites and performance (all articles on this topic)3 min read

Embedding an external app in a frame needs consent from both sides: the site that embeds and the service being embedded. If either side withholds it, the browser shows an empty frame – with no readable error on screen. We explain how this “contract” works, what has recently changed in browsers and how to set it up so it both works and stays secure.

How to safely embed games or widgets: iframe and CSP rules

What's changing right now

Browsers isolate content from different services more and more strictly. An iframe tag used to be enough; today security headers on both sides decide what may be embedded – above all the Content-Security-Policy (CSP). The second change is less known: since 2023 Chrome (following Firefox and Safari) partitions frame storage by the site they are embedded in. A game or widget in a frame on your site therefore has a separate localStorage from the same app opened directly – saved progress or sign-in do not carry over between them.

Concentric glass frames linked by data lines – an illustration of isolating content embedded in an iframe

What you can do better

Concretely, starting today

  • Set up the “two sides of the contract” in the Content-Security-Policy header: the embedded app lists your domain in the frame-ancestors directive, and your site – if it has its own CSP – lists the provider’s domain in frame-src. The older X-Frame-Options header (DENY / SAMEORIGIN) blocks embedding on other domains; frame-ancestors supersedes it.
  • Do not use a wildcard (*) – list specific domains. Especially in frame-ancestors: too broad a list lets any site embed your app, which opens the door to clickjacking (tricking users into clicking a hidden frame).
  • Set frame permissions separately: the allow attribute enables features (e.g. fullscreen, autoplay, camera), while the sandbox attribute restricts the frame and unlocks only what is needed (e.g. allow-scripts). Note: allow-scripts together with allow-same-origin for content from your own domain effectively removes the sandbox.
  • Run a new CSP first as Content-Security-Policy-Report-Only – the browser reports violations but blocks nothing. Once checked, you switch it on for real.
  • If you embed an app in a modal window, set the frame’s src to about:blank when the window closes – this immediately stops the app in the background (e.g. game audio).

Proof from our work

On dtl.pl, games from the FAJNA WERONI studio open in a window above the page. The game sends frame-ancestors listing the allowed domains (including dtl.pl and its subdomains, Wirtualny Otwock), and dtl.pl allows it in frame-src. The rest of the game’s CSP still runs in report-only mode. It works the other way too: dtl.pl lets FlashAI embed our calculator. The frame has allow="fullscreen; autoplay" and resets to a blank page when the window closes. We also have a refusal case: the Domowy Burger brand games send X-Frame-Options: SAMEORIGIN, so on dtl.pl we open them in a new tab – and the note under the game window honestly says that progress from the window does not carry over to the full version.

A browser window with a game embedded in a frame and a glowing ring – an illustration of a game opened in a window on a site

Frequently asked questions

Why is the iframe empty despite the correct URL?

Most often the provider’s server blocks it: an X-Frame-Options header or a frame-ancestors directive without your domain. The second cause is your own site’s CSP missing the provider’s domain in frame-src. You will see the reason in the browser console (developer tools).

Can an embedded frame read my site’s data?

A frame from another domain has no access to your site’s content, cookies or storage – the same-origin policy protects you. The sandbox attribute additionally limits what the frame can do, such as redirecting the whole page, opening windows or submitting forms. The weakest point is usually an overly broad list of domains in the CSP.

Why does a game in a frame not remember my progress?

Because browsers partition frame storage by the site the frame is embedded in. The same game embedded on another site or opened directly has a separate localStorage. If progress should be shared, you need an account or server-side saving.

Need secure integrations?

We will help you correctly configure the security architecture for your apps and widgets.

Prefer a human? Paweł replies personally

Kasia

DTL’s AI assistant

will match the offer, quote and take the order, 24/7

Your configuration

Write to Paweł
Ask Kasia Write to Paweł