英文:
How to prevent Cross-Site Scripting on form fields in Coldfusion 2016?
问题
项目使用Coldfusion 2016具有基本的关键字搜索功能。安全扫描产生了一个'跨站脚本攻击: 反射'的关键漏洞。'黑客'可能通过表单字段(例如:<aUdIo SrC=x OnErRoR=alert(74358)>)将脚本注入到结果页面中。
表单验证应该在表单页面上通过JavaScript进行吗?还是在提交后,剥离输入中的任何特殊字符,如<>等?我需要修改代码以通过这个安全扫描。
代码:
<input type="text" name="keyword" value="search by keyword" onfocus="clearThis(this)" />
结果页面代码:
<cfset input = "#form.keyword#">
<cfset keyword = rereplace(input, "[^A-Za-z0-9-]", " ", "all") />
提前感谢-
HeatherG
英文:
Project using Coldfusion 2016 has a basic keyword search feature. Security scan produced a 'Cross-Site Scripting: Reflected' critical vulnerability. 'Hacker' could potentially inject scripts into the results page (ie: <aUdIo SrC=x OnErRoR=alert(74358)>) via the form field.
Should the form validation happen via javascript on the form page? Or once submitted, strip the input of any special characters like <>, etc? I need to modify the code for it to pass this security scan.
code:
<input type="text" name="keyword" value="search by keyword" onfocus="clearThis(this)" />
results page code:
<cfset input = "#form.keyword#">
<cfset keyword = rereplace(input,"[^A-Za-z0-9-]"," ","all") />
Thanks in advance-
HeatherG
答案1
得分: 1
AntiSamy Filter
首先,您必须使用AntiSamy过滤器清理所有用户输入。ColdFusion附带了一个默认配置文件,但我找不到相关引用。我建议从这里找到的antisamy-slashdot.xml
文件开始。
在Application.cfc
中加载特定的配置文件(引用文件时应位于应用程序的Web根目录之外)。
<cfset this.security.antisamypolicy = "antisamy-slashdot.xml">
现在,您可以使用本机的ColdFusion函数getSafeHTML()
。
<cfset SafeHTML = getSafeHTML(inputHTML, "", true)>
参数如下:
- 要清理的字符串。
- 指定的配置文件。空字符串引用默认配置文件或应用程序定义的文件。
true
引发错误,false
返回空字符串。
当我实施这个功能时,我创建了一个简单的函数来循环遍历struct
并清理所有字符串,然后将其添加到我的Application.cfc
中。
<cffunction name="getCleanStructData" access="public" output="false" returntype="void">
<cfargument name="data" type="struct" required="true" hint="Form struct">
<cfloop collection="#arguments.data#" item="local.x">
<cfif isSimpleValue(arguments.data[local.x])>
<cfset arguments.data[local.x] = trim(getSafeHTML(arguments.data[local.x], "", ))>
</cfif>
</cfloop>
</cffunction>
最后,我在onRequestStart()
中添加了对该函数的调用:
<cfset getCleanStructData(form)>
<cfset getCleanStructData(URL)>
现在,根据AntiSamy配置文件中定义的规则,所有用户输入数据都已经被清理。您可以安全地处理这些数据,如您所需。
编码用户输入
为了进一步确保您的输出是安全的,当输出用户生成的数据时,您必须有条件地调用ESAPI编码函数之一。
旧版的ColdFusion只有htmlEditFormat
和JSStringFormat
。从ColdFusion 10开始,有一些新的函数,如encodeForHTML()
和encodeForJavaScript()
,可以转义更大的UTF-8字符集。这将把字符串转换为适当的HTML实体,以在网页上显示。例如:
#encodeForHTML('<script>')#
将呈现为:
&lt;script&gt;
更多信息和示例请参阅这里:https://owasp.org/www-community/attacks/xss/
Samy是我的英雄
附加信息:如果您不是在这个蠕虫肆虐MySpace的时代开始开发的,这是我们了解XSS攻击的方式。
英文:
AntiSamy Filter
First, you have to scrub all user input using an AntiSamy filter. CF ships with a default configuration file, but I can't find a reference to which it is. I recommend starting with the antisamy-slashdot.xml
file found here.
Load a specific config file in Application.cfc
(reference the file from outside of the application's web root folder).
<cfset this.security.antisamypolicy = "antisamy-slashdot.xml">
Now you can use the native CF function getSafeHTML()
.
<cfset SafeHTML = getSafeHTML(inputHTML, "", true)>
The parameters are
- The string to clean.
- A specific config file. An empty string references the default or application-defined file.
true
throws an error,false
returns an empty string.
When I implemented this, I created a simple function to loop through a struct
and clean all strings, then added it to my Application.cfc
.
<cffunction name="getCleanStructData" access="public" output="false" returntype="void">
<cfargument name="data" type="struct" required="true" hint="Form struct">
<cfloop collection="#arguments.data#" item="local.x">
<cfif isSimpleValue(arguments.data[local.x])>
<cfset arguments.data[local.x]=trim(getSafeHTML(arguments.data[local.x], "", ))>
</cfif>
</cfloop>
</cffunction>
Finally, I added calls to that function in onRequestStart()
:
<cfset getCleanStructData(form)>
<cfset getCleanStructData(URL)>
Now all user input data is cleaned according to the rules defined in the AntiSamy config file. You should be able to safely process this data as you see fit.
Encoding User Input
To further ensure your output is safe, you have to contextually call one of the ESAPI Encoding Functions when outputting user-generated data.
Old school CF only had htmlEditFormat
and JSStringFormat
. Since CF 10, there are newer functions like encodeForHTML()
and encodeForJavaScript()
that can escape the larger set of UTF-8 characters. This will convert strings into the appropriate HTML entities to display on a web page. For example:
#encodeForHTML('<script>')#
will render like this, stopping the linked file from being executed.
&lt;script&gt;
More info and examples here: https://owasp.org/www-community/attacks/xss/
Samy is My Hero
Bonus: If you weren't in dev back when this worm ravaged MySpace, here's how we learned XSS was a thing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论