Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

The browser refuses to show Keycloak inside a frame from my React app

Errorviolating Content Security Policy directive: frame-ancestors 'self'

Short answer

Keycloak sends headers that stop other sites from framing its pages, to prevent clickjacking. When your React app's origin loads Keycloak in an iframe, the browser enforces frame-ancestors 'self' and refuses. Add your app's origin to the Content-Security-Policy field in Realm Settings, Security Defenses, and keep the X-Frame-Options field consistent.

  • Applies to Keycloak 26, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the browser developer tools, load the page that shows the error and read the content-security-policy response header of the framed Keycloak URL.

    # Developer tools > Network > select the Keycloak request > Response headers > content-security-policy
  2. 2Match what you see, then jump to its fix

    1. 1The header contains frame-ancestors 'self' and the parent page is on another originYour app's origin is not allowed by the realm's Content-Security-PolicyFix
    2. 2The CSP is fixed but an older browser or a proxy still refuses to frame the pageThe X-Frame-Options field still blocks the frameFix

What does the error look like?

  • violating Content Security Policy directive: frame-ancestors 'self'

Cause 1 of 2

Your app's origin is not allowed by the realm's Content-Security-Policy

1Check

Keycloak's administration guide says its default headers block framing of its login pages, so that other websites cannot include a login dialog in an iframe. CSP's frame-ancestors decides which parents may embed a page.

# Admin Console > Realm settings > Security defenses > Content-Security-Policy

2Fix

In Realm settings, Security defenses, add your app's origin to the Content-Security-Policy field, with the exact scheme, host and port.

frame-ancestors 'self' https://app.example.com

3Verify

Save, reload the app and check that the iframe now loads and the response header lists your origin.

# Developer tools > Network > content-security-policy header

Confirmed by keycloak.org

Did this fix it?

Cause 2 of 2

The X-Frame-Options field still blocks the frame

1Check

Keycloak's guide tells you to enter the allowed origin in both the X-Frame-Options and Content-Security-Policy fields. The CSP specification says X-Frame-Options is ignored when an enforced policy applies, so modern browsers follow the CSP.

# Admin Console > Realm settings > Security defenses > X-Frame-Options

2Fix

Update the X-Frame-Options field to match what you allow in the CSP, as the guide describes, and keep the two in step.

# no command: edit both fields in Security defenses and save

3Verify

Reload the page and read both headers in the response.

# Developer tools > Network > Response headers

Confirmed by w3c.github.io

Did this fix it?

How do I stop it coming back?

  • Allow only the exact origins that must embed Keycloak; do not remove frame-ancestors altogether.
  • Change the headers per realm, and test the admin console after each edit.
  • Prefer a normal redirect login over an iframe when you can.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.