From df673074f14435d16e484f20794956152ae41380 Mon Sep 17 00:00:00 2001 From: Maulana Satya Adi Date: Sun, 2 Feb 2025 23:57:51 +0700 Subject: [PATCH] Deprecated: payload must be valid JSON object https://www.rfc-editor.org/rfc/rfc7519#section-7.2 --- sign.js | 4 ++++ test/non_object_values.tests.js | 34 +++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/sign.js b/sign.js index 82bf526e..bd2a742d 100644 --- a/sign.js +++ b/sign.js @@ -84,6 +84,10 @@ const options_for_objects = [ ]; module.exports = function (payload, secretOrPrivateKey, options, callback) { + if (typeof payload !== 'object') { + throw Error('Payload must be valid JSON object') + } + if (typeof options === 'function') { callback = options; options = {}; diff --git a/test/non_object_values.tests.js b/test/non_object_values.tests.js index a3de4ea6..4955b9d2 100644 --- a/test/non_object_values.tests.js +++ b/test/non_object_values.tests.js @@ -3,16 +3,34 @@ var expect = require('chai').expect; describe('non_object_values values', function() { - it('should work with string', function () { - var token = jwt.sign('hello', '123'); - var result = jwt.verify(token, '123'); - expect(result).to.equal('hello'); + it('should does not work with string', function () { + expect(function () { + jwt.sign('hello', '123'); + }).to.throw('Payload must be valid JSON object'); }); - it('should work with number', function () { - var token = jwt.sign(123, '123'); - var result = jwt.verify(token, '123'); - expect(result).to.equal('123'); + it('should does not work with number', function () { + expect(function () { + jwt.sign(123, '123'); + }).to.throw('Payload must be valid JSON object'); }); + it('should does not work with function', function () { + expect(function () { + jwt.sign(function () { return 0 }, '123') + }).to.throw('Payload must be valid JSON object') + }) + + it('should does not work with boolean', function () { + expect(function () { + jwt.sign(true, '123') + }).to.throw('Payload must be valid JSON object') + }) + + it('should does not work with undefined', function () { + expect(function () { + jwt.sign(undefined, '123') + }).to.throw('Payload must be valid JSON object') + }) + });