Skip to content

Commit aa88382

Browse files
committed
fix: stricter toString conversion for unsupported types
1 parent f9a05dd commit aa88382

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

lib/grant-types/refresh-token-grant-type.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@ class RefreshTokenGrantType extends AbstractGrantType {
7575
throw new InvalidRequestError('Invalid parameter: `refresh_token`');
7676
}
7777

78+
// normalize string|number to string
7879
const refreshToken = toString(request.body.refresh_token);
7980

8081
if (!isFormat.vschar(refreshToken)) {
8182
throw new InvalidRequestError('Invalid parameter: `refresh_token`');
8283
}
8384

85+
// XXX: still passing the original value from request to model
8486
const token = await this.model.getRefreshToken(request.body.refresh_token);
8587

8688
if (!token) {

lib/handlers/token-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class TokenHandler {
129129
throw new InvalidRequestError('Invalid parameter: `client_id`');
130130
}
131131

132-
if (credentials.clientSecret && !isFormat.vschar(credentials.clientSecret)) {
132+
if (credentials.clientSecret && (!isStringOrNumber(credentials.clientSecret) || !isFormat.vschar(toString(credentials.clientSecret)))) {
133133
throw new InvalidRequestError('Invalid parameter: `client_secret`');
134134
}
135135

lib/utils/param-util.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,25 @@ function isDefined (value) {
3636
* @return {string}
3737
*/
3838
function toString(value) {
39-
if (typeof value === 'string') {
39+
const type = typeof value;
40+
if (type === 'string') {
4041
return value;
4142
}
4243

43-
if (Object.prototype.hasOwnProperty.call(value, 'toString')) {
44-
return value.toString();
44+
if (type === 'undefined' || value === null) {
45+
throw new TypeError(`Cannot convert ${value} to a string`);
4546
}
4647

47-
if (value === null || value === undefined) {
48-
return '';
48+
if (type === 'number' || type === 'bigint') {
49+
const val = String(value);
50+
if (val === 'NaN' || val === 'Infinity' || val === '-Infinity') {
51+
throw new TypeError(`Invalid numeric value ${value}, cannot be converted to a string (${val})`);
52+
}
53+
return val;
4954
}
5055

51-
return String(value);
56+
57+
throw new TypeError(`Cannot convert value ${value} of type ${type} to a string`);
5258
}
5359

5460
module.exports = { isInTypes, isDefined, toString };

0 commit comments

Comments
 (0)