mirror of
https://github.com/Awuqing/BackupX.git
synced 2026-05-28 05:39:39 +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>
66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package notify
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
"net/url"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
type WebhookNotifier struct {
|
||
client *http.Client
|
||
}
|
||
|
||
func NewWebhookNotifier() *WebhookNotifier {
|
||
return &WebhookNotifier{client: &http.Client{Timeout: 10 * time.Second}}
|
||
}
|
||
func (n *WebhookNotifier) Type() string { return "webhook" }
|
||
func (n *WebhookNotifier) SensitiveFields() []string { return []string{"secret"} }
|
||
|
||
func (n *WebhookNotifier) Validate(config map[string]any) error {
|
||
raw := strings.TrimSpace(asString(config["url"]))
|
||
if raw == "" {
|
||
return fmt.Errorf("webhook url is required")
|
||
}
|
||
parsed, err := url.Parse(raw)
|
||
if err != nil {
|
||
return fmt.Errorf("webhook url is invalid: %w", err)
|
||
}
|
||
// 仅允许 http/https,杜绝 file://、gopher:// 等可被用于 SSRF 的协议。
|
||
if parsed.Scheme != "http" && parsed.Scheme != "https" {
|
||
return fmt.Errorf("webhook url must use http or https scheme")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (n *WebhookNotifier) Send(ctx context.Context, config map[string]any, message Message) error {
|
||
if err := n.Validate(config); err != nil {
|
||
return err
|
||
}
|
||
body, err := json.Marshal(map[string]any{"title": message.Title, "body": message.Body, "fields": message.Fields})
|
||
if err != nil {
|
||
return fmt.Errorf("marshal webhook payload: %w", err)
|
||
}
|
||
request, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimSpace(asString(config["url"])), bytes.NewReader(body))
|
||
if err != nil {
|
||
return fmt.Errorf("create webhook request: %w", err)
|
||
}
|
||
request.Header.Set("Content-Type", "application/json")
|
||
if secret := strings.TrimSpace(asString(config["secret"])); secret != "" {
|
||
request.Header.Set("X-BackupX-Secret", secret)
|
||
}
|
||
response, err := n.client.Do(request)
|
||
if err != nil {
|
||
return fmt.Errorf("send webhook request: %w", err)
|
||
}
|
||
defer response.Body.Close()
|
||
if response.StatusCode >= http.StatusBadRequest {
|
||
return fmt.Errorf("webhook response status: %s", response.Status)
|
||
}
|
||
return nil
|
||
}
|