fix(tenant): 收紧双站后台边界并补齐货架管理

This commit is contained in:
yml
2026-11-15 22:28:40 +08:00
parent a68eb815a9
commit 0cb385d84f
42 changed files with 831 additions and 97 deletions
+42
View File
@@ -0,0 +1,42 @@
package middleware
import (
"net/http"
"strings"
"hfb_sys/backend/internal/config"
"hfb_sys/backend/internal/platformscope"
"hfb_sys/backend/internal/sitecode"
"hfb_sys/backend/internal/tenancy"
"hfb_sys/backend/pkg/response"
"github.com/gin-gonic/gin"
)
// RequireKnownApplicationHost prevents unknown hosts from falling through to
// legacy unscoped handlers when the application is reached without the Caddy
// on-demand TLS guard.
func RequireKnownApplicationHost(appEnv string) gin.HandlerFunc {
if !config.IsProductionEnv(appEnv) {
return func(c *gin.Context) { c.Next() }
}
return func(c *gin.Context) {
if platformscope.IsScope(c.Request.Context()) ||
tenancy.IDFromContext(c.Request.Context()) > 0 ||
sitecode.HasDomainScope(c.Request.Context()) ||
isInfrastructureOrCallbackPath(c.Request.URL.Path) {
c.Next()
return
}
response.Error(c, http.StatusNotFound, "site_not_found", "当前域名未配置站点")
c.Abort()
}
}
func isInfrastructureOrCallbackPath(path string) bool {
switch path {
case "/health", "/health/ready", "/api/health", "/api/site/check-domain":
return true
}
return strings.HasPrefix(path, "/api/payments/") && strings.HasSuffix(path, "/notify")
}
@@ -0,0 +1,58 @@
package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"hfb_sys/backend/internal/tenancy"
"github.com/gin-gonic/gin"
)
func TestRequireKnownApplicationHostFailsClosedOnlyInProduction(t *testing.T) {
gin.SetMode(gin.TestMode)
newEngine := func(appEnv string) *gin.Engine {
engine := gin.New()
engine.Use(RequireKnownApplicationHost(appEnv))
engine.GET("/api/listings", func(c *gin.Context) { c.Status(http.StatusOK) })
engine.POST("/api/payments/leshua/notify", func(c *gin.Context) { c.Status(http.StatusOK) })
return engine
}
production := newEngine("production")
req := httptest.NewRequest(http.MethodGet, "http://unknown.example.com/api/listings", nil)
req.Host = "unknown.example.com"
rec := httptest.NewRecorder()
production.ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
t.Fatalf("unknown production host status = %d, want 404", rec.Code)
}
req = httptest.NewRequest(http.MethodPost, "http://callback.example.com/api/payments/leshua/notify", nil)
req.Host = "callback.example.com"
rec = httptest.NewRecorder()
production.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("payment callback status = %d, want 200", rec.Code)
}
req = httptest.NewRequest(http.MethodGet, "http://unknown.example.com/api/listings", nil)
req.Host = "unknown.example.com"
req = req.WithContext(tenancy.WithScope(req.Context(), tenancy.Scope{TenantID: 101}))
rec = httptest.NewRecorder()
production.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("known tenant status = %d, want 200", rec.Code)
}
development := newEngine("development")
req = httptest.NewRequest(http.MethodGet, "http://unknown.example.com/api/listings", nil)
req.Host = "unknown.example.com"
rec = httptest.NewRecorder()
development.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("development unknown host status = %d, want 200", rec.Code)
}
}
@@ -81,6 +81,7 @@ func SiteDomainFallback(resolver SiteDomainResolver, logger *zap.Logger) gin.Han
}
if code := resolveSiteCodeByHost(c, resolver, cache, logger); code != "" {
c.Request.Header.Set(sitecode.HeaderName, code)
c.Request = c.Request.WithContext(sitecode.WithDomainScope(c.Request.Context()))
}
c.Next()
}