File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -160,9 +160,7 @@ class AuthorizationCodeGrantType extends AbstractGrantType {
160160
161161 // xxx: Use timingSafeEqual to prevent against timing attacks when comparing
162162 // the hash of the code_verifier to the stored code_challenge.
163- const hashesAreEqual = crypto . timingSafeEqual ( Buffer . from ( hash ) , Buffer . from ( code . codeChallenge ) ) ;
164-
165- if ( ! hashesAreEqual ) {
163+ if ( ! this . hashesAreEqual ( hash , code . codeChallenge ) ) {
166164 throw new InvalidGrantError ( 'Invalid grant: code verifier is invalid' ) ;
167165 }
168166 }
@@ -174,6 +172,15 @@ class AuthorizationCodeGrantType extends AbstractGrantType {
174172 }
175173 }
176174
175+ hashesAreEqual ( trusted , untrusted ) {
176+ const trustedBuf = Buffer . isBuffer ( trusted ) ? trusted : Buffer . from ( trusted ) ;
177+ const untrustedBuf = Buffer . isBuffer ( untrusted ) ? untrusted : Buffer . from ( untrusted ) ;
178+ const equalLength = trustedBuf . byteLength === untrustedBuf . byteLength ;
179+ // if the buffers are the same length, compare them,
180+ // otherwise only compare with the trusted buffer but return false anyway
181+ return crypto . timingSafeEqual ( trustedBuf , equalLength ? untrustedBuf : trustedBuf ) && equalLength ;
182+ }
183+
177184 getCodeChallengeMethod ( method ) {
178185 if ( method ) {
179186 return method ;
Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ const { base64URLEncode } = require('../../lib/utils/string-util');
4545const { createHash } = require ( '../../lib/utils/crypto-util' ) ;
4646const { InvalidRequestError } = require ( '../../index' ) ;
4747const ServerError = require ( '../../lib/errors/server-error' ) ;
48- const InvalidGrantError = require ( '../../lib/errors/invalid-grant-error' )
48+ const InvalidGrantError = require ( '../../lib/errors/invalid-grant-error' ) ;
4949require ( 'chai' ) . should ( ) ;
5050
5151/**
You can’t perform that action at this time.
0 commit comments