Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 8dfaab1

Browse files
Alan Shawvasco-santos
authored andcommitted
fix: validate createKey params properly (#26)
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent f95fef4 commit 8dfaab1

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/keychain.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const deepmerge = require('lodash/merge')
66
const crypto = require('libp2p-crypto')
77
const DS = require('interface-datastore')
88
const pull = require('pull-stream')
9+
const isString = require('lodash/isString')
10+
const isSafeInteger = require('lodash/isSafeInteger')
911
const CMS = require('./cms')
1012

1113
const keyPrefix = '/pkcs8/'
@@ -30,6 +32,7 @@ const defaultOptions = {
3032

3133
function validateKeyName (name) {
3234
if (!name) return false
35+
if (!isString(name)) return false
3336
return name === sanitize(name.trim())
3437
}
3538

@@ -182,6 +185,15 @@ class Keychain {
182185
if (!validateKeyName(name) || name === 'self') {
183186
return _error(callback, `Invalid key name '${name}'`)
184187
}
188+
189+
if (!isString(type)) {
190+
return _error(callback, `Invalid key type '${type}'`)
191+
}
192+
193+
if (!isSafeInteger(size)) {
194+
return _error(callback, `Invalid key size '${size}'`)
195+
}
196+
185197
const dsname = DsName(name)
186198
self.store.has(dsname, (err, exists) => {
187199
if (err) return _error(callback, err)

test/keychain.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,30 @@ module.exports = (datastore1, datastore2) => {
117117
})
118118
})
119119

120+
it('should validate name is string', (done) => {
121+
ks.createKey(5, 'rsa', 2048, (err) => {
122+
expect(err).to.exist()
123+
expect(err.message).to.contain('Invalid key name')
124+
done()
125+
})
126+
})
127+
128+
it('should validate type is string', (done) => {
129+
ks.createKey('TEST' + Date.now(), null, 2048, (err) => {
130+
expect(err).to.exist()
131+
expect(err.message).to.contain('Invalid key type')
132+
done()
133+
})
134+
})
135+
136+
it('should validate size is integer', (done) => {
137+
ks.createKey('TEST' + Date.now(), 'rsa', 'string', (err) => {
138+
expect(err).to.exist()
139+
expect(err.message).to.contain('Invalid key size')
140+
done()
141+
})
142+
})
143+
120144
describe('implements NIST SP 800-131A', () => {
121145
it('disallows RSA length < 2048', (done) => {
122146
ks.createKey('bad-nist-rsa', 'rsa', 1024, (err) => {

0 commit comments

Comments
 (0)