Skip to content

Commit a84c57e

Browse files
committed
Fixed #500: -Infinity, Infinity and NaN should fail validation
1 parent 88bc965 commit a84c57e

File tree

4 files changed

+16
-90
lines changed

4 files changed

+16
-90
lines changed

sign.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ var sign_options_schema = {
2323
mutatePayload: { isValid: isBoolean, message: '"mutatePayload" must be a boolean' }
2424
};
2525

26+
var isValidNumber = function(value) {
27+
return isNumber(value) && isFinite(value) && !isNaN(value);
28+
};
29+
2630
var registered_claims_schema = {
27-
iat: { isValid: isNumber, message: '"iat" should be a number of seconds' },
28-
exp: { isValid: isNumber, message: '"exp" should be a number of seconds' },
29-
nbf: { isValid: isNumber, message: '"nbf" should be a number of seconds' }
31+
iat: { isValid: isValidNumber , message: '"iat" should be a number of seconds' },
32+
exp: { isValid: isValidNumber, message: '"exp" should be a number of seconds' },
33+
nbf: { isValid: isValidNumber, message: '"nbf" should be a number of seconds' }
3034
};
3135

3236
function validate(schema, allowUnknown, object, parameterName) {

test/claim-exp.test.js

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ describe('expires', function() {
113113
['foo'],
114114
{},
115115
{foo: 'bar'},
116+
-Infinity,
117+
Infinity,
118+
NaN,
116119
].forEach((exp) => {
117120
it(`should error with with value ${util.inspect(exp)}`, function (done) {
118121
signWithExpiresIn(undefined, {exp}, (err) => {
@@ -241,39 +244,6 @@ describe('expires', function() {
241244
});
242245
});
243246

244-
// TODO an exp of -Infinity should fail validation
245-
it('should set null "exp" when given -Infinity', function (done) {
246-
signWithExpiresIn(undefined, {exp: -Infinity}, (err, token) => {
247-
const decoded = jwt.decode(token);
248-
testUtils.asyncCheck(done, () => {
249-
expect(err).to.be.null;
250-
expect(decoded).to.have.property('exp', null);
251-
});
252-
});
253-
});
254-
255-
// TODO an exp of Infinity should fail validation
256-
it('should set null "exp" when given value Infinity', function (done) {
257-
signWithExpiresIn(undefined, {exp: Infinity}, (err, token) => {
258-
const decoded = jwt.decode(token);
259-
testUtils.asyncCheck(done, () => {
260-
expect(err).to.be.null;
261-
expect(decoded).to.have.property('exp', null);
262-
});
263-
});
264-
});
265-
266-
// TODO an exp of NaN should fail validation
267-
it('should set null "exp" when given value NaN', function (done) {
268-
signWithExpiresIn(undefined, {exp: NaN}, (err, token) => {
269-
const decoded = jwt.decode(token);
270-
testUtils.asyncCheck(done, () => {
271-
expect(err).to.be.null;
272-
expect(decoded).to.have.property('exp', null);
273-
});
274-
});
275-
});
276-
277247
it('should set correct "exp" when "iat" is passed', function (done) {
278248
signWithExpiresIn(-10, {iat: 80}, (e1, token) => {
279249
testUtils.verifyJWTHelper(token, undefined, {}, (e2, decoded) => {

test/claim-iat.test.js

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ describe('issue at', function() {
3737
['foo'],
3838
{},
3939
{foo: 'bar'},
40+
-Infinity,
41+
Infinity,
42+
NaN,
4043
].forEach((iat) => {
4144
it(`should error with iat of ${util.inspect(iat)}`, function (done) {
4245
signWithIssueAt(iat, {}, (err) => {
@@ -111,27 +114,6 @@ describe('issue at', function() {
111114
expectedIssueAt: 100,
112115
options: {}
113116
},
114-
// TODO an iat of -Infinity should fail validation
115-
{
116-
description: 'should set null "iat" when given -Infinity',
117-
iat: -Infinity,
118-
expectedIssueAt: null,
119-
options: {}
120-
},
121-
// TODO an iat of Infinity should fail validation
122-
{
123-
description: 'should set null "iat" when given Infinity',
124-
iat: Infinity,
125-
expectedIssueAt: null,
126-
options: {}
127-
},
128-
// TODO an iat of NaN should fail validation
129-
{
130-
description: 'should set to current time for "iat" when given value NaN',
131-
iat: NaN,
132-
expectedIssueAt: 60,
133-
options: {}
134-
},
135117
{
136118
description: 'should remove default "iat" with "noTimestamp" option',
137119
iat: undefined,

test/claim-nbf.test.js

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ describe('not before', function() {
113113
['foo'],
114114
{},
115115
{foo: 'bar'},
116+
-Infinity,
117+
Infinity,
118+
NaN,
116119
].forEach((nbf) => {
117120
it(`should error with with value ${util.inspect(nbf)}`, function (done) {
118121
signWithNotBefore(undefined, {nbf}, (err) => {
@@ -238,39 +241,6 @@ describe('not before', function() {
238241
});
239242
});
240243

241-
// TODO an nbf of -Infinity should fail validation
242-
it('should set null "nbf" when given -Infinity', function (done) {
243-
signWithNotBefore(undefined, {nbf: -Infinity}, (err, token) => {
244-
const decoded = jwt.decode(token);
245-
testUtils.asyncCheck(done, () => {
246-
expect(err).to.be.null;
247-
expect(decoded).to.have.property('nbf', null);
248-
});
249-
});
250-
});
251-
252-
// TODO an nbf of Infinity should fail validation
253-
it('should set null "nbf" when given value Infinity', function (done) {
254-
signWithNotBefore(undefined, {nbf: Infinity}, (err, token) => {
255-
const decoded = jwt.decode(token);
256-
testUtils.asyncCheck(done, () => {
257-
expect(err).to.be.null;
258-
expect(decoded).to.have.property('nbf', null);
259-
});
260-
});
261-
});
262-
263-
// TODO an nbf of NaN should fail validation
264-
it('should set null "nbf" when given value NaN', function (done) {
265-
signWithNotBefore(undefined, {nbf: NaN}, (err, token) => {
266-
const decoded = jwt.decode(token);
267-
testUtils.asyncCheck(done, () => {
268-
expect(err).to.be.null;
269-
expect(decoded).to.have.property('nbf', null);
270-
});
271-
});
272-
});
273-
274244
it('should set correct "nbf" when "iat" is passed', function (done) {
275245
signWithNotBefore(-10, {iat: 40}, (e1, token) => {
276246
testUtils.verifyJWTHelper(token, undefined, {}, (e2, decoded) => {

0 commit comments

Comments
 (0)