Skip to content

Commit

Permalink
Massaging object name to encode all sensitive characters to allow pro…
Browse files Browse the repository at this point in the history
…perly interaction with Knox client/auth
  • Loading branch information
jrnt30 committed Jul 24, 2015
1 parent 42f67d2 commit bc90850
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/multipartupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function MultiPartUpload(opts, callback) {

callback = callback || function(err, results) {};

this.objectName = opts.objectName;
this.objectName = encodeSpecialCharacters(opts.objectName);
this.fileName = opts.file;
this.headers = opts.headers || {};
this.client = opts.client;
Expand Down Expand Up @@ -110,15 +110,6 @@ util.inherits(MultiPartUpload, EventEmitter);
* Attempts to initiate the MultiPartUpload request (gets the upload ID)
*/
MultiPartUpload.prototype._initiate = function(callback) {

if( this.objectName && this.objectName.indexOf(' ') != -1 ){
/*Replace the spaces in the name with URL encode.
If the spaces are retained, "path" in the HTTP request will be deemed invalid.
Not using native JS API for url encode, because the name might have a forward slash,
that needs to be retained.*/
this.objectName = this.objectName.replace(/ /g, '%20');
}

// Send the initiate request
var req = this.client.request('POST', this.objectName + '?uploads', this.headers),
mpu = this;
Expand All @@ -127,14 +118,14 @@ MultiPartUpload.prototype._initiate = function(callback) {
parse.xmlResponse(req, function(err, body) {

if (err) return callback(err);

if (body === null) {
return callback((function (){
var err = new Error('Unexpected response from AWS.');
return err;
})());
}

if (!body.UploadId) {
return callback((function (){
var err = new Error('Unexpected response from AWS:' + util.inspect(body, false, null));
Expand Down Expand Up @@ -491,6 +482,15 @@ MultiPartUpload.prototype._abortUploads = function(callback) {

module.exports = MultiPartUpload;


function encodeSpecialCharacters(filename) {
// Note: these characters are valid in URIs, but S3 does not like them for
// some reason.
return encodeURI(filename).replace(/[!'()#*+? ]/g, function (char) {
return '%' + char.charCodeAt(0).toString(16);
});
}

function random_seed(){
return 'xxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);});
}

0 comments on commit bc90850

Please sign in to comment.