Skip to content

Commit

Permalink
fix: limit SW registration to content root
Browse files Browse the repository at this point in the history
Introduces hardening proposed in:
ipfs#4025 (comment)

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
lidel authored and Walter Beegle committed Jun 6, 2020
1 parent 2af3eca commit 42e2c62
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
}
}

// Service Worker registration request
if r.Header.Get("Service-Worker") == "script" {
// Disallow Service Worker registration on namespace roots
// https://github.com/ipfs/go-ipfs/issues/4025
matched, _ := regexp.MatchString(`^/ip[fn]s/[^/]+$`, r.URL.Path)
if matched {
err := fmt.Errorf("registration is not allowed for this scope")
webError(w, "navigator.serviceWorker", err, http.StatusBadRequest)
return
}
}

parsedPath := ipath.New(urlPath)
if err := parsedPath.IsValid(); err != nil {
webError(w, "invalid ipfs path", err, http.StatusBadRequest)
Expand Down

0 comments on commit 42e2c62

Please sign in to comment.