Skip to content

Commit bd527f5

Browse files
committed
fix(auth): migrate to secure usage of jwt for token authentication
There is a vulnerability in v8 of the `jsonwebtoken` dependency. This commit upgrades to v9 to resolve the vulnerability. Additionally, they made an effort in this version to discourage the less secure "decode" method in favor of the more secure "verify" method (1). This commit also refactors the code and tests to use the "verify" method. (1) See this PR for context: auth0/node-jsonwebtoken#741 Signed-off-by: Dustin Popp <[email protected]>
1 parent f0aa9e5 commit bd527f5

File tree

5 files changed

+78
-122
lines changed

5 files changed

+78
-122
lines changed

auth/token-managers/jwt-token-manager.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19-
import { decode } from 'jsonwebtoken';
19+
import { verify } from 'jsonwebtoken';
2020
import logger from '../../lib/logger';
2121
import { TokenManager, TokenManagerOptions } from './token-manager';
2222

@@ -80,15 +80,26 @@ export class JwtTokenManager extends TokenManager {
8080
throw new Error(err);
8181
}
8282

83-
// the time of expiration is found by decoding the JWT access token
84-
// exp is the time of expire and iat is the time of token retrieval
85-
const decodedResponse = decode(this.accessToken);
83+
let decodedResponse;
84+
try {
85+
decodedResponse = verify(this.accessToken);
86+
} catch (e) {
87+
// the token is either an invalid JWT or it could not be verified
88+
logger.error('Failed to verify the JWT. See error message:');
89+
logger.error(e);
90+
throw new Error(e);
91+
}
92+
93+
// the 'catch' method above should handle any verificiation/decoding issues but
94+
// this check is here as a failsafe
8695
if (!decodedResponse) {
8796
const err = 'Access token recieved is not a valid JWT';
8897
logger.error(err);
8998
throw new Error(err);
9099
}
91100

101+
// the time of expiration is found by decoding the JWT access token
102+
// 'exp' is the time of expire and 'iat' is the time of token retrieval
92103
const { exp, iat } = decodedResponse;
93104
// There are no required claims in JWT
94105
if (!exp || !iat) {

package-lock.json

Lines changed: 41 additions & 96 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"file-type": "16.5.4",
8686
"form-data": "^2.3.3",
8787
"isstream": "~0.1.2",
88-
"jsonwebtoken": "^8.5.1",
88+
"jsonwebtoken": "^9.0.0",
8989
"lodash.isempty": "^4.4.0",
9090
"mime-types": "~2.1.18",
9191
"object.omit": "~3.0.0",

test/unit/iam-token-manager.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const jwt = require('jsonwebtoken');
2121
jest.mock('../../dist/lib/request-wrapper');
2222
const { RequestWrapper } = require('../../dist/lib/request-wrapper');
2323

24-
jwt.decode = jest.fn(() => ({ exp: 100, iat: 100 }));
24+
jwt.verify = jest.fn(() => ({ exp: 100, iat: 100 }));
2525

2626
const { IamTokenManager } = require('../../dist/auth');
2727

0 commit comments

Comments
 (0)