From eedd5d5b73519e0e026a4d0f62a7070739616e3d Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 20 Nov 2015 00:28:07 +0100 Subject: [PATCH 1/5] Add correct access control headers to the default api config License: MIT Signed-off-by: Friedel Ziegelmayer --- repo/config/init.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/repo/config/init.go b/repo/config/init.go index eaa23d285..0119d734d 100644 --- a/repo/config/init.go +++ b/repo/config/init.go @@ -44,6 +44,16 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) { API: "/ip4/127.0.0.1/tcp/5001", Gateway: "/ip4/127.0.0.1/tcp/8080", }, + API: API{ + HTTPHeaders: map[string][]string{ + "Access-Control-Allow-Headers": []string{ + "X-Stream-Output, X-Chunked-Output", + }, + "Access-Control-Expose-Headers": []string{ + "X-Stream-Output, X-Chunked-Output", + }, + }, + }, Bootstrap: BootstrapPeerStrings(bootstrapPeers), SupernodeRouting: *snr, From e09c397aba9d1d6efa667d6783760296e1248514 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 20 Nov 2015 09:35:40 +0100 Subject: [PATCH 2/5] Try to fix tests License: MIT Signed-off-by: Friedel Ziegelmayer --- test/sharness/t0230-channel-streaming-http-content-type.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/sharness/t0230-channel-streaming-http-content-type.sh b/test/sharness/t0230-channel-streaming-http-content-type.sh index f4dc81c26..1a950f839 100755 --- a/test/sharness/t0230-channel-streaming-http-content-type.sh +++ b/test/sharness/t0230-channel-streaming-http-content-type.sh @@ -21,6 +21,8 @@ test_ls_cmd() { test_expect_success "Text encoded channel-streaming command output looks good" ' printf "HTTP/1.1 200 OK\r\n" >expected_output && + printf "Access-Control-Allow-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output && + printf "Access-Control-Expose-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output && printf "Content-Type: text/plain\r\n" >>expected_output && printf "Trailer: X-Stream-Error\r\n" >>expected_output && printf "Transfer-Encoding: chunked\r\n" >>expected_output && @@ -41,6 +43,8 @@ test_ls_cmd() { test_expect_success "JSON encoded channel-streaming command output looks good" ' printf "HTTP/1.1 200 OK\r\n" >expected_output && + printf "Access-Control-Allow-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output && + printf "Access-Control-Expose-Headers: X-Stream-Output, X-Chunked-Output\r\n" >>expected_output && printf "Content-Type: application/json\r\n" >>expected_output && printf "Trailer: X-Stream-Error\r\n" >>expected_output && printf "Transfer-Encoding: chunked\r\n" >>expected_output && From b036b23a233a64faa6a456522b0f45763be70e64 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Fri, 20 Nov 2015 20:25:25 +0100 Subject: [PATCH 3/5] Add access-control-headers to gateway config License: MIT Signed-off-by: Friedel Ziegelmayer --- repo/config/init.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/repo/config/init.go b/repo/config/init.go index 0119d734d..a59423b4c 100644 --- a/repo/config/init.go +++ b/repo/config/init.go @@ -81,6 +81,14 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) { Gateway: Gateway{ RootRedirect: "", Writable: false, + HTTPHeaders: map[string][]string{ + "Access-Control-Allow-Headers": []string{ + "X-Stream-Output, X-Chunked-Output", + }, + "Access-Control-Expose-Headers": []string{ + "X-Stream-Output, X-Chunked-Output", + }, + }, }, } From bdc1b27c51ff3dc7b465c8c5c0a98b97ae6454dc Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 30 Nov 2015 14:43:37 -0800 Subject: [PATCH 4/5] hard code things License: MIT Signed-off-by: Jeromy --- commands/http/handler.go | 5 +++++ core/corehttp/gateway_handler.go | 5 +++++ repo/config/init.go | 18 ------------------ 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/commands/http/handler.go b/commands/http/handler.go index 4a59bb8a0..857b7d77a 100644 --- a/commands/http/handler.go +++ b/commands/http/handler.go @@ -255,6 +255,11 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req h.Set(contentTypeHeader, mime) h.Set(transferEncodingHeader, "chunked") + // set 'allowed' headers + h.Set("Access-Control-Allow-Headers", "X-Stream-Output, X-Chunked-Output") + // expose those headers + h.Set("Access-Control-Expose-Headers", "X-Stream-Output, X-Chunked-Output") + if r.Method == "HEAD" { // after all the headers. return } diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 59c57e437..8cb6dc0f8 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -134,6 +134,11 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("X-IPFS-Path", urlPath) + // set 'allowed' headers + w.Header().Set("Access-Control-Allow-Headers", "X-Stream-Output, X-Chunked-Output") + // expose those headers + w.Header().Set("Access-Control-Expose-Headers", "X-Stream-Output, X-Chunked-Output") + // Suborigin header, sandboxes apps from each other in the browser (even // though they are served from the same gateway domain). // diff --git a/repo/config/init.go b/repo/config/init.go index a59423b4c..eaa23d285 100644 --- a/repo/config/init.go +++ b/repo/config/init.go @@ -44,16 +44,6 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) { API: "/ip4/127.0.0.1/tcp/5001", Gateway: "/ip4/127.0.0.1/tcp/8080", }, - API: API{ - HTTPHeaders: map[string][]string{ - "Access-Control-Allow-Headers": []string{ - "X-Stream-Output, X-Chunked-Output", - }, - "Access-Control-Expose-Headers": []string{ - "X-Stream-Output, X-Chunked-Output", - }, - }, - }, Bootstrap: BootstrapPeerStrings(bootstrapPeers), SupernodeRouting: *snr, @@ -81,14 +71,6 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) { Gateway: Gateway{ RootRedirect: "", Writable: false, - HTTPHeaders: map[string][]string{ - "Access-Control-Allow-Headers": []string{ - "X-Stream-Output, X-Chunked-Output", - }, - "Access-Control-Expose-Headers": []string{ - "X-Stream-Output, X-Chunked-Output", - }, - }, }, } From 74b8a0ffaf9dc29e324e6a2bce2d99117ff6205d Mon Sep 17 00:00:00 2001 From: Jeromy Date: Thu, 3 Dec 2015 22:18:04 -0800 Subject: [PATCH 5/5] fix tests License: MIT Signed-off-by: Jeromy --- commands/http/handler_test.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/commands/http/handler_test.go b/commands/http/handler_test.go index 86f1e8118..e40fb72b5 100644 --- a/commands/http/handler_test.go +++ b/commands/http/handler_test.go @@ -11,6 +11,8 @@ import ( coremock "github.com/ipfs/go-ipfs/core/mock" ) +const AllowedExposedHeaders = "X-Stream-Output, X-Chunked-Output" + func assertHeaders(t *testing.T, resHeaders http.Header, reqHeaders map[string]string) { for name, value := range reqHeaders { if resHeaders.Get(name) != value { @@ -170,7 +172,7 @@ func TestAllowedOrigins(t *testing.T) { ACAMethods: "", ACACredentials: "", "Access-Control-Max-Age": "", - "Access-Control-Expose-Headers": "", + "Access-Control-Expose-Headers": AllowedExposedHeaders, }, Code: http.StatusOK, } @@ -198,7 +200,7 @@ func TestWildcardOrigin(t *testing.T) { ACAMethods: "", ACACredentials: "", "Access-Control-Max-Age": "", - "Access-Control-Expose-Headers": "", + "Access-Control-Expose-Headers": AllowedExposedHeaders, }, Code: http.StatusOK, } @@ -258,7 +260,7 @@ func TestAllowedReferer(t *testing.T) { ACAMethods: "", ACACredentials: "", "Access-Control-Max-Age": "", - "Access-Control-Expose-Headers": "", + "Access-Control-Expose-Headers": AllowedExposedHeaders, }, Code: http.StatusOK, } @@ -286,7 +288,7 @@ func TestWildcardReferer(t *testing.T) { ACAMethods: "", ACACredentials: "", "Access-Control-Max-Age": "", - "Access-Control-Expose-Headers": "", + "Access-Control-Expose-Headers": AllowedExposedHeaders, }, Code: http.StatusOK, }