Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:补充d.ts;支持设置host不参与签名; #143

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ declare namespace COS {
ProgressInterval?: number,
/** 上传队列最长大小,超出的任务如果状态不是 waiting、checking、uploading 会被清理,默认10000 */
UploadQueueSize?: number,
/** 上传队列最长大小,超出的任务如果状态不是 waiting、checking、uploading 会被清理,默认10000 */
/** 调用操作存储桶和对象的 API 时自定义请求域名。可以使用模板,如"{Bucket}.cos.{Region}.myqcloud.com",即在调用 API 时会使用参数中传入的 Bucket 和 Region 进行替换。 */
Domain?: string,
/** 强制使用后缀式模式发请求。后缀式模式中 Bucket 会放在域名后的 pathname 里,并且 Bucket 会加入签名 pathname 计算,默认 false */
/** getService方法可以使用的自定义域名 */
ServiceDomain?: string,
/** 强制使用后缀式模式发请求。后缀式模式中 Bucket 会放在域名后的 pathname 里,并且 Bucket 会加入签名 pathname 计算,默认 false */
Protocol?: string,
Expand Down Expand Up @@ -181,6 +181,8 @@ declare namespace COS {
/** 是否开启长链接,默认开启 */
KeepAlive?: boolean,
Ip?: string,
/** 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true */
ForceSignHost?: boolean,
/** 获取签名的回调方法,如果没有 SecretId、SecretKey 时,必选 */
getAuthorization?: (
options: GetAuthorizationOptions,
Expand Down Expand Up @@ -1150,7 +1152,8 @@ declare namespace COS {
// getObjectStream
/** getObject 接口参数 */
interface GetObjectParams extends ObjectParams {
BodyType?: 'text' | 'blob' | 'arraybuffer',
// nodejs getObject 不支持传参BodyType
// BodyType?: 'text' | 'blob' | 'arraybuffer',
/** 写入流,可以传本地文件写入流 */
Output?: Stream,
/** 请求里的 Url Query 参数,传入该值中的 key/value 将会被 URLEncode */
Expand Down Expand Up @@ -1922,6 +1925,8 @@ Bulk:批量模式,恢复时间为24 - 48小时。 */
Query?: Query,
/** 请求里的 Header 参数 */
Headers?: Headers,
/** 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true */
ForceSignHost?: boolean,
}

}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-nodejs-sdk-v5",
"version": "2.11.6",
"version": "2.11.7",
"description": "cos nodejs sdk v5",
"main": "index.js",
"types": "index.d.ts",
Expand Down
12 changes: 10 additions & 2 deletions sdk/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3159,6 +3159,7 @@ function getObjectUrl(params, callback) {
Headers: params.Headers,
Query: params.Query,
SignHost: SignHost,
ForceSignHost: params.ForceSignHost === false ? false : self.options.ForceSignHost, // getObjectUrl支持传参ForceSignHost
}, function (err, AuthData) {
if (!callback) return;
if (err) {
Expand Down Expand Up @@ -3339,8 +3340,11 @@ function getAuthorizationAsync(params, callback) {
if (k.toLowerCase() === 'host') headerHost = v;
});

// ForceSignHost明确传入false才不加入host签名
var forceSignHost = params.ForceSignHost === false ? false : true;

// Host 加入签名计算
if (!headerHost && params.SignHost) headers.Host = params.SignHost;
if (!headerHost && params.SignHost && forceSignHost) headers.Host = params.SignHost;


// 获取凭证的回调,避免用户 callback 多次
Expand Down Expand Up @@ -3412,7 +3416,8 @@ function getAuthorizationAsync(params, callback) {
Expires: params.Expires,
UseRawKey: self.options.UseRawKey,
SystemClockOffset: self.options.SystemClockOffset,
KeyTime: KeyTime
KeyTime: KeyTime,
ForceSignHost: forceSignHost,
});
var AuthData = {
Authorization: Authorization,
Expand Down Expand Up @@ -3476,6 +3481,7 @@ function getAuthorizationAsync(params, callback) {
Headers: headers,
Scope: Scope,
SystemClockOffset: self.options.SystemClockOffset,
ForceSignHost: forceSignHost,
}, function (AuthData) {
if (typeof AuthData === 'string') AuthData = {Authorization: AuthData};
var AuthError = checkAuthError(AuthData);
Expand Down Expand Up @@ -3517,6 +3523,7 @@ function getAuthorizationAsync(params, callback) {
Expires: params.Expires,
UseRawKey: self.options.UseRawKey,
SystemClockOffset: self.options.SystemClockOffset,
ForceSignHost: forceSignHost,
});
var AuthData = {
Authorization: Authorization,
Expand Down Expand Up @@ -3594,6 +3601,7 @@ function submitRequest(params, callback) {
Action: params.Action,
ResourceKey: params.ResourceKey,
Scope: params.Scope,
ForceSignHost: self.options.ForceSignHost,
}, function (err, AuthData) {
if (err) return callback(err);
params.AuthData = AuthData;
Expand Down
1 change: 1 addition & 0 deletions sdk/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var defaultOptions = {
UseAccelerate: false,
UserAgent: '',
ConfCwd: '',
ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true
};

// 对外暴露的类
Expand Down
6 changes: 5 additions & 1 deletion sdk/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ var getAuth = function (opt) {
pathname.indexOf('/') !== 0 && (pathname = '/' + pathname);
}

// ForceSignHost明确传入false才不加入host签名
var forceSignHost = opt.ForceSignHost === false ? false : true;

// 如果有传入存储桶,那么签名默认加 Host 参与计算,避免跨桶访问
if (!headers.Host && !headers.host && opt.Bucket && opt.Region) headers.Host = opt.Bucket + '.cos.' + opt.Region + '.myqcloud.com';
if (!headers.Host && !headers.host && opt.Bucket && opt.Region && forceSignHost) headers.Host = opt.Bucket + '.cos.' + opt.Region + '.myqcloud.com';

if (!SecretId) throw new Error('missing param SecretId');
if (!SecretKey) throw new Error('missing param SecretKey');
Expand Down Expand Up @@ -429,6 +432,7 @@ var apiWrapper = function (apiName, apiFn) {
var formatResult = function (result) {
if (result && result.headers) {
result.headers['x-cos-request-id'] && (result.RequestId = result.headers['x-cos-request-id']);
result.headers['x-ci-request-id'] && (result.RequestId = result.headers['x-ci-request-id']);
result.headers['x-cos-version-id'] && (result.VersionId = result.headers['x-cos-version-id']);
result.headers['x-cos-delete-marker'] && (result.DeleteMarker = result.headers['x-cos-delete-marker']);
}
Expand Down