diff --git a/common/errors.go b/common/errors.go index 2be4cc5..4105d25 100644 --- a/common/errors.go +++ b/common/errors.go @@ -133,8 +133,8 @@ func NewTemplateLoadError(template string, cause error) *CommonError { return NewError(ErrTemplateLoad, fmt.Sprintf("failed to load template: %s", template), cause) } -func NewTemplateParseError(cause error) *CommonError { - return NewError(ErrTemplateParse, "failed to parse template", cause) +func NewTemplateParseError(data []byte, cause error) *CommonError { + return NewError(ErrTemplateParse, fmt.Sprintf("failed to parse template: %s", data), cause) } // Subscription errors @@ -142,8 +142,8 @@ func NewSubscriptionLoadError(url string, cause error) *CommonError { return NewError(ErrSubscriptionLoad, fmt.Sprintf("failed to load subscription: %s", url), cause) } -func NewSubscriptionParseError(cause error) *CommonError { - return NewError(ErrSubscriptionParse, "failed to parse subscription", cause) +func NewSubscriptionParseError(data []byte, cause error) *CommonError { + return NewError(ErrSubscriptionParse, fmt.Sprintf("failed to parse subscription: %s", string(data)), cause) } // Regex errors diff --git a/common/sub.go b/common/sub.go index b479798..72bf439 100644 --- a/common/sub.go +++ b/common/sub.go @@ -129,7 +129,7 @@ func BuildSub(clashType model.ClashType, query model.ConvertConfig, template str err = yaml.Unmarshal(templateBytes, &temp) if err != nil { logger.Logger.Debug("parse template failed", zap.Error(err)) - return nil, NewTemplateParseError(err) + return nil, NewTemplateParseError(templateBytes, err) } var proxyList []P.Proxy @@ -168,7 +168,7 @@ func BuildSub(clashType model.ClashType, query model.ConvertConfig, template str zap.String("data", string(data)), zap.Error(err), ) - return nil, NewSubscriptionParseError(err) + return nil, NewSubscriptionParseError(data, err) } p, err := parser.ParseProxies(parser.ParseConfig{UseUDP: query.UseUDP}, strings.Split(base64, "\n")...) if err != nil { diff --git a/common/template.go b/common/template.go index a628364..4bb0424 100644 --- a/common/template.go +++ b/common/template.go @@ -3,11 +3,27 @@ package common import ( "io" "os" + "path/filepath" + "strings" ) -func LoadTemplate(templatePath string) ([]byte, error) { - if _, err := os.Stat(templatePath); err == nil { - file, err := os.Open(templatePath) +const templatesDir = "templates" + +// LoadTemplate 只读取运行目录下的 templates 目录,防止其他文件内容泄漏 +func LoadTemplate(templateName string) ([]byte, error) { + // 清理路径,防止目录遍历攻击 + cleanTemplateName := filepath.Clean(templateName) + + // 检查是否尝试访问父目录 + if strings.HasPrefix(cleanTemplateName, "..") || strings.Contains(cleanTemplateName, string(filepath.Separator)+".."+string(filepath.Separator)) { + return nil, NewFileNotFoundError(templateName) // 拒绝包含父目录的路径 + } + + // 构建完整路径,确保只从 templates 目录读取 + fullPath := filepath.Join(templatesDir, cleanTemplateName) + + if _, err := os.Stat(fullPath); err == nil { + file, err := os.Open(fullPath) if err != nil { return nil, err } @@ -22,5 +38,5 @@ func LoadTemplate(templatePath string) ([]byte, error) { } return result, nil } - return nil, NewFileNotFoundError(templatePath) + return nil, NewFileNotFoundError(templateName) } diff --git a/server/static/index.html b/server/static/index.html index 3c61452..a626f5c 100644 --- a/server/static/index.html +++ b/server/static/index.html @@ -89,7 +89,7 @@