Skip to content
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
7 changes: 2 additions & 5 deletions code/go/0chain.net/blobbercore/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,9 @@ 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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check err here? We need to know if db throw any error?

func GetShareInfo(ctx context.Context, clientID string, filePathHash string) (*ShareInfo, error) {
	db := datastore.GetStore().GetTransaction(ctx)
	shareInfo := &ShareInfo{}
	err := db.Table(TableName()).
		Where(&ShareInfo{
			ClientID:     clientID,
			FilePathHash: filePathHash,
		}).
		First(shareInfo).Error

	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}
	return shareInfo, nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it's wrong 'tricky' implementation that causes previous bugs. I removed this check from DB level

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)
Expand Down
4 changes: 2 additions & 2 deletions code/go/0chain.net/blobbercore/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
28 changes: 16 additions & 12 deletions code/go/0chain.net/blobbercore/handler/object_operation_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,19 +423,26 @@ 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,
)
if err != nil {
return nil, errors.New("error during share info lookup in database" + err.Error())
} else if shareInfo == nil || shareInfo.Revoked {

if err == nil && shareInfo.Revoked {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

app might crash if shareInfo is nil? It is better to check it first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly what was done in the previous comment. It' can't be nil if err is nil.

return nil, errors.New("client does not have permission to download the file. share revoked")
}
}

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("client does not have permission to download the file. share does not exist")
}

Expand All @@ -450,9 +457,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{}
Expand Down
3 changes: 0 additions & 3 deletions code/go/0chain.net/blobbercore/reference/shareinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down