Skip to content

Commit

Permalink
re-escape urls to avoid mixed-case hex from input url
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero committed Jan 8, 2025
1 parent 2adbe37 commit ffce1d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gateway/handler_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func (i *handler) serveCodecHTML(ctx context.Context, w http.ResponseWriter, r *
if r.URL.RawQuery != "" {
suffix = suffix + "?" + url.PathEscape(r.URL.RawQuery)
}

Check warning on line 174 in gateway/handler_codec.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_codec.go#L173-L174

Added lines #L173 - L174 were not covered by tests
// Re-escape path instead of reusing RawPath to avod mix of lawer
// and upper hex that may come from RawPath.
if strings.IndexRune(requestURI.RawPath, '%') != -1 {

Check failure on line 177 in gateway/handler_codec.go

View workflow job for this annotation

GitHub Actions / go-check / All

should use strings.ContainsRune(requestURI.RawPath, '%') instead (S1003)
requestURI.RawPath = ""

Check warning on line 178 in gateway/handler_codec.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_codec.go#L178

Added line #L178 was not covered by tests
}
// /ipfs/cid/foo?bar must be redirected to /ipfs/cid/foo/?bar
redirectURL := requestURI.EscapedPath() + suffix
http.Redirect(w, r, redirectURL, http.StatusMovedPermanently)
Expand Down
6 changes: 6 additions & 0 deletions gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
if r.URL.RawQuery != "" {
suffix = suffix + "?" + url.PathEscape(r.URL.RawQuery)
}

Check warning on line 51 in gateway/handler_unixfs_dir.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_unixfs_dir.go#L50-L51

Added lines #L50 - L51 were not covered by tests
// Re-escape path instead of reusing RawPath to avod mix of lawer
// and upper hex that may come from RawPath.
if strings.IndexRune(requestURI.RawPath, '%') != -1 {

Check failure on line 54 in gateway/handler_unixfs_dir.go

View workflow job for this annotation

GitHub Actions / go-check / All

should use strings.ContainsRune(requestURI.RawPath, '%') instead (S1003)
requestURI.RawPath = ""

Check warning on line 55 in gateway/handler_unixfs_dir.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler_unixfs_dir.go#L55

Added line #L55 was not covered by tests
}
// /ipfs/cid/foo?bar must be redirected to /ipfs/cid/foo/?bar
redirectURL := requestURI.EscapedPath() + suffix
rq.logger.Debugw("directory location moved permanently", "status", http.StatusMovedPermanently)

http.Redirect(w, r, redirectURL, http.StatusMovedPermanently)
return true
}
Expand Down

0 comments on commit ffce1d8

Please sign in to comment.