Skip to content

Commit 9a76674

Browse files
committed
Add Suborigin header to gateway responses (ipfs#3209)
This existed before but was disabled in 912a972 because the Suborigin spec changed and it became incompatible. This commit updates the generated Suborigin header to be conformant with the latest spec. License: MIT Signed-off-by: James Stanley <[email protected]>
1 parent a6e96e6 commit 9a76674

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

core/corehttp/gateway_handler.go

+28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
2828
routing "gx/ipfs/QmafuecpeZp3k3sHJ5mUARHd4795revuadECQMkmHB8LfW/go-libp2p-routing"
2929
node "gx/ipfs/Qmb3Hm9QDFmfYuET4pu7Kyg8JV78jFa1nvZx5vnCZsK4ck/go-ipld-format"
30+
multibase "gx/ipfs/QmcxkxTVuURV2Ptse8TvkqH5BQDwV62X1x19JqqvbBzwUM/go-multibase"
3031
)
3132

3233
const (
@@ -210,6 +211,33 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
210211
// expose those headers
211212
w.Header().Set("Access-Control-Expose-Headers", "X-Stream-Output, X-Chunked-Output")
212213

214+
// Suborigin header, sandboxes apps from each other in the browser (even
215+
// though they are served from the same gateway domain).
216+
//
217+
// Omitted if the path was treated by IPNSHostnameOption(), for example
218+
// a request for http://example.net/ would be changed to /ipns/example.net/,
219+
// which would turn into an incorrect Suborigin: example.net header.
220+
// In this case the correct thing to do is omit the header because it is already
221+
// handled correctly without a Suborigin.
222+
//
223+
// NOTE: This is not yet widely supported by browsers.
224+
if !ipnsHostname {
225+
// e.g.: 0="ipfs.io", 1="ipfs", 2="QmYuNaKwY...", ...
226+
pathComponents := strings.SplitN(urlPath, "/", 4)
227+
suboriginRaw := []byte(strings.ToLower(pathComponents[2]))
228+
cidDecoded, err := cid.Decode(pathComponents[2])
229+
if err == nil {
230+
suboriginRaw = cidDecoded.Bytes()
231+
}
232+
base32Encoded, err := multibase.Encode(multibase.Base32, suboriginRaw)
233+
if err != nil {
234+
internalWebError(w, err)
235+
return
236+
}
237+
suborigin := pathComponents[1] + "000" + strings.ToLower(base32Encoded)
238+
w.Header().Set("Suborigin", suborigin)
239+
}
240+
213241
// set these headers _after_ the error, for we may just not have it
214242
// and dont want the client to cache a 500 response...
215243
// and only if it's /ipfs!

0 commit comments

Comments
 (0)