简体   繁体   English

PHP 在浏览器中安全地查看不受信任的 email 内容

[英]PHP Securely view untrusted email content in browser

What would be the recommended way to securely view emails in a browser (in PHP)?在浏览器(PHP)中安全地查看电子邮件的推荐方法是什么?

Emails are highly insecure content and desktop email software obviously implements only a very limited subset of HTML and no javascript at all to prevent attacks.电子邮件是高度不安全的内容,桌面 email 软件显然只实现了 HTML 的一个非常有限的子集,根本没有实现 javascript 来防止攻击。 But if I'd take an email HTML source and display it in a browser, javascript code and other stuff would be executed.但是,如果我将 email HTML 源代码显示在浏览器中,则会执行 javascript 代码和其他内容。

I thought a solution would be to send a header like this along with the email source:我认为一个解决方案是像这样发送 header 以及 email 源:

header("Content-Security-Policy: sandbox");

But this would prevent me from fetching inline images from the server as I still would need a PHP session id to be transmitted to understand that the user is allowed to fetch this content.但这会阻止我从服务器获取内联图像,因为我仍然需要传输 PHP session id 以了解允许用户获取此内容。

As there are many web email clients out there I wonder if there is a best practice model.由于有很多 web email 客户,我想知道是否有最佳实践 model。

(FYI: I try to implement my own web email tool fitting to specific needs of a larger software suite) (仅供参考:我尝试实现自己的 web email 工具以满足更大软件套件的特定需求)

You can address the issue of images by not requiring authentication and then making the URLs hard to guess (ex: <img src="/resources/SomeReallyLongHardToGuessRandomString"> ).可以通过不需要身份验证然后使 URL 难以猜测来解决图像问题(例如: <img src="/resources/SomeReallyLongHardToGuessRandomString"> )。

More broadly though, securely displaying user generated HTML is hard.但更广泛地说,安全地显示用户生成的 HTML 是很困难的。 Like really hard.就像真的很难。 This is a case where you should use a library.在这种情况下,您应该使用库。 Keep in mind that you might have a user with a browser that is too old for the Content-Security-Policy header. This browser would happily run any scripts on the page.请记住,您的用户可能使用的浏览器对于Content-Security-Policy header 来说太旧了。该浏览器会很乐意在页面上运行任何脚本。 HTML Purifier is my personal choice, but there are others. HTML Purifier是我个人的选择,但也有其他的。 Also, keep in mind that this is a dependency you will want to update often as people are constantly discovering new bugs.另外,请记住,这是一个您需要经常更新的依赖项,因为人们会不断发现新的错误。

As an additional line of defense, many sites use a seperate domain for user generated content.作为一道额外的防线,许多站点为用户生成的内容使用单独的域。 For example Google uses googleusercontent.com.例如谷歌使用 googleusercontent.com。 That way if something does slip by, they haven't compromised the whole application.那样的话,即使有什么东西漏掉了,他们也不会危及整个应用程序。 Note that this would still be bad, as an attacker might be able to read user content they shouldn't be able to (emails in this case).请注意,这仍然很糟糕,因为攻击者可能能够读取他们不应读取的用户内容(在本例中为电子邮件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM