@cryptotaxi247 / kubo / commits / 23d35184c

fix: use http.Error for sending errors

This sets a few headers that prevent browsers from misinterpreting the error text.

Steven Allen committed May 24, 2019 at 19:33 UTC 23d35184c34251fdf388aca4aa12a924f5cab760
5 files changed +13 -18
core/corehttp/gateway_handler.go
+5 -6
@@ -102,14 +102,15 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
102 }
103
104 errmsg := "Method " + r.Method + " not allowed: "
105 + var status int
106 if !i.config.Writable {
106 - w.WriteHeader(http.StatusMethodNotAllowed)
107 + status = http.StatusMethodNotAllowed
108 errmsg = errmsg + "read only access"
109 } else {
109 - w.WriteHeader(http.StatusBadRequest)
110 + status = http.StatusBadRequest
111 errmsg = errmsg + "bad request for " + r.URL.Path
112 }
112 - fmt.Fprint(w, errmsg)
113 + http.Error(w, errmsg, status)
114 }
115
116 func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request) {
@@ -600,9 +601,7 @@ func webError(w http.ResponseWriter, message string, err error, defaultCode int)
601 }
602
603 func webErrorWithCode(w http.ResponseWriter, message string, err error, code int) {
603 - w.WriteHeader(code)
604 -
605 - fmt.Fprintf(w, "%s: %s\n", message, err)
604 + http.Error(w, fmt.Sprintf("%s: %s", message, err), code)
605 if code >= 500 {
606 log.Warningf("server error: %s: %s", err)
607 }
core/corehttp/mutex_profile.go
+4 -6
@@ -15,25 +15,23 @@ func MutexFractionOption(path string) ServeOption {
15 return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
16 mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
17 if r.Method != http.MethodPost {
18 - w.WriteHeader(http.StatusMethodNotAllowed)
18 + http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
19 return
20 }
21 if err := r.ParseForm(); err != nil {
22 - w.WriteHeader(http.StatusBadRequest)
23 - _, _ = w.Write([]byte(err.Error()))
22 + http.Error(w, err.Error(), http.StatusBadRequest)
23 return
24 }
25
26 asfr := r.Form.Get("fraction")
27 if len(asfr) == 0 {
29 - w.WriteHeader(http.StatusBadRequest)
28 + http.Error(w, "parameter 'fraction' must be set", http.StatusBadRequest)
29 return
30 }
31
32 fr, err := strconv.Atoi(asfr)
33 if err != nil {
35 - w.WriteHeader(http.StatusBadRequest)
36 - _, _ = w.Write([]byte(err.Error()))
34 + http.Error(w, err.Error(), http.StatusBadRequest)
35 return
36 }
37 log.Infof("Setting MutexProfileFraction to %d", fr)
core/corehttp/proxy.go
+1 -3
@@ -73,7 +73,5 @@ func parseRequest(request *http.Request) (*proxyRequest, error) {
73 }
74
75 func handleError(w http.ResponseWriter, msg string, err error, code int) {
76 - w.WriteHeader(code)
77 - fmt.Fprintf(w, "%s: %s\n", msg, err)
78 - log.Warningf("http proxy error: %s: %s", err)
76 + http.Error(w, fmt.Sprintf("%s: %s", msg, err), code)
77 }
go.mod
+1 -1
@@ -34,7 +34,7 @@ require (
34 github.com/ipfs/go-ipfs-blockstore v0.0.1
35 github.com/ipfs/go-ipfs-blocksutil v0.0.1
36 github.com/ipfs/go-ipfs-chunker v0.0.1
37 - github.com/ipfs/go-ipfs-cmds v0.0.7
37 + github.com/ipfs/go-ipfs-cmds v0.0.8
38 github.com/ipfs/go-ipfs-config v0.0.3
39 github.com/ipfs/go-ipfs-ds-help v0.0.1
40 github.com/ipfs/go-ipfs-exchange-interface v0.0.1
go.sum
+2 -2
@@ -267,8 +267,8 @@ github.com/ipfs/go-ipfs-blocksutil v0.0.1 h1:Eh/H4pc1hsvhzsQoMEP3Bke/aW5P5rVM1IW
267 github.com/ipfs/go-ipfs-blocksutil v0.0.1/go.mod h1:Yq4M86uIOmxmGPUHv/uI7uKqZNtLb449gwKqXjIsnRk=
268 github.com/ipfs/go-ipfs-chunker v0.0.1 h1:cHUUxKFQ99pozdahi+uSC/3Y6HeRpi9oTeUHbE27SEw=
269 github.com/ipfs/go-ipfs-chunker v0.0.1/go.mod h1:tWewYK0we3+rMbOh7pPFGDyypCtvGcBFymgY4rSDLAw=
270 -github.com/ipfs/go-ipfs-cmds v0.0.7 h1:0N2NXxYAZn1kHpHrZMHZYRcVGJSxQogDD89oKc0GZMg=
271 -github.com/ipfs/go-ipfs-cmds v0.0.7/go.mod h1:E5ou2OpwkAtR8LdneNdq4w1vPcrTWvh/6WPhjxGaX/Y=
270 +github.com/ipfs/go-ipfs-cmds v0.0.8 h1:ZMo0ZeQOr10ZKY4yxYA3lRHUbnF/ZYcV9cpU0IrlGFI=
271 +github.com/ipfs/go-ipfs-cmds v0.0.8/go.mod h1:TiK4e7/V31tuEb8YWDF8lN3qrnDH+BS7ZqWIeYJlAs8=
272 github.com/ipfs/go-ipfs-config v0.0.1 h1:6ED08emzI1imdsAjixFi2pEyZxTVD5ECKtCOxLBx+Uc=
273 github.com/ipfs/go-ipfs-config v0.0.1/go.mod h1:KDbHjNyg4e6LLQSQpkgQMBz6Jf4LXiWAcmnkcwmH0DU=
274 github.com/ipfs/go-ipfs-config v0.0.3 h1:Ep4tRdP1iVK76BgOprD9B/qtOEdpno+1Xb57BqydgGk=