diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index d3c4d26392d..5040f8faf73 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -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)