jose/test/jwk/general.test.js
Filip Skokan 5b53cb0155 fix: limit calculation of missing RSA private components
- this deprecates the use of `JWK.importKey` in favor of
`JWK.asKey`
- this deprecates the use of `JWKS.KeyStore.fromJWKS` in favor of
`JWKS.asKeyStore`

Both `JWK.importKey` and `JWKS.KeyStore.fromJWKS` could have resulted
in the process getting blocked when large bitsize RSA private keys
were missing their components and could also result in an endless
calculation loop when the private key's private exponent was outright
invalid or tampered with.

The new methods still allow to import private RSA keys with these
optimization key parameters missing but its disabled by default and one
should choose to enable it when working with keys from trusted sources

It is recommended not to use @panva/jose versions with this feature in
its original on-by-default form - v1.1.0 and v1.2.0 These will
2019-06-20 23:32:13 +02:00

46 lines
1.4 KiB
JavaScript

const test = require('ava')
const { JWK: { generateSync, isKey, asKey } } = require('../..')
test('.isKey() only key objects return true', t => {
;[[], false, true, null, Infinity, 0].forEach((val) => {
t.false(isKey(val))
})
;['RSA', 'EC', 'oct'].forEach((kty) => {
t.true(isKey(generateSync(kty)))
})
})
test('"use" must be either `alg` or `enc`', t => {
;[[], false, true, null, Infinity, 0].forEach((val) => {
t.throws(
() => generateSync('oct', undefined, { use: val }),
{ instanceOf: TypeError, message: '`use` must be either "sig" or "enc" string when provided' }
)
})
})
test('"alg" must be a non-empty string', t => {
;[[], false, true, null, '', Infinity, 0].forEach((val) => {
t.throws(
() => generateSync('oct', undefined, { alg: val }),
{ instanceOf: TypeError, message: '`alg` must be a non-empty string when provided' }
)
})
})
test('"kid" must be a non-empty string', t => {
;[[], false, true, null, '', Infinity, 0].forEach((val) => {
t.throws(
() => generateSync('oct', undefined, { kid: val }),
{ instanceOf: TypeError, message: '`kid` must be a non-empty string when provided' }
)
})
})
test('"kid" from JWK is used when available and its different from thumbprint', t => {
const { kid: generatedThumbprint, ...jwk } = generateSync('oct').toJWK(true)
const key = asKey({ ...jwk, kid: 'foo' })
t.is(key.kid, 'foo')
t.is(key.thumbprint, generatedThumbprint)
})