package hosting import ( "encoding/json" "errors" "net/url" "path" "regexp" "strings" ) type WebsiteSettings struct { Rewrite string `json:"rewrite"` RedirectURL string `json:"redirectURL"` RedirectCode int `json:"redirectCode"` ForceHTTPS bool `json:"forceHTTPS"` Index []string `json:"index"` Error404 string `json:"error404"` Error503 string `json:"error503"` Maintenance bool `json:"maintenance"` } type Startup struct { EntryFile string `json:"entryFile"` WorkDir string `json:"workDir"` Arguments []string `json:"arguments"` Environment map[string]string `json:"environment"` } var simpleFile = regexp.MustCompile(`^[A-Za-z0-9_./-]+$`) var envName = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]{0,63}$`) func DefaultWebsite() WebsiteSettings { return WebsiteSettings{Rewrite: "none", RedirectCode: 301, Index: []string{"index.php", "index.html", "index.htm"}} } func ValidRelative(v string) bool { return v != "" && v != "." && len(v) <= 255 && simpleFile.MatchString(v) && !strings.HasPrefix(v, "/") && path.Clean(v) == v && !strings.HasPrefix(v, "../") && !strings.Contains(v, "/.sea-") && !strings.HasPrefix(v, ".sea-") && path.Base(v) != ".user.ini" } func (v *WebsiteSettings) Validate() error { if !map[string]bool{"none": true, "wordpress": true, "laravel": true, "thinkphp": true, "spa": true}[v.Rewrite] { return errors.New("伪静态模板无效") } if len(v.Index) < 1 || len(v.Index) > 10 { return errors.New("请设置 1–10 个默认首页") } for _, name := range v.Index { if !ValidRelative(name) || strings.Contains(name, "/") { return errors.New("默认首页只能填写文件名") } } for _, name := range []string{v.Error404, v.Error503} { if name != "" && (!ValidRelative(name) || (!strings.HasSuffix(name, ".html") && !strings.HasSuffix(name, ".htm"))) { return errors.New("错误页应为网站目录内的 HTML 文件") } } if v.RedirectCode != 301 && v.RedirectCode != 302 { return errors.New("跳转状态码应为 301 或 302") } if v.RedirectURL != "" { u, e := url.Parse(v.RedirectURL) if e != nil || u.Hostname() == "" || u.User != nil || (u.Scheme != "http" && u.Scheme != "https") || len(v.RedirectURL) > 2048 || strings.ContainsAny(v.RedirectURL, "\x00\r\n\t \"'{};$\\") { return errors.New("跳转地址无效") } } return nil } func (v *Startup) Validate() error { if raw, e := json.Marshal(v); e != nil || len(raw) > 24<<10 { return errors.New("启动配置不能超过 24 KiB") } if v.EntryFile != "" && !ValidRelative(v.EntryFile) { return errors.New("启动入口必须位于网站目录内") } if v.WorkDir == "" { v.WorkDir = "." } if v.WorkDir != "." && !ValidRelative(v.WorkDir) { return errors.New("工作目录必须位于网站目录内") } if len(v.Arguments) > 64 || len(v.Environment) > 64 { return errors.New("启动参数或环境变量过多") } for _, arg := range v.Arguments { if len(arg) > 2048 || strings.ContainsAny(arg, "\x00\r\n") { return errors.New("启动参数无效") } } for k, value := range v.Environment { upper := strings.ToUpper(k) if !envName.MatchString(k) || strings.HasPrefix(upper, "LD_") || strings.HasPrefix(upper, "DYLD_") || map[string]bool{"PORT": true, "PATH": true, "HOME": true, "USER": true, "LOGNAME": true, "SHELL": true, "BASH_ENV": true, "ENV": true}[upper] || len(value) > 4096 || strings.ContainsAny(value, "\x00\r\n") { return errors.New("环境变量名称被保留,或值包含换行/超过限制") } } return nil }