1

您如何处理需要限制为特定值集的用户输入(unicode),并且您希望将数据传递给应用程序的风险降到最低。例如,如果我要将数据存储在 SQL 中,我希望消除任何 SQL 注入的可能性。如果我要通过 HTTP 通过网络发送它,我想确保它不会使请求不正确,等等。

我想我要问的是有什么通用的数据清理方法吗?

4

3 回答 3

1

Each interface has its own problems when it comes to ways to compromise the system. If you want to play it safe you will need to tailor the validations to suit the problems and/or threats that are relevant in the current context.

If a certain text box in a user interface should be used for numeric input, make sure that the user cannot type (or paste) anything non-numeric into it. If a certain control is used to collect a date from the user, validate that the given value is indeed a valid date (perhaps it should even fall within a certain range; validate that too).

Make sure to url encode anything that is being passed as a query string value in a http request. Use stored procedures and pass the values as parameters to them.

And so on. There is no free lunch, unfortunately.

于 2009-05-21T22:47:42.190 回答
0

如果保存到数据库,这非常简单。只需使用参数(DbParameter 对象) - 它们将保护您免受 SQL 注入,并且在必要时还会添加转义符号。

代码可以是这样的:

OleDbConnection cn = new OleDbConnection(strConn);
cn.Open();
strSQL = "INSERT INTO customers (Name) VALUES (@Name)";
OleDbCommand cmd = new OleDbCommand(strSQL, cn);
cmd.Parameters.Add("@Name", "John O'Brian");
cmd.ExecuteNonQuery();
于 2009-05-21T22:39:48.057 回答
0

就像 nightcoder 建议的那样,参数是避免 SQL 注入的方法。但是,如果您使用的是 SQL,请考虑使用 SqlClient 命名空间,因为它比其 OleDb 对应物更有效,并且是专门为 SQL Server 7 及更高版本创建的。

使用 nightcoder 上面的例子:

SqlConnection cn = new SqlConnection(strConn);
cn.Open();
strSQL = "INSERT INTO customers (Name) VALUES (@Name)";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.Varchar)).Value = "John O'Brian";
cmd.ExecuteNonQuery();

关于 SqlClient 命名空间要记住的一点是,如果您正在为旧系统 (Win98) 编写代码,那么可能存在兼容性问题,这使得 OldDBxxx 成为更好的选择。

干杯!

于 2009-05-22T01:25:18.657 回答