views:

215

answers:

3

I was wondering how I could retrieve a cookie that has a path specified - something like path=/foo/bar... If I use document.cookie that only retrieves me JSessionId cookie.. which I guess is the only one with path=/.

A: 

Are you trying to retrieve the cookie based on the path and not the name? It's possible to have many cookies that match a specified path. Also have a look at jQuery and the Cookie plugin. Setting and retrieving cookies is as easy as:

// get cookie
$.cookie(COOKIE_NAME)

// set cookie
$.cookie(COOKIE_NAME, 'test', { path: '/your/path', expires: 7});

http://plugins.jquery.com/project/cookie

CRasco
I am trying to retrieve the cookie based on name AND path. Specifically I know that there will be only one cookie with that path so using js to get a cookie based on path would work for me. The other thing is that I cannot use other libraries - I have to use pure js due to the nature of the project.
markovuksanovic
Basically the problem i am facing is that when I use document.cookie I get only cookies with path=/, but other ones that I actually need are not retrieved. Any ideas how to retrieve a cookie with specific path, for example path=/foo/bar
markovuksanovic
I don't know enough about cookies to know if you are allowed to create 2 cookies with identical names and different paths. It's entirely possible that you can't and simply getting a cookie based on name alone is going to work.
CRasco
A: 

I have figured out how to solve the problem... Just to let anyone reading this, the idea was to test one of the applications for cross site scripting attack (xss), and the cookie contained valuable information that i wanted to retrieve. The problem was that the cookie was on the other path than the web app itself. I had to access the app using /somedomain/project and the cookie had the /somedomain/project/project path set. SO I somehow had to open /somedomain/project/project url to be able to retrieve the cookie I needed. To get to that cookie I have injected an iframe element. inside that iframe element i made an ajax call. it was a dummy call to /somedomain/project/project/ just to get some information in the iframe ant to make sure iframe's document objects get created. that iframe's document object contained the cookie that i needed. After that I have made an XmlHTTPRequest call to a remote service and sent the cookie as a parameter to the remote server.

markovuksanovic