views:

500

answers:

7

I use my PHP backend to detect Ajax like this: $_SERVER['HTTP_X_REQUESTED_WITH']. This gives me a reliable detection, making sure the request is Ajaxed.

How can I make sure the request came from my own domain, and not an external domain/robot?

www.example.com/ajax?true could allow anyone to make an Ajax call and cut the information.

I could make sessions for everyone that enters my website normally, and then allow Ajax calls.. but that can be faked too.

Does it even matter, in these days?

A: 

Check the $_SERVER['HTTP_REFERER']. This will work in many cases, but shouldn't be confused for a completely-secure solution.

Jonathan Sampson
This will give you what you need, but is not reliable, as the browser can put anything it likes in this. It is easy to fake, so the value can not be truly trusted. Fundamentally, you can not guarantee that the request came from anywhere, even if you issue tokens to the page and make your ajax requests pass the token back to you. Really, you have to ask yourself, does it matter?
rikh
+7  A: 

You can check the HTTP_REFERRER, but not all browsers set it. The best way is to write a wrapper for your ajax calls on the JavaScript side which sends part of document.cookie back to the server--only your domain has access to the cookie. You can compare the cookie in the request headers with the cookie in the AJAX call in php.

In response to, "does it even matter, these days"--YES, it does! Read this.

Annie
I'm not sure this method would accomplish anything except security through obscurity. It's trivial to craft a web request, which is all an Ajax request is. It's just slightly less trivial to examine the Javascript wrapper and add whatever it would be adding. All this does is emulate a session token.
zombat
I guess it might keep robots at bay, if they didn't use Javascript. But it woudln't make the request *secure*, per se.
zombat
This method requires access to the session cookie. If a robot has access to the session cookie, then yes, the site is not secure. But you also have bigger problems than XSRF!
Annie
I read the blog article, it was extremely informative since this subject is new to me.From my checkups, CakePHP does that automatically, and resets the cookie if the browser has been restarted. That should handle registered users.What happens, in public areas against robots, going around?example.com/blog/title1?ajax=true | example.com/blog/title2?ajax=trueAbout "does it matter nowadays", I guess that I simply can't stop a robot from crawling easily through all of my public areas.
Yossi
About public areas, you can use spam prevention techniques like captchas: http://en.wikipedia.org/wiki/CAPTCHA
Annie
A: 

@Jonathan Sampson - I can catch the request in something like BurpSuite and change the HTTP_REFERER...so I dont think that's the solution. I can also do it in a script w/curl.

Mr-sk
A: 

David Walsh has a good solution

/* decide what the content should be up here .... */
$content = get_content(); //generic function;

/* AJAX check  */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    /* special ajax here */
    die($content);
}

/* not ajax, do more.... */
Ben Shelock
That has nothing to do with securing the request.
Xeoncross
+4  A: 

Let you Controller

  • generate access token
  • store in session for later comparison

In your View

  • declare the access token as JS variable
  • send the token with each request

Back in your Controller

  • validate HTTP_X_REQUESTED_WITH
  • validate token

Check these security guidelines from OpenAjax.
Also, read the article on codinghorror.com Annie linked.

Gordon
+1 Since browser-based AJAX requests send cookies with each request - all you need is to add the a token check on each page.
Xeoncross
+1  A: 

Really, the most secure way to do this is to, as you suggested, use server-side sessions, as these cannot be crafted as cookies can.

Granted, someone can still hijack a session ID, but if you also store the user's IP address in their session and check it on each request, you can weed out a lot of hijacks. Only someone on the same LAN or proxy could hijack it.

Any other method mentioned--cookies, javascript, http referer--depends on client-side data, which is insecure and should always be suspected of being fake, forged, hijacked and maliciously constructed.

Lucas Oman
Agree, and one can change the way session are generated so they'll be more secure.
Quamis
+1  A: 

Regarding your last question: "Does it even matter, in these days?" This is a case by case question. If the ajax request is doing something that does not require security (e.g. loading latest stock quotes) then it really doesn't matter IMHO. If the request is loading information that should be secured (e.g. returning identifying information or doing something on the server) then you should treat it as such.

I personally don't use the server variables to know when something is an ajax request. Instead I just add a query parameter to the ajax call (e.g. http://domain.com/?ajax=true). If I need to secure the ajax call then I would use the same methods as securing a regular page request (using both client and server). As Lucas Oman pointed out, anything on the client side can be faked. Bottom line don't trust any request even if you think it is coming from your site or database. Always follow the mantra "filter input - escape output".

Jim
Yes, yes, yes. Jim is talking sense. Listen to him.
David Dorward