在浏览器中显示电子邮件:安全风险

Showing email in the browser: security risks

我们有一个公司网站接收外部电子邮件,处理它们,并在浏览器中显示给用户。如果可用,我们将以 HTML 格式显示电子邮件。但是,这基本上意味着我们将显示用户生成的 HTML 代码(据我所知,您可以在电子邮件中发送任何 HTML)。

这里有哪些安全隐患?采取哪些措施来最大程度地降低这些风险?

我目前能想到的:

就这些吗?删除 HTML 标签总是容易出错,所以我想知道是否有更好的方法在显示电子邮件时以某种方式禁用外部脚本?

据我所知,安全风险与跨站点脚本 (XSS) 相同。 OWASP 对风险的描述如下:

XSS can cause a variety of problems for the end user that range in severity from an annoyance to complete account compromise. The most severe XSS attacks involve disclosure of the user’s session cookie, allowing an attacker to hijack the user’s session and take over the account. Other damaging attacks include the disclosure of end user files, installation of Trojan horse programs, redirect the user to some other page or site, or modify presentation of content. An XSS vulnerability allowing an attacker to modify a press release or news item could affect a company’s stock price or lessen consumer confidence.

Source

防御它必须层层防御,例如但不限于:

  • DOMPurify 之类的东西对 HTML 进行消毒。
  • 为安全敏感 cookie 使用仅 HTTP cookie,因此无法从 JavaScript 读取它们。 Source
  • 添加内容安全策略,以便浏览器仅信任来自您告诉它信任的域的脚本。 Source

根据您的要求,也可以将电子邮件内容加载到 sandbox iframe 中,作为一项额外的安全措施。可以这样做:

var sanitizedHTML = DOMPurify('<div>...</div>');
var iframe = document.getElementById('iframeId');
iframe.srcdoc = sanitizedHTML;