fix(distribution): 修复分销后台域名登录被拦截
This commit is contained in:
@@ -27,6 +27,7 @@ type Config struct {
|
||||
PrimaryDomain string
|
||||
TenantJieyouDomain string
|
||||
OpsDomain string
|
||||
DistributionAdminDomain string
|
||||
Storage StorageConfig
|
||||
StorageMirror StorageConfig
|
||||
SMS SMSConfig
|
||||
@@ -110,6 +111,11 @@ func Load() Config {
|
||||
PrimaryDomain: primaryDomain,
|
||||
TenantJieyouDomain: strings.TrimSpace(getEnv("TENANT_JIEYOU_DOMAIN", "")),
|
||||
OpsDomain: opsDomain(appEnv, getEnv("OPS_DOMAIN", ""), primaryDomain),
|
||||
DistributionAdminDomain: distributionAdminDomain(
|
||||
appEnv,
|
||||
getEnv("DISTRIBUTION_ADMIN_DOMAIN", ""),
|
||||
primaryDomain,
|
||||
),
|
||||
Storage: StorageConfig{
|
||||
Endpoint: getEnv("STORAGE_ENDPOINT", "http://localhost:19090"),
|
||||
Bucket: getEnv("STORAGE_BUCKET", "hfb-sys"),
|
||||
@@ -176,6 +182,19 @@ func opsDomain(appEnv string, value string, primaryDomain string) string {
|
||||
return "ops.localhost"
|
||||
}
|
||||
|
||||
func distributionAdminDomain(appEnv string, value string, primaryDomain string) string {
|
||||
if domain := strings.TrimSpace(value); domain != "" {
|
||||
return domain
|
||||
}
|
||||
if strings.EqualFold(strings.TrimSpace(appEnv), "production") {
|
||||
if primary := strings.TrimSpace(primaryDomain); primary != "" {
|
||||
return "distribution." + primary
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return "distribution.localhost"
|
||||
}
|
||||
|
||||
func (c Config) ValidateProductionSecurity() error {
|
||||
if !IsProductionEnv(c.AppEnv) {
|
||||
return nil
|
||||
|
||||
@@ -48,6 +48,34 @@ func TestOpsDomain(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDistributionAdminDomain(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
appEnv string
|
||||
value string
|
||||
primary string
|
||||
expected string
|
||||
}{
|
||||
{name: "开发默认", appEnv: "development", expected: "distribution.localhost"},
|
||||
{name: "生产按主域推导", appEnv: "production", primary: "hao858.com", expected: "distribution.hao858.com"},
|
||||
{
|
||||
name: "显式覆盖",
|
||||
appEnv: "production",
|
||||
value: "dist-admin.example.com",
|
||||
primary: "hao858.com",
|
||||
expected: "dist-admin.example.com",
|
||||
},
|
||||
{name: "生产无主域", appEnv: "production", expected: ""},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := distributionAdminDomain(tc.appEnv, tc.value, tc.primary); got != tc.expected {
|
||||
t.Fatalf("distributionAdminDomain() = %q, want %q", got, tc.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDatabaseConfigFromEnvironment(t *testing.T) {
|
||||
t.Setenv("DATABASE_MAX_OPEN_CONNS", "80")
|
||||
t.Setenv("DATABASE_MAX_IDLE_CONNS", "16")
|
||||
|
||||
@@ -16,11 +16,22 @@ import (
|
||||
// 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 {
|
||||
func RequireKnownApplicationHost(appEnv string, extraKnownHosts ...string) gin.HandlerFunc {
|
||||
if !config.IsProductionEnv(appEnv) {
|
||||
return func(c *gin.Context) { c.Next() }
|
||||
}
|
||||
knownHosts := make(map[string]struct{}, len(extraKnownHosts))
|
||||
for _, rawHost := range extraKnownHosts {
|
||||
if host := normalizeKnownHost(rawHost); host != "" {
|
||||
knownHosts[host] = struct{}{}
|
||||
}
|
||||
}
|
||||
return func(c *gin.Context) {
|
||||
requestHost := sitecode.ExtractHost(c.Request.Host)
|
||||
if _, ok := knownHosts[requestHost]; ok {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
if platformscope.IsScope(c.Request.Context()) ||
|
||||
tenancy.IDFromContext(c.Request.Context()) > 0 ||
|
||||
sitecode.HasDomainScope(c.Request.Context()) ||
|
||||
@@ -33,6 +44,16 @@ func RequireKnownApplicationHost(appEnv string) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeKnownHost(raw string) string {
|
||||
host := strings.TrimSpace(raw)
|
||||
host = strings.TrimPrefix(host, "https://")
|
||||
host = strings.TrimPrefix(host, "http://")
|
||||
if slash := strings.IndexByte(host, '/'); slash >= 0 {
|
||||
host = host[:slash]
|
||||
}
|
||||
return sitecode.ExtractHost(host)
|
||||
}
|
||||
|
||||
func isInfrastructureOrCallbackPath(path string) bool {
|
||||
switch path {
|
||||
case "/health", "/health/ready", "/api/health", "/api/site/check-domain":
|
||||
|
||||
@@ -13,15 +13,16 @@ import (
|
||||
func TestRequireKnownApplicationHostFailsClosedOnlyInProduction(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
newEngine := func(appEnv string) *gin.Engine {
|
||||
newEngine := func(appEnv string, extraKnownHosts ...string) *gin.Engine {
|
||||
engine := gin.New()
|
||||
engine.Use(RequireKnownApplicationHost(appEnv))
|
||||
engine.Use(RequireKnownApplicationHost(appEnv, extraKnownHosts...))
|
||||
engine.GET("/api/listings", func(c *gin.Context) { c.Status(http.StatusOK) })
|
||||
engine.POST("/api/distribution/auth/login", 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")
|
||||
production := newEngine("production", "https://distribution.hao858.com/")
|
||||
req := httptest.NewRequest(http.MethodGet, "http://unknown.example.com/api/listings", nil)
|
||||
req.Host = "unknown.example.com"
|
||||
rec := httptest.NewRecorder()
|
||||
@@ -30,6 +31,14 @@ func TestRequireKnownApplicationHostFailsClosedOnlyInProduction(t *testing.T) {
|
||||
t.Fatalf("unknown production host status = %d, want 404", rec.Code)
|
||||
}
|
||||
|
||||
req = httptest.NewRequest(http.MethodPost, "http://distribution.hao858.com/api/distribution/auth/login", nil)
|
||||
req.Host = "distribution.hao858.com"
|
||||
rec = httptest.NewRecorder()
|
||||
production.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("distribution admin host status = %d, want 200", rec.Code)
|
||||
}
|
||||
|
||||
req = httptest.NewRequest(http.MethodPost, "http://callback.example.com/api/payments/leshua/notify", nil)
|
||||
req.Host = "callback.example.com"
|
||||
rec = httptest.NewRecorder()
|
||||
|
||||
@@ -378,7 +378,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
if distributionRepo != nil {
|
||||
engine.Use(middleware.SiteDomainFallback(distributionRepo, logger))
|
||||
}
|
||||
engine.Use(middleware.RequireKnownApplicationHost(cfg.AppEnv))
|
||||
engine.Use(middleware.RequireKnownApplicationHost(cfg.AppEnv, cfg.DistributionAdminDomain))
|
||||
engine.Use(middleware.PlatformOpsReadOnlyGuard())
|
||||
distributionHandler := distribution.NewHandler(distributionService)
|
||||
if invitationHandler != nil && distributionService != nil {
|
||||
|
||||
Reference in New Issue
Block a user