Skip to content

Commit 69f7d9e

Browse files
committed
优化签名错误问题
1 parent 19d602d commit 69f7d9e

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cos-nodejs-sdk-v5",
3-
"version": "2.8.1",
3+
"version": "2.8.2",
44
"description": "cos nodejs sdk v5",
55
"main": "index.js",
66
"scripts": {

sdk/util.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ var getAuth = function (opt) {
3838
if (!SecretId) return console.error('missing param SecretId');
3939
if (!SecretKey) return console.error('missing param SecretKey');
4040

41-
var getObjectKeys = function (obj) {
41+
var getObjectKeys = function (obj, forKey) {
4242
var list = [];
4343
for (var key in obj) {
4444
if (obj.hasOwnProperty(key)) {
45-
list.push(key);
45+
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
4646
}
4747
}
4848
return list.sort(function (a, b) {
@@ -59,8 +59,7 @@ var getAuth = function (opt) {
5959
for (i = 0; i < keyList.length; i++) {
6060
key = keyList[i];
6161
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
62-
key = key.toLowerCase();
63-
key = camSafeUrlEncode(key);
62+
key = camSafeUrlEncode(key).toLowerCase();
6463
val = camSafeUrlEncode(val) || '';
6564
list.push(key + '=' + val)
6665
}
@@ -83,8 +82,8 @@ var getAuth = function (opt) {
8382
var qAk = SecretId;
8483
var qSignTime = KeyTime || now + ';' + exp;
8584
var qKeyTime = KeyTime || now + ';' + exp;
86-
var qHeaderList = getObjectKeys(headers).join(';').toLowerCase();
87-
var qUrlParamList = getObjectKeys(queryParams).join(';').toLowerCase();
85+
var qHeaderList = getObjectKeys(headers, true).join(';').toLowerCase();
86+
var qUrlParamList = getObjectKeys(queryParams, true).join(';').toLowerCase();
8887

8988
// 签名算法说明文档:https://www.qcloud.com/document/product/436/7778
9089
// 步骤一:计算 SignKey

test/test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,3 +2979,41 @@ group('Promise', function () {
29792979
});
29802980
});
29812981
});
2982+
2983+
group('Query 的键值带有特殊字符', function () {
2984+
test('getAuth()', function (done, assert) {
2985+
var content = Date.now().toString();
2986+
var key = '1.txt';
2987+
cos.putObject({
2988+
Bucket: config.Bucket,
2989+
Region: config.Region,
2990+
Key: key,
2991+
Body: content,
2992+
}, function (err, data) {
2993+
var str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM,./;\'[]\\-=0987654321`~!@#$%^&*()_+{}|":>?<';
2994+
var qs = {};
2995+
qs[str] = str;
2996+
var AuthData = cos.getAuth({
2997+
Method: 'GET',
2998+
Key: key,
2999+
Query: qs,
3000+
});
3001+
if (typeof AuthData === 'string') {
3002+
AuthData = {Authorization: AuthData};
3003+
}
3004+
var link = 'http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' +
3005+
camSafeUrlEncode(key).replace(/%2F/g, '/') +
3006+
'?sign=' + camSafeUrlEncode(AuthData.Authorization) +
3007+
(AuthData.XCosSecurityToken ? '&x-cos-security-token=' + AuthData.XCosSecurityToken : '') +
3008+
'&' + camSafeUrlEncode(str) + '=' + camSafeUrlEncode(str);
3009+
request({
3010+
method: 'GET',
3011+
url: link,
3012+
}, function (err, response, body) {
3013+
assert.ok(response.statusCode === 200);
3014+
assert.ok(body === content);
3015+
done();
3016+
});
3017+
});
3018+
});
3019+
});

0 commit comments

Comments
 (0)