I have an asp.net 4.0 IIS7.5 site which I need secured using the x-frame headers option
I also need to enable my site pages to be iframed from my same domain as well as from my facebook app.
Currently I have my site configured with a site headed of:
Response.Headers.Add("X-Frame-Options", "ALLOW-FROM SAMEDOMAIN, www.facebook.com/MyFBSite")
When I viewed my Facebook page with Chrome or FireFox my sites pages (being iframed with my facebook page) are display ok, but under IE9, I get the error
"this page cannot be displayed…" (because of the
X-Frame_Options
restriction).
How do I set the X-Frame-Options: ALLOW-FROM
to support more than a single domain?
X-FRAME-OPTION
being a new feature seems fundamentally flawed if only a single domain can be defined.
How about an approach that not only allows multiple domains, but allows dynamic domains.
The use case here is with a Sharepoint app part which loads our site inside of Sharepoint via an iframe. The problem is that sharepoint has dynamic subdomains such as https://yoursite.sharepoint.com. So for IE, we need to specify ALLOW-FROM https://.sharepoint.com
Tricky business, but we can get it done knowing two facts:
When an iframe loads, it only validates the X-Frame-Options on the first request. Once the iframe is loaded, you can navigate within the iframe and the header isn't checked on subsequent requests.
Also, when an iframe is loaded, the HTTP referer is the parent iframe url.
You can leverage these two facts server side. In ruby, I'm using the following code:
Here we can dynamically allow domains based upon the parent domain. In this case, we ensure that the host ends in sharepoint.com keeping our site safe from clickjacking.
I'd love to hear feedback on this approach.
I had to add X-Frame-Options for IE and Content-Security-Policy for other browsers. So i did something like following.
YES. This method allowed multiple domain.
VB.NET
X-Frame-Options
is deprecated. From MDN:The modern alternative is the
Content-Security-Policy
header, which along many other policies can white-list what URLs are allowed to host your page in a frame, using theframe-ancestors
directive.frame-ancestors
supports multiple domains and even wildcards, for example:Unfortunately, for now, Internet Explorer does not fully support Content-Security-Policy.
UPDATE: MDN has removed their deprecation comment. Here's a similar comment from W3C's Content Security Policy Level
Necromancing.
The provided answers are incomplete.
First, as already said, you cannot add multiple allow-from hosts, that's not supported.
Second, you need to dynamically extract that value from the HTTP referrer, which means that you can't add the value to Web.config, because it's not always the same value.
It will be necessary to do browser-detection to avoid adding allow-from when the browser is Chrome (it produces an error on the debug - console, which can quickly fill the console up, or make the application slow). That also means you need to modify the ASP.NET browser detection, as it wrongly identifies Edge as Chrome.
This can be done in ASP.NET by writing a HTTP-module which runs on every request, that appends a http-header for every response, depending on the request's referrer. For Chrome, it needs to add Content-Security-Policy.
You need to register the context_EndRequest function in the HTTP-module Init function.
Next you need to add the module to your application. You can either do this programmatically in Global.asax by overriding the Init function of the HttpApplication, like this:
or you can add entries to Web.config if you don't own the application source-code:
The entry in system.webServer is for IIS7+, the other in system.web is for IIS 6.
Note that you need to set runAllManagedModulesForAllRequests to true, for that it works properly.
The string in type is in the format
"Namespace.Class, Assembly"
. Note that if you write your assembly in VB.NET instead of C#, VB creates a default-Namespace for each project, so your string will look likeIf you want to avoid this problem, write the DLL in C#.
From RFC 7034:
So,
You can't. As a workaround you can use different URLs for different partners. For each URL you can use it's own
X-Frame-Options
value. For example:For
yousite.com
you can just useX-Frame-Options: deny
.BTW, for now Chrome (and all webkit-based browsers) does not support
ALLOW-FROM
statements at all.