News for hackers. Stuff that matters.
Re:SPAs Are Dead? by timw4mail on Wednesday April 07, 2021 @02:47PM (#26729201)
Cookies suck. The interface is beyond terrible, they were never scoped properly, and they don't have to be used.

Browser storage (sessionStorage, localStorage) is perfectly valid for storing an authentication token.

Re:SPAs Are Dead? by midrus on Wednesday April 07, 2021 @03:06PM (#26729517)
No, it is not. And I hope I never end up using any application developed this way.

Tokens stored in those storages you mention can be read by any javascript code, even third party.

That doesn't happen with http-only cookies.

Be careful with what you recommend publicly, as others might end up assuming this is fine, when it is clearly not.

Re:SPAs Are Dead? by warent on Wednesday April 07, 2021 @03:25PM (#26729790)
If your third party libraries are so poisoned that you're leaking localstorage, you've got bigger problems than just localstorage... This argument against using localstorage makes no sense
Re:SPAs Are Dead? by tmpz22 on Wednesday April 07, 2021 @03:41PM (#26730039)
Chrome extensions can also inject code via a content script and gather local storage data. You can't control what extensions people are running.
Re:SPAs Are Dead? by minhazm on Wednesday April 07, 2021 @03:54PM (#26730249)
That also applies to cookies. Users can run any browser or script to access your site and do whatever they want with the cookies.
Re:SPAs Are Dead? by gigaftp on Wednesday April 07, 2021 @04:13PM (#26730528)
In the context of a compliant web browser you can set a cookie as http only as to disallow access it via Js.
Re:SPAs Are Dead? by Jenk on Wednesday April 07, 2021 @04:20PM (#26730623)
Not for http-only cookies it doesn't.
Re:SPAs Are Dead? by fractionalhare on Wednesday April 07, 2021 @04:55PM (#26731125)
No they can’t, refer to the documentation on cookie flags and attributes like httpOnly: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
Re:SPAs Are Dead? by midrus on Thursday April 08, 2021 @03:01AM (#26735903)
Security is not black and white. It is a spectrum of possibilities and an arms race between security measures and new attack strategies. So lowering all your barriers because one of them could be already weak is not a good strategy.

If you're sending your password in plain text without HTTPs, then saying "ok, nevermind, we already have that problem so let's just not use any CSRF protection" you're just making things worse.

Authentication tokens that allow anyone reading them to impersonate you are bad, no discussion about it. Go and google for this if you don't believe me. Ask any security expert (which I'm not).

Cookies have the exact same problem. Except if you set the http-only flag in them, which is what I'm advocating for here.

You can still use localStorage, just not for security tokens.

Re:SPAs Are Dead? by anaphor on Thursday April 08, 2021 @01:24PM (#26741439)
You seem to be unaware of the existence of capability based security. https://en.wikipedia.org/wiki/Capability-based_security
Re:SPAs Are Dead? by rwieruch on Wednesday April 07, 2021 @03:41PM (#26730043)
Related to this I was always wondering where to store refresh tokens when using both, access token and refresh token. Reference: https://stackoverflow.com/q/57650692
Re:SPAs Are Dead? by inetknght on Wednesday April 07, 2021 @04:27PM (#26730745)
> And I hope I never end up using any application developed this way.

Slack [0] has entered the chat.

Jira [1] has entered that chat.

I could go on but I don't need to. Many web applications that people use store an unnecessarily obnoxious amount of data locally. I can only imagine how much of that is used just once or possibly even never (like images in settings windows that were never accessed).

[0]: On one instance of Firefox on one computer, app.slack.com has stored 1.9GB. On another instance of Firefox on another computer, app.slack.com has stored about 633KB. On a third instance of Firefox on yet another computer, app.slack.com has stored about 600MB.

[1]: Jira doesn't load _at all_ if you've got any secure settings enabled (like... no CORS and no cross-domain cookies and no cross-domain XHRs and ... the list goes on). So Jira loads in an incognito window in a VM. But suffice to say that its local storage is even more _fucking obnoxious_.

> Tokens stored in those storages you mention can be read by any javascript code, even third party.

I don't doubt you. But I am not a javascript developer. I'd like it if you could link to some demonstrations of how that can be abused.

Re:SPAs Are Dead? by midrus on Thursday April 08, 2021 @02:56AM (#26735879)
I'm not saying you shouldn't use localStorage, or sessionStorage, or indexedDB. They are valid tools and they are there for a reason.

I'm saying you should not use for SECURITY tokens which, if leaked, can allow others to impersonate your user. So it doesn't matter it uses 40Gb of cached data. Security is not black or white, it is a full spectrum and despite it would be bad for a hacker to obtain a full chat historory or your list of jira card titles it is still less worrysome that somebody able to impersonate you in those services. Security is an arms race....you need to raise the barrier more and more as attacks get more and more sophisticated. So the "then just don't run extensions" or "just don't use untrusted third party scripts", despite it is something we should do, it is not a justification for lowering the bar of all the other stuff we should be doing.

Regarding the references you ask for, what can I say. localStorage can be accessed by any JavaScript running in the page, even from browser extensions or XSS attacks. I'm pretty sure you will find people more knowledgeable than me here [1] and overal just googling for it you will find a lot of resources about why storing authentication tokens in localStorage is a bad idea

[1] https://security.stackexchange.com/search?q=localstorage+sec...

Re:SPAs Are Dead? by timw4mail on Wednesday April 07, 2021 @04:01PM (#26730366)
So the article is about being unable to access third-party cookies due to browser privacy updates.

1. If you are using Http-only cookies, these are clearly unrelated. 2. If you are sharing an authentication token with the browser, cookie or not, it can be read by any scripts on the same origin.

So...what exactly are you trying to say?

Re:SPAs Are Dead? by midrus on Sunday April 11, 2021 @09:07AM (#26770092)
> If you are sharing an authentication token with the browser, cookie or not, it can be read by any scripts on the same origin.

No, it can't. Read about http-only cookies.

Re:SPAs Are Dead? by wayneftw on Wednesday April 07, 2021 @04:17PM (#26730592)
Meh. Working at a consulting shop I've seen hundreds of apps built with tokens stored in the big bad localStorage. It's easy, it works and if you don't load scripts from untrustworthy sources then there is no attack vector outside of rendering un-sanitized user input as HTML or browser extensions.

We've dealt with plenty of security issues and exactly zero of them centered around XSS. I've seen more issues surrounding bad dependencies that could end up running right inside your API server. Browser extensions can do so many other things, like reading all the forms and middling your API requests. So, I can't take responsibility for that.

I would recommend http-only cookies over localStorage, but if you protect yourself in other ways then putting auth tokens in localStorage is not the end of the world. It hasn't ever affected us, so I'll leave the pearl-clutching over this up to the experts.

Re:SPAs Are Dead? by midrus on Sunday April 11, 2021 @09:06AM (#26770082)
Yep, better to wait until the security issues happen to fix them. Great mentality.
Re:SPAs Are Dead? by wayneftw on Monday April 12, 2021 @02:54PM (#26783529)
I hate to break it to you but that's largely the way the world works.

But, we haven't had any issues with using localStorage. Probably mainly because we are not a huge target. Nobody is going to write a chrome extension to try to hack one of our tiny little sites.

Re:SPAs Are Dead? by kadfak on Wednesday April 07, 2021 @03:05PM (#26729493)
Leaking your auth tokens through XSS sucks more.
Re:SPAs Are Dead? by asabil on Wednesday April 07, 2021 @03:39PM (#26730011)
If you have an xss your problem is much bigger than leaking an ephemeral access token through localStorage
Re:SPAs Are Dead? by leipert on Wednesday April 07, 2021 @03:08PM (#26729542)
I don’t know whether you are referring to only local and session storage being feasible or not, but on can access cookies from JS as well.
Re:SPAs Are Dead? by AlbinoDrought on Wednesday April 07, 2021 @03:11PM (#26729583)
Normal cookies are JS-accessible, but HTTP-only cookies should not be: "A cookie with the HttpOnly attribute is inaccessible to the JavaScript Document.cookie API; it is sent only to the server."

https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#re...

Re:SPAs Are Dead? by leipert on Wednesday April 07, 2021 @03:22PM (#26729743)
Ah thanks! This is new to me. That is indeed a concern, but probably can be worked around, e.g. by proxying requests to third party domains through the same Domain.
Re:SPAs Are Dead? by kreetx on Wednesday April 07, 2021 @04:37PM (#26730880)
You can't make the browser to send you cookies for other origins so you won't be able to use them from your server.
Re:SPAs Are Dead? by fractionalhare on Wednesday April 07, 2021 @03:13PM (#26729629)
Which is why you use domain scoping, httpOnly and Secure cookie flags so they can only be read by matching hosts (with greater granularity than same-origin policy) over HTTPS and can’t be read by JavaScript. The Web Storage API does not offer these protections.
Re:SPAs Are Dead? by dkarras on Wednesday April 07, 2021 @03:47PM (#26730121)
They can't be read BUT the browser will send the cookie with every request. If you have an XSS, it is game over. The attacker can just send requests from your browser. Slightly less convenient. You are merely taking away the convenience of the attacker doing the attack manually on his own browser, which he probably doesn't want to do anyways. If he can inject js into your site, he will make your browser send the request(s) to do the actions with your credentials in an automated and quick way. Your browser will send the cookie automatically. From the attacker, it would merely be nice if he could read your tokens, but it is absolutely not necessary.

Like some here, I don't understand the hate around keeping tokens in localstorage. People immediately say "but js can read it!" but so what? If someone can put malicious js in my site, it is GAME OVER, secure http-only cookie or not. When that is the case, the saner option is doing away with an old and misused invention called cookies. The upside with ditching cookies is that you are an order of magnitude safer against CSRF since your browser does not send anything automatically. You don't need to keep CSRF token state in your server(s) either (helps with scale, one less state to worry about), it is a win.

http-only secure cookies do not give you any additional security. Ditching cookies does.

Re:SPAs Are Dead? by anaphor on Wednesday April 07, 2021 @04:05PM (#26730433)
I think some people don't realize that CSRF tokens are basically the same thing as bearer tokens (which JWT also does), but it's just that they get re-generated every time you open a new page usually. So it's a bit ironic when everyone screams that tokens are bad, but they're all using them to protect against confused deputy attacks.
Re:SPAs Are Dead? by fractionalhare on Wednesday April 07, 2021 @04:52PM (#26731093)
I am talking about a much more general class of security than just XSS. You’re making perfect the enemy of good here - yes, of course XSS is not completely mitigated by httpOnly. That was not my point.

My actual point stands, the Web Storage API doesn’t offer the same protections as cookies. Don’t store sensitive data in localStorage, that is emphatically not it’s intended use.

Re:SPAs Are Dead? by dkarras on Wednesday April 07, 2021 @11:33PM (#26734833)
>I am talking about a much more general class of security than just XSS.

And what would those be that are relevant to this discussion? The way we (ab)use cookies is arguably not their intended use either.

I can't think of a scenario in this context where an attacker says "damn he is using http-only cookies, I won't be able to do what I want to do"

The only pragmatic difference between both is js accessibility. That only matters when someone can inject scripts into your site. My point is, when that happens, cookies are also bust.

Re:SPAs Are Dead? by shock-value on Wednesday April 07, 2021 @04:19PM (#26730608)
A good option is doing both.

Store a security token in localStorage and additionally store a secure signature for it in a secure, HTTP-only cookie. On your backend, verify validity of both the token and its additional signature contained in the cookie.

Re:SPAs Are Dead? by dkarras on Wednesday April 07, 2021 @04:36PM (#26730866)
I don't believe it adds any meaningful security that justifies the cost (development, testing, hardening, scaling the state across servers if necessary etc.) With security "more complicated" does not necessarily mean "more secure". Doing it without multiplying the number of ways things can go wrong is deceptively hard.
Re:SPAs Are Dead? by shock-value on Wednesday April 07, 2021 @04:43PM (#26730972)
With this method there is no additional per-user state, fortunately.
Re:SPAs Are Dead? by dkarras on Wednesday April 07, 2021 @05:00PM (#26731196)
Yes, just that with regards to security I've seen to many burned by "it can't hurt" processes. With your suggestion, assuming perfect implementation, I personally can't see where it would help. Like, if attacker can run js in your site, they can just set the cookie as necessary before making requests (if the cookie does not exist already) since that is something they can already do. If the cookie exists (most likely scenario), the browser will send it with each request anyways so no added security there either.
Re:SPAs Are Dead? by kostarelo on Wednesday April 07, 2021 @03:03PM (#26729460)
By interface you mean.. the HTTP protocol? At least they can be scoped on the domain level. Browser storage can't.
Re:SPAs Are Dead? by leipert on Wednesday April 07, 2021 @03:10PM (#26729580)
Local storage and Session Storage are scoped per origin as well: https://developer.mozilla.org/en-US/docs/Glossary/Origin
Re:SPAs Are Dead? by pmlnr on Wednesday April 07, 2021 @03:48PM (#26730139)
Re:SPAs Are Dead? by wdb on Wednesday April 07, 2021 @10:58PM (#26734617)
Got to admit I only learned about this because of the Github Private Page item posted earlier on HN. I still not clear what this list does or tries to do. Can't find much about it on MDN. Need to do a bit more digging
Re:SPAs Are Dead? by notsureaboutpg on Wednesday April 07, 2021 @03:13PM (#26729624)
localStorage doesn't work in many browsers' private modes, so no... it's not