Skip to content

Commit be3f4e4

Browse files
committed
fix: do not 'in' operator when importing keys as string
1 parent d5fc18e commit be3f4e4

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎lib/jwk/import.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const importKey = (key, parameters) => {
4040
break
4141
default:
4242
}
43-
} else if ('kty' in key && key.kty === 'oct') { // symmetric key <Object>
43+
} else if (typeof key === 'object' && 'kty' in key && key.kty === 'oct') { // symmetric key <Object>
4444
try {
4545
secret = createSecretKey(base64url.decodeToBuffer(key.k))
4646
} catch (err) {
@@ -49,7 +49,7 @@ const importKey = (key, parameters) => {
4949
}
5050
}
5151
parameters = mergedParameters(parameters, key)
52-
} else if ('kty' in key) { // assume JWK formatted asymmetric key <Object>
52+
} else if (typeof key === 'object' && 'kty' in key) { // assume JWK formatted asymmetric key <Object>
5353
let parsedJWK
5454
try {
5555
parsedJWK = jwkToPem(key)

‎test/jwk/import.test.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ test('parameters must be a plain object', t => {
3636
})
3737
})
3838

39+
Object.entries(fixtures.PEM).forEach(([type, { private: priv, public: pub }]) => {
40+
test(`${type} private can be imported as a string`, t => {
41+
const k = importKey(priv.toString('ascii'))
42+
t.true(k.private)
43+
})
44+
test(`${type} public can be imported as a string`, t => {
45+
const k = importKey(pub.toString('ascii'))
46+
t.true(k.public)
47+
})
48+
test(`${type} private can be imported as a buffer`, t => {
49+
const k = importKey(priv)
50+
t.true(k.private)
51+
})
52+
test(`${type} public can be imported as a buffer`, t => {
53+
const k = importKey(pub)
54+
t.true(k.public)
55+
})
56+
})
57+
3958
test('failed to import throws an error', t => {
4059
t.throws(() => {
4160
importKey({
@@ -44,3 +63,16 @@ test('failed to import throws an error', t => {
4463
})
4564
}, { instanceOf: errors.JWKImportFailed, code: 'ERR_JWK_IMPORT_FAILED' })
4665
})
66+
67+
;[
68+
`-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIHXLsXm1lsq5HtyqJwQyFmpfEluuf0KOqP6DqMgGxxDL\n-----END PRIVATE KEY-----`,
69+
`-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAEXRYV3v5ucrHVR3mKqyPXxXqU34lASwc7Y7MoOvaqcs=\n-----END PUBLIC KEY-----`,
70+
`-----BEGIN PRIVATE KEY-----\nMEcCAQAwBQYDK2VxBDsEObxytD95dGN3Hxk7kVk+Lig1rGYTRr3YdaHjRog++Sgk\nQD7KwKmxroBURtkE2N0JbQ3ctdrpGRB5DQ==\n-----END PRIVATE KEY-----`,
71+
`-----BEGIN PUBLIC KEY-----\nMEMwBQYDK2VxAzoAIESY3jnpGdB5UVJDCznrv0vmBFIzgSMu+gafsbCX1rFtsJwR\nM6XUDQiEY7dk6rmm/Fktyawna5EA\n-----END PUBLIC KEY-----`
72+
].forEach((unsupported, i) => {
73+
test.skip(`fails to import unsupported PEM ${i + 1}/4`, t => {
74+
t.throws(() => {
75+
importKey(unsupported)
76+
}, { instanceOf: errors.JWKImportFailed, code: 'ERR_JWK_IMPORT_FAILED' })
77+
})
78+
})

0 commit comments

Comments
 (0)