Kaynağa Gözat

Fix panic on some stupid input

世界 5 ay önce
ebeveyn
işleme
d8b2d5142f

+ 4 - 1
route/rule/rule_default.go

@@ -102,7 +102,10 @@ func NewDefaultRule(ctx context.Context, logger log.ContextLogger, options optio
 		rule.allItems = append(rule.allItems, item)
 	}
 	if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
-		item := NewDomainItem(options.Domain, options.DomainSuffix)
+		item, err := NewDomainItem(options.Domain, options.DomainSuffix)
+		if err != nil {
+			return nil, err
+		}
 		rule.destinationAddressItems = append(rule.destinationAddressItems, item)
 		rule.allItems = append(rule.allItems, item)
 	}

+ 4 - 1
route/rule/rule_dns.go

@@ -93,7 +93,10 @@ func NewDefaultDNSRule(ctx context.Context, logger log.ContextLogger, options op
 		rule.allItems = append(rule.allItems, item)
 	}
 	if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
-		item := NewDomainItem(options.Domain, options.DomainSuffix)
+		item, err := NewDomainItem(options.Domain, options.DomainSuffix)
+		if err != nil {
+			return nil, err
+		}
 		rule.destinationAddressItems = append(rule.destinationAddressItems, item)
 		rule.allItems = append(rule.allItems, item)
 	}

+ 4 - 1
route/rule/rule_headless.go

@@ -47,7 +47,10 @@ func NewDefaultHeadlessRule(ctx context.Context, options option.DefaultHeadlessR
 		rule.allItems = append(rule.allItems, item)
 	}
 	if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
-		item := NewDomainItem(options.Domain, options.DomainSuffix)
+		item, err := NewDomainItem(options.Domain, options.DomainSuffix)
+		if err != nil {
+			return nil, err
+		}
 		rule.destinationAddressItems = append(rule.destinationAddressItems, item)
 		rule.allItems = append(rule.allItems, item)
 	} else if options.DomainMatcher != nil {

+ 13 - 2
route/rule/rule_item_domain.go

@@ -5,6 +5,7 @@ import (
 
 	"github.com/sagernet/sing-box/adapter"
 	"github.com/sagernet/sing/common/domain"
+	E "github.com/sagernet/sing/common/exceptions"
 )
 
 var _ RuleItem = (*DomainItem)(nil)
@@ -14,7 +15,17 @@ type DomainItem struct {
 	description string
 }
 
-func NewDomainItem(domains []string, domainSuffixes []string) *DomainItem {
+func NewDomainItem(domains []string, domainSuffixes []string) (*DomainItem, error) {
+	for _, domainItem := range domains {
+		if domainItem == "" {
+			return nil, E.New("domain: empty item is not allowed")
+		}
+	}
+	for _, domainSuffixItem := range domainSuffixes {
+		if domainSuffixItem == "" {
+			return nil, E.New("domain_suffix: empty item is not allowed")
+		}
+	}
 	var description string
 	if dLen := len(domains); dLen > 0 {
 		if dLen == 1 {
@@ -40,7 +51,7 @@ func NewDomainItem(domains []string, domainSuffixes []string) *DomainItem {
 	return &DomainItem{
 		domain.NewMatcher(domains, domainSuffixes, false),
 		description,
-	}
+	}, nil
 }
 
 func NewRawDomainItem(matcher *domain.Matcher) *DomainItem {