Skip to content

-Infinity, Infinity and NaN should fail validation #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ var sign_options_schema = {
mutatePayload: { isValid: isBoolean, message: '"mutatePayload" must be a boolean' }
};

var isValidNumber = function(value) {
return isNumber(value) && isFinite(value) && !isNaN(value);
};

var registered_claims_schema = {
iat: { isValid: isNumber, message: '"iat" should be a number of seconds' },
exp: { isValid: isNumber, message: '"exp" should be a number of seconds' },
nbf: { isValid: isNumber, message: '"nbf" should be a number of seconds' }
iat: { isValid: isValidNumber , message: '"iat" should be a number of seconds' },
exp: { isValid: isValidNumber, message: '"exp" should be a number of seconds' },
nbf: { isValid: isValidNumber, message: '"nbf" should be a number of seconds' }
};

function validate(schema, allowUnknown, object, parameterName) {
Expand Down
36 changes: 3 additions & 33 deletions test/claim-exp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ describe('expires', function() {
['foo'],
{},
{foo: 'bar'},
-Infinity,
Infinity,
NaN,
].forEach((exp) => {
it(`should error with with value ${util.inspect(exp)}`, function (done) {
signWithExpiresIn(undefined, {exp}, (err) => {
Expand Down Expand Up @@ -241,39 +244,6 @@ describe('expires', function() {
});
});

// TODO an exp of -Infinity should fail validation
it('should set null "exp" when given -Infinity', function (done) {
signWithExpiresIn(undefined, {exp: -Infinity}, (err, token) => {
const decoded = jwt.decode(token);
testUtils.asyncCheck(done, () => {
expect(err).to.be.null;
expect(decoded).to.have.property('exp', null);
});
});
});

// TODO an exp of Infinity should fail validation
it('should set null "exp" when given value Infinity', function (done) {
signWithExpiresIn(undefined, {exp: Infinity}, (err, token) => {
const decoded = jwt.decode(token);
testUtils.asyncCheck(done, () => {
expect(err).to.be.null;
expect(decoded).to.have.property('exp', null);
});
});
});

// TODO an exp of NaN should fail validation
it('should set null "exp" when given value NaN', function (done) {
signWithExpiresIn(undefined, {exp: NaN}, (err, token) => {
const decoded = jwt.decode(token);
testUtils.asyncCheck(done, () => {
expect(err).to.be.null;
expect(decoded).to.have.property('exp', null);
});
});
});

it('should set correct "exp" when "iat" is passed', function (done) {
signWithExpiresIn(-10, {iat: 80}, (e1, token) => {
testUtils.verifyJWTHelper(token, undefined, {}, (e2, decoded) => {
Expand Down
24 changes: 3 additions & 21 deletions test/claim-iat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ describe('issue at', function() {
['foo'],
{},
{foo: 'bar'},
-Infinity,
Infinity,
NaN,
].forEach((iat) => {
it(`should error with iat of ${util.inspect(iat)}`, function (done) {
signWithIssueAt(iat, {}, (err) => {
Expand Down Expand Up @@ -111,27 +114,6 @@ describe('issue at', function() {
expectedIssueAt: 100,
options: {}
},
// TODO an iat of -Infinity should fail validation
{
description: 'should set null "iat" when given -Infinity',
iat: -Infinity,
expectedIssueAt: null,
options: {}
},
// TODO an iat of Infinity should fail validation
{
description: 'should set null "iat" when given Infinity',
iat: Infinity,
expectedIssueAt: null,
options: {}
},
// TODO an iat of NaN should fail validation
{
description: 'should set to current time for "iat" when given value NaN',
iat: NaN,
expectedIssueAt: 60,
options: {}
},
{
description: 'should remove default "iat" with "noTimestamp" option',
iat: undefined,
Expand Down
36 changes: 3 additions & 33 deletions test/claim-nbf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ describe('not before', function() {
['foo'],
{},
{foo: 'bar'},
-Infinity,
Infinity,
NaN,
].forEach((nbf) => {
it(`should error with with value ${util.inspect(nbf)}`, function (done) {
signWithNotBefore(undefined, {nbf}, (err) => {
Expand Down Expand Up @@ -238,39 +241,6 @@ describe('not before', function() {
});
});

// TODO an nbf of -Infinity should fail validation
it('should set null "nbf" when given -Infinity', function (done) {
signWithNotBefore(undefined, {nbf: -Infinity}, (err, token) => {
const decoded = jwt.decode(token);
testUtils.asyncCheck(done, () => {
expect(err).to.be.null;
expect(decoded).to.have.property('nbf', null);
});
});
});

// TODO an nbf of Infinity should fail validation
it('should set null "nbf" when given value Infinity', function (done) {
signWithNotBefore(undefined, {nbf: Infinity}, (err, token) => {
const decoded = jwt.decode(token);
testUtils.asyncCheck(done, () => {
expect(err).to.be.null;
expect(decoded).to.have.property('nbf', null);
});
});
});

// TODO an nbf of NaN should fail validation
it('should set null "nbf" when given value NaN', function (done) {
signWithNotBefore(undefined, {nbf: NaN}, (err, token) => {
const decoded = jwt.decode(token);
testUtils.asyncCheck(done, () => {
expect(err).to.be.null;
expect(decoded).to.have.property('nbf', null);
});
});
});

it('should set correct "nbf" when "iat" is passed', function (done) {
signWithNotBefore(-10, {iat: 40}, (e1, token) => {
testUtils.verifyJWTHelper(token, undefined, {}, (e2, decoded) => {
Expand Down