Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 9f42edc

Browse files
author
Alan Shaw
committed
fix: better input validation for add
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 14a4471 commit 9f42edc

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/files/add.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const promisify = require('promisify-es6')
44
const ConcatStream = require('concat-stream')
55
const once = require('once')
66
const isStream = require('is-stream')
7-
const OtherBuffer = require('buffer').Buffer
7+
const isString = require('lodash/isString')
88
const isSource = require('is-pull-stream').isSource
99
const FileResultStreamConverter = require('../utils/file-result-stream-converter')
1010
const SendFilesStream = require('../utils/send-files-stream')
@@ -25,15 +25,23 @@ module.exports = (send) => {
2525
}
2626
options.converter = FileResultStreamConverter
2727

28-
const ok = Buffer.isBuffer(_files) ||
29-
isStream.readable(_files) ||
30-
Array.isArray(_files) ||
31-
OtherBuffer.isBuffer(_files) ||
32-
typeof _files === 'object' ||
33-
isSource(_files)
28+
// Buffer, pull stream or Node.js stream
29+
const isBufferOrStream = obj => Buffer.isBuffer(obj) || isStream.readable(obj) || isSource(obj)
30+
// An object like { content?, path? }, where content isBufferOrStream and path isString
31+
const isContentObject = obj => {
32+
if (typeof obj !== 'object') return false
33+
// path is optional if content is present
34+
if (obj.content) return isBufferOrStream(obj.content)
35+
// path must be a non-empty string if no content
36+
return Boolean(obj.path) && isString(obj.path)
37+
}
38+
// An input atom: a buffer, stream or content object
39+
const isInput = obj => isBufferOrStream(obj) || isContentObject(obj)
40+
// All is ok if data isInput or data is an array of isInput
41+
const ok = isInput(_files) || (Array.isArray(_files) && _files.every(isInput))
3442

3543
if (!ok) {
36-
return callback(new Error('first arg must be a buffer, readable stream, pull stream, an object or array of objects'))
44+
return callback(new Error('invalid input: expected buffer, readable stream, pull stream, object or array of objects'))
3745
}
3846

3947
const files = [].concat(_files)

0 commit comments

Comments
 (0)