【问题标题】:How to allow iframe embedding only for whitelisted websites?如何仅允许白名单网站嵌入 iframe?
【发布时间】:2017-01-21 20:11:23
【问题描述】:

我想将一个表单嵌入到我的白名单中的网站中。

尝试嵌入它的其他网站应该只会得到一个错误页面。

<iframe src="https://domain.tld/getForm.php?embed=1&formId=123456"></iframe>

我希望我可以在getForm.php 中使用$_SERVER['HTTP_REFERER'] 来检查嵌入网站,但它不起作用。

有没有人知道最佳实践或解决方法?

提前致谢!

【问题讨论】:

  • 您必须检查向getForm.php 发出请求的远程地址的 IP,但是,IP 可能会被欺骗。看看$_SERVER supper global。我认为REMOTE_ADDR 将是发出呼叫的站点的 IP,但我不能 100% 确定,这可能只是客户端 IP。
  • 您能找到解决方案吗?我试过 $_SERVER['HTTP_REFERER'] 并且它工作正常,但我在某处读到它可以被欺骗,所以有更好的解决方案吗?

标签: php html forms iframe referer


【解决方案1】:

大多数浏览器都会支持 X-Frame-Options 标头。

此标头将阻止访问:

X-Frame-Options: SAMEORIGIN

并且这个标头允许访问:

X-Frame-Options: ALLOW-FROM [uri]

选项示例:

X-Frame-Options: DENY
X-Frame-Options: SAMEORIGIN
X-Frame-Options: ALLOW-FROM https://example.com/

PHP 中的一个例子:

<?php header('X-Frame-Options: SAMEORIGIN'); ?>

您可以在此处进一步阅读: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options

希望对你有所帮助!

【讨论】:

  • allow-from 现在已过时
  • 是否有另一种方式允许来自特定域,因为允许来自已过时。
  • 你可以使用Content-Security-Policy: frame-ancestors 'self' yoursites;
【解决方案2】:

现在推荐使用内容安全策略标头。

来自 MDN 的示例:

// iframe can be embedded in pages on the origin and also on https://www.example.org
Content-Security-Policy: frame-ancestors 'self' https://www.example.org;

更多详情见:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors

【讨论】:

  • 只是为了帮助其他人,我必须在我的 apache conf 中以这种方式设置它。标头集 Content-Security-Policy "frame-ancestors 'self' iframetester.com;"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 2011-11-17
  • 2011-08-25
  • 2016-06-05
  • 2016-07-19
  • 2018-12-18
  • 2021-04-01
相关资源
最近更新 更多