Skip to content

Commit

Permalink
Merge pull request ipfs/kubo#8318 from ipfs/fix/path-panic
Browse files Browse the repository at this point in the history
fix: avoid out of bounds error when rendering short hashes

This commit was moved from ipfs/kubo@7c76118
  • Loading branch information
Stebalien committed Aug 13, 2021
2 parents 628b0a4 + e52ba61 commit 978ac9b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 5 additions & 4 deletions gateway/core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
size = humanize.Bytes(uint64(s))
}

hash := ""
if r, err := i.api.ResolvePath(r.Context(), ipath.Join(resolvedPath, dirit.Name())); err == nil {
// Path may not be resolved. Continue anyways.
hash = r.Cid().String()
resolved, err := i.api.ResolvePath(r.Context(), ipath.Join(resolvedPath, dirit.Name()))
if err != nil {
internalWebError(w, err)
return
}
hash := resolved.Cid().String()

// See comment above where originalUrlPath is declared.
di := directoryItem{
Expand Down
3 changes: 3 additions & 0 deletions gateway/core/corehttp/gateway_indexPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func breadcrumbs(urlPath string, dnslinkOrigin bool) []breadcrumb {
}

func shortHash(hash string) string {
if len(hash) <= 8 {
return hash
}
return (hash[0:4] + "\u2026" + hash[len(hash)-4:])
}

Expand Down

0 comments on commit 978ac9b

Please sign in to comment.