tags:

views:

518

answers:

7

My henchman is learning PHP currently,
and he sent me his PHP code,
and I found that he uses $_REQUEST in his code.

The textbook I read says that
$_REQUEST has security problem so
we better use $_POST.

so I replied to the henchman that
we better to use $_POST.

Is this OK?

+16  A: 

Well, the reason that $_REQUEST has issues is that it picks up values from $_GET, $_POST, and $_COOKIE, which means that if you code things certain ways and make certain invalid trusting-the-client assumptions, a malicious user could take advantage of that by supplying a value in a different place than you expected and overriding the one you were trying to pass.

This also means that you may have given your henchman incorrect instructions, because it may have been a GET or COOKIE value that he was picking up from $_REQUEST. You would need to use whatever place the value you're looking for actually shows up, not necessarily $_POST.

chaos
That said, it's always entirely possible that any of those values is being edited anyway. There's plenty of addons to allow you to change POST data on the fly for Firefox (and I assume other browsers too). Changing GET and COOKIE data is trivial on just about any browser.
Matthew Scharley
This raises a question. If something is suppose to be 'secure', shouldn't it be able to accept parameters from any of them, and simply accept or deny it based on the actual data. If it is a duplicate entry because they refreshed it, shouldn't you see that it is a duplicate? I mean, it is really just data. Shouldn't matter how exactly you entered it.Yes/No?
Chacha102
@matthew You could always post in a form from a specially crafted page elsewhere, with your own set of data.
Chacha102
thanks. .
jim-prove
@Chacha102: I agree, as far as ideals are concerned, but it's a lot easier to teach the base of PHP users to use $_GET and $_POST than it is to teach them to not trust the client (hell, even what's happening on the client and what's happening on the server), to validate data and operations, and to generally write secure code.
chaos
@chaos: that's the problem. We're trying to make a list of rules to get a secure app, and the only way to do it that works is to know what you're doing, not follow a rulebook.
Joeri Sebrechts
Yeah, that's the problem, all right. But since we can't stop people who don't know what they're doing from writing web applications using PHP, we have to do the best we can. cf. http://en.wikipedia.org/wiki/Harm_reduction
chaos
+5  A: 

It's certainly okay to tell people to use $_POST instead of $_REQUEST. It is always better to be more sure about where your getting your data.

acrosman
thanks .
jim-prove
Just blindly advising to use $_POST is a bad idea -- $_REQUEST contains cookies and query params, and the developer may be intending the data to come from those sources. A better answer would be "It's OK to tell people to use $_POST, $_GET or $_COOKIE if you know where the data's supposed to come from"
Josh
+21  A: 

I would say that it is dangerous to characterise $_POST as more secure than $_REQUEST.

If the data is not being validated and sanitized before being used, you have a possible vector of attack.

In short: It doesn't matter where the data comes from if it is not being handled in a secure manner.

Toby Hede
I wish I could upvote more - data from the client can NEVER be trusted - I can fake POST data as easily as anything else. The fact that it's not easily visible in the address bar only means that I have to pull out my packet sniffer.ALWAYS validate incoming data and work on the assumption that any data from the client may have been sent with evil intentions.
Eclipse
While what you say it's true, I'd still advocate to get the data from where it is supposed to come from. Not to skip validation, but to ease it.
Vinko Vrsalovic
+7  A: 

As was mentioned already in several answers: Any data coming from the client cannot be trusted and must be treated as being malicious by default. This includes $_POST, $_GET, $_COOKIE and $_REQUEST (the combination of the former) but others as well.

When talking about some of them being more dangerous than others I would indeed separate $_GET and $_REQUEST (as it includes $_GET) out from $_POST, as it is slightly harder to generate, i.e. manipulate, a POST request than a GET request. The emphasis here is slightly, but using POST for sensitive operations at least removes another layer of low hanging fruits to exploit.

Especially when it comes to Cross Site Scripting (or XSS) and cookie theft, it is rather easy to get a victim's browser to issue a GET request to the server under attack by simply inserting a hidden image with a manipulated URL into a page or forging a link.

Issuing a POST request at least requires some JavaScript, which is slightly harder to inject into the victim's browser for execution (depending on the situation). Obviously POST requests can be generated by attackers directly, so they can't be trusted either, but for scenarios where an attacker is going through a 3rd party browser, they are a little bit harder to manipulate.

Security is always about making it as hard as possible to break your application - taking implementation constraints etc. into account. It can never be about being 100% secure. So it's best practise to choose the alternative which is more difficult to exploit, even if the difference is marginal, when having the choice between different implementation approaches.

In the end it is always about removing low hanging fruits. Sure, POST requests can be manipulated as well, but for any operation that has an elevated risk, use a POST request and restrict yourself to using $_POST in your code. That way you have have already excluded some very easy GET drive-by attacks and can now focus on validating your POST data. Just don't assume that using POST suddenly made the operation safe by default.

Christian Hang
$_REQUEST is the combination of $_GET, $_POST *and* $_COOKIE. And $_COOKIE is not plural
Josh
Thanks for pointing that out, I just fixed that
Christian Hang
A: 

The most secure way is to verify and validate the data. I usually generate a random unique id for a form and store it in the user's session, but this is easily bypassed by a determined attacker. Much better is to clean up all incoming data. Check out htmlspecialchars() and its related function. I also use a third party utility for cross-site, like HTML Purfier

On some practical notes, always use intval() what is supposed to be numeric, escape all incoming strings, use regex for phone numbers, emails or anything that is going to be part of a SQL query.

Hope this helps.

Extrakun
A: 

There's no real security difference between using $_POST and $_REQUEST, you should sanitise the data with equal scrutiny.

The biggest problem with $_REQUEST is you may be trying to get data from a POST'd form, but might have a GET parameter with the same name. Where will the data come from? It's best to explicitly request the data from where you expect it, $_POST in that example

There are slight security benefits - it's easier to perform XSS (more specifically XSRF) attacks on GET parameters, which is possible if you use $_REQUEST, when you really just want POST data..

There's very few situations when you need data either from POST, GET or cookie.. If you want to get POST data, use $_POST, if you want to get data from from GET parameters, use $_GET, if you want cookie data, use $_COOKIE

dbr
+1  A: 

@Christian:

When talking about some of them being more dangerous than others I would indeed separate $_GET and $_REQUEST (as it includes $_GET) out from $_POST, as it is slightly harder to generate, i.e. manipulate, a POST request than a GET request. The emphasis here is slightly, but using POST for sensitive operations at least removes another layer of low hanging fruits to exploit.

Bzzt. Sorry, but this just ain't true.

Anybody who understands the difference between GET and POST or how unsanitized inputs might be exploitable, won't hesitate for a second to fire up Tamper Data.

Some people have it right here: there is NO security lost or gained by using $_REQUEST in a well-designed system.

mehaase