From f4406e19a037ae16523cec0599d049707ffa3ec2 Mon Sep 17 00:00:00 2001 From: Artem Bogomaz Date: Fri, 6 Aug 2021 00:05:51 +0700 Subject: [PATCH 1/6] :bug: fix with revoked share --- .../blobbercore/handler/object_operation_handler.go | 7 +++---- code/go/0chain.net/blobbercore/reference/shareinfo.go | 3 --- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go index 94556eb52..219e27263 100644 --- a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go +++ b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go @@ -435,7 +435,9 @@ func (fsh *StorageHandler) DownloadFile( ) if err != nil { return nil, errors.New("error during share info lookup in database" + err.Error()) - } else if shareInfo == nil || shareInfo.Revoked { + } + + if shareInfo.Revoked { return nil, errors.New("client does not have permission to download the file. share does not exist") } @@ -450,9 +452,6 @@ func (fsh *StorageHandler) DownloadFile( if err := encscheme.InitForDecryption("filetype:audio", fileref.EncryptedKey); err != nil { return nil, err } - if err != nil { - return nil, err - } totalSize := len(respData) result := []byte{} diff --git a/code/go/0chain.net/blobbercore/reference/shareinfo.go b/code/go/0chain.net/blobbercore/reference/shareinfo.go index f0ab62ec6..1caafae55 100644 --- a/code/go/0chain.net/blobbercore/reference/shareinfo.go +++ b/code/go/0chain.net/blobbercore/reference/shareinfo.go @@ -74,9 +74,6 @@ func GetShareInfo(ctx context.Context, clientID string, filePathHash string) (*S }). First(shareInfo).Error - if err == gorm.ErrRecordNotFound { - return nil, nil - } if err != nil { return nil, err } From dc944b413ad8925af1782dd0b1e4ad3104462049 Mon Sep 17 00:00:00 2001 From: Artem Bogomaz Date: Fri, 6 Aug 2021 17:43:30 +0700 Subject: [PATCH 2/6] fixup! :bug: fix with revoked share --- code/go/0chain.net/blobbercore/handler/handler.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/code/go/0chain.net/blobbercore/handler/handler.go b/code/go/0chain.net/blobbercore/handler/handler.go index cf56a9b43..307384459 100644 --- a/code/go/0chain.net/blobbercore/handler/handler.go +++ b/code/go/0chain.net/blobbercore/handler/handler.go @@ -453,10 +453,7 @@ func InsertShare(ctx context.Context, r *http.Request) (interface{}, error) { ExpiryAt: common.ToTime(authTicket.Expiration), } - existingShare, err := reference.GetShareInfo(ctx, authTicket.ClientID, authTicket.FilePathHash) - if err != nil { - return nil, err - } + existingShare, _ := reference.GetShareInfo(ctx, authTicket.ClientID, authTicket.FilePathHash) if existingShare != nil { err = reference.UpdateShareInfo(ctx, shareInfo) From ebe7881a2cc58ca484569ecca118bdc965e545cb Mon Sep 17 00:00:00 2001 From: Artem Bogomaz Date: Fri, 6 Aug 2021 21:48:08 +0700 Subject: [PATCH 3/6] fixup! fixup! :bug: fix with revoked share --- .../handler/object_operation_handler.go | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go index 219e27263..d8011ee49 100644 --- a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go +++ b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go @@ -423,12 +423,9 @@ func (fsh *StorageHandler) DownloadFile( "couldn't save latest read marker: %v", err) } - if len(fileref.EncryptedKey) > 0 { - if authToken == nil { - return nil, errors.New("auth ticket is required to download encrypted file") - } - // check if client is authorized to download - shareInfo, err := reference.GetShareInfo( + var shareInfo *reference.ShareInfo + if authToken != nil { + shareInfo, err = reference.GetShareInfo( ctx, readMarker.ClientID, authToken.FilePathHash, @@ -440,6 +437,17 @@ func (fsh *StorageHandler) DownloadFile( if shareInfo.Revoked { return nil, errors.New("client does not have permission to download the file. share does not exist") } + } + + if len(fileref.EncryptedKey) > 0 { + if authToken == nil { + return nil, errors.New("auth ticket is required to download encrypted file") + } + + // should not happen, just in case + if shareInfo == nil { + return nil, errors.New("error during share info lookup in database, shareInfo is nil") + } buyerEncryptionPublicKey := shareInfo.ClientEncryptionPublicKey encscheme := zencryption.NewEncryptionScheme() From c1fd157b2631c232b7a335e4394650f904c847a7 Mon Sep 17 00:00:00 2001 From: Artem Bogomaz Date: Wed, 11 Aug 2021 22:25:11 +0700 Subject: [PATCH 4/6] :bug: messed up with error message --- .../blobbercore/handler/object_operation_handler.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go index d8011ee49..b2c1b26de 100644 --- a/code/go/0chain.net/blobbercore/handler/object_operation_handler.go +++ b/code/go/0chain.net/blobbercore/handler/object_operation_handler.go @@ -430,12 +430,9 @@ func (fsh *StorageHandler) DownloadFile( readMarker.ClientID, authToken.FilePathHash, ) - if err != nil { - return nil, errors.New("error during share info lookup in database" + err.Error()) - } - if shareInfo.Revoked { - return nil, errors.New("client does not have permission to download the file. share does not exist") + if err == nil && shareInfo.Revoked { + return nil, errors.New("client does not have permission to download the file. share revoked") } } @@ -446,7 +443,7 @@ func (fsh *StorageHandler) DownloadFile( // should not happen, just in case if shareInfo == nil { - return nil, errors.New("error during share info lookup in database, shareInfo is nil") + return nil, errors.New("client does not have permission to download the file. share does not exist") } buyerEncryptionPublicKey := shareInfo.ClientEncryptionPublicKey From 6985d440a2612b1d6dd67fbc5380961fff546aa5 Mon Sep 17 00:00:00 2001 From: Artem Bogomaz Date: Tue, 17 Aug 2021 17:20:17 +0700 Subject: [PATCH 5/6] :hammer: double check for share --- code/go/0chain.net/blobbercore/handler/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/go/0chain.net/blobbercore/handler/handler.go b/code/go/0chain.net/blobbercore/handler/handler.go index 307384459..80f7e4439 100644 --- a/code/go/0chain.net/blobbercore/handler/handler.go +++ b/code/go/0chain.net/blobbercore/handler/handler.go @@ -455,7 +455,7 @@ func InsertShare(ctx context.Context, r *http.Request) (interface{}, error) { existingShare, _ := reference.GetShareInfo(ctx, authTicket.ClientID, authTicket.FilePathHash) - if existingShare != nil { + if existingShare != nil && len(existingShare.OwnerID) > 0 { err = reference.UpdateShareInfo(ctx, shareInfo) } else { err = reference.AddShareInfo(ctx, shareInfo) From 8f04c0fe978745b492de0940d3f4df31633694ee Mon Sep 17 00:00:00 2001 From: Artem Bogomaz Date: Tue, 17 Aug 2021 17:40:37 +0700 Subject: [PATCH 6/6] faling tests --- code/go/0chain.net/blobbercore/handler/handler_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/go/0chain.net/blobbercore/handler/handler_test.go b/code/go/0chain.net/blobbercore/handler/handler_test.go index 68e7053ea..25aa3f045 100644 --- a/code/go/0chain.net/blobbercore/handler/handler_test.go +++ b/code/go/0chain.net/blobbercore/handler/handler_test.go @@ -1279,8 +1279,8 @@ func TestHandlers_Requiring_Signature(t *testing.T) { mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "marketplace_share_info" WHERE`)). WithArgs("abcdefgh", "f15383a1130bd2fae1e52a7a15c432269eeb7def555f1f8b9b9a28bd9611362c"). WillReturnRows( - sqlmock.NewRows([]string{"client_id"}). - AddRow("abcdefgh"), + sqlmock.NewRows([]string{"client_id", "owner_id"}). + AddRow("abcdefgh", "owner"), ) aa := sqlmock.AnyArg()