Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
feat(s3): don't encode, prefix, or lower-case certain params
Browse files Browse the repository at this point in the history
TODO:
- unit tests
- doc updates
#1494
  • Loading branch information
rnicholus committed Dec 30, 2015
1 parent dbb7e1d commit 2a5f261
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
10 changes: 8 additions & 2 deletions client/js/s3/uploader.basic.js
Expand Up @@ -227,15 +227,21 @@
};
});

// Param names should be lower case to avoid signature mismatches
// Some param names should be lower case to avoid signature mismatches
qq.override(this._paramsStore, function(super_) {
return {
get: function(id) {
var oldParams = super_.get(id),
modifiedParams = {};

qq.each(oldParams, function(name, val) {
modifiedParams[name.toLowerCase()] = qq.isFunction(val) ? val() : val;
var paramName = name;

if (qq.indexOf(qq.s3.util.CASE_SENSITIVE_PARAM_NAMES, paramName) < 0) {
paramName = paramName.toLowerCase();
}

modifiedParams[paramName] = qq.isFunction(val) ? val() : val;
});

return modifiedParams;
Expand Down
49 changes: 35 additions & 14 deletions client/js/s3/util.js
Expand Up @@ -25,6 +25,23 @@ qq.s3.util = qq.s3.util || (function() {

V4_SIGNATURE_PARAM_NAME: "x-amz-signature",

CASE_SENSITIVE_PARAM_NAMES: [
"Cache-Control",
"Content-Disposition",
"Content-Encoding",
"Content-MD5"
],

UNPREFIXED_PARAM_NAMES: [
"Cache-Control",
"Content-Disposition",
"Content-Encoding",
"Content-MD5",
"x-amz-server-side-encryption-customer-algorithm",
"x-amz-server-side-encryption-customer-key",
"x-amz-server-side-encryption-customer-key-MD5"
],

/**
* This allows for the region to be specified in the bucket's endpoint URL, or not.
*
Expand Down Expand Up @@ -71,18 +88,10 @@ qq.s3.util = qq.s3.util || (function() {
* See: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
*/
_getPrefixedParamName: function(name) {
switch (name) {
case "Cache-Control":
case "Content-Disposition":
case "Content-Encoding":
case "Content-MD5": // CAW: Content-MD5 might not be appropriate from user-land
case "x-amz-server-side-encryption-customer-algorithm":
case "x-amz-server-side-encryption-customer-key":
case "x-amz-server-side-encryption-customer-key-MD5":
return name;
default:
return qq.s3.util.AWS_PARAM_PREFIX + name;
if (qq.indexOf(qq.s3.util.UNPREFIXED_PARAM_NAMES, name) >= 0) {
return name;
}
return qq.s3.util.AWS_PARAM_PREFIX + name;
},

/**
Expand Down Expand Up @@ -169,7 +178,13 @@ qq.s3.util = qq.s3.util || (function() {
var awsParamName = qq.s3.util._getPrefixedParamName(name),
param = {};

param[awsParamName] = encodeURIComponent(val);
if (qq.indexOf(qq.s3.util.UNPREFIXED_PARAM_NAMES, awsParamName) >= 0) {
param[awsParamName] = val;
}
else {
param[awsParamName] = encodeURIComponent(val);
}

conditions.push(param);
});

Expand Down Expand Up @@ -269,10 +284,16 @@ qq.s3.util = qq.s3.util || (function() {

// Custom (user-supplied) params must be prefixed with the value of `qq.s3.util.AWS_PARAM_PREFIX`.
// Params such as Cache-Control or Content-Disposition will not be prefixed.
// All param values will be URI encoded as well.
// Prefixed param values will be URI encoded as well.
qq.each(customParams, function(name, val) {
var awsParamName = qq.s3.util._getPrefixedParamName(name);
awsParams[awsParamName] = encodeURIComponent(val);

if (qq.indexOf(qq.s3.util.UNPREFIXED_PARAM_NAMES, awsParamName) >= 0) {
awsParams[awsParamName] = val;
}
else {
awsParams[awsParamName] = encodeURIComponent(val);
}
});

if (signatureVersion === 2) {
Expand Down

0 comments on commit 2a5f261

Please sign in to comment.