Skip to content

Commit

Permalink
fix: _signatureForURL bug (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiyie authored Apr 17, 2020
1 parent 262cc4c commit cef1840
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,7 @@ parameters:
- [process] {String} image process params, will send with `x-oss-process`
e.g.: `{process: 'image/resize,w_200'}`
- [trafficLimit] {Number} traffic limit, range: `819200`~`838860800`.
- [subResource] {Object} additional signature parameters in url.
- [response] {Object} set the response headers for download
- [content-type] {String} set the response content type
- [content-disposition] {String} set the response content disposition
Expand Down
6 changes: 2 additions & 4 deletions lib/common/signUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ exports.authorization = function authorization(accessKeyId, accessKeySecret, can
* @param {String} resource
* @param {Number} expires
*/
exports._signatureForURL = function _signatureForURL(accessKeySecret, options, resource, expires) {
exports._signatureForURL = function _signatureForURL(accessKeySecret, options = {}, resource, expires) {
const headers = {};
const subResource = {};
const { subResource = {} } = options;

if (options.process) {
const processKeyword = 'x-oss-process';
Expand Down Expand Up @@ -133,8 +133,6 @@ exports._signatureForURL = function _signatureForURL(accessKeySecret, options, r
headers[key] = value;
} else if (lowerKey.indexOf('content-type') === 0) {
headers[key] = value;
} else if (lowerKey !== 'expires' && lowerKey !== 'response' && lowerKey !== 'process' && lowerKey !== 'method' && lowerKey !== 'trafficlimit') {
subResource[lowerKey] = value;
}
});

Expand Down
26 changes: 25 additions & 1 deletion test/node/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ describe('test/object.test.js', () => {
assert.equal(urlRes.data.toString(), result.content.toString());
});

it('should signature url with reponse limitation', async () => {
it('should signature url with response limitation', async () => {
const response = {
'content-type': 'xml',
'content-language': 'zh-cn'
Expand All @@ -961,6 +961,30 @@ describe('test/object.test.js', () => {
assert(url.indexOf('response-content-language=zh-cn') !== -1);
});

it('should signature url with options contains other parameters', async () => {
const options = {
expires: 3600,
subResource: {
'x-oss-process': 'image/resize,w_200',
},
// others parameters
filename: 'test.js',
testParameters: 'xxx',
};
const imageName = `${prefix}ali-sdk/oss/nodejs-test-signature-1024x768.png`;
const originImagePath = path.join(__dirname, 'nodejs-1024x768.png');
path.join(__dirname, 'nodejs-processed-w200.png');
await store.put(imageName, originImagePath, {
mime: 'image/png',
});

const signUrl = store.signatureUrl(imageName, options);
const processedKeyword = 'x-oss-process=image%2Fresize%2Cw_200';
assert.equal(signUrl.match(processedKeyword), processedKeyword);
const urlRes = await urllib.request(signUrl);
assert.equal(urlRes.status, 200);
});

it('should signature url with image processed and get object ok', async () => {
const imageName = `${prefix}ali-sdk/oss/nodejs-test-signature-1024x768.png`;
const originImagePath = path.join(__dirname, 'nodejs-1024x768.png');
Expand Down

0 comments on commit cef1840

Please sign in to comment.