-
Notifications
You must be signed in to change notification settings - Fork 99
Simple Whitelist for File Extensions #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1073,14 +1073,31 @@ FtpConnection.prototype._command_RNFR = function(commandArg) { | |
| FtpConnection.prototype._command_RNTO = function(commandArg) { | ||
| var self = this; | ||
| var fileto = withCwd(self.cwd, commandArg); | ||
| self.fs.rename(pathModule.join(self.root, self.filefrom), pathModule.join(self.root, fileto), function(err) { | ||
| if (err) { | ||
| self._logIf(LOG.ERROR, 'Error renaming file from ' + self.filefrom + ' to ' + fileto); | ||
| self.respond('550 Rename failed' + (err.code === 'ENOENT' ? '; file does not exist' : '')); | ||
| } else { | ||
| self.respond('250 File renamed successfully'); | ||
| } | ||
| }); | ||
|
|
||
| // Filter extensions if whitelist available | ||
| var whitelist = self.server.options.allowedExtensions; | ||
| var accepted = true; | ||
| if (whitelist && typeof whitelist === 'object' && Array.isArray(whitelist) && whitelist.length) { | ||
| accepted = false; | ||
| whitelist.forEach(function (ext) { | ||
| if (fileto.endsWith(ext)) accepted = true; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check what version of Node added support for
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should allow RegExps too, or Cartesian product will explode for relatively simple stuff like |
||
| }); | ||
| } | ||
| if (accepted) { | ||
| self.fs.rename(pathModule.join(self.root, self.filefrom), pathModule.join(self.root, fileto), function(err) { | ||
| if (err) { | ||
| self._logIf(LOG.ERROR, 'Error renaming file from ' + self.filefrom + ' to ' + fileto); | ||
| self.respond('550 Rename failed' + (err.code === 'ENOENT' ? '; file does not exist' : '')); | ||
| } else { | ||
| self.respond('250 File renamed successfully'); | ||
| } | ||
| }); | ||
| } else { | ||
| self.respond("553 Rename failed; file type not allowed", function () { | ||
| self._logIf(3, "Disallowed renaming of file from " + self.filefrom + " to " + fileto); | ||
| }); | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| FtpConnection.prototype._command_SIZE = function(commandArg) { | ||
|
|
@@ -1213,9 +1230,24 @@ FtpConnection.prototype._STOR_usingWriteFile = function(filename, flag) { | |
| time: startTime, | ||
| }); | ||
|
|
||
| self.respond('150 Ok to send data', function() { | ||
| self._whenDataReady(handleUpload); | ||
| }); | ||
| // Filter extensions if whitelist available | ||
| var whitelist = self.server.options.allowedExtensions; | ||
| var accepted = true; | ||
| if (whitelist && typeof whitelist === 'object' && Array.isArray(whitelist) && whitelist.length) { | ||
| accepted = false; | ||
| whitelist.forEach(function (ext) { | ||
| if (filename.endsWith(ext)) accepted = true; | ||
| }); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're repeating this whitelist checking logic, I think it would make sense to put it in a function so we can do:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also it should be have "extension" in its name because we might add other whitelists in the future. |
||
| if (accepted) { | ||
| self.respond("150 Ok to send data", function () { | ||
| self._whenDataReady(handleUpload); | ||
| }); | ||
| } else { | ||
| self.respond("553 Requested file action aborted; file type not allowed", function () { | ||
| if (self.dataSocket) self._closeSocket(self.dataSocket); | ||
| }); | ||
| } | ||
|
|
||
| function handleUpload() { | ||
| self.dataSocket.on('data', dataHandler); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array.isArray(whitelist) && whitelist.lengthshould be sufficient, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, if it's an object and has a length, why check for array-ity?