Skip to content

Commit

Permalink
check object key name
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzong18 authored and huiguangjun committed Nov 16, 2022
1 parent 0fcd445 commit c66049f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions oss/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 15 additions & 0 deletions oss/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions oss/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c66049f

Please sign in to comment.