0

I am migrating from programming to 'web programming', so this may sound too basic

My question is about a HTTP request that is responded by "victim site" with some "non-public/sensitive" information as HTML or XML or JSON.

And victim site is just relying on session cookies for authentication before responding with "non public" info for the http request.

If a the hacker's site has a JS that makes ajax HTTP request to "victim site" and user is already logged in to "victim site" and hence has cookie for victim site in browser.

Will the ajax request be responded by "victim server" and if so can the hackers JS post this "non-public" info back to hackers site.

How would one prevent this?

Yi Jiang
  • 46,385
  • 16
  • 133
  • 131
Tiwari
  • 924
  • 2
  • 10
  • 22

3 Answers3

2
  1. You cannot make a request through AJAX to other domain than the one where the JS with the AJAX is being executed.

    If the victim's site is example.com and hacker's site is example2.com then a JS file from example2.com can't make an AJAX request to another site outer than example2.

  2. The cookies from the victim's server that the client has won't be sent to the hacker's server in an HTTP request. The cookies can be stolen if the victim's site has a XSS that can be exploited to send the cookies from that site to hackers.

Community
  • 1
  • 1
Santiago Alessandri
  • 6,082
  • 28
  • 44
  • 1
    SanSS is correct, and I like to add something. While AJAX can't be abused for cross-domain posts, javascript itself can be abused for this, because a hacker can generate a new form that does a cross-domain post or have an iframe on the page to do this. While the hacker can't access the victim's cookie, when the hacker uses an iframe to load the response of the victim's site, the hacker can use javascript to read the returned information. – Steven Jan 21 '11 at 14:26
0

Wikipedia has a few quick bits of advice: http://en.wikipedia.org/wiki/Csrf

Keep your session cookie lifetimes short. If you're logged out after 15 minutes of inactivity, you're less likely to fall victim to problems. Not very friendly, but it's something to consider based on the value of your data.

Check Referer: headers to see they came from your site. Not great. Not even good. But it's something.

Use user-specific form contents to prevent attackers from creating a simple form POST: Understanding the Rails Authenticity Token This mechanism is great, and if your framework helps you with it, nearly painless on your end. If your framework doesn't help you, then it's one more reason to consider a nicer framework. :)

Community
  • 1
  • 1
sarnold
  • 96,852
  • 21
  • 162
  • 219
0

ajax in the strict sense (xmlhttprequest), indeed is generally considered to be limited to the same domain, but a lot (if not everything) depends on the way browsers implement the security model. one thing I noticed is that Firefox will issue a cross-domain request even with normal (non cross-domain) ajax, but it blocks the entire response from coming in (looks like it aborts the request, leading to http 206 response code). that means that, at least in firefox, "write calls" need to be protected against CSRF attacks even for normal ajax.

next to browser glitches like this, most browsers also support 'cross domain resource sharing' which can be used with xmlhttprequests as well. when done the correct way, cross domain ajax can be pretty safe.

but adoption of CORS seems to hampered by the success of 'jsonp'; dynamically inserted scripts that contain data in json wrapped in a callback parameter which are not limited by the same domain principel. doing this the safe way (i.e. preventing an attacker site to dynamically insert and execute script from the victim site for a logged on user for which the cookie would indeed be sent along) is somewhat more difficult (requiring a session-alike token in each request which is not in the cookie).

conclusion: read-operations using traditional ajax are safe, for writes & jsonp you'll have to do some extra work to be safe. if you really want to go cross domain, you should probably look into CORS as an alternative to jsonp.

futtta
  • 5,577
  • 2
  • 19
  • 32