mirror of
https://github.com/Awuqing/BackupX.git
synced 2026-05-28 03:29:35 +08:00
* fix(server): 后端直接托管 Web 控制台,修复无 nginx 时 404 (#62) 问题 #62:在未安装 nginx 的服务器上,访问 :8340/ 返回 "route not found"(404),Web 控制台完全无法打开;同时 systemd 服务以 backupx 用户启动时因无权读取 root:root 0640 的配置文件 而反复退出(exit 1)。 修复: - 后端新增 SPA 静态托管:自动探测前端目录(./web、./web/dist、 /opt/backupx/web 等,或 server.web_root 显式指定),命中后直接 提供静态文件与 index.html 回退,无需额外 nginx 反向代理即可访问 控制台。/api、/health、/metrics、/install 等保留前缀仍返回结构化 JSON 404,不会被 SPA 回退污染(沿用 issue #46 的约定)。 - 含 ".." 的请求路径由文件服务层直接拒绝,叠加 filepath.Rel 容器 校验,杜绝目录穿越。 - install.sh 以 backupx:backupx 安装配置文件并显式 chown,修复历史 版本 root:root 0640 导致服务无法读取配置而启动失败的问题;安装 完成提示同步说明可直接通过 :8340 访问,并给出 journalctl 排查命令。 - 新增 spa_test.go 覆盖目录探测、保留前缀判定、SPA 回退与穿越防护。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(security): 修复邮件头注入,加固 webhook 与整数转换 CodeQL 静态扫描在 main 上的真实告警修复: - 邮件通知(email.go):From/To/Subject 头部此前直接拼接用户可控 内容(备份任务名会进入 Subject),存在 SMTP 头注入风险(可注入 Bcc 等额外头部或伪造正文)。新增 buildRawMessage/sanitizeHeaderValue 剔除头部值中的 CR/LF;正文保持原样。新增 email_test.go 覆盖。 - webhook 通知(webhook.go):Validate 增加 URL 解析与 http/https 协议校验,杜绝 file://、gopher:// 等可用于 SSRF 的协议。 - 整数转换(auth_service.go、storage_target_handler.go、 backup_record_handler.go):将 ParseUint 的 bitSize 由 64 改为 0 (即 uint 宽度),消除 uint64→uint 的潜在截断(32 位平台上为越界 拒绝而非静默截断),并清除 go/incorrect-integer-conversion 告警。 注:archive.go/file_runner.go 的 zipslip 告警为误报(已有 HasPrefix 容器校验且不解压符号链接);node FS 浏览与 webhook 目标主机由设计上 的鉴权用户控制,不在本次行为变更范围内。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
158 lines
4.2 KiB
Go
158 lines
4.2 KiB
Go
package config
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/spf13/viper"
|
||
)
|
||
|
||
type Config struct {
|
||
Server ServerConfig `mapstructure:"server"`
|
||
Database DatabaseConfig `mapstructure:"database"`
|
||
Security SecurityConfig `mapstructure:"security"`
|
||
Backup BackupConfig `mapstructure:"backup"`
|
||
Log LogConfig `mapstructure:"log"`
|
||
}
|
||
|
||
type ServerConfig struct {
|
||
Host string `mapstructure:"host"`
|
||
Port int `mapstructure:"port"`
|
||
Mode string `mapstructure:"mode"`
|
||
ExternalURL string `mapstructure:"external_url"`
|
||
// WebRoot 指向前端构建产物目录。留空时后端会按部署惯例自动探测
|
||
// (./web、./web/dist、/opt/backupx/web 等)。探测命中后后端直接托管
|
||
// 前端 SPA,无需额外的 nginx 反向代理即可访问 Web 控制台。
|
||
WebRoot string `mapstructure:"web_root"`
|
||
}
|
||
|
||
type DatabaseConfig struct {
|
||
Path string `mapstructure:"path"`
|
||
}
|
||
|
||
type SecurityConfig struct {
|
||
JWTSecret string `mapstructure:"jwt_secret"`
|
||
JWTExpire string `mapstructure:"jwt_expire"`
|
||
EncryptionKey string `mapstructure:"encryption_key"`
|
||
}
|
||
|
||
type BackupConfig struct {
|
||
TempDir string `mapstructure:"temp_dir"`
|
||
MaxConcurrent int `mapstructure:"max_concurrent"`
|
||
Retries int `mapstructure:"retries"` // 底层 HTTP 请求重试次数,默认 10
|
||
BandwidthLimit string `mapstructure:"bandwidth_limit"` // 带宽限制,如 "10M",空不限
|
||
}
|
||
|
||
type LogConfig struct {
|
||
Level string `mapstructure:"level"`
|
||
File string `mapstructure:"file"`
|
||
MaxSize int `mapstructure:"max_size"`
|
||
MaxBackups int `mapstructure:"max_backups"`
|
||
MaxAge int `mapstructure:"max_age"`
|
||
}
|
||
|
||
func Load(configPath string) (Config, error) {
|
||
v := viper.New()
|
||
applyDefaults(v)
|
||
v.SetConfigType("yaml")
|
||
v.SetEnvPrefix("BACKUPX")
|
||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||
v.AutomaticEnv()
|
||
|
||
if configPath != "" {
|
||
v.SetConfigFile(configPath)
|
||
if err := v.ReadInConfig(); err != nil {
|
||
return Config{}, fmt.Errorf("read config: %w", err)
|
||
}
|
||
} else {
|
||
v.SetConfigName("config")
|
||
v.AddConfigPath(".")
|
||
v.AddConfigPath("./server")
|
||
v.AddConfigPath("/etc/backupx")
|
||
if err := v.ReadInConfig(); err != nil {
|
||
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||
return Config{}, fmt.Errorf("read config: %w", err)
|
||
}
|
||
}
|
||
}
|
||
|
||
var cfg Config
|
||
if err := v.Unmarshal(&cfg); err != nil {
|
||
return Config{}, fmt.Errorf("decode config: %w", err)
|
||
}
|
||
|
||
if cfg.Server.Host == "" {
|
||
cfg.Server.Host = "0.0.0.0"
|
||
}
|
||
if cfg.Server.Port == 0 {
|
||
cfg.Server.Port = 8340
|
||
}
|
||
if cfg.Server.Mode == "" {
|
||
cfg.Server.Mode = "release"
|
||
}
|
||
if cfg.Database.Path == "" {
|
||
cfg.Database.Path = "./data/backupx.db"
|
||
}
|
||
if cfg.Security.JWTExpire == "" {
|
||
cfg.Security.JWTExpire = "24h"
|
||
}
|
||
if cfg.Backup.TempDir == "" {
|
||
cfg.Backup.TempDir = "/tmp/backupx"
|
||
}
|
||
if cfg.Backup.MaxConcurrent <= 0 {
|
||
cfg.Backup.MaxConcurrent = 2
|
||
}
|
||
if cfg.Backup.Retries <= 0 {
|
||
cfg.Backup.Retries = 10
|
||
}
|
||
if cfg.Log.Level == "" {
|
||
cfg.Log.Level = "info"
|
||
}
|
||
if cfg.Log.File == "" {
|
||
cfg.Log.File = "./data/backupx.log"
|
||
}
|
||
if cfg.Log.MaxSize <= 0 {
|
||
cfg.Log.MaxSize = 100
|
||
}
|
||
if cfg.Log.MaxBackups <= 0 {
|
||
cfg.Log.MaxBackups = 3
|
||
}
|
||
if cfg.Log.MaxAge <= 0 {
|
||
cfg.Log.MaxAge = 30
|
||
}
|
||
|
||
return cfg, nil
|
||
}
|
||
|
||
func MustJWTDuration(cfg SecurityConfig) time.Duration {
|
||
duration, err := time.ParseDuration(cfg.JWTExpire)
|
||
if err != nil {
|
||
return 24 * time.Hour
|
||
}
|
||
return duration
|
||
}
|
||
|
||
func (c Config) Address() string {
|
||
return fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port)
|
||
}
|
||
|
||
func applyDefaults(v *viper.Viper) {
|
||
v.SetDefault("server.host", "0.0.0.0")
|
||
v.SetDefault("server.port", 8340)
|
||
v.SetDefault("server.mode", "release")
|
||
v.SetDefault("server.external_url", "")
|
||
v.SetDefault("server.web_root", "")
|
||
v.SetDefault("database.path", "./data/backupx.db")
|
||
v.SetDefault("security.jwt_expire", "24h")
|
||
v.SetDefault("backup.temp_dir", "/tmp/backupx")
|
||
v.SetDefault("backup.max_concurrent", 2)
|
||
v.SetDefault("backup.retries", 10)
|
||
v.SetDefault("backup.bandwidth_limit", "")
|
||
v.SetDefault("log.level", "info")
|
||
v.SetDefault("log.file", "./data/backupx.log")
|
||
v.SetDefault("log.max_size", 100)
|
||
v.SetDefault("log.max_backups", 3)
|
||
v.SetDefault("log.max_age", 30)
|
||
}
|