From c66049fae50c2e7b2f811214e115fea1db366641 Mon Sep 17 00:00:00 2001 From: yangpeng <183851063@qq.com> Date: Tue, 27 Sep 2022 15:37:18 +0800 Subject: [PATCH] check object key name --- oss/bucket.go | 5 +++++ oss/bucket_test.go | 15 +++++++++++++++ oss/utils.go | 7 +++++++ 3 files changed, 27 insertions(+) diff --git a/oss/bucket.go b/oss/bucket.go index 4eb2f44c..51ccc319 100644 --- a/oss/bucket.go +++ b/oss/bucket.go @@ -901,6 +901,11 @@ func (bucket Bucket) RestoreObjectXML(objectKey, configXML string, options ...Op // error it's nil if no error, otherwise it's an error object. // func (bucket Bucket) SignURL(objectKey string, method HTTPMethod, expiredInSec int64, options ...Option) (string, error) { + err := CheckObjectName(objectKey) + if err != nil { + return "", err + } + if expiredInSec < 0 { return "", fmt.Errorf("invalid expires: %d, expires must bigger than 0", expiredInSec) } diff --git a/oss/bucket_test.go b/oss/bucket_test.go index d36c931a..c7cdbda8 100644 --- a/oss/bucket_test.go +++ b/oss/bucket_test.go @@ -647,6 +647,21 @@ func (s *OssBucketSuite) TestSignURLWithEscapedKey(c *C) { s.SignURLWithEscapedKeyTestFunc(c, AuthV2, []string{"host", "range", "user-agent"}) } +func (s *OssBucketSuite) TestSignURLWithEmptyObjectName(c *C) { + client, err := New(endpoint, accessID, accessKey) + c.Assert(err, IsNil) + + bucketName := bucketNamePrefix + RandLowStr(6) + err = client.CreateBucket(bucketName) + c.Assert(err, IsNil) + + bucket, err := client.Bucket(bucketName) + c.Assert(err, IsNil) + _, err = bucket.SignURL("", "GET", 3600) + c.Assert(err, NotNil) + ForceDeleteBucket(client, bucketName, c) +} + func (s *OssBucketSuite) SignURLWithEscapedKeyAndPorxyTestFunc(c *C, authVersion AuthVersionType, extraHeaders []string) { // Key with '/' objectName := "zyimg/86/e8/653b5dc97bb0022051a84c632bc4" diff --git a/oss/utils.go b/oss/utils.go index aae6076c..9ed7a80a 100644 --- a/oss/utils.go +++ b/oss/utils.go @@ -440,6 +440,13 @@ func CheckBucketName(bucketName string) error { return nil } +func CheckObjectName(objectName string) error { + if len(objectName) == 0 { + return fmt.Errorf("object name is empty") + } + return nil +} + func GetReaderLen(reader io.Reader) (int64, error) { var contentLength int64 var err error