-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gateway: add path prefix for directory listings #1971
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,16 +92,24 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request | |
|
||
urlPath := r.URL.Path | ||
|
||
// If the gateway is behind a reverse proxy and mounted at a sub-path, | ||
// the prefix header can be set to signal this sub-path. | ||
// It will be prepended to links in directory listings and the index.html redirect. | ||
prefix := "" | ||
if prefixHdr := r.Header["X-Ipfs-Gateway-Prefix"]; len(prefixHdr) > 0 { | ||
log.Debugf("X-Ipfs-Gateway-Prefix: %s", prefixHdr[0]) | ||
prefix = prefixHdr[0] | ||
} | ||
|
||
// IPNSHostnameOption might have constructed an IPNS path using the Host header. | ||
// In this case, we need the original path for constructing redirects | ||
// and links that match the requested URL. | ||
// For example, http://example.net would become /ipns/example.net, and | ||
// the redirects and links would end up as http://example.net/ipns/example.net | ||
originalUrlPath := urlPath | ||
originalUrlPath := prefix + urlPath | ||
ipnsHostname := false | ||
hdr := r.Header["X-IPNS-Original-Path"] | ||
if len(hdr) > 0 { | ||
originalUrlPath = hdr[0] | ||
if hdr := r.Header["X-Ipns-Original-Path"]; len(hdr) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this change going to break anyone? or are headers case insensitive? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. net/http seems to convert response header keys to camel-case. I passed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding breaking existing stuff by changing the header: it's implementation detail between IPNSHostnameOption and gateway handler |
||
originalUrlPath = prefix + hdr[0] | ||
ipnsHostname = true | ||
} | ||
|
||
|
@@ -211,7 +219,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request | |
if r.Method != "HEAD" { | ||
// construct the correct back link | ||
// https://github.com/ipfs/go-ipfs/issues/1365 | ||
var backLink string = urlPath | ||
var backLink string = prefix + urlPath | ||
|
||
// don't go further up than /ipfs/$hash/ | ||
pathSplit := strings.Split(backLink, "/") | ||
|
@@ -233,7 +241,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request | |
|
||
// strip /ipfs/$hash from backlink if IPNSHostnameOption touched the path. | ||
if ipnsHostname { | ||
backLink = "/" | ||
backLink = prefix + "/" | ||
if len(pathSplit) > 5 { | ||
// also strip the trailing segment, because it's a backlink | ||
backLinkParts := pathSplit[3 : len(pathSplit)-2] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this SGTM. though not sure if this gives anyone power by putting it as a req header. cant think of an attack now, but there may be one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsetting this header for all other vhosts in nginx. The prefix "only" affects rendering of URLs (and redirects), do you think that opens up attack surface in other setups? One thing I should definitely make sure is that you can pass full URLs, but only what's expected (paths). An attacker must not be able to trick a server into arbitrary redirects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EDIT: can't pass full URLs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah didnt see this before merging-- does this need a fixup too then? (making sure it's a path)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep but I'll get to it today