From f18392bc38f04f696a2f65c5ffbbdbbf2a28e3f3 Mon Sep 17 00:00:00 2001 From: Ivan Schasny Date: Sun, 22 Jan 2023 17:26:05 +0000 Subject: [PATCH] Fix dhash client bug --- api/v0/finder/client/http/dhash_client.go | 21 +++++++++++++++++---- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/api/v0/finder/client/http/dhash_client.go b/api/v0/finder/client/http/dhash_client.go index e571ab363..44a9706cb 100644 --- a/api/v0/finder/client/http/dhash_client.go +++ b/api/v0/finder/client/http/dhash_client.go @@ -7,6 +7,7 @@ import ( "io" "net/http" + "github.com/ipni/dhstore" "github.com/ipni/go-indexer-core/store/dhash" "github.com/ipni/storetheindex/api/v0/finder/model" "github.com/ipni/storetheindex/api/v0/httpclient" @@ -25,6 +26,8 @@ type DHashClient struct { metadataUrl string } +// NewDHashClient instantiates a new client that uses Reader Privacy API for querying data. +// It requires more roundtrips to fullfill one query however it also protects the user from a passive observer. func NewDHashClient(baseURL string, options ...httpclient.Option) (*DHashClient, error) { c, err := New(baseURL, options...) if err != nil { @@ -72,7 +75,10 @@ func (c *DHashClient) Find(ctx context.Context, mh multihash.Multihash) (*model. return nil, err } - c.decryptFindResponse(ctx, findResponse, map[string]multihash.Multihash{smh.B58String(): mh}) + err = c.decryptFindResponse(ctx, findResponse, map[string]multihash.Multihash{smh.B58String(): mh}) + if err != nil { + return nil, err + } return findResponse, nil } @@ -109,7 +115,7 @@ func (c *DHashClient) decryptFindResponse(ctx context.Context, resp *model.FindR if pinfo == nil { pinfo, err = c.GetProvider(ctx, pid) if err != nil { - continue + return err } pinfoCache[pid] = pinfo } @@ -117,7 +123,7 @@ func (c *DHashClient) decryptFindResponse(ctx context.Context, resp *model.FindR // fetch metadata metadata, err := c.fetchMetadata(ctx, vk) if err != nil { - continue + return err } mhr.ProviderResults = append(mhr.ProviderResults, model.ProviderResult{ @@ -157,5 +163,12 @@ func (c *DHashClient) fetchMetadata(ctx context.Context, vk []byte) ([]byte, err return nil, httpclient.ReadError(resp.StatusCode, body) } - return dhash.DecryptMetadata(body, vk) + findResponse := &dhstore.GetMetadataResponse{} + err = json.Unmarshal(body, findResponse) + + if err != nil { + return nil, err + } + + return dhash.DecryptMetadata(findResponse.EncryptedMetadata, vk) } diff --git a/go.mod b/go.mod index 0af50f207..568dfeeae 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/ipld/go-ipld-prime v0.19.0 github.com/ipld/go-ipld-prime/storage/dsadapter v0.0.0-20230102063945-1a409dc236dd github.com/ipld/go-storethehash v0.3.13 - github.com/ipni/go-indexer-core v0.7.1-0.20230120184340-38cba41d97e3 + github.com/ipni/go-indexer-core v0.7.1-0.20230122165842-8feed957168a github.com/libp2p/go-libp2p v0.23.4 github.com/libp2p/go-libp2p-gostream v0.5.0 github.com/libp2p/go-libp2p-pubsub v0.8.1 diff --git a/go.sum b/go.sum index da046a8cd..2ba6ccd72 100644 --- a/go.sum +++ b/go.sum @@ -667,8 +667,8 @@ github.com/ipld/go-storethehash v0.3.13 h1:1T6kX5K57lAgxbsGitZEaZMAR3RdWch2kO8ok github.com/ipld/go-storethehash v0.3.13/go.mod h1:KCYpzmamubnSwm7fvWcCkm0aIwQh4WRNtzrKK4pVhAQ= github.com/ipni/dhstore v0.0.2-0.20230120184057-c54e9d7c72f7 h1:w4sIScjReSAME4FKuSPaUT0H6rUi4UxEy42lEi5QKJY= github.com/ipni/dhstore v0.0.2-0.20230120184057-c54e9d7c72f7/go.mod h1:CIz5tpqY9GRSTx2ZML3tmvCZzdDCudDuE3DP57R4ZYo= -github.com/ipni/go-indexer-core v0.7.1-0.20230120184340-38cba41d97e3 h1:XBqu/fzwzmOmH1U5mBm9DrP28EwlTZ8a8yyIAq/k2xc= -github.com/ipni/go-indexer-core v0.7.1-0.20230120184340-38cba41d97e3/go.mod h1:UluJCGNLKAWzxDgHX6zVD+hqpKQxEyuj+wkcAc9j4Po= +github.com/ipni/go-indexer-core v0.7.1-0.20230122165842-8feed957168a h1:Oqx3NW0kEsMYAqPVuFVfQ1Dy8d7l7hvCJ0xVp791pJI= +github.com/ipni/go-indexer-core v0.7.1-0.20230122165842-8feed957168a/go.mod h1:UluJCGNLKAWzxDgHX6zVD+hqpKQxEyuj+wkcAc9j4Po= github.com/ipsn/go-secp256k1 v0.0.0-20180726113642-9d62b9f0bc52 h1:QG4CGBqCeuBo6aZlGAamSkxWdgWfZGeE49eUOWJPA4c= github.com/ipsn/go-secp256k1 v0.0.0-20180726113642-9d62b9f0bc52/go.mod h1:fdg+/X9Gg4AsAIzWpEHwnqd+QY3b7lajxyjE1m4hkq4= github.com/iris-contrib/blackfriday v2.0.0+incompatible/go.mod h1:UzZ2bDEoaSGPbkg6SAB4att1aAwTmVIx/5gCVqeyUdI=