Skip to content

Commit

Permalink
feat: decode multihash string for B58 and Hex (#2630)
Browse files Browse the repository at this point in the history
* decode multihash string for B58 and Hex
* If bad encoding, show both b58 and hex errors
* Add unit tests, update error message

---------

Co-authored-by: gammazero <11790789+gammazero@users.noreply.github.com>
  • Loading branch information
LexLuthr and gammazero authored Aug 9, 2024
1 parent e15e5d5 commit e16b358
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 9 additions & 4 deletions server/find/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
indexer "github.com/ipni/go-indexer-core"
"github.com/ipni/go-indexer-core"
coremetrics "github.com/ipni/go-indexer-core/metrics"
"github.com/ipni/go-libipni/apierror"
"github.com/ipni/go-libipni/find/model"
Expand Down Expand Up @@ -181,9 +181,14 @@ func (s *Server) findMultihash(w http.ResponseWriter, r *http.Request) {
mhVar := path.Base(r.URL.Path)
m, err := multihash.FromB58String(mhVar)
if err != nil {
log.Errorw("error decoding multihash", "multihash", mhVar, "err", err)
httpserver.HandleError(w, err, "find")
return
var hexErr error
m, hexErr = multihash.FromHexString(mhVar)
if hexErr != nil {
msg := "find: input is not a valid base58 or hex encoded multihash"
log.Errorw(msg, "multihash", mhVar, "err", err, "hexErr", hexErr)
http.Error(w, msg, http.StatusBadRequest)
return
}
}
s.getIndexes(w, []multihash.Multihash{m}, stream)
}
Expand Down
17 changes: 17 additions & 0 deletions server/find/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ func TestServer_CORSWithExpectedContentType(t *testing.T) {
reqUrl: "/multihash/" + mhs[0].B58String(),
wantContentType: "application/json",
},
{
reqMethod: http.MethodGet,
reqUrl: "/multihash/" + mhs[0].HexString(),
wantContentType: "application/json",
},
{
reqMethod: http.MethodGet,
reqUrl: "/multihash/" + "deadbeef",
wantContentType: "application/json",
statusCode: http.StatusBadRequest,
},
{
reqMethod: http.MethodPost,
reqUrl: "/multihash",
Expand All @@ -115,6 +126,12 @@ func TestServer_CORSWithExpectedContentType(t *testing.T) {
reqUrl: "/",
wantContentType: "text/html",
},
{
reqMethod: http.MethodGet,
reqUrl: "/multihash/1qaai0lO_aa^",
wantContentType: "application/json",
statusCode: http.StatusBadRequest,
},
}

cl := http.DefaultClient
Expand Down

0 comments on commit e16b358

Please sign in to comment.