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/kubo#4025 (comment)

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>


This commit was moved from ipfs/kubo@455e498
  • Loading branch information
lidel authored and Stebalien committed Jan 17, 2020
1 parent cc72040 commit bb228d1
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions gateway/core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
gopath "path"
"regexp"
"runtime/debug"
"strings"
"time"
Expand Down Expand Up @@ -151,6 +152,18 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
ipnsHostname = true
}

// 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 bb228d1

Please sign in to comment.