Cookie migration when changing domain - javascript

Recently I'm moving my website to a new domain, and I want to migrate user cookies to the new domain too, so they don't have to log in again.
After some research, I found there are two potential ways to do it:
Land user first on old-domain.com, update all cookies with new-domain.com so they can be accessed from new-domain.com, then do a browser redirect to new-domain.com;
Alternatively, on the new-domain.com, I can inject an iframe from old-domain.com, to let it write cookies to the new website...
I'm not sure if these two can achieve my goal to migrate cookies between domains. Is there a suggestied ways to do it, so that user doesn't have to sign in again to use the new domain?

I think something like #2 can work.
On the new page, check if the cookies are already set. If not, inject an iframe like:
<iframe src="http://old-domain.com/getcookies.html" style="height: 0; width: 0;">
getcookies.html will just contain Javascript that gets the cookies and uses postMessage() to send them to the new page. Javascript on the new page will receive the message and then set the cookies that it receives.
There are some potential issues with this:
Loading the iframe and having it send the cookies is asynchronous. What should the page do while it's waiting?
If you use cookies on the server, the above code doesn't set them until the client receives the page from the server. The server will need to deal with non-migrated users specially, by first sending them the script that copies the cookies, and it then redirects them back to the server script.
You have to deal with users that were never on the old domain. getcookies.html should detect that none of the cookies are set, and send back a message indicating this.
I suggest you add a new cookie migrated=yes that can be used to detect whether the user needs to go through any of this.

Related

Can an iframe be created without cookie data?

Using JavaScript, is it possible to inject an iframe and have the iframe hold no cookie/session data? Effectively an 'incognito' iframe?
The reason for this is a Chrome extension I was thinking about, where I'd like to display a preview of pre-auth pages of other websites, and so to ensure they're not redirected the iframe would ideally non use existing cookie data.
I know this is similar to this question, but I don't have full control of the pages I'm previewing in the iframe.
If you don't use any HTTP-only cookies, you could save a copy of all cookies in localStorage, remove all cookies, load the iframe, and restore the cookies from localStorage.
Pseudo-code:
localStorage.setItem("cookies", document.cookie);
removeAllCookies();
openIframe();
document.cookie = localStorage.cookies;
localStorage.removeItem("cookies");
Another way is to create a proxy on your server, which wouldn't use cookies. For example request to http://example.com/proxy?url=/preview would make a request to http://example.com/preview without cookies.

How can I share a data variable or localstorage across different websites? iframe?postMessage?

I am trying to get session data between different websites, similar to what stackexchange does with all its sister sites, in which I login once and my session is detected on the other sites.
But the difference is I would need to create a session on www.site1.com and access it on www.site2.com, and also would post this code on 3rd party sites to still access the session from their site. The information contained is non sensitive(password/id) and would just let me know who the user is via a publicID.
Im using nodejs and redis for my sessions.
Things I have tried:
1) Iframes, postMessage, localStorage:
-User sends session info from site1.com to iframe on the same page containing "site2.com" via PostMessage. On site2.com, when I receive the message, I am attempting to save to site2.com localstorage, the goal is to access site2.com session from any other websites having the site2.com iframe on their page.
The error I got, was unable to save localStorage on other domain by sending postMessage from site1.com to the site2.com iframe.
On site1.com I have an iframe showing site2.com, where site2.com is my server that will save the session.
2) CORS ajax: was not able to save/remember session
I've heard of browser fingerprinting, like how facebook/google can track users across sites and this may be the type of solution i need.
Can someone please let me know what I can try different? Thanks.
You could maybe use access tokens stored on the cookies or passed on the url, similar to the way RESTful apps work. This might be a helpful read:
http://www.sitepoint.com/using-json-web-tokens-node-js/

access cookies from email?

Is it possible to access cookies from an email? My fear is that one can for instance steal facebook login cookies simply by sending an email.
I know it's possible to redirect a user to an url without him to be aware of it. For instance, I used to display a 1x1 gif to redirect the user to a url (I used that to make email opening stats). What if on the target url I create a malicious js script: will I be able to access the user's cookies?
Or to put it differently, if there is a link in the email and the user clicks the link, is the target website able to access user's cookies?
I read this; does anyone have more details on the subject?
#user3345621
Thanks for your answer, it seems correct to me.
But to take on the facebook example again, I have a couple more questions:
I may be wrong, but I think the cookie encryption does not help in this case.
The cookie encryption will help to hide the password in case for instance I access
your local machine and look in the cookies directly.
However, if I steel the encrypted cookie, I will be able to use them,
and let facebook do the uncryption work.
So in other words, I think it does not matter whether or not cookies are encrypted,
as long as the application (facebook in this example) will decode them for you.
Now, same remark about the fact that the cookie is recreated.
I think this is a direct consequence of using session_regenerate_id function.
But anyway, my understanding (which may be wrong) is that even if the cookie is recreated,
if the hacker send you a malicious email, he will get the newest version of the cookies
anyway since in the technique I'm describing, you're redirected to a malicious website,
so that website, when opening would have access to the current cookies (if possible).
?
I might be incorrect, since I'm fairly new to application security.
Here goes my best shot, concerning cookies now:
Cookies are domain specific (FACT), when you have a facebook cookie storing your user ID and your email (perhaps?) only the facebook domain has access to that cookie. Also, in most cases, the information in your cookie, especially in enterprise systems such as facebook, is encrypted, in other cases a hash is used to mask the information (Sort of fact).
So let's take facebook as an example, since they use a strong encryption format (FACT). If for instance you were able to get hold of a users facebook cookie, you would need to de-crypt the information to start off with, for it to be of any use to you. By that time a new cookie would have been generated (darn facebook adddicts).
Onto the security issue, if by some means you were able to get hold of a users cookie that does NOT belong to your domain, it would be a hack (do'h!), and you would need to check for any browser (Yes you should be exploiting the browser), that has such an exploit, or look for one yourself..
So here they are:
Browser Exploit, hack a specific browser.
De-crypt (or de-hash) the cookie, if it's encrypted.
And do this all be for the cookie has expired.
And the world is yours.
I tried to send me an email with the following image:
<img src="http://localwebsite/js.php" />
On my local machine, I created a page at this url:
http://localwebsite/js.php
Which would alert("something") using javascript.
Sending the email to myself,
I expected that the mail client would open a web browser page and open the js popup,
but that's not the case at all.
What happened is that since it's not really an image,
my mail client (using mail on mac) did display a blue square exclamation mark,
indicating that he could not display that image.
Even if I click on "load images".
Then nothing more happens:
I presume the mail client goes to the url and tries to display the expected image in the message,
but since there is no image, nothing changes.
The url wasn't open in the browser at all, everything was done in the background.
Reading more about javascript in emails, it seems that generally, javascript is not interpreted at all
in emails.
I tested that too: sending an email containing:
<script type="application/javascript">alert("pou")</script>
Mail (mac) does not execute the script.
So to answer the question,
I believe that the only thing a hacker can do with mail is:
sending a link, then if you click on that link, anything can happen
create an image that he can use to track whether or not you've opened the mail
So if you're cautious enough, mail are'nt a big threat.
I was paranoid…

CSRF vulnerability / cookies question

Just want to get input from people who know. I was considering CSRF vulnerabilities, and the seemingly the most popular method I know to fight against it. That method is to create a token in the returned html and adding a cookie with the same value. So if a script tries to do a post they would have to guess the token thats embedded in the web page for it to be successful.
But if they're targeting a specific website why can't they just use a script that
Calls a get on the page (the cookie will be returned even though the script can't access it)
Parses the html and gets the token
Calls a post with that token in it (the cookie that came back will be sent back)
They've successfully submitted a form without the users knowledge
The script doesn't need to know the contents of the cookie, it's just using the fact that cookies get sent back and forth all the time.
What am I missing here? Is this not possible? I think this is pretty scary if you think about it.
Below this line is not required reading to answer the question :)
This vulnerability banks on the fact that authentication is done based on cookies, which I think is the main way authentication is currently occurring.
Another solution I can think of is making authentication be on the page level. So
when they log in the returned html will have that token in it. every link that they click contains that token so when the web server gets a request it has a way to identify the user/session. The problem with it is that if they use any navigation other than that they will be 'unauthenticated'(e.g. type in a url) , also it doesn't look nice in the url because it would probably look something like this:
https://www.example.com/SuperSecretPage/1/123j4123jh12pf12g3g4j2h3g4b2k3jh4h5g55j3h3
But I do understand that if safety is more important, then a pretty URL is second place.
I don't know everything about cookies but what if user agents were a little more careful with their cookies?
For example, what if the cookies sent depended on the tab? We all surf using tabs by now, right? so what if the scope of the cookie was the tab? so if i have my banking site open on tab 1 and i'm surfing on tab 2, any scripts calling gets/posts on
tab 2 will only send the cookies accrued in tab 2.
Or what if cookies were stored / domain. So while I'm on example.com any cookies that come back go into the example.com cookie collection. and then when i'm on www.mybankingsite.com all the cookies get put into the mybankingsite.com collection. So if I go to example.com and it runs a script that calls a get/post the user agent will only send example.com cookies. This is different than sending the cookies of the requested domain. E.g. if a script calls a get of mybankingsite.com within a web page of example.com the user agent will not send the mybankingsite.com cookies.
I know i have no control over what user agents do, but I'm just exploring possibilities
So I think the problem here becomes the attacker's attempt to get the page's contents. To get the authenticated user's page, the attacker needs to be able to send a request on their behalf and read the contents. AJAX won't send cross-domain requests, iframes won't let you read the response. I am struggling to think of other ways in which an attacker would get the contents first.
A more likely hack is using clickjacking to have the user just submit the form. This technique doesn't seem too likely. (caveat: it's security, we can always be wrong.)
Does anyone care to share some code on this issue as I just hacked my own site (Not in production) with CSRF. All I had to do was the following
At: www.badguy.com/ write the following html
img src="www.goodguy.com/secure/user/delete/5">
What this does
So the admin goes to to www.badguy.com/ and the the image makes a request to
www.goodguy.com/secure/user/delete/5 from the users browser so the admin unknowingly just deleted a user. If you create a loop your in some trouble. Expect I never delete data just change its status :) but still I don't like the looks of this.
The CSRF token has to be unique per session. If a malicious server requests the same page, they will get a different token. If they try to request the contents of the page via JavaScript on the client's machine, the same-origin policy will prevent them.

How can I automatically answer a password prompt from an embedded item in an (X)HTML page?

I wrote a web page that displays images from several servers on my network via simple img tags with appropriate href values. The servers require authentication before they will send the images.
It works alright, except on first load the page presents the user with a series of password prompts (one for each server). The user can select the "Remember my password" checkbox, and then subsequent refreshes of the page work without prompting, with correctly updated images. That is, until someone closes out the browser, after which a new set of prompts awaits anyone who opens the page again.
All of the credentials needed are known beforehand, and I don't care if someone could read them in the page source, since this page is in a protected part of an internal intranet site. Everyone with access to this page knows the passwords anyway.
The only browser we're allowed to use is IE 7, so I don't care about compatibility with other browsers at the moment.
Is there any way I can use JavaScript (or some other client-side code) to automatically answer those prompts so the user never sees them?
Thanks very much, in advance.
You can include the authentication in the URL:
<img src="http://paulfisher:tastybacon#internalwebs/path/to/image.png">
Where, of course, paulfisher is my username and my password is tastybacon.
No, javascript can't do this. Here are a couple of options that I've used before to solve this problem:
Change the authentication on the other servers to be either anonymous or integrated.
Proxy in the images: On the server serving the page, add another page that takes in the URL of the remote server. This new page makes a webrequest to the other server and streams the image back. The webrequest can plug in the correct credentials.
Depending on the servers' DNS names, it might be possible to share an authentication cookie across all of the servers. Then you could set up some kind of module on all of the servers to allow the shared authentication.

Categories

Resources