Is it safe to not parameterize an SQL query when the parameter is not a string?

前端 未结 11 836
一生所求
一生所求 2020-12-23 15:57

In terms of SQL injection, I completely understand the necessity to parameterize a string parameter; that\'s one of the oldest tricks in the book. But when can

11条回答
  •  借酒劲吻你
    2020-12-23 16:14

    This is not safe even for non-string types. Always use parameters. Period.

    Consider following code example:

    var utcNow = DateTime.UtcNow;
    var sqlCommand = new SqlCommand("SELECT * FROM People WHERE created_on <= '" + utcNow + "'");
    

    At the first glance code looks safe, but everything changes if you make some changes in Windows Regional Settings and add injection in short date format:

    Now resulting command text looks like this:

    SELECT * FROM People WHERE created_on <= '26.09.2015' OR '1'<>' 21:21:43'
    

    The same can be done for int type as user can define custom negative sign which can be easily changed into SQL injection.

    One could argue that invariant culture should be used instead of current culture, but I have seen string concatenations like this so many times and it is quite easy to miss when concatenating strings with objects using +.

提交回复
热议问题