Avoiding SQL injection in a user-generated SQL-regex

半世苍凉 提交于 2019-11-29 15:24:52

If you use prepared statements, SQL injection will be impossible. You should always use prepared statements.

Roborg makes an excellent point though about expensive regexes.

Greg

You should just pass the string through mysql_escape_string or mysql_real_escape_string.

I'd be wary of accepting any old regex though - some of them can run for a long time and will tie up your DB server.

From Pattern Syntax:

Beware of patterns that contain nested indefinite repeats. These can take a long time to run when applied to a string that does not match. Consider the pattern fragment (a+)*

This can match "aaaa" in 33 different ways, and this number increases very rapidly as the string gets longer.

If it is anly for the purposes of display this reg expression then most programs simply Html Encode the value and store in the DB and then the Decode on the way out. Again only for Display purposes though, if you need to use the reg exp that is submitted this won't work.

Also know there is a method where the person intent on injecting writes out there SQL, Converts it to varbinary and submits the exec command with the base 64 representation of the query which I have been hit with in the past.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!