124 Commits
Author SHA1 Message Date
925d95de54 Merge pull request from GHSA-8cf7-32gw-wr33
* Check if node version supports asymmetricKeyDetails

* Validate algorithms for ec key type

* Rename variable

* Rename function

* Add early return for symmetric keys

* Validate algorithm for RSA key type

* Validate algorithm for RSA-PSS key type

* Check key types for EdDSA algorithm

* Rename function

* Move validateKey function to module

* Convert arrow to function notation

* Validate key in verify function

* Simplify if

* Convert if to switch..case

* Guard against empty key in validation

* Remove empty line

* Add lib to check modulus length

* Add modulus length checks

* Validate mgf1HashAlgorithm and saltLength

* Check node version before using key details API

* Use built-in modulus length getter

* Fix Node version validations

* Remove duplicate validateKey

* Add periods to error messages

* Fix validation in verify function

* Make asymmetric key validation the latest validation step

* Change key curve validation

* Remove support for ES256K

* Fix old test that was using wrong key types to sign tokens

* Enable RSA-PSS for old Node versions

* Add specific RSA-PSS validations on Node 16 LTS+

* Improve error message

* Simplify key validation code

* Fix typo

* Improve error message

* Change var to const in test

* Change const to let to avoid reassigning problem

* Improve error message

* Test incorrect private key type

* Rename invalid to unsupported

* Test verifying of jwt token with unsupported key

* Test invalid private key type

* Change order of object parameters

* Move validation test to separate file

* Move all validation tests to separate file

* Add prime256v1 ec key

* Remove modulus length check

* WIP: Add EC key validation tests

* Fix node version checks

* Fix error message check on test

* Add successful tests for EC curve check

* Remove only from describe

* Remove `only`

* Remove duplicate block of code

* Move variable to a different scope and make it const

* Convert allowed curves to object for faster lookup

* Rename variable

* Change variable assignment order

* Remove unused object properties

* Test RSA-PSS happy path and wrong length

* Add missing tests

* Pass validation if no algorithm has been provided

* Test validation of invalid salt length

* Test error when signing token with invalid key

* Change var to const/let in verify tests

* Test verifying token with invalid key

* Improve test error messages

* Add parameter to skip private key validation

* Replace DSA key with a 4096 bit long key

* Test allowInvalidPrivateKeys in key signing

* Improve test message

* Rename variable

* Add key validation flag tests

* Fix variable name in Readme

* Change private to public dsa key in verify

* Rename flag

* Run EC validation tests conditionally

* Fix tests in old node versions

* Ignore block of code from test coverage

* Separate EC validations tests into two different ones

* Add comment

* Wrap switch in if instead of having an early return

* Remove unsupported algorithms from asymmetric key validation

* Rename option to allowInvalidAsymmetricKeyTypes and improve Readme

* 9.0.0

* adding migration notes to readme

* adding changelog for version 9.0.0

Co-authored-by: julienwoll <[email protected]>
2022-12-21 13:36:01 +01:00
david-renaud-oktaandGitHub 44ce6fd923 fix!: Prevent accidental use of insecure key sizes & misconfiguration of secrets (#852)
* fix!: Disable use of weak RSA key sizes for asymmetric algorithms

Added checks to prevent invalid secrets from being used with the HS*** algorithms when signing and verifying
Added checks to prevent the use of insecure asymmetric key sizes except when explicitly overriden via options
Prevented Buffers containing malicious objects from being used as key material.

BREAKING CHANGE: Requires node 12.x or later to allow use of `KeyObject`
2022-11-29 11:58:59 -05:00
Jake LaceyandGitHub 59d32eeef4 fix(sign&verify)!: Remove default none support from sign and verify methods, and require it to be explicitly configured (#851)
* fix(sign&verify)!: Remove default none support from sign and verify methods, and require it to be explicitly configured

BREAKING CHANGE: Removes fallback for none algorithm for the verify method.
2022-11-29 14:32:41 +00:00
Filip SkokanandEduardo Diaz 11a0453c51 feat: add PS JWA support for applicable node versions (#573) 2019-02-20 13:42:37 +01:00
Javier EspinosaandEduardo Diaz 1e880c8c19 Add complete option in jwt.verify (#522)
* Add complete option in verify

* Remove comment

* Update README.md

Co-Authored-By: javespi <[email protected]>

* Move tests in a specific file
2019-02-20 13:32:23 +01:00
Lio李欧andEduardo Diaz f1f30a38a3 Force use_strict during testing (#577)
* Force use_strict during testing

* Add string payload test cases to .iat tests
2019-02-20 13:22:27 +01:00
Tim OramandEduardo Diaz 53a76f1ca2 Add tests for private claims in the payload (#555)
This change adds tests for private claims added to the payload during
sign and ensures that after verifying the payload contains the expected
claim.
2018-12-14 15:17:45 +01:00
Tim OramandEduardo Diaz 18945ff721 Refactor tests related to jti and jwtid (#544)
This change extracts all tests related to the jti claim and the jwtid
option into a single test file. Additional tests were added that were
missing.
2018-11-23 12:12:59 +01:00
kazuki229andEduardo Diaz c7e16cb5d5 Add verify option for nonce validation (#540)
* Add verify option for nonce validation

* Update README.md

Co-Authored-By: kazuki229 <[email protected]>

* Refactor option-nonce test

* Add nonce option validation
2018-11-14 11:02:59 +01:00
Tim OramandEduardo Diaz b280f8cc55 Refactor tests related to kid and keyid (#545)
Thie change extracts the tests related to the kid header and the keyid
option into a single file. Additonal tests were added that were missing.
2018-11-02 11:05:03 +01:00
Tim OramandEduardo Diaz 72223a1f2c Refactor tests related to iss and issuer (#543)
This change extracts all tests related to the iss claim and the issuer
option into a single test file. Additional tests were added that were
missing.
2018-11-02 11:02:16 +01:00
Andrew NesterandEduardo Diaz e1af5cfd31 Fixed error message when empty string passed as expiresIn or notBefore option (#531)
* Fixed error message when empty string passed as expiresIn or notBefore option

* Moved tests to option validation block
2018-10-22 20:40:25 +02:00
Tim OramandEduardo Diaz a4fcadd5b3 Implement async/sync tests for the aud claim (#535)
Refactor existing tests for the aud claim to use the async/sync test
helpers.
2018-10-17 11:37:29 +02:00
Tim OramandEduardo Diaz bb40b2103d Implement async/sync tests for nbf claim (#537)
Refactor the existing tests for the nbf claim to use the async/sync test
helpers. This required fixing a case where an error was improperly
handled for an async call.
2018-10-16 19:46:21 +02:00
Tim OramandEduardo Diaz da8a850628 Implement async/sync tests for exp claim (#536)
Refactor existing tests for the exp claim to use the async/sync test
helpers. This required fixing a case where an error was improperly
handled in the async case.
2018-10-16 18:57:20 +02:00
Tim OramandEduardo Diaz 86a40d8924 Implement async/sync tests for sub claim (#534)
Refactor existing tests for the sub claim to use the async/sync test
helpers.
2018-10-16 18:45:42 +02:00
Tim OramandEduardo Diaz 5ecfc5402e Create and implement async/sync test helpers (#523)
It is difficult to write tests that ensure that both the asynchronous
and synchronous calls to the sign and verify functions had the same
result. These helpers ensure that the calls are the same and return the
common result.

As a proof of concept, the iat claim tests have been updated to use the
new helpers.
2018-10-10 15:37:34 -03:00
Tim OramandEduardo Diaz 477e4f7426 Update dependencies used for running tests (#518)
Update chai and mocha to the latest versions and update nyc to the
latest version that supports Node 4.
2018-09-10 11:48:41 +02:00
Tim OramandEduardo Diaz cc7c764c64 Refactor tests related to iat and maxAge (#507)
This change extracts all tests related to the iat claim and the maxAge
verify option into two test files. Several additional tests are added
that were missing from the existing tests.
2018-08-26 12:52:15 +02:00
Tim OramandEduardo Diaz af874f5891 Refactor tests related to subject and sub (#505)
This change extracts all tests in the existing files related to the
subject option and sub claim into a single test file. Several other
tests are also added that were missing from the existing files.
2018-07-20 18:22:10 +02:00
Tim OramandEduardo Diaz 3d59ed478b Minor test refactoring for recently added tests (#504)
* Prefix claim- to claim related test files

* Fix typo of "signWithNoBfore" in notBefore tests
2018-07-20 18:14:02 +02:00
Tim OramandEduardo Diaz 86a39e7e9e Refactor tests related to audience and aud (#503)
This change extracts all tests in the existing test files related to
audience and aud into a single test file. Several other tests are also
added that were missing from the existing files.
2018-07-12 14:36:35 +02:00
Tim OramandEduardo Diaz 48edd0e3f7 Refactor tests related to expiresIn and exp (#501)
This change extracts all tests in the current test files related
to expiresIn and exp into a single test file. It also adds several
missing tests.
2018-07-06 23:40:49 +02:00
Tim OramandEduardo Diaz 6b0b47fc01 Refactor tests related to notBefore and nbf (#497)
This change extracts all tests in the current files related to notBefore
and nbf into a single test file. It also adds several missing related
tests.
2018-06-27 16:52:32 +02:00
Tim OramandEduardo Diaz c86e573d16 Complete ESLint conversion and cleanup (#490)
* Add extension to ESLint config file

The .eslintrc file without an extension was deprecated a few years ago,
so this change renames the file to add the required extension.

See: https://github.com/eslint/eslint/commit/c9a8883d450d63a8d044a9e66d275f5b1973a3ba

* Add ESLint to package.json

This change adds ESLint as a dev-dependency and adds a lint script that
will run ESLint.

* Complete switch from JSHint to ESLint

Convert all the JSHint rules to the ESLint equivalents where possible.
The no-undef rule in ESLint caught a few cases of undefined usages in
the tests, so they were also fixed.

* Add a .eslintignore file

The HTML coverage report is currently being linted, which causes a lot
if invalid linting errors. This change adds a ignore file to ensure these
files are properly skipped during linting.
2018-06-25 19:54:04 +02:00
Tim OramandEduardo Diaz cc61447047 Use lolex for faking date in tests (#491)
Sinon.JS provides a project called lolex to handle faking dates. This
change replaces the fakeDate utility with the equivalent Sinon.JS code.
2018-06-16 18:36:05 +02:00
Jaco KosterandEduardo Diaz 4d2a22c975 Secret callback revisited (#480)
* Introduction of the secret callback

Without the more contentious 'none'-changes

* Removed some spaces...

I should really add a editor.config and eslint to this project ;-)

* Removed xtend as a dependency, as the native Object.Assign can do this as well

* Removed xtend as a dependency, as the native Object.Assign can do this as well

* Resolve feedback from review

* Added extra test and fixed the associated bug

* The return of the header

* Forgot to change this one as well... Sorry bout that

* Updated the readme and made the if-statements consistent

* Space; The final frontier
2018-06-11 17:32:29 +02:00
Nicolas CognauxandEduardo Diaz e50764b070 Check payload is not null when decoded. (#444)
* Check payload is not null when decoded.
Fixed "Cannot read property 'nbf' of null"

* Condition on obj !== null for clarity

* Added test for decoding null token
2018-04-05 13:28:41 +02:00
Jonathan Gros-DuboisandEduardo Diaz cab1c43ee4 Add a new mutatePayload option (#446)
This option allows you to keep a reference to the raw token payload after claims have been applied to it but before it has been encoded.
2018-03-02 13:48:25 +01:00
Tim OramandEduardo Diaz 9f9d93b40e Not Before (nbf) calculated based on iat/timestamp (#437)
fix #435
2018-01-19 18:28:49 +01:00
TheBusCantSwimandEduardo Diaz 5193ee5616 Enhance audience check to verify against regular expressions (#398)
* Enhance audience check to verify against regular expressions

* Enhance audience check to verify against regular expressions

* Adapted README to have a showcase of the new RegExp-check for the audience validation
2017-10-09 19:29:15 +02:00
Fredrik LiljegrenandGitHub 3f2bff62d4 Merge pull request #349 from ziluvatar/fix-max-age-number-and-seconds
maxAge: Fix logic with number + use seconds instead of ms
2017-09-06 16:43:23 +02:00
Chris PriceandEduardo Diaz 75b897b11d Remove joi to shrink module size (#348)
* Add cost-of-modules report to npm test

Results before any changes

┌─────────────┬────────────┬───────┐
│ name        │ children   │ size  │
├─────────────┼────────────┼───────┤
│ joi         │ 4          │ 3.12M │ <--!!!
├─────────────┼────────────┼───────┤
│ jws         │ 5          │ 0.18M │
├─────────────┼────────────┼───────┤
│ lodash.once │ 0          │ 0.01M │
├─────────────┼────────────┼───────┤
│ ms          │ 0          │ 0.01M │
├─────────────┼────────────┼───────┤
│ xtend       │ 0          │ 0.00M │
├─────────────┼────────────┼───────┤
│ 5 modules   │ 9 children │ 3.32M │
└─────────────┴────────────┴───────┘

* Replace joi with bespoke validator based on lodash

Dramatically reduces the module size without breaking ES5 compatability -

┌──────────────────────┬────────────┬───────┐
│ name                 │ children   │ size  │
├──────────────────────┼────────────┼───────┤
│ jws                  │ 5          │ 0.18M │
├──────────────────────┼────────────┼───────┤
│ lodash.includes      │ 0          │ 0.02M │
├──────────────────────┼────────────┼───────┤
│ lodash.once          │ 0          │ 0.01M │
├──────────────────────┼────────────┼───────┤
│ lodash.isinteger     │ 0          │ 0.01M │
├──────────────────────┼────────────┼───────┤
│ ms                   │ 0          │ 0.01M │
├──────────────────────┼────────────┼───────┤
│ lodash.isplainobject │ 0          │ 0.01M │
├──────────────────────┼────────────┼───────┤
│ xtend                │ 0          │ 0.00M │
├──────────────────────┼────────────┼───────┤
│ lodash.isstring      │ 0          │ 0.00M │
├──────────────────────┼────────────┼───────┤
│ lodash.isboolean     │ 0          │ 0.00M │
├──────────────────────┼────────────┼───────┤
│ lodash.isnumber      │ 0          │ 0.00M │
├──────────────────────┼────────────┼───────┤
│ lodash.isarray       │ 0          │ 0.00M │
├──────────────────────┼────────────┼───────┤
│ 11 modules           │ 5 children │ 0.25M │
└──────────────────────┴────────────┴───────┘

* Enhance validator error messages and add tests
2017-09-06 16:24:55 +02:00
Eduardo Diaz 23cb6f1d24 maxAge: Add validation to timespan result 2017-08-29 12:24:08 +02:00
Eduardo Diaz d63c87dc7c add test & modify guard code 2017-08-17 13:50:26 +02:00
Rhys Anthony McCaig 7ff56f77f3 Fixed alg non + secret set unit test 2017-08-11 17:32:06 -07:00
Rhys Anthony McCaig 95e73a269a Fix for #381. Set secret string before using jws when alg is none 2017-08-11 17:25:55 -07:00
Eduardo Diaz 4cc2615033 sign: add check to be sure secret has a value 2017-07-24 16:59:07 -05:00
Eduardo Diaz 7ca537e2bf Add tests for ES256 2017-06-11 13:18:50 +02:00
Eduardo Diaz 543bf7f71d maxAge: Fix logic with number + use seconds instead of ms 2017-05-13 17:29:19 +02:00
Eduardo Diaz 87d9e777ed Make Options object optional for callback-ish sign 2017-03-08 14:09:04 +01:00
Michael BorstandEduardo Diaz 912e40685f Allow user to specify now. (#274) 2017-02-10 19:27:44 +01:00
Sergey MaslyakovandEduardo Diaz 047f1ae04b Raise jws.decode error to avoid confusion with "invalid token" error (#294)
* Corrected indistinguishable error messages

jws.decode() never throws an error. At least, in its current version. However, if it were to throw an exception, the diagnostics would be indistinguishable from a soft failure to decode a token. I had an extra trailing space on my JWT and it took me some additional debugging work to trace the actual root cause because the error message was not distinct.

* Allowed an exception from inside of jws.decode to be handled by the caller. Currently, jws.decode never throws an exception. The change is made per discsussion in the original PR

* Added a test case and proper forwarding of the possible exception thrown from jws.decode

* Typo correction
2017-02-09 13:54:58 +01:00
Ricardo GamaandEduardo Diaz c4f7670972 Fix handling non string tokens (#305) 2017-02-03 21:07:54 +01:00
Ryan Parman 2f220de780 Simpler PR which implements the changes from #249. 2016-11-28 11:50:37 -08:00
José F. Romaniello d06359ef3b Revert "Merge branch 'venatir-master'"
This reverts commit d66d4ebd3e, reversing
changes made to 5117aacd01.
2016-08-11 13:22:15 -03:00
Mircea Danila Dumitrescu 01903bcdc6 Fixed tests, however typ: 'JWT' should not be in the options at all, so please review other tests 2016-08-10 11:33:45 +01:00
José F. Romaniello 6b50ff324b fix issue with buffer payload. closes #216 2016-07-15 09:32:35 -03:00
José F. Romaniello 758ca5eeca temporarly fix an issue of double callback in JWS 2016-07-14 17:30:25 -03:00
José F. Romaniello bece881609 add redundant test 2016-07-14 17:18:41 -03:00