Application Security – JavaScript Web Tokens
JWT’s and User Initiated Logouts
The following article is a discussion that explores JavaScript Web Tokens, and how developers generate JWT signing keys and how they create, verify, and terminate sessions.
Background
JavaScript web tokens are well documented and are used for authentication. JWT’s consist of three parts; the header, payload and signature. The header is used to describe the JWT, specifically, it will indicate how the signature is calculated. The payload of the JWT will contain claims about the user presenting the JWT, claims are just another word for attributes. There are a few predesignated claims there should be included in your JWT, other claims can be free form, that means the payload can contain any data that the developer wants it to contain. The signature is used to prevent tampering of the JWT. The most common signing algorithms are HS256, RS256, and ES256, however, there are other algorithms. One of the most important claims that should always exist in the payload is the “exp” claim. The “exp” claim represents the epoch time that the JWT will expire, without this claim the JWT is valid forever.
The expiration claim on the JWT introduces an interesting issue with session management, when a user intentionally terminates a session, how do you invalidate the JWT? A common design pattern that I see on application tests is for the client-side application to receive a JWT from the server and then store it in the browser’s session storage. Subsequent Ajax requests will include the JWT as a bearer token in the authorization header. When a user intentionally terminates a session, otherwise known as logging out, the application will simply delete the JWT from the browser’s session storage. This has the effect of causing the browser to believe the session was terminated because the application can no longer authenticate to the server since the application can no longer find the JWT in the session store. However, this is an illusion, deleting the JWT from the session store does nothing to invalidate the session, the deleted JWT will remain valid until the expiration date has been reached. If an attacker had captured the JWT, they would be able to use it until the exp claim lapses. This type of session handling runs afoul of the OWASP best practices, OWASP states that a secure session termination requires at least the following components.
- Availability of user interface controls that allow the user to manually log out.
- Session termination after a given amount of time without activity (session timeout).
- Proper invalidation of server-side session state.
This leaves the developer with the before mentioned question, how can a JWT be invalidated when the user logs out? One solution that I have heard is to have short lived expirations and to use a refresh token to re-issue the JWT upon expiration. While this solution limits the window in which an abandoned session could be used, it doesn’t actually solve the problem. I have seen a few other recommendations that should work in theory, but they are not always practical or easy to implement. Below I describe the easiest method that I have found. It makes changes to how developers generate JWT signing keys and how they create, verify, and terminate sessions.
Session Creation
When the session is created, the session key should be generated and stored in the datastore. For the purposes of this writing, it will be assumed the datastore is a database table with the following fields.
- SessionID GUID
- SessionKey TEXT
- ExpirationDate Timestamp
Once the JWT is populated and ready to be signed, the KID claim should be populated with the SessionID GUID and the JWT should be signed with the SessionKey value as demonstrated with the below pseudo code.
$key = SecureRandom.Generate(32); // 32 random bytes
$id = GUID.New(); // get new 32-bit GUID
$expires = date_add($now, “1h”); // expire in 1 hour
$db.execute(“insert into SessionKeys(SessionID, SessionKey, ExpirationDate) values(?, ?, ?)”, $id, $key,$expires);
$header = {“alg”:”HS256”, “kid”:”$id”}
$payload = {“exp”:”$expires”}; // insert claims here
$jwt = JWT.sign($header, $payload, $key)
Session Verification
When it is time to verify the JWT, the application should read the KID claim and lookup that value in the database using the below pseudo code.
$qSession = $db.Query(“select * from SessionKeys where SessionID = ?”, $jwt[“kid”]);
It goes without saying that parameterized statements should always be used when querying a database.
$isvalid = JWT.verify($jwt, $qSession[“SessionKey”]);
Session Termination
When the user initiates a logout, the JWT verified using the process outlined above and then the session key should be deleted from the database as shown in the below pseudo code.
$qSession = $db.Query(“select * from SessionKeys where SessionID = ?”, $jwt[“kid”]);
$isValid = JWT.verify(jwt, qSession[“SessionKey”]);
If($isValid)
{
$db.execute(“delete from SessionKeys where SessionKey = ? or ExpirationDate <= ?”, $qSession[“SessionID”], $now);
}
By deleting the session key, the JWT is no longer able to be validated and is now in a de facto state of being expired. The “or ExpirationDate <= $now” portion of the above pseudo code does some automated garbage collection by deleting the current session and any sessions that have already expired. At this point, it is safe for the client-side application to delete the JWT from session storage.
Conclusion
JWTs are a convenient way to implement authentication, however they are not without their complexities, managing JWTs can be likened to managing PKI. With PKI, it can be difficult to invalidate certificates once they are issued, revocations require a whole other process (CRLs) to maintain a healthy PKI. The same can be said for managing JWT’s, however, the revocation process can be simplified using the process outlined in this writing.
Related Articles
-
Offensive Security
What is Offensive Security? Discover Offensive Security and learn how... -
What is Social Hacking?
Social hacking is an attack on the human operating system,... -
What You Need to Know About PCI Penetration Testing
A pen test, on the other hand, is a manual... -
What is Penetration Testing (pen-testing)?
Penetration testing (pen-testing) is the art and science of... -
Our Nation Under Attack
The basic necessities of life; water, power and transportation are... -
Manual Penetration Testing – Manual Testing vs Automated Testing
Manual Penetration Testing is essential for critical infrastructure. Scanning... -
What is Penetration Testing & Its Different Types
Manual Penetration Testing is essential for critical infrastructure. Scanning... -
Common cybersecurity issues that are easy to fix
Most companies know that critical vulnerabilities can be resolved simply...
Cyber threat news feed
Check out the latest cybersecurity news around the globe
Cymbiotic will provide unparalleled security insight with the ability to manage teams, clients, on-demand testing with rapid internal VM deployment […]
-
Grohe AG mutmaßlich von Ransomware-Attacke betroffen
Die Ransomware-Bande Ransomhub will 100 Gigabyte Daten von der Grohe AG erbeutet haben.CeltStudio […]
-
Cisco patches antivirus decommissioning bug as exploit code surfaces
Cisco has patched a denial-of-service (DoS) vulnerability affecting its open-source antivirus […]
-
10 top XDR tools and how to evaluate them
Little in the modern IT world lends itself to manual or siloed management, and this is doubly true […]
-
Python administrator moves to improve software security
The administrators of the Python Package Index (PyPI) have begun an effort to improve the hundreds […]
-
Geben Sie LLM-Alarmismus keine Chance!
Die Mär von der Cybercrime-KI-Revolution?Overearth | shutterstock.com Cybersicherheitsexperten […]
Redbot Social