core/corehttp: Added gateway path whitelisting
Matt Bell committed
Feb 2, 2015 at 18:00 UTC
b1ca07d6c5fcd0db16e5594d2c15c2ab02f49908
4 files changed
+20
-9
cmd/ipfs/daemon.go
+13
-1
@@ -3,6 +3,7 @@ package main
3
import (
4
"fmt"
5
"os"
6
+ "strings"
7
8
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9
cmds "github.com/jbenet/go-ipfs/commands"
@@ -192,10 +193,21 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
193
}()
194
}
195
196
+ blocklist := &corehttp.BlockList{}
197
+ blocklist.SetDecider(func(s string) bool {
198
+ // only allow paths that begin with the WebUI path
199
+ return strings.HasPrefix(s, corehttp.WebUIPath)
200
+ })
201
+ gatewayConfig := corehttp.GatewayConfig{
202
+ Writable: true,
203
+ BlockList: blocklist,
204
+ }
205
+ gatewayOption := corehttp.NewGateway(gatewayConfig).ServeOption()
206
+
207
var opts = []corehttp.ServeOption{
208
corehttp.CommandsOption(*req.Context()),
209
corehttp.WebUIOption,
198
- corehttp.GatewayOption(true),
210
+ gatewayOption,
211
}
212
if rootRedirect != nil {
213
opts = append(opts, rootRedirect)
core/corehttp/gateway.go
-1
@@ -47,7 +47,6 @@ func GatewayOption(writable bool) ServeOption {
47
type Decider func(string) bool
48
49
type BlockList struct {
50
-
50
mu sync.RWMutex
51
d Decider
52
}
core/corehttp/gateway_handler.go
+5
-5
@@ -48,15 +48,15 @@ type directoryItem struct {
48
// gatewayHandler is a HTTP handler that serves IPFS objects (accessible by default at /ipfs/<path>)
49
// (it serves requests like GET /ipfs/QmVRzPKPzNtSrEzBFm2UZfxmPAgnaLke4DMcerbsGGSaFe/link)
50
type gatewayHandler struct {
51
- node *core.IpfsNode
52
- dirList *template.Template
53
- config GatewayConfig
51
+ node *core.IpfsNode
52
+ dirList *template.Template
53
+ config GatewayConfig
54
}
55
56
func newGatewayHandler(node *core.IpfsNode, conf GatewayConfig) (*gatewayHandler, error) {
57
i := &gatewayHandler{
58
- node: node,
59
- config: conf,
58
+ node: node,
59
+ config: conf,
60
}
61
err := i.loadTemplate()
62
if err != nil {
core/corehttp/webui.go
+2
-2
@@ -1,6 +1,6 @@
1
package corehttp
2
3
// TODO: move to IPNS
4
-const webuiPath = "/ipfs/QmctngrQAt9fjpQUZr7Bx3BsXUcif52eZGTizWhvcShsjz"
4
+const WebUIPath = "/ipfs/QmctngrQAt9fjpQUZr7Bx3BsXUcif52eZGTizWhvcShsjz"
5
6
-var WebUIOption = RedirectOption("webui", webuiPath)
6
+var WebUIOption = RedirectOption("webui", WebUIPath)