Skip to content

Commit

Permalink
SFTPStream: stricter object value checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mscdex committed Jul 17, 2019
1 parent 0de75c7 commit e289238
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/sftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ SFTPStream.prototype.open = function(path, flags_, attrs, cb) {
if (typeof attrs === 'string' || typeof attrs === 'number') {
attrs = { mode: attrs };
}
if (typeof attrs === 'object') {
if (typeof attrs === 'object' && attrs !== null) {
attrs = attrsToBytes(attrs);
attrFlags = attrs.flags;
attrBytes = attrs.nbytes;
Expand Down Expand Up @@ -999,7 +999,7 @@ function fastXfer(src, dst, srcPath, dstPath, opts, cb) {

if (typeof opts === 'function') {
cb = opts;
} else if (typeof opts === 'object') {
} else if (typeof opts === 'object' && opts !== null) {
if (typeof opts.concurrency === 'number'
&& opts.concurrency > 0
&& !isNaN(opts.concurrency))
Expand Down Expand Up @@ -1526,7 +1526,7 @@ SFTPStream.prototype.mkdir = function(path, attrs, cb) {
cb = attrs;
attrs = undefined;
}
if (typeof attrs === 'object') {
if (typeof attrs === 'object' && attrs !== null) {
attrs = attrsToBytes(attrs);
flags = attrs.flags;
attrBytes = attrs.nbytes;
Expand Down Expand Up @@ -1600,7 +1600,7 @@ SFTPStream.prototype.readdir = function(where, opts, cb) {
cb = opts;
opts = {};
}
if (typeof opts !== 'object')
if (typeof opts !== 'object' || opts === null)
opts = {};

doFilter = (opts && opts.full ? false : true);
Expand Down Expand Up @@ -1793,7 +1793,7 @@ SFTPStream.prototype.setstat = function(path, attrs, cb) {
var attrBytes = 0;
var state = this._state;

if (typeof attrs === 'object') {
if (typeof attrs === 'object' && attrs !== null) {
attrs = attrsToBytes(attrs);
flags = attrs.flags;
attrBytes = attrs.nbytes;
Expand Down Expand Up @@ -1840,7 +1840,7 @@ SFTPStream.prototype.fsetstat = function(handle, attrs, cb) {
var attrBytes = 0;
var state = this._state;

if (typeof attrs === 'object') {
if (typeof attrs === 'object' && attrs !== null) {
attrs = attrsToBytes(attrs);
flags = attrs.flags;
attrBytes = attrs.nbytes;
Expand Down Expand Up @@ -2296,10 +2296,11 @@ SFTPStream.prototype.name = function(id, names) {
if (!this.server)
throw new Error('Server-only method called in client mode');

if (!Array.isArray(names) && typeof names === 'object')
if (!Array.isArray(names)) {
if (typeof names !== 'object' || names === null)
throw new Error('names is not an object or array');
names = [ names ];
else if (!Array.isArray(names))
throw new Error('names is not an object or array');
}

var count = names.length;
var namesLen = 0;
Expand Down Expand Up @@ -2328,7 +2329,7 @@ SFTPStream.prototype.name = function(id, names) {
: name.longname);
namesLen += 4 + Buffer.byteLength(longname);

if (typeof name.attrs === 'object') {
if (typeof name.attrs === 'object' && name.attrs !== null) {
nameAttrs = attrsToBytes(name.attrs);
namesLen += 4 + nameAttrs.nbytes;
attrs.push(nameAttrs);
Expand Down Expand Up @@ -2396,7 +2397,7 @@ SFTPStream.prototype.attrs = function(id, attrs) {
if (!this.server)
throw new Error('Server-only method called in client mode');

if (typeof attrs !== 'object')
if (typeof attrs !== 'object' || attrs === null)
throw new Error('attrs is not an object');

var info = attrsToBytes(attrs);
Expand Down Expand Up @@ -2527,6 +2528,9 @@ function attrsToBytes(attrs) {
var ret = [];
var i = 0;

if (typeof attrs !== 'object' || attrs === null)
return { flags: flags, nbytes: attrBytes, bytes: ret };

if (typeof attrs.size === 'number') {
flags |= ATTR.SIZE;
attrBytes += 8;
Expand Down

0 comments on commit e289238

Please sign in to comment.