The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
[How To] Never Worry About SQL Injections and Queries Again
Why use this? Queries are normally cumbersome to write for vBulletin, and when managing a lot of queries in even a small-medium-sized, it is easy to miss sanitizing an input here or there. If you use this tool, the SQL Injection prevention is completely automatic, queries are easier to create and maintain, and blocks of code that are common among similar queries are eliminated. What is this? This tool prevents SQL injections by placing a layer of protection between the query and database. First, it is important to note that preventing SQL injections is top priority. It takes 1 SQL Injection for someone to take over an entire forum, as history has proven time and time again. After developing a few dozen add-ons, I realized that queries are annoying to write for vB, especially with this fact in mind. I constantly found myself checking queries over and over to make sure everything was safe. It became a big problem for applications that were too small to be moved over to a PHP framework (which has tools like this built in), but too large to police each database query. This tool is intended for add-on authors or people adding custom functionality to their forums themselves. How is this used? This tool will require only basic knowledge of strings, arrays, functions, and printf's format -- nothing specific to vBulletin. If you are already writing vBulletin queries, then you are mostly ready. How to use this is listed below. Let's compare the common ways of executing queries versus using Extended Databases. 1. Fetch one record; we are getting the username and userid of a user with the email address we input. vB_Database::query_first (common way) PHP Code:
PHP Code:
2. Fetch multiple records; we are getting the usernames for each user who has a username starting with our input (e.g. we input lancer, it returns lancerforhire). vB_Database::query_read_slave (common way) PHP Code:
PHP Code:
3. Fetch a single field from a single record; we are getting the first full username for the user who has a username starting with our input (e.g. we input lancer, it returns lancerforhire). New in 1.4! vB_Database::query_first (common way) PHP Code:
PHP Code:
4. Fetch multiple records; we are getting the username, user id, and user group id of all users whose primary groups match our input (array). New in 1.4! vB_Database::query_read_slave (common way) PHP Code:
PHP Code:
ExtendedDatabases_Query::write is the successor to vB_Database::query_write; I do not believe this warrants another example. --- Implement this Right Now If you are a coder, implement this immediately. If you are not a coder, ask the developers of your installed add-ons to do it. It's very simple and has long term benefits, but if you don't do it right now, chances are you never will (unless your forum implodes). --- How do I get this? Download the product-extended_databases_ Xml and add it (extended_databases) as a product dependency. Yes; that's it! |
#2
|
|||
|
|||
Interesting idea - it definitely would help by making sure that any strings are escaped. However, I don't agree with your assertion that it's "a lot simpler". In fact, I find the second example to be a bit misleading because normally you would not collect all the records in an array, but instead you'd process them in a loop. So the second case using your product would require a for loop after it to process the records - the same loop that appears in the "common way" example. Also, collecting all the records in an array holds them all in memory at the same time, which could be an issue for a query that returns a lot of large records. One more thing that I admit is nit-picking - you probably would not need to worry about escaping a userid that was passed in $vbulletin->GPC['userid'] because you would have "cleaned" it using TYPE_INT (and you'd probably want to check for it being a postivie integer before going ahead with a query).
That said, it's a good idea, and there are probably people who aren't confident in their ability to ensure that all strings are escaped before using them in a query. Can't argue with the fact that that's been the issue in a couple of recent mod security problems, so using this would probably have avoided them. Thanks for sharing this. |
#3
|
|||
|
|||
Quote:
I really do hope coders start using this; it would change hundreds if not thousands of lives. My pleasure to share. |
#5
|
|||
|
|||
Ah, sorry!
|
#6
|
||||
|
||||
your threads are just wonderful man !
|
#7
|
|||
|
|||
|
#8
|
||||
|
||||
Hmm - wouldn't a good way be to use stored procedures ?
Then this problem is solved on the DB level instead of trying to escape some input - which won't hurt too for sure. And the DB user would just need the EXECUTE permission, no more "dangerous" things like CREATE TABLE, DROP etc. |
#9
|
|||
|
|||
Quote:
|
#10
|
||||
|
||||
Prepared Statements (this efectively is smth. "like prepared statements light" ) are great, but hard to extend.
Quote:
You would need a stored precedure for everything but the kitchen sink to really prevent all prossible injections. |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|