Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
Using the vBulletin Input Cleaner
Alan @ CIT
Join Date: Nov 2004
Posts: 625

South UK
by Alan @ CIT Alan @ CIT is offline 22 Jun 2006

Note: This article assumes that you are familier with PHP, and will introduce you to input filtering using vBulletin

Using the vBulletin Input Cleaner Class

Introduction

Most scripts will require data from a user at some point. When using this data, you should never assume that it is "clean" data. With XSS (Cross-Site Scripting) and SQL exploits being identified in scripts on a daily basis, you should do everything you can to ensure that all data coming from the user has been cleaned ("sanatized").

vBulletin provides us with the vB_Input_Cleaner class to do just this.
The vBulletin Input Cleaner class is setup when the page loads, and can be accessed as $vbulletin->input,

Data Types

When you accept data from the user, you should know what type of data you are expecting to receive. the vBulletin Input Cleaner allows the following types of data to be cleaned:
  • TYPE_NOCLEAN
    Will not be cleaned
  • TYPE_BOOL
    Will check it is either true or false
  • TYPE_INT
    Will check that it is an integer
  • TYPE_UINT
    Will check that it is an unsigned integer
  • TYPE_NUM
    Will check that it is a number
  • TYPE_UNUM
    Will check that it is an unsigned number
  • TYPE_UNIXTIME
    Will check that it is a unix-style timestamp (unsigned int)
  • TYPE_STR
    Will check that it is a string, and runs trim() on it
  • TYPE_NOTRIM
    Will check that it is a string and will not run trim() on it
  • TYPE_NOHTML
    WIll check that it is a string and run htmlspecialchars_uni() and trim() on it
  • TYPE_ARRAY
    WIll check that it is an array
  • TYPE_FILE
    Will check that it is a file (ie, uploaded by the user)
You can also clean arrays of these types by using TYPE_ARRAY_<type>. For example, if you had an array of numbers, you could use TYPE_ARRAY_INT, or TYPE_ARRAY_NUM.

Cleaning Functions

The input cleaner class provides a number of useful functions that we can use to clean our data, depending on what data you wish to clean.

Cleaning Superglobal Arrays

By Superglobal, I mean $_POST, $_GET, $_REQUEST and so on. These arrays are created automaticly by PHP and contain the user-sent input. They are referenced in the vBulletin Input Cleaner by nice short single letter names. These are:
  • p - $_POST
  • g - $_GET
  • r - $_REQUEST
  • s - $_SERVER
  • e - $_ENV
  • c - $_COOKIE
  • f - $_FILES
The vBulletin Input Cleaner class provides the clean_array_gpc() function which allows us to clean data in these Superglobal arrays in one hit, without having to clean every individual variable in them.

Example:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

As you can see from this example, clean_array_gpc() takes 2 paramaters. The first paramater specifies which Superglobal array you wish to clean, and the second is an array of variables and their types.

So, in the example above, we are telling clean_array_gpc() that we wish to clean the $_POST array, and that $_POST contains 3 variables, 'name', 'age', and 'usepm', and that we wish to clean them as TYPE_NOHTML, TYPE_UINT and TYPE_BOOL respectivly.

Once cleaned, the new (clean) variables will be available in the $vbulletin->GPC array. So, to follow on from our previous example, we would use something like:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Cleaning a Single Superglobal Variable

If you have a single variable that you wish to clean, use the clean_gpc() function. This function allows you to specify a single variable in any of the Superglobal arrays, and it's type.

Example:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

In this example, the 'age' variable in the $_GET Superglobal array will be cleaned to make sure it is an unsigned integer.

Cleaning a Single Variable

If you wish to clean a single variable that is not in one of the Superglobal arrays, you should use the clean() function.

Example:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

From this example you can see that clean() takes 2 paramaters. The first is the variable that you wish to clean and the second is its type. Unlike the last 2 functions, clean() returns the variable directly.

Cleaning an Array of Variables

For times when you wish to clean an array of variables of mixed types, vBulletin provides the clean_array() function. The clean_array() function takes 2 paramaters. The first is the array to be cleaned, and the second is an array of variable names, and their types.

This function works exactly the same as clean_array_gpc(), except instead of specifying which Superglobal array to clean, you specify your own array.

Conclusion

So, to sum up - always run all input from the user through the vBulletin Input Cleaner! As well as being a good coding practice, this will drasticly decrease the chances of someone exploiting your script using an XSS or SQL attack.

Good luck using your new found knowledge of the vBulletin Input Cleaner class, and remember: If you get stuck, just ask! Knowledge sharing is what vBulletin.org is all about!

(Note: If you want to reproduce this article anywhere, I have no objections, but I do request that you give me credit for writing it, and a PM letting me know would be appreciated )
Views: 19505
Reply With Quote
Comments
  #2  
Old 22 Jun 2006, 20:40
noppid noppid is offline
 
Join Date: Mar 2003
Another fine article. Good work.
__________________
Add a photo gallery garage to your auto forum and get noticed by your members. Find out how at the vBulletin Computer Help Forum.
An informed rider makes their first destination the motorcycle forum at rider info.
Reply With Quote
  #3  
Old 22 Jun 2006, 20:56
-=Sniper=-'s Avatar
-=Sniper=- -=Sniper=- is offline
 
Join Date: May 2002
very nice. thanks dude
Reply With Quote
  #4  
Old 28 Jun 2006, 22:49
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
Thanks Alan, i have been trying to use the cleaners as opposed to $_GET, $_POST, etc... this should help me well.
__________________
Please feel free to PM me about custom work, installations, and upgrades!

Eclipse Records - Streetcult Street Team

Last edited by Antivirus : 17 Jul 2006 at 04:26.
Reply With Quote
  #5  
Old 17 Jul 2006, 05:10
markp_2000 markp_2000 is offline
 
Join Date: Jul 2006
Error in variable

This is great. One question for a novice hack. How do we know there as something wrong? If I wanted a number and the user input a letter how would I know there was an error?

Do I need to further determine the cleaned variable is a number?

Mark
Reply With Quote
  #6  
Old 17 Jul 2006, 17:53
harmor19 harmor19 is offline
 
Join Date: Apr 2005
Real name: Andrew Harmor
You'd use
if(!is_numeric($vbulletin->GPC['age']))
Reply With Quote
  #7  
Old 17 Jul 2006, 18:11
Alan @ CIT Alan @ CIT is offline
 
Join Date: Nov 2004
Location: South UK
Real name: Alan
For reference, when checking numbers, if vBulletin doesn't find a number, it will return 0 instead. (when using TYPE_INT or TYPE_UINT)

The types and what it will return if it can't find what you are asking for is below:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Thanks,
Alan.
__________________
IMPORTANT: I'm afraid that I no longer develop software / modifications for vBulletin. However, thank you to everyone who installed / supported my modifications!
Reply With Quote
  #8  
Old 18 Jul 2006, 04:13
markp_2000 markp_2000 is offline
 
Join Date: Jul 2006
Thanks for the information. So I'm thinking I need to check the variables even before it posts with javascript or with php.
Reply With Quote
  #9  
Old 19 Jul 2006, 09:19
Danny.VBT's Avatar
Danny.VBT Danny.VBT is offline
 
Join Date: Oct 2004
You can always do:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

If the value of 0 isn't a possibility.
__________________
while($coffeeMachine->supply($Danny)) { $Danny->run(); }

Twitter Blog
Reply With Quote
  #10  
Old 08 Nov 2006, 15:57
Antivirus's Avatar
Antivirus Antivirus is offline
 
Join Date: Sep 2004
how would you go about cleaning $_SESSION arrays? I have been cleaning the vars before placing them into $_SESSION but I wonder if there's some way to clean the $_SESSION afterwards.?
__________________
Please feel free to PM me about custom work, installations, and upgrades!

Eclipse Records - Streetcult Street Team

Last edited by Antivirus : 15 Nov 2006 at 22:39.
Reply With Quote
  #11  
Old 22 Jan 2007, 05:44
Billspaintball's Avatar
Billspaintball Billspaintball is offline
 
Join Date: Sep 2003
Real name: Bill
Ohhh just what I was looking for.

Thanks Alan, its all clear now
Reply With Quote
  #12  
Old 14 Feb 2007, 16:33
ccasselman ccasselman is offline
 
Join Date: Sep 2006
Is there anyway I can use this class for an external script?

How would I approach that?

chad
Reply With Quote
  #13  
Old 24 Feb 2007, 16:53
Adrian.'s Avatar
Adrian. Adrian. is offline
 
Join Date: Oct 2005
Real name: Adrian
Does this work on vB 3.0.x?

I can't seem to get it working.
__________________
Reply With Quote
  #14  
Old 07 Apr 2007, 08:54
adhari_com's Avatar
adhari_com adhari_com is offline
 
Join Date: May 2004
Real name: Hassan AlDoy
Thank you so much for the great useful info, but I need to ask the TYPE_NOHTML does get rid of the <script> entry?
Reply With Quote
  #15  
Old 23 Apr 2007, 22:45
cashpath cashpath is offline
 
Join Date: Jul 2003
Does this still work?

I put this..


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

And when I pass
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

I get a mysql error
Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

__________________
HizzleBall
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Article Options

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Jump


New To Site? Need Help?

All times are GMT. The time now is 19:27.

Layout Options | Width: Wide Color: