(抱歉,如果这是一个愚蠢的问题......)
Veracode 报告我的网站存在与使用 web.config 中的连接字符串相关的安全问题。
这是我的代码。
Public Function ExecuteScalar(ByVal sql As String) As Object
Dim obj As Object = Nothing
Try
Dim connStr as String = ConfigurationManager.ConnectionStrings("mydatabase").ConnectionString
Using conn As New SqlConnection(connStr) '''Veracode reports the issue come from this line
conn.Open()
If conn IsNot Nothing Then
'''execute my sql
End If
End Using
Catch ex As Exception
Throw ex
End Try
Return obj
End Function
Veracode 说:
This call to system_data_dll.System.Data.SqlClient.SqlConnection.!newinit_0_1() allows external control of system settings. The argument to the function is constructed using user-supplied input, which can disrupt service or cause an application to behave in unexpected ways. The first argument to !newinit_0_1() contains tainted data from the variable connStr. The tainted data originated from earlier calls to system_web_dll.system.web.httprequest.get_item, system_data_dll.system.data.common.dbdataadapter.fill, system_data_dll.system.data.sqlclient.sqlcommand.executescalar, and fmmobile8_dll.virtualcontroller.vc_wcfentry.
修复:
Never allow user-supplied or otherwise untrusted data to control system-level settings. Always validate user-supplied input to ensure that it conforms to the expected format, using centralized data validation routines when possible.
CWE 报告了相同的使用情况:http://cwe.mitre.org/data/definitions/15.html
好的,Veracode 的建议是我应该在使用连接字符串创建 SqlConnection 对象之前检查它的格式。
我还向Google教授询问了如何检查连接字符串的格式。但返回的结果表明我们应该创建 SqlConnection 对象,然后打开它。
如果响应正常,则连接字符串也意味着格式有效。否则,连接字符串无效。
不幸的是,Veracode 不接受这个答案。
所以,我的问题是:
我们应该在创建 SqlConnection 对象之前检查连接字符串的格式吗(如 Veracode 所说)?如果是,怎么办?
最佳答案
问题不在于连接字符串的格式,而在于它可能被非预期的人控制。例如,攻击者可能能够更改您的 web.config 并使您的应用程序连接到虚假数据库以提供虚假数据。请注意,此类攻击者可能来自组织内部(心怀不满的 IT 运营员工),也可能是已获得一定级别访问权限的外部攻击者。
所以问题是,根据您的威胁模型,您是否信任您的 web.config 文件。您这样做可能有几个原因(您有良好的流程来降低风险),在这种情况下,用 Veracode 术语来说,这将是“通过设计来缓解”。
基本上,这只是一个警告,以引起注意,web.config 在某种意义上是您的应用程序的外部,并且可以由比您最初想象的更多的人更改,并且由非预期的人更改它可能会导致不需要的结果。
关于asp.net-mvc - 系统或配置设置的外部控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40837436/