Skip to content

Commit 2411f92

Browse files
committed
compliance(pkce): throw InvalidRequestError if code request contains unsupported code challenge method
1 parent 6e6edcb commit 2411f92

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/handlers/authorize-handler.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const UnauthorizedClientError = require('../errors/unauthorized-client-error');
2121
const isFormat = require('@node-oauth/formats');
2222
const tokenUtil = require('../utils/token-util');
2323
const url = require('url');
24+
const pkce = require('../pkce/pkce');
2425

2526
/**
2627
* Response types.
@@ -381,9 +382,18 @@ AuthorizeHandler.prototype.getCodeChallenge = function(request) {
381382
/**
382383
* Get code challenge method from request or defaults to plain.
383384
* https://www.rfc-editor.org/rfc/rfc7636#section-4.3
385+
*
386+
* @throws {InvalidRequestError} if request contains unsupported code_challenge_method
387+
* (see https://www.rfc-editor.org/rfc/rfc7636#section-4.4)
384388
*/
385389
AuthorizeHandler.prototype.getCodeChallengeMethod = function(request) {
386-
return request.body.code_challenge_method || 'plain';
390+
const algorithm = request.body.code_challenge_method;
391+
392+
if (algorithm && !pkce.isValidMethod(algorithm)) {
393+
throw new InvalidRequestError(`Invalid request: transform algorithm '${algorithm}' not supported`);
394+
}
395+
396+
return algorithm || 'plain';
387397
};
388398

389399
/**

test/integration/handlers/authorize-handler_test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,26 @@ describe('AuthorizeHandler integration', function() {
13161316
codeChallengeMethod.should.equal('S256');
13171317
});
13181318

1319+
it('should throw if the code challenge method is not supported', async function () {
1320+
const model = {
1321+
getAccessToken: function() {},
1322+
getClient: function() {},
1323+
saveAuthorizationCode: function() {}
1324+
};
1325+
const handler = new AuthorizeHandler({ authorizationCodeLifetime: 120, model: model });
1326+
const request = new Request({ body: {code_challenge_method: 'foo'}, headers: {}, method: {}, query: {} });
1327+
1328+
try {
1329+
handler.getCodeChallengeMethod(request);
1330+
1331+
should.fail();
1332+
} catch (e) {
1333+
// defined in RFC 7636 - 4.4
1334+
e.should.be.an.instanceOf(InvalidRequestError);
1335+
e.message.should.equal('Invalid request: transform algorithm \'foo\' not supported');
1336+
}
1337+
});
1338+
13191339
it('should get default code challenge method plain if missing', function() {
13201340
const model = {
13211341
getAccessToken: function() {},

0 commit comments

Comments
 (0)