Skip to content

Commit 1444bd8

Browse files
committed
upd:host参与签名计算
1 parent 2b5054d commit 1444bd8

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/base.js

+42-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,17 @@ function getService(params, callback) {
3535
domain = protocol + '//service.cos.myqcloud.com';
3636
}
3737

38+
var SignHost = '';
39+
var standardHost = region ? 'cos.' + region + '.myqcloud.com' : 'service.cos.myqcloud.com';
40+
var urlHost = domain.replace(/^https?:\/\/([^/]+)(\/.*)?$/, '$1');
41+
if (standardHost === urlHost) SignHost = standardHost;
42+
3843
submitRequest.call(this, {
3944
Action: 'name/cos:GetService',
4045
url: domain,
4146
method: 'GET',
4247
headers: params.Headers,
48+
SignHost: SignHost,
4349
}, function (err, data) {
4450
if (err) return callback(err);
4551
var buckets = (data && data.ListAllMyBucketsResult && data.ListAllMyBucketsResult.Buckets
@@ -2958,6 +2964,8 @@ function getAuth(params) {
29582964
return util.getAuth({
29592965
SecretId: params.SecretId || this.options.SecretId || '',
29602966
SecretKey: params.SecretKey || this.options.SecretKey || '',
2967+
Bucket: params.Bucket,
2968+
Region: params.Region,
29612969
Method: params.Method,
29622970
Key: params.Key,
29632971
Query: params.Query,
@@ -2999,13 +3007,21 @@ function getObjectUrl(params, callback) {
29993007
queryParamsStr += (queryParamsStr ? '&' : '') + params.QueryString;
30003008
}
30013009

3010+
// 签名加上 Host,避免跨桶访问
3011+
var SignHost = '';
3012+
var standardHost = 'cos.' + params.Region + '.myqcloud.com';
3013+
if (!self.options.ForcePathStyle) standardHost = params.Bucket + '.' + standardHost;
3014+
var urlHost = url.replace(/^https?:\/\/([^/]+)(\/.*)?$/, '$1');
3015+
if (standardHost === urlHost) SignHost = standardHost;
3016+
30023017
var syncUrl = url;
30033018
if (params.Sign !== undefined && !params.Sign) {
30043019
queryParamsStr && (syncUrl += '?' + queryParamsStr);
30053020
callback(null, {Url: syncUrl});
30063021
return syncUrl;
30073022
}
3008-
3023+
3024+
var SignHost = getSignHost.call(this, {Bucket: params.Bucket, Region: params.Region, Url: url});
30093025
var AuthData = getAuthorizationAsync.call(this, {
30103026
Action: ((params.Method || '').toUpperCase() === 'PUT' ? 'name/cos:PutObject' : 'name/cos:GetObject'),
30113027
Bucket: params.Bucket || '',
@@ -3015,6 +3031,7 @@ function getObjectUrl(params, callback) {
30153031
Expires: params.Expires,
30163032
Headers: params.Headers,
30173033
Query: params.Query,
3034+
SignHost: SignHost,
30183035
}, function (err, AuthData) {
30193036
if (!callback) return;
30203037
if (err) {
@@ -3033,7 +3050,6 @@ function getObjectUrl(params, callback) {
30333050
callback(null, {Url: signUrl});
30343051
});
30353052
});
3036-
30373053
if (AuthData) {
30383054
syncUrl += '?' + AuthData.Authorization +
30393055
(AuthData.SecurityToken ? '&x-cos-security-token=' + AuthData.SecurityToken : '');
@@ -3044,7 +3060,6 @@ function getObjectUrl(params, callback) {
30443060
return syncUrl;
30453061
}
30463062

3047-
30483063
/**
30493064
* 私有方法
30503065
*/
@@ -3163,14 +3178,35 @@ function getUrl(params) {
31633178
return url;
31643179
}
31653180

3181+
var getSignHost = function (opt) {
3182+
if (!opt.Bucket || !opt.Bucket) return '';
3183+
var url = opt.Url || getUrl({
3184+
ForcePathStyle: this.options.ForcePathStyle,
3185+
protocol: this.options.Protocol,
3186+
domain: this.options.Domain,
3187+
bucket: opt.Bucket,
3188+
region: opt.Region,
3189+
});
3190+
var urlHost = url.replace(/^https?:\/\/([^/]+)(\/.*)?$/, '$1');
3191+
var standardHostReg = new RegExp('^([a-z\\d-]+-\\d+\\.)?(cos|cosv6|ci|pic)\\.([a-z\\d-]+)\\.myqcloud\\.com$');
3192+
if (standardHostReg.test(urlHost)) return urlHost;
3193+
return '';
3194+
}
3195+
3196+
31663197
// 异步获取签名
31673198
function getAuthorizationAsync(params, callback) {
31683199

31693200
var headers = util.clone(params.Headers);
3201+
var headerHost = '';
31703202
util.each(headers, function (v, k) {
31713203
(v === '' || ['content-type', 'cache-control', 'expires'].indexOf(k.toLowerCase()) > -1) && delete headers[k];
3204+
if (k.toLowerCase() === 'host') headerHost = v;
31723205
});
31733206

3207+
// Host 加入签名计算
3208+
if (!headerHost && params.SignHost) headers.Host = params.SignHost;
3209+
31743210
// 获取凭证的回调,避免用户 callback 多次
31753211
var cbDone = false;
31763212
var cb = function (err, AuthData) {
@@ -3405,6 +3441,8 @@ function submitRequest(params, callback) {
34053441
var Query = util.clone(params.qs);
34063442
params.action && (Query[params.action] = '');
34073443

3444+
var paramsUrl = params.url || params.Url;
3445+
var SignHost = params.SignHost || getSignHost.call(this, {Bucket: params.Bucket, Region: params.Region, Url: paramsUrl});
34083446
var next = function (tryTimes) {
34093447
var oldClockOffset = self.options.SystemClockOffset;
34103448
getAuthorizationAsync.call(self, {
@@ -3414,6 +3452,7 @@ function submitRequest(params, callback) {
34143452
Key: params.Key,
34153453
Query: Query,
34163454
Headers: params.headers,
3455+
SignHost: SignHost,
34173456
Action: params.Action,
34183457
ResourceKey: params.ResourceKey,
34193458
Scope: params.Scope,

src/util.js

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ var getAuth = function (opt) {
6262
pathname.indexOf('/') !== 0 && (pathname = '/' + pathname);
6363
}
6464

65+
// 如果有传入存储桶,那么签名默认加 Host 参与计算,避免跨桶访问
66+
if (!headers.Host && !headers.host && opt.Bucket && opt.Region) headers.Host = opt.Bucket + '.cos.' + opt.Region + '.myqcloud.com';
67+
6568
if (!SecretId) throw new Error('missing param SecretId');
6669
if (!SecretKey) throw new Error('missing param SecretKey');
6770

0 commit comments

Comments
 (0)