How to prevent Sql-Injection on User-Generated Sql Queries

前端 未结 15 2183
伪装坚强ぢ
伪装坚强ぢ 2021-02-10 11:55

I have a project (private, ASP.net website, password protected with https) where one of the requirements is that the user be able to enter Sql queries that will directly query t

15条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-10 12:28

    Plenty of answers saying that it's a bad idea but somethimes that's what the requirements insist on. There is one gotcha that I haven't spotted mentioned in the "If you have to do it anyway" suggestions though: Make sure that any update statements include a WHERE clause. It's all too easy to run

    UPDATE ImportantTable
    SET VitalColumn = NULL
    

    and miss out the important

    WHERE UserID = @USER_NAME
    

    If an update is required across the whole table then it's easy enough to add

    WHERE 1 = 1
    

    Requiring the where clause doesn't stop a malicious user from doing bad things but it should reduce accidental whole table changes.

提交回复
热议问题