@cryptotaxi247 / kubo / commits / 72490f7ed

gateway: simplify/improve dnslink rewrite handling

Instead of adding a new fake header (that could be spoofed by the client...), just read the original request URI from the request object. This also removes support for suborigins. They have never been implemented in browsers and it looks like efforts have stalled. We can add support back if we need it but, well, maintaining support was going to be more trouble than it was worth. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Mar 14, 2019 at 16:09 UTC 72490f7ed04494b5d96bae06476faff472bc37a9
2 files changed +7 -53
core/corehttp/gateway_handler.go
+7 -52
@@ -24,7 +24,6 @@ import (
24 coreiface "github.com/ipfs/interface-go-ipfs-core"
25 ipath "github.com/ipfs/interface-go-ipfs-core/path"
26 routing "github.com/libp2p/go-libp2p-core/routing"
27 - "github.com/multiformats/go-multibase"
27 )
28
29 const (
@@ -148,12 +147,11 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
147 // and links that match the requested URL.
148 // For example, http://example.net would become /ipns/example.net, and
149 // the redirects and links would end up as http://example.net/ipns/example.net
151 - originalUrlPath := prefix + urlPath
152 - ipnsHostname := false
153 - if hdr := r.Header.Get("X-Ipns-Original-Path"); len(hdr) > 0 {
154 - originalUrlPath = prefix + hdr
155 - ipnsHostname = true
150 + requestURI, err := url.ParseRequestURI(r.RequestURI)
151 + if err != nil {
152 + webError(w, "failed to parse request path", err, http.StatusInternalServerError)
153 }
154 + originalUrlPath := prefix + requestURI.Path
155
156 // Service Worker registration request
157 if r.Header.Get("Service-Worker") == "script" {
@@ -206,39 +204,6 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
204 w.Header().Set("X-IPFS-Path", urlPath)
205 w.Header().Set("Etag", etag)
206
209 - // Suborigin header, sandboxes apps from each other in the browser (even
210 - // though they are served from the same gateway domain).
211 - //
212 - // Omitted if the path was treated by IPNSHostnameOption(), for example
213 - // a request for http://example.net/ would be changed to /ipns/example.net/,
214 - // which would turn into an incorrect Suborigin header.
215 - // In this case the correct thing to do is omit the header because it is already
216 - // handled correctly without a Suborigin.
217 - //
218 - // NOTE: This is not yet widely supported by browsers.
219 - if !ipnsHostname {
220 - // e.g.: 1="ipfs", 2="QmYuNaKwY...", ...
221 - pathComponents := strings.SplitN(urlPath, "/", 4)
222 -
223 - var suboriginRaw []byte
224 - cidDecoded, err := cid.Decode(pathComponents[2])
225 - if err != nil {
226 - // component 2 doesn't decode with cid, so it must be a hostname
227 - suboriginRaw = []byte(strings.ToLower(pathComponents[2]))
228 - } else {
229 - suboriginRaw = cidDecoded.Bytes()
230 - }
231 -
232 - base32Encoded, err := multibase.Encode(multibase.Base32, suboriginRaw)
233 - if err != nil {
234 - internalWebError(w, err)
235 - return
236 - }
237 -
238 - suborigin := pathComponents[1] + "000" + strings.ToLower(base32Encoded)
239 - w.Header().Set("Suborigin", suborigin)
240 - }
241 -
207 // set these headers _after_ the error, for we may just not have it
208 // and dont want the client to cache a 500 response...
209 // and only if it's /ipfs!
@@ -322,10 +287,10 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
287
288 // construct the correct back link
289 // https://github.com/ipfs/go-ipfs/issues/1365
325 - var backLink string = prefix + urlPath
290 + var backLink string = originalUrlPath
291
292 // don't go further up than /ipfs/$hash/
328 - pathSplit := path.SplitList(backLink)
293 + pathSplit := path.SplitList(urlPath)
294 switch {
295 // keep backlink
296 case len(pathSplit) == 3: // url: /ipfs/$hash
@@ -342,18 +307,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
307 }
308 }
309
345 - // strip /ipfs/$hash from backlink if IPNSHostnameOption touched the path.
346 - if ipnsHostname {
347 - backLink = prefix + "/"
348 - if len(pathSplit) > 5 {
349 - // also strip the trailing segment, because it's a backlink
350 - backLinkParts := pathSplit[3 : len(pathSplit)-2]
351 - backLink += path.Join(backLinkParts) + "/"
352 - }
353 - }
354 -
310 var hash string
356 - if !strings.HasPrefix(originalUrlPath, ipfsPathPrefix) {
311 + if !strings.HasPrefix(urlPath, ipfsPathPrefix) {
312 hash = resolvedPath.Cid().String()
313 }
314
core/corehttp/ipns_hostname.go
-1
@@ -28,7 +28,6 @@ func IPNSHostnameOption() ServeOption {
28 name := "/ipns/" + host
29 _, err := n.Namesys.Resolve(ctx, name, nsopts.Depth(1))
30 if err == nil || err == namesys.ErrResolveRecursion {
31 - r.Header.Set("X-Ipns-Original-Path", r.URL.Path)
31 r.URL.Path = name + r.URL.Path
32 }
33 }