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
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack (3) Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 09-13-2007, 02: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: 2,280
Thanks: 90
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, 206 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 04: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
 


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 10:42 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design