Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 95c6d5d

Browse files
author
Alan Shaw
committed
fix: better input validation for add
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent d8cfbd9 commit 95c6d5d

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/core/components/files.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const waterfall = require('async/waterfall')
1414
const isStream = require('is-stream')
1515
const isSource = require('is-pull-stream').isSource
1616
const Duplex = require('readable-stream').Duplex
17-
const OtherBuffer = require('buffer').Buffer
17+
const isString = require('lodash/isString')
1818
const CID = require('cids')
1919
const toB58String = require('multihashes').toB58String
2020
const errCode = require('err-code')
@@ -270,15 +270,23 @@ module.exports = function files (self) {
270270

271271
options = options || {}
272272

273-
const ok = Buffer.isBuffer(data) ||
274-
isStream.readable(data) ||
275-
Array.isArray(data) ||
276-
OtherBuffer.isBuffer(data) ||
277-
typeof data === 'object' ||
278-
isSource(data)
273+
// Buffer, pull stream or Node.js stream
274+
const isBufferOrStream = obj => Buffer.isBuffer(obj) || isStream.readable(obj) || isSource(obj)
275+
// An object like { content?, path? }, where content isBufferOrStream and path isString
276+
const isContentObject = obj => {
277+
if (typeof obj !== 'object') return false
278+
// path is optional if content is present
279+
if (obj.content) return isBufferOrStream(obj.content)
280+
// path must be a non-empty string if no content
281+
return Boolean(obj.path) && isString(obj.path)
282+
}
283+
// An input atom: a buffer, stream or content object
284+
const isInput = obj => isBufferOrStream(obj) || isContentObject(obj)
285+
// All is ok if data isInput or data is an array of isInput
286+
const ok = isInput(data) || (Array.isArray(data) && data.every(isInput))
279287

280288
if (!ok) {
281-
return callback(new Error('first arg must be a buffer, readable stream, pull stream, an object or array of objects'))
289+
return callback(new Error('invalid input: expected buffer, readable stream, pull stream, object or array of objects'))
282290
}
283291

284292
// CID v0 is for multihashes encoded with sha2-256

0 commit comments

Comments
 (0)