Browse Source

WebUI: skip checks for static resource

Signed-off-by: Nicola Murino <[email protected]>
Nicola Murino 1 year ago
parent
commit
12f599fd65
2 changed files with 24 additions and 1 deletions
  1. 12 0
      internal/httpd/httpd_test.go
  2. 12 1
      internal/httpd/server.go

+ 12 - 0
internal/httpd/httpd_test.go

@@ -12903,6 +12903,18 @@ func TestDefender(t *testing.T) {
 	rr = executeRequest(req)
 	rr = executeRequest(req)
 	checkResponseCode(t, http.StatusForbidden, rr)
 	checkResponseCode(t, http.StatusForbidden, rr)
 	assert.Contains(t, rr.Body.String(), "your IP address is blocked")
 	assert.Contains(t, rr.Body.String(), "your IP address is blocked")
+	// requests for static files should be always allowed
+	req, err = http.NewRequest(http.MethodGet, "/static/favicon.ico", nil)
+	assert.NoError(t, err)
+	req.RemoteAddr = remoteAddr
+	rr = executeRequest(req)
+	checkResponseCode(t, http.StatusOK, rr)
+
+	req, err = http.NewRequest(http.MethodGet, "/.well-known/acme-challenge/foo", nil)
+	assert.NoError(t, err)
+	req.RemoteAddr = remoteAddr
+	rr = executeRequest(req)
+	checkResponseCode(t, http.StatusNotFound, rr)
 
 
 	_, err = httpdtest.RemoveUser(user, http.StatusOK)
 	_, err = httpdtest.RemoveUser(user, http.StatusOK)
 	assert.NoError(t, err)
 	assert.NoError(t, err)

+ 12 - 1
internal/httpd/server.go

@@ -1231,6 +1231,17 @@ func (s *httpdServer) mustStripSlash(r *http.Request) bool {
 		!strings.HasPrefix(urlPath, webStaticFilesPath) && !strings.HasPrefix(urlPath, acmeChallengeURI)
 		!strings.HasPrefix(urlPath, webStaticFilesPath) && !strings.HasPrefix(urlPath, acmeChallengeURI)
 }
 }
 
 
+func (s *httpdServer) mustCheckPath(r *http.Request) bool {
+	var urlPath string
+	rctx := chi.RouteContext(r.Context())
+	if rctx != nil && rctx.RoutePath != "" {
+		urlPath = rctx.RoutePath
+	} else {
+		urlPath = r.URL.Path
+	}
+	return !strings.HasPrefix(urlPath, webStaticFilesPath) && !strings.HasPrefix(urlPath, acmeChallengeURI)
+}
+
 func (s *httpdServer) initializeRouter() {
 func (s *httpdServer) initializeRouter() {
 	var hasHTTPSRedirect bool
 	var hasHTTPSRedirect bool
 	s.tokenAuth = jwtauth.New(jwa.HS256.String(), getSigningKey(s.signingPassphrase), nil)
 	s.tokenAuth = jwtauth.New(jwa.HS256.String(), getSigningKey(s.signingPassphrase), nil)
@@ -1240,7 +1251,7 @@ func (s *httpdServer) initializeRouter() {
 	s.router.Use(s.parseHeaders)
 	s.router.Use(s.parseHeaders)
 	s.router.Use(logger.NewStructuredLogger(logger.GetLogger()))
 	s.router.Use(logger.NewStructuredLogger(logger.GetLogger()))
 	s.router.Use(middleware.Recoverer)
 	s.router.Use(middleware.Recoverer)
-	s.router.Use(s.checkConnection)
+	s.router.Use(middleware.Maybe(s.checkConnection, s.mustCheckPath))
 	if s.binding.Security.Enabled {
 	if s.binding.Security.Enabled {
 		secureMiddleware := secure.New(secure.Options{
 		secureMiddleware := secure.New(secure.Options{
 			AllowedHosts:            s.binding.Security.AllowedHosts,
 			AllowedHosts:            s.binding.Security.AllowedHosts,