从我公司的内部文档中复制此内容。不确定图像是否正确上传,因为 imagr 被阻止@工作。如果是这样,我稍后会重新上传。将来我会将此内容和更多相关内容发布到 Gihib 存储库。
您应该将其存储为简单的文本,但请确保在使用AntiSamy 库的过程中清理您的数据。一旦数据安全,请确保在输出时使用适当的编码器对数据进行编码。仅供参考,encodeForHTML() 和 encodeForHTMLAttribute() 的输出之间存在很大差异。
在以下示例中,将定义电子邮件地址的变量替换为数据库中的数据。
PROTIP:不要在 CFFORM 标签中使用这些编码器。这些标签会为您处理编码。 CF 9 及以下使用HTMLEditFormat(),CF 10 及以上最有可能使用encodeForHTMLAttribute()。
简单实现
一个基本的实现是包含一个电子邮件地址,以便填充新电子邮件窗口的“收件人”字段。
CFML
<cfset email = "someone@example.com" />
<a href="mailto:#email#">E-mail</a>
HTML 输出
<a href="mailto:someone@example.com">E-mail</a>
具有正确编码的CFML
<cfset email = "someone@example.com" />
<a href="mailto:#encodeForURL(email)#">E-mail</a>
编码的 HTML 输出
请注意,“@”符号被正确地百分比编码为“%40”。
<a href="mailto:someone%40example.com">E-mail</a>
点击结果
如果您打算在页面上显示电子邮件地址作为链接的一部分:
<cfset email = "someone@example.com" />
<a href="mailto:#encodeForURL(email)#">#encodeForHTML(email)#</a>
攻击向量
高级实现包括“收件人”和“抄送”的电子邮件地址。它还可以预先填充新电子邮件的正文和主题。
CFML 无编码
<cfset email = "someone@example.com" />
<cfset email_cc = "someone_else@example.com" />
<cfset subject = "This is the subject" />
<cfset body = "This is the body" />
<a href="mailto:#email#?cc=#email_cc#&subject=#subject#&body=#body#">E-mail</a>
HTML 输出
<a href="mailto:someone@example.com?cc=someone_else@example.com&subject=This is the subject&body=This is the body">E-mail</a>
点击结果
请注意,subject 和 body 参数包含空格。虽然这个字符串在技术上可以工作,但它仍然容易受到攻击。
想象 body 的值是由数据库查询的结果设置的。此记录已被恶意用户“感染”,并且默认正文消息附加了“密件抄送”地址,因此某些恶意用户可以获取通过此链接发送的电子邮件的副本。
受感染的数据
<cfset body = "This is the body&bcc=someone@evil.com" />
HTML 输出
<a href="mailto:someone@example.com?cc=someone_else@example.com&subject=This is the subject&body=This is the body&bcc=someone@evil.com">E-mail</a>
点击结果
为了阻止这个 MAILTO 链接被感染,这个字符串需要被正确编码。
带有 HTML 属性编码的 CFML
由于“href”是 标签的一个属性,您可能会考虑使用 HTML 属性编码器。 这是不正确的。
<cfset email = "someone@example.com" />
<cfset email_cc = "someone_else@example.com" />
<cfset subject = "This is the subject" />
<cfset body = "This is the body&bcc=someone@evil.com" />
<a href="mailto:#encodeForHTMLAttribute(email)#?cc=#encodeForHTMLAttribute(email_cc)#&subject=#encodeForHTMLAttribute(subject)#&body=#encodeForHTMLAttribute(body)#">E-mail</a>
HTML 输出
<a href="mailto:someone&#x40;example.com?cc=someone_else&#x40;example.com&subject=This&#x20;is&#x20;the&#x20;subject&body=This&#x20;is&#x20;the&#x20;body&amp;bcc&#x3d;someone&#x40;evil.com">E-mail</a>
点击结果
带有 URL 编码的 CFML
MAILTO 链接的正确编码是通过 URL 编码器完成的。
<cfset email = "someone@example.com" />
<cfset email_cc = "someone_else@example.com" />
<cfset subject = "This is the subject" />
<cfset body = "This is the body&bcc=someone@evil.com" />
<a href="mailto:#encodeForURL(email)#?cc=#encodeForURL(email_cc)#&subject=#encodeForURL(subject)#&body=#encodeForURL(body)#">E-mail</a>
正确编码的 HTML 输出
请注意有关 URL 编码器的以下内容:
- 每个空格 (" ") 都转换为加号 ("+"),而不是其预期的百分比值 ("%20")。
- 否则使用百分比 ("%") 值进行编码。
- 由于对各个查询参数进行了编码,因此连接每个参数的与号 ("&") 未编码。
- “body”参数编码时,包含恶意注入的“&body=”字符串。这整个字符串现在是邮件正文的一部分,可防止电子邮件的意外“密件抄送”。
<a href="mailto:someone%40example.com?cc=someone_else%40example.com&subject=This+is+the+subject&body=This+is+the+body%26bcc%3Dsomeone%40evil.com">E-mail</a>
点击结果
加号是什么意思? 正确解码这些 URL 编码值取决于各个邮件客户端(例如 Outlook、GMail 等)。