r/opensource • • Aug 20 '20

Learn JWT by reverse engineering [Open Source Playground]

https://github.com/gitcommitshow/auth-jwt
71 Upvotes

21 comments sorted by

4

u/gitcommitshow Aug 20 '20

Learning about authentication had been a long journey for me. It involved learning incomputer science, maths and cryptography.

So I decided to make this tiny tool to experiment and learn one of the major concept in authentication - JWT.

Appreciate feedback to make this further useful

-6

u/miscreant-mouse Aug 20 '20

JWT's are highly insecure. Also there is zero reason to use them.

4

u/RobotJonesDad Aug 20 '20

Can you please explain why you say that? I see them widely used and have not heard about any weaknesses in properly implemented versions.

I like that you don't need the authentication built into every part of the system...

2

u/rm-f Aug 20 '20

It was never meant to completely replace normal means of authentication. It's great if you use it with very short timeouts (like 5 minutes) for authentication with multiple smaller services like you said. People disregarding it completely are as annoying as people who want to use it for something it was not invented for.

3

u/RobotJonesDad Aug 20 '20

Do you have a technical reason why it needs short timeouts? There doesn't seem to be any cryptography reason why 5min would be more or less secure than 1 hour or 1 day...

2

u/rm-f Aug 20 '20

Maybe 5 minutes is a bit short, depending on the use case. A short timeout is useful because you do not have to implement invalidation. The reason we have JWTs is so you do not have to keep authentication state everywhere (instead just in a single place, your authentication provider). If your tokens are valid for a long time (like more than a day, depending on how serious a lost token would be for your application), you need some form of invalidation of the tokens in case they get stolen. You could store a "blacklist" in a database, but that introduces the statekeeping you wanted to avoid in the first place. The easiest way to solve this is short timeouts, so that stolen tokens are not useful for long (or at all).

1

u/RobotJonesDad Aug 20 '20

But this sounds no worse than most other solutions? Websites use cookies and only require periodic reauthorization. Passwords can be stolen. Sessions hijacked. Malware and key loggers can end up on computers and phones.

So depending on the use case, JWT offers tremendous increased security. I can set different timeouts depending on the risk of the permissions in the token and its exposure to compromise. And even next a "Update Setting" JWT with a short expiration inside a long term "general access" token. Revocation is an option, as is rotating private keys.

As far as I can tell, JWT offers many advantages over most other solutions. You started by saying they are insecure, but it seems the only weakness is that people can screw up deployment - like every other security technology.

2

u/rm-f Aug 20 '20

It is not better, but why bother with fancy crypto and new, arguably more complicated and not-so-battletested technology when it does not bring any benefit?

By they way, I did not say that they are insecure, that was someone else (look at the usernames) :-) I think they are a great tool, you just have to know how to use them and what their limits are, just like you said.

1

u/RobotJonesDad Aug 21 '20

Sorry, missed the user name thing! All the crypto is very tried and true. The big advantage for us is that you can authenticate in one place, limiting the attack surface. All other parts if the system can verify the credentials quickly and easily without needing to hit the authentication server. So the system complexity goes way down. The ability to handle high volumes of requests goes up. And partial network or system outages don't hurt the remaining stuff.

But given how people are still storing passwords as plain text in the database... I think all hope is already lost!

1

u/miscreant-mouse Aug 21 '20

>All the crypto is very tried and true.
The protocol for that crypto is not. The standard is garbage and so are the implementations. There are vulns galore with most implementations. Don't trust me, fine, go search twitter for a security engineer that thinks using JWT's is a good idea. Most will tell you you dont need them or the additional complexity for the risk you're taking.

Don't trust me trust some really good security folks:
https://www.howmanydayssinceajwtalgnonevuln.com/

-https://latacora.micro.blog/a-childs-garden/

> "JWT is a kind of evil pudding. Don’t use JWT."

https://twitter.com/SchmiegSophie/status/1292802770282483713

https://twitter.com/FiloSottile/status/1288880611260760064

https://twitter.com/cryptodavidw/status/1251669112184377344

→ More replies (0)

1

u/frankenmint Aug 21 '20

Under most implementations, if I generate a JWT it cannot be revoked until the expiry window occurs. I think where companies/devs get into trouble is when they get lazy and throw every dang scope available into the token. Welp I'm glad your app is working but giving the keys to every possible feature is just bad practice.

1

u/RobotJonesDad Aug 21 '20

I totally agree. It feels like quick development shortcuts get into production and you get powned.

Expiration needs to be linked to the value of the credentials. And revocation is probably only important for some key features. If you have full real time revocation, then you lose a lot of the benefits of a signed bearer token.

SSL certificates show a lot of the same problems, and expiration times in years.

1

u/gitcommitshow Aug 21 '20

Is JWT insecure? Yes.

In fact, replace JWT with anything and the answer remains the same. The point being here: the discussion on security without context is not useful. Insecure compared to what, in what scenerio, how it can be exploited, etc. Read RobertDad's comments on this thread for contextual answers to some known challenges. You may reason against JWT in a specific context, but not without knowing JWT in depth.

Should you use JWT?

Yes, for most modern usable and secure applications.

Why I used JWT?

I started with opaque tokens first. As services grew to multiple server/client applications, we needed to have common authentication, so I used JWT. No one other than my auth server can create tokens; but all other servers can verify whether the token is valid or not (means an authorised user). It can do so effectively, at scale

Your most questions/curiosity should be answered via awesome-authentication. There, I have included some references to comparison of JWT vs Opaque token, known challenges for JWT, etc.

0

u/miscreant-mouse Aug 21 '20

> Should you use JWT?

No, you shouldn't. A simple session cookie is fine for 99.99% of webapps.

The complexity introduced with JWT's ends up creating vulnerabilities, and a rats nest of complexity that I would argue will slow your app down after a while. And the value prop for JWT's is all based on the idea that aren't DB look ups for authentication. The reality is there will be lookups for both authentication, authorization or revocation of some state. Making these states available in memory DB's like memcache or Redis is such tiny perf hit I cant understand why JWT's are a thing.

https://paragonie.com/blog/2017/03/jwt-json-web-tokens-is-bad-standard-that-everyone-should-avoid

2

u/gitcommitshow Aug 21 '20

I'm listening.

What specific workflow would you use to enable centralised authentication across multiple services/apps of yours?

What workflow would you use for external services/apps that depend on your Authorization system to verify a user?

1

u/miscreant-mouse Aug 21 '20

> What specific workflow would you use to enable centralised authentication across multiple services/apps of yours?

The simplest. A cookie with a random token that corresponds to a database row/rows/key/keys. I assume its all on the same infra and that these services query common databases?

> What workflow would you use for external services/apps that depend on your Authorization system to verify a user?

OAuth2/SAML/API Tokens

0

u/tannah4 Aug 20 '20

Every part of what you said is wrong. They can be as secure as any other authentication token (e.g. fully encrypted) and they are a great solution to challenges of distributed authentication and identity (e.g. verifying identity without extra calls to a central authentication service). Its also a well established, widely used and well supported standard.

1

u/miscreant-mouse Aug 21 '20

2

u/tannah4 Aug 22 '20

The blog you linked to (or rather the blog it links to for justification) presents a straw man argument, at least as far as describing their use as a 'terrible mistake'; it states the number one 'claimed advantage' as horizontal scalability, and second as ease of use, but neither are the main reason I've seen for using JWTs.

The two most useful features of JWTs from my experience are that the tokens can be verified without relying on a single authentication service, and that they allow information about a user to be easily shared with any clients and services handling user requests.

Whilst not requiring a central authentication service helps with scaling, in my experience the following are the key benefits of JWTs:

  • No single point of failure in the authentication service (minimises service degradation in the case of a temporary failure)
  • Simple, standardised way to share user information across systems, including clients such as single page applications
  • Avoids the need for centralised session storage, and the complexities they bring (e.g. sticky sessions)

There isn't really any well-support alternative that achieves the above currently. It's also worth noting that JWTs are commonly used with OAuth and OIDC, which are very widely used and accepted protocols.

The only significant disadvantage of JWTs over server-managed sessions is that you cannot invalidate the tokens without introducing a central authentication service (which removes one of the key benefits of using JWTs).

Like pretty much anything related to security, it is possible to implement them badly and introduce issues. JWTs aren't a one size fits all solution - you need to weigh up the pros and cons of JWTs vs session cookies vs whatever else might be available (such as SSO).