TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack (3) Thread Tools Search this Thread Display Modes
Old 09-13-2007, 03:32 PM   3 links from elsewhere to this Post. Click to view. #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,654
Thanks: 73
Wildhoney is on a distinguished road
Big Grin The Dangers of the Header Function

The header function may seem relatively straightforward on the surface. You issue the function along with a header as the first argument and it does the rest for you. Many people rely religiously on the header function working to forward users to the next page.

To exemplify this, as a programmer you may code the following lines to be placed into your script:

PHP Code:
if($pMember->doLogin())
{
    
header('location: http://www.talkphp.com/login/success/');
}

$pMember->doLogout(); 
This will login a user if the login is available, otherwise if the doLogin returns false or NULL then it logs the user out. Now, this will work absolutely perfectly if everything goes the way you expect it to. The user is logged in and then forwarded to a page where you can praise them for valid credentials.

However, what if the user is logged in and then logged out straight after? It may seem impossible based on the above code as the header() has been issued to send users to another page before we get down to the doLogout() function.

This is where paying attention may save the integrity of you as a programmer. Or a blossoming programmer in the very least. The header function is a header instruction sent to the client's browser. It is entirely up to the browser whether or not to act on that instruction. In the simplest terms, the browser makes up its own mind whether or not to follow the location to your desired destination.

What would happen if the browser is stubborn and decides not to exit when the location header is issued? That's right! The script will continue executing causing many adverse effects. In our case logging a user out straight after they've logged in may be an annoyance, but at least it doesn't cause any blatant security issues. However, many programmers rely on the header to protect their scripts.

The security issues arise when you realise how many programmers use location to divert users away from code which should not be executed. To exemplify, the following is a good example of where location is used to divert users away from the page if they are accessing it directly and not via another page that includes this page:

PHP Code:
if(!isset($bUsingSSI))
{
    
header('location: http://www.talkphp.com/');

Please see the attachment for this in action. I have emulated the scenario using Telnet as my browser. Telnet is not going to follow any location unless I explicitly instruct it to.

The lesson to be learned today? ALWAYS issue the exit construct after any header(). Like so:

PHP Code:
if($pMember->doLogin())
{
    
header('location: http://www.talkphp.com/login/success/');
    exit;
}

$pMember->doLogout(); 
There is then absolutely no way a user will be logged out if they have been logged in a couple of lines above.
Attached Files
File Type: zip Bypassing Header - TalkPHP.zip (33.9 KB, 134 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.

Last edited by Wildhoney : 09-13-2007 at 05:53 PM.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-13-2007, 06:19 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 753
Thanks: 2
Salathe is on a distinguished road
Default

This isn't so much a danger of using the header function (which can be used for far more than just a Location header) but more of "correctly terminating the script when it needs to terminate".
__________________
Salathe is offline  
Reply With Quote
Old 09-13-2007, 07:56 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,654
Thanks: 73
Wildhoney is on a distinguished road
Default

Precisely. Such as on a location: destination where you are expecting the script to halt. However, as you rightly said, if you're feeding other information to the browser, such as authentication headers, then you are not necessarily going to want to halt the script's execution.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-18-2007, 10:32 AM   #4 (permalink)
The Wanderer
 
Join Date: Sep 2007
Posts: 11
Thanks: 0
Chaos King is on a distinguished road
Default

Very good article. I always tell coders to do that, because you are never too sure what will happen after you send a Location header.

Thanks for bringing out the awareness in simple concepts. People always tend to forget the most basic of rules which cripple their code.
Chaos King is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/absolute-beginners/1102-dangers-header-function.html
Posted By For Type Date
Quick Web Source - your site could be insecure - the dangers of the header function This thread Refback 01-10-2008 02:44 PM
PHP Your Site could be Insecure: The Dangers of the Header Function Tutorial This thread Refback 12-28-2007 09:18 AM
PHP Security Your Site could be Insecure: The Dangers of the Header Function Tutorial This thread Refback 12-22-2007 07:52 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:07 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0