Apache X-Frame-选项允许来自多个域

问题描述 投票:0回答:7

当我在 apache 中使用 x-frame headers 选项时出现错误。

Header always append X-Frame-Options ALLOW-FROM site1,site2,site3

Header always append X-Frame-Options ALLOW-FROM=site1,site2,site3

Header always append X-Frame-Options ALLOW-FROM=site1
Header always append X-Frame-Options ALLOW-FROM=site2
Header always append X-Frame-Options ALLOW-FROM=site3

我如何设置 X-Frame-Options: ALLOW-FROM 以支持多个域?

谢谢!

apache x-frame-options
7个回答
11
投票

值得注意的是,ALLOW-FROM 正在从 Firefox 70 中被删除,其他浏览器也可能会效仿。您将需要使用 CSP 的 frame-ancestors 指令,大约 99% 的浏览器支持该指令。

你的例子是:

Header always append Content-Security-Policy "frame-ancestors site1 site2 site3;"
编辑:frame-ancestors 在新浏览器中覆盖 X-FRAME-OPTIONS,因此理论上您可以在其中为旧浏览器设置一个值,并让 CSP 在新浏览器中覆盖它,但问题是没有 X-FRAME-OPTIONS值可以让您嵌入多个网页。唯一有效的选项是拒绝(任何地方都不允许)、同源(仅限您的网站)和允许(从现代浏览器中删除,无论如何只允许一个网站)。

您想要覆盖的旧 X-FRAME-OPTIONS 值根本没有。这将允许您将您的网站嵌入到多个其他网站(所有这些网站)中,并将其限制为您在现代浏览器中允许的网站。

如果不嵌入不允许的站点比嵌入允许的站点更重要,则将上述内容与:

Header always append X-Frame-Options "DENY"
这将阻止您的网站嵌入到大约 3% 的浏览器中的所有网站中,仅在 95% 的浏览器中允许的网站中显示,并在其余 0.1% 的浏览器中随处显示(甚至不支持 X-FRAME-OPTIONS 

到处都是)。


6
投票

编辑 2018 年 1 月 17 日: 这是正确的:

Header set X-Frame-Options SAMEORIGIN Header append X-Frame-Options "ALLOW-FROM http://www.example.com/" Header append X-Frame-Options "ALLOW-FROM http://example.com/" Header append X-Frame-Options "ALLOW-FROM https://www.example.com/" Header append X-Frame-Options "ALLOW-FROM https://example.com/"

所以基本上您只允许来自您的网站 (SAMEORIGIN) 的 iframe,并使用“附加”指定允许的 url 列表。如果不添加“追加”,每一行都会覆盖前一行。

这实际上适用于 Internet Explorer 11,不适用于 Firefox 57,并且会被 Chrome 忽略...

使用

https://securityheaders.io 进行测试不会给你一个“A”,因为它们无法处理多个 uri

We couldn't detect a valid configuration. Expected values are "DENY", "SAMEORIGIN", "ALLOW-FROM (URL)" and "ALLOWALL".

似乎在 IE11 和 Firefox 中工作的另一种可能性是:

Header always set X-Frame-Options "ALLOW-FROM https://www.example.fr/ https://example.fr/ http://www.example.fr/ http://example.fr/"

当您使用

https://securityheaders.io 检查结果时,它会给出“A”

顺便说一句,我想知道使用世界上最常用的浏览器(Chrome)可以绕过的安全设置有什么意义??


5
投票
SetEnvIf Referer "^(https:\/\/.*\.example1\.com)/.*" X_FRAME_OPTIONS_ALLOWED=$1 SetEnvIf Referer "^(https:\/\/.*\.example2\.com)/.*" X_FRAME_OPTIONS_ALLOWED=$1 Header set X-Frame-Options SAMEORIGIN Header append X-Frame-Options "ALLOW-FROM %{X_FRAME_OPTIONS_ALLOWED}e" env=X_FRAME_OPTIONS_ALLOWED`
    

3
投票
由于对

ALLOW-FROM

 的支持在不同浏览器的实现和支持方面都有所不同,我尝试了以下解决方案,它可以设置 
SAMEORIGIN
 或有条件地完全删除 
X-Frame-Options

在 apache-2.4 上尝试过。

# Set X-Frame-Options SAMEORIGIN _unless_ the referer is any of my allowed sites. # Add one or more SetEnvIf - whatever suits your purpose # This part you MUST adapt. # ALLOW https://my.allowed.site.com SetEnvIf Referer "^https:\/\/my\.allowed\.site\.com\/.*" X_FRAME_OPTIONS_ALLOWED # ALLOW https://mysite.tld.com and https://yoursite.tld.com SetEnvIf Referer "^https:\/\/(mysite|yoursite)\.tld\.com\/.*" X_FRAME_OPTIONS_ALLOWED # ALLOW https://mysite.tld.com and https://yoursite.theother.org SetEnvIf Referer "^https:\/\/(mysite\.tld\.com|yoursite\.theother\.org)\/.*" X_FRAME_OPTIONS_ALLOWED # Set X-Frame-Options = SAMEORIGIN _unless_ the referer is in the allow list. Header always set X-Frame-Options SAMEORIGIN env=!X_FRAME_OPTIONS_ALLOWED # Always _unset_ X-Frame-Options if the referer is in the allow list. Header always unset X-Frame-Options env=X_FRAME_OPTIONS_ALLOWED

您可以添加多个

SetEnvIf

 或扩展正则表达式 - YMMV。

你的同事会喜欢你让事情变得可读......


2
投票
Header always append X-Frame-Options ALLOW-FROM=site1 Header always append X-Frame-Options ALLOW-FROM=site2 Header always append X-Frame-Options ALLOW-FROM=site3

这样就OK了。

但是我第一次使用的时候出现了错误。
也许我写错了角色。


1
投票

编辑 2018 年 1 月 17 日:

下面的解决方案不正确,因为每一行的设置都会覆盖前一行。所以你只允许 http://example.com/

最后我找到了正确的语法。根据该网站:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options

Header set X-Frame-Options "ALLOW-FROM https://example.com/"

这对我有用:

Header always set X-Frame-Options "ALLOW-FROM https://www.example.com/" Header always set X-Frame-Options "ALLOW-FROM https://example.com/" Header always set X-Frame-Options "ALLOW-FROM http://www.example.com/" Header always set X-Frame-Options "ALLOW-FROM http://example.com/"
    

1
投票
X-Frame-Options 规范仅指定使用 DENY、SAMEORIGIN 和 ALLOW-FROM 之一(

https://www.rfc-editor.org/rfc/rfc7034#section-2.1)。有些浏览器可能支持多个 ALLOW-FROM,但许多浏览器根本不支持 ALLOW-FROM。

您的最佳选择是使用frame-ancestors指令实现Content-Security-Policy标头。这允许配置多个 URI,并且大多数浏览器都可以理解,但 IE 和 Edge 14 及以下版本除外。

对于 IE 和 Edge 14 支持,您还可以使用 ALLOW-FROM 设置 X-Frame-Options。如果您创建值白名单,您可以根据引荐来源网址设置 ALLOW-FROM URI。

设置两个标题并没有什么坏处。理解 Content-Security-Policy 框架祖先的浏览器将忽略 X-Frame-Options,而那些不理解框架祖先的浏览器将忽略它并使用 X-Frame-Options(如果可用)。组合

https://caniuse.com/#search=csphttps://caniuse.com/#search=x-frame-options 这适用于除“UC 浏览器 for Android”之外的所有浏览器

© www.soinside.com 2019 - 2024. All rights reserved.