vol*_*one 2 coldfusion coldfusion-10 coldfusion-11
当用户在站点上注册时,我们应该使用EncodeForHTML()还是EncodeForURL()在将值存储到数据库之前?
我之所以这样说是因为当我向包含一个包含电子邮件地址作为URL变量的URL的人发送电子邮件时,我必须使用EncodeForURL().但是如果这个电子邮件地址已经使用了编码EncodeForHTML(),那就意味着我必须先再使用Canonicalize(),然后再使用EncodeForURL()它.
因此,我认为这EncodeForURL()可能是好的,但在将值存储在数据库中时,它是否"安全"和"正确"?
更新:阅读文档后,它说EncodeForURL仅用于在URL中使用值.因此,似乎有意义的是我应该将其存储为EncodedForHTML,然后在URL上下文中使用它时对其进行Canonicalize和重新编码.我不知道所有这些编码会在我的服务器上占用多少性能......?
从我公司的内部文档中复制它.由于想象被阻止@ work,不确定图像是否正确上传.如果是这样,我稍后会重新上传它们.我将来会发布这个和更多相关的内容给Githib回购.
您应该将其存储为简单文本,但请确保在使用AntiSamy库的过程中清理数据.一旦数据安全,请确保使用正确的编码器在出路时对数据进行编码.而且仅供参考,encodeForHTML()和的输出之间存在很大差异encodeForHTMLAttribute().
在下面的示例中,使用来自数据库的数据替换定义电子邮件地址的变量.
PROTIP:不要在CFFORM标签中使用这些编码器.这些标签会为您处理编码.CF 9及以下使用HTMLEditFormat(),CF 10及以上使用最有可能encodeForHTMLAttribute().
基本实现是包括单个电子邮件地址,以便填充新电子邮件窗口的"收件人"字段.
<cfset email = "someone@example.com" />
<a href="mailto:#email#">E-mail</a>Run Code Online (Sandbox Code Playgroud)
<a href="mailto:someone@example.com">E-mail</a>
Run Code Online (Sandbox Code Playgroud)
<cfset email = "someone@example.com" />
<a href="mailto:#encodeForURL(email)#">E-mail</a>Run Code Online (Sandbox Code Playgroud)
请注意,"@"符号被正确地百分比编码为"%40".
<a href="mailto:someone%40example.com">E-mail</a>
Run Code Online (Sandbox Code Playgroud)

如果您打算在页面上显示电子邮件地址作为链接的一部分:
<cfset email = "someone@example.com" />
<a href="mailto:#encodeForURL(email)#">#encodeForHTML(email)#</a>Run Code Online (Sandbox Code Playgroud)
高级实现包括"收件人"和"CC"的电子邮件地址.它还可以预先填充新电子邮件的正文和主题.
<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>Run Code Online (Sandbox Code Playgroud)
<a href="mailto:someone@example.com?cc=someone_else@example.com&subject=This is the subject&body=This is the body">E-mail</a>Run Code Online (Sandbox Code Playgroud)

请注意,subject和body参数包含空格.虽然这个字符串在技术上会起作用,但仍然容易受到攻击向量的影响.
想象一下,body的值是由数据库查询的结果设置的.此记录已被恶意用户"感染",并且默认正文消息具有附加的"BCC"地址,因此某些恶意用户可以获取通过此链接发送的电子邮件的副本.
<cfset body = "This is the body&bcc=someone@evil.com" />Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)

由于"href"是<a>标签的属性,因此您可能会考虑使用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>Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)

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>Run Code Online (Sandbox Code Playgroud)
注意这些关于URL编码器的事情:
<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>Run Code Online (Sandbox Code Playgroud)
