Live data from Hacker News

How to Use JSON Web Tokens

github.com

11–20 of 135 posts

Re: How to Use JSON Web Tokens

#11
post #9
post #3

Earlier quoted context omitted.

I keep hearing this. Is there a good writeup available?

JWTs can be read by every script you add to your site, making XSS attacks easier. A cookie with the HttpOnly flag prevents this. I also found cookies easier to use, since the browser handles them and you don't need to attach headers to each request. Any other important concerns or which way is recommended currently?

> JWTs can be read by every script you add to your site, making XSS attacks easier. A cookie with the HttpOnly flag prevents this.

You're confusing JWTs (a standardized token / means of representing claims) and JavaScript localStorage (a storage which can be read by scripts, partitioned by origin). The two are completely orthogonal; you could store a JWT in a cookie, if you wanted to.

> JWTs [… make] XSS attacks easier. I also found cookies easier to use, since the browser handles them and you don't need to attach headers to each request.

The browser handling them automatically means you need to think about CSRF, which I think largely negates the benefits.

If your site is vulnerable to XSS, a cookie won't save you; the XSS attacker just makes the necessary authenticated request using an AJAX.

My current favorite writeup on this is https://portswigger.net/blog/web-storage-the-lesser-evil-for...

Re: How to Use JSON Web Tokens

#12
post #3
post #2

JWTs are radioactive. Proceed with caution.

I keep hearing this. Is there a good writeup available?

Blog post: https://paragonie.com/blog/2017/03/jwt-json-web-tokens-is-ba...

DEFCON CPV talk: https://paragonie.com/files/talks/NoWayJoseCPV2018.pdf + https://youtu.be/RijGNytjbOI

Alternative design that isn't radioactive: https://paseto.io

Apologies if this wasn't more readily available or commonly known. I'm worse at marketing than I am at engineering.

Re: How to Use JSON Web Tokens

#13
post #4
post #3

Earlier quoted context omitted.

I keep hearing this. Is there a good writeup available?

The parent comment isn't very helpful, but from what I understand people dislike JWTs because it makes it hard to invalidate a session without some sort of work-around. For example, you can use 2 tokens, one short lived, and one long lived to get around the invalidation problem, but then you will need to occasionally validate that both tokens are still valid, and that state needs to be stored, and now you're storing…

The way I understand it the only sane use for JWTs is for short-lived delegation of authorisation.

E.g. a user wants to talk to service A but access to that service requires certain privileges. Instead of authenticating with service A, the user authenticates with service B (e.g. using a long-lived conventional session mechanism that requires DB lookup), which issues a token the user can then pass to service B (which trusts service A the info is valid and needs no lookup to process the token). JWT standardises a format for that token.

Most uses of JWT in the wild however seem to be for authenticating the user of a (web) app with the backend of that same app, so the token is passed from the backend to itself (via the user). This use case is better suited for conventional session tokens.

Re: How to Use JSON Web Tokens

#14
post #9

Earlier quoted context omitted.

JWTs can be read by every script you add to your site, making XSS attacks easier. A cookie with the HttpOnly flag prevents this. I also found cookies easier to use, since the browser handles them and you don't need to attach headers to each request. Any other important concerns or which way is recommended currently?

> JWTs can be read by every script you add to your site, making XSS attacks easier. A cookie with the HttpOnly flag prevents this. You're confusing JWTs (a standardized token / means of representing claims) and JavaScript localStorage (a storage which can be read by scripts, partitioned by origin). The two are completely orthogonal; you could store a JWT in a cookie, if you wanted to. > JWTs [… make] XSS attacks easi…

CSRF is mitigated by using the samesite cookie flag. XSS is mitigated by httponly, except where XSS makes legitimate requests to domains specified by the cookie.

This article describes some of the most vulnerable ways to use a JWT in 2019, but please let's stop talking about none algorithms.

Re: How to Use JSON Web Tokens

#15
This is a good Javascript-centric writeup. This caught my eye thought:

> Since JSON Web Tokens (JWT) are not signed using asymmetric encryption ...

You don't have to, but can of course sign your JWTs with asymmetric keys, and many libraries support this. Third party token issuers (firebase, google, auth0) require asymmetric, since they're clearly not going to share a secret with you.

Asymmetric also provides two-way token verification: The issuer signs the token with a private key, and receivers of the token can then verify that the token is legitimate using the issuers "well-known" public key.

Re: How to Use JSON Web Tokens

#16

There are other ways to pass claims -- like using bearer tokens in the HTTP header, or using OAuth 2. (All with TLS, of course)

Yes, JWTs don't bring anything new to the table. In fact, most articles promoting their use don't even explain why you should use them. This is cargo cult programming at its utmost.

Re: How to Use JSON Web Tokens

#17
JWTs are useful, but there are a few things that are not immediately obvious.

1) They are signed not encrypted. Anything you put in there is public readable, unless you encrypt your token after you generate it

2) you can accept a range of encryption types, don't. Stick to one type and disallow any token that doesn't conform (this protects against people making their own tokens with 'None' as the signing algorithm)

I am surprised at how naive the JWT spec is. Why in this day an age is encryption a default for the _payload_? Also, why on earth is 'None' allowed as a signing algorithm? Thats just a cookie..

Re: How to Use JSON Web Tokens

#18
post #9

Earlier quoted context omitted.

JWTs can be read by every script you add to your site, making XSS attacks easier. A cookie with the HttpOnly flag prevents this. I also found cookies easier to use, since the browser handles them and you don't need to attach headers to each request. Any other important concerns or which way is recommended currently?

> JWTs can be read by every script you add to your site, making XSS attacks easier. A cookie with the HttpOnly flag prevents this. You're confusing JWTs (a standardized token / means of representing claims) and JavaScript localStorage (a storage which can be read by scripts, partitioned by origin). The two are completely orthogonal; you could store a JWT in a cookie, if you wanted to. > JWTs [… make] XSS attacks easi…

> you could store a JWT in a cookie, if you wanted to.

Huh? If it's an HTTPOnly cookie, how would you then insert the JWT token into the auth header?

Re: How to Use JSON Web Tokens

#19
post #3

Earlier quoted context omitted.

I keep hearing this. Is there a good writeup available?

Blog post: https://paragonie.com/blog/2017/03/jwt-json-web-tokens-is-ba... DEFCON CPV talk: https://paragonie.com/files/talks/NoWayJoseCPV2018.pdf + https://youtu.be/RijGNytjbOI Alternative design that isn't radioactive: https://paseto.io Apologies if this wasn't more readily available or commonly known. I'm worse at marketing than I am at engineering.

[bookmarks]

Thanks a lot! This is exactly what I needed.

Re: How to Use JSON Web Tokens

#20
This page doesn’t have any discussion of strategies for expiring JWT, one of the biggest security issues with this auth mechanism. Even the code sample doesn’t include an expiration.
Post reply on HN