Apache X-Frame-Options 允许来自多个域

Apache X-Frame-Options Allow-From multiple domains

当我在 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 以支持多个域?

谢谢!

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

这样就可以了
但是我第一次使用它时遇到了错误。
可能我打错字了

编辑 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/"

编辑 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/"

所以基本上您只允许来自您网站的 iframe (SAMEORIGIN) 并且您使用 "append" 指定允许列表 url。如果你不添加 "append" 每行将覆盖前一行。

这实际上适用于 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) ??

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 header。这允许配置多个 URI,并且大多数浏览器都能理解,但 IE 和 Edge 14 及以下版本。

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

同时设置两个 header 也没什么坏处。理解 Content-Security-Policy frame-ancestors 的浏览器将忽略 X-Frame-Options 而那些不理解 frame-ancestors 的浏览器将忽略它并使用 X-Frame-Options (如果可用)。结合 https://caniuse.com/#search=csphttps://caniuse.com/#search=x-frame-options 这适用于所有浏览器,除了“UC Browser for Android”

SetEnvIf Referer "^(https:\/\/.*\.example1\.com)/.*" X_FRAME_OPTIONS_ALLOWED=
SetEnvIf Referer "^(https:\/\/.*\.example2\.com)/.*" X_FRAME_OPTIONS_ALLOWED=

Header set X-Frame-Options SAMEORIGIN
Header append X-Frame-Options "ALLOW-FROM %{X_FRAME_OPTIONS_ALLOWED}e" env=X_FRAME_OPTIONS_ALLOWED`

值得注意的是,removed from Firefox 70, and other browsers are likely to follow. You will want to use CSP's frame-ancestors directive instead, which is supported in about 95% 的浏览器正在使用 ALLOW-FROM。

您的示例将是:

Header always append Content-Security-Policy "frame-ancestors site1 site2 site3;"

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

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

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

Header always append X-Frame-Options "DENY"

这将防止您的网站在大约 3% 的浏览器中嵌入所有网站,仅在 95% 的浏览器允许的网站中显示,并在其余 2% 的浏览器中无处不在(甚至 X-FRAME-OPTIONS isn't supported everywhere).

由于对 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。

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