Skip to content

Commit 30e9ffb

Browse files
authored
Merge pull request #143 from livehigh/feat/v2.11.7
feat:补充d.ts;支持设置host不参与签名;
2 parents 561e01e + eb27076 commit 30e9ffb

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

index.d.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ declare namespace COS {
146146
ProgressInterval?: number,
147147
/** 上传队列最长大小,超出的任务如果状态不是 waiting、checking、uploading 会被清理,默认10000 */
148148
UploadQueueSize?: number,
149-
/** 上传队列最长大小,超出的任务如果状态不是 waiting、checking、uploading 会被清理,默认10000 */
149+
/** 调用操作存储桶和对象的 API 时自定义请求域名。可以使用模板,如"{Bucket}.cos.{Region}.myqcloud.com",即在调用 API 时会使用参数中传入的 Bucket 和 Region 进行替换。 */
150150
Domain?: string,
151-
/** 强制使用后缀式模式发请求。后缀式模式中 Bucket 会放在域名后的 pathname 里,并且 Bucket 会加入签名 pathname 计算,默认 false */
151+
/** getService方法可以使用的自定义域名 */
152152
ServiceDomain?: string,
153153
/** 强制使用后缀式模式发请求。后缀式模式中 Bucket 会放在域名后的 pathname 里,并且 Bucket 会加入签名 pathname 计算,默认 false */
154154
Protocol?: string,
@@ -181,6 +181,8 @@ declare namespace COS {
181181
/** 是否开启长链接,默认开启 */
182182
KeepAlive?: boolean,
183183
Ip?: string,
184+
/** 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true */
185+
ForceSignHost?: boolean,
184186
/** 获取签名的回调方法,如果没有 SecretId、SecretKey 时,必选 */
185187
getAuthorization?: (
186188
options: GetAuthorizationOptions,
@@ -1150,7 +1152,8 @@ declare namespace COS {
11501152
// getObjectStream
11511153
/** getObject 接口参数 */
11521154
interface GetObjectParams extends ObjectParams {
1153-
BodyType?: 'text' | 'blob' | 'arraybuffer',
1155+
// nodejs getObject 不支持传参BodyType
1156+
// BodyType?: 'text' | 'blob' | 'arraybuffer',
11541157
/** 写入流,可以传本地文件写入流 */
11551158
Output?: Stream,
11561159
/** 请求里的 Url Query 参数,传入该值中的 key/value 将会被 URLEncode */
@@ -1922,6 +1925,8 @@ Bulk:批量模式,恢复时间为24 - 48小时。 */
19221925
Query?: Query,
19231926
/** 请求里的 Header 参数 */
19241927
Headers?: Headers,
1928+
/** 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true */
1929+
ForceSignHost?: boolean,
19251930
}
19261931

19271932
}

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.11.6",
3+
"version": "2.11.7",
44
"description": "cos nodejs sdk v5",
55
"main": "index.js",
66
"types": "index.d.ts",

sdk/base.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,6 +3159,7 @@ function getObjectUrl(params, callback) {
31593159
Headers: params.Headers,
31603160
Query: params.Query,
31613161
SignHost: SignHost,
3162+
ForceSignHost: params.ForceSignHost === false ? false : self.options.ForceSignHost, // getObjectUrl支持传参ForceSignHost
31623163
}, function (err, AuthData) {
31633164
if (!callback) return;
31643165
if (err) {
@@ -3339,8 +3340,11 @@ function getAuthorizationAsync(params, callback) {
33393340
if (k.toLowerCase() === 'host') headerHost = v;
33403341
});
33413342

3343+
// ForceSignHost明确传入false才不加入host签名
3344+
var forceSignHost = params.ForceSignHost === false ? false : true;
3345+
33423346
// Host 加入签名计算
3343-
if (!headerHost && params.SignHost) headers.Host = params.SignHost;
3347+
if (!headerHost && params.SignHost && forceSignHost) headers.Host = params.SignHost;
33443348

33453349

33463350
// 获取凭证的回调,避免用户 callback 多次
@@ -3412,7 +3416,8 @@ function getAuthorizationAsync(params, callback) {
34123416
Expires: params.Expires,
34133417
UseRawKey: self.options.UseRawKey,
34143418
SystemClockOffset: self.options.SystemClockOffset,
3415-
KeyTime: KeyTime
3419+
KeyTime: KeyTime,
3420+
ForceSignHost: forceSignHost,
34163421
});
34173422
var AuthData = {
34183423
Authorization: Authorization,
@@ -3476,6 +3481,7 @@ function getAuthorizationAsync(params, callback) {
34763481
Headers: headers,
34773482
Scope: Scope,
34783483
SystemClockOffset: self.options.SystemClockOffset,
3484+
ForceSignHost: forceSignHost,
34793485
}, function (AuthData) {
34803486
if (typeof AuthData === 'string') AuthData = {Authorization: AuthData};
34813487
var AuthError = checkAuthError(AuthData);
@@ -3517,6 +3523,7 @@ function getAuthorizationAsync(params, callback) {
35173523
Expires: params.Expires,
35183524
UseRawKey: self.options.UseRawKey,
35193525
SystemClockOffset: self.options.SystemClockOffset,
3526+
ForceSignHost: forceSignHost,
35203527
});
35213528
var AuthData = {
35223529
Authorization: Authorization,
@@ -3594,6 +3601,7 @@ function submitRequest(params, callback) {
35943601
Action: params.Action,
35953602
ResourceKey: params.ResourceKey,
35963603
Scope: params.Scope,
3604+
ForceSignHost: self.options.ForceSignHost,
35973605
}, function (err, AuthData) {
35983606
if (err) return callback(err);
35993607
params.AuthData = AuthData;

sdk/cos.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var defaultOptions = {
4242
UseAccelerate: false,
4343
UserAgent: '',
4444
ConfCwd: '',
45+
ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true
4546
};
4647

4748
// 对外暴露的类

sdk/util.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ var getAuth = function (opt) {
8686
pathname.indexOf('/') !== 0 && (pathname = '/' + pathname);
8787
}
8888

89+
// ForceSignHost明确传入false才不加入host签名
90+
var forceSignHost = opt.ForceSignHost === false ? false : true;
91+
8992
// 如果有传入存储桶,那么签名默认加 Host 参与计算,避免跨桶访问
90-
if (!headers.Host && !headers.host && opt.Bucket && opt.Region) headers.Host = opt.Bucket + '.cos.' + opt.Region + '.myqcloud.com';
93+
if (!headers.Host && !headers.host && opt.Bucket && opt.Region && forceSignHost) headers.Host = opt.Bucket + '.cos.' + opt.Region + '.myqcloud.com';
9194

9295
if (!SecretId) throw new Error('missing param SecretId');
9396
if (!SecretKey) throw new Error('missing param SecretKey');
@@ -429,6 +432,7 @@ var apiWrapper = function (apiName, apiFn) {
429432
var formatResult = function (result) {
430433
if (result && result.headers) {
431434
result.headers['x-cos-request-id'] && (result.RequestId = result.headers['x-cos-request-id']);
435+
result.headers['x-ci-request-id'] && (result.RequestId = result.headers['x-ci-request-id']);
432436
result.headers['x-cos-version-id'] && (result.VersionId = result.headers['x-cos-version-id']);
433437
result.headers['x-cos-delete-marker'] && (result.DeleteMarker = result.headers['x-cos-delete-marker']);
434438
}

0 commit comments

Comments
 (0)