fix(security): 修复 P2 中危项(cookie/协议限速/路径遍历/默认口令/中继TLS/安全头/信息泄露)
- 会话 cookie 增加 Secure 标志;新增 [web].cookie_secure 配置 (默认 true,仅本地 HTTP 调试关闭;缺失字段按安全默认处理) - SMTP/IMAP/POP3 认证接入封禁体系(store.RecordAuthFailure 与 Web 共用 ban_entries):失败计数达 ban.max_fail_attempts 即封禁 IP, 已封禁 IP 拒绝认证,堵住协议层暴力破解 - 附件存储路径遍历防护重写:FullPath 白名单校验(UUID 文件名格式) + baseDir 前缀兜底,非法路径返回错误;Save 扩展名白名单化 - 初始管理员不再使用 admin/admin:密码取 MAILGO_ADMIN_PASSWORD 或 随机生成并打印一次;新增 MustChangePassword 首登强制改密 (管理员重置密码同样触发) - 外发中继默认验证 TLS 证书(保护 AUTH 凭据,防 MITM),直投 MX 保持机会式 TLS;新增 outbound.relay_tls_insecure 开关(默认 false) - 新增安全响应头中间件:HSTS、X-Frame-Options DENY、nosniff、 Referrer-Policy、基础 CSP(frame-ancestors 'none' 防点击劫持, connect-src/form-action 'self' 防数据外泄) - LDAP/OAuth 登录错误统一为通用文案,原始错误只写日志, 不再回显邮箱/内部细节(防用户枚举与信息泄露) - 新增 25 个回归测试:cookie 标志、封禁阈值、路径遍历用例、 中继 TLS 验证(自签证书 STARTTLS 集成)、安全头、OAuth 文案 部署注意:升级后所有会话失效需重新登录;若直接以 HTTP 提供 服务需显式配置 cookie_secure = false。
This commit is contained in:
+16
-3
@@ -32,6 +32,10 @@ type WebConfig struct {
|
||||
// 随机密钥并持久化到配置文件;也可通过环境变量 MAILGO_SECRET_KEY
|
||||
// 覆盖(覆盖值不落盘,适合容器部署)。
|
||||
SecretKey string `toml:"secret_key"`
|
||||
// CookieSecure 控制会话 cookie 是否仅通过 HTTPS 传输(Secure 标志)。
|
||||
// 默认 true;仅当应用直接以 HTTP 提供服务(本地调试、内网明文)时
|
||||
// 才应改为 false。
|
||||
CookieSecure bool `toml:"cookie_secure"`
|
||||
}
|
||||
|
||||
// SecretKeyEnvVar 是覆盖会话签名密钥的环境变量名。
|
||||
@@ -127,6 +131,10 @@ type OutboundConfig struct {
|
||||
RelayUser string `toml:"relay_user"` // 中继认证用户名(AUTH PLAIN)
|
||||
RelayPassword string `toml:"relay_password"` // 中继认证密码
|
||||
RelayStartTLS bool `toml:"relay_starttls"` // 非 465 端口是否使用 STARTTLS
|
||||
// RelayTLSInsecure 是否跳过中继服务器的 TLS 证书验证。
|
||||
// 默认 false(验证证书),避免凭据被中间人截获;仅当使用自签证书的
|
||||
// 内网中继且明确知晓风险时才设为 true。
|
||||
RelayTLSInsecure bool `toml:"relay_tls_insecure"`
|
||||
|
||||
// IP family and source address binding for outbound connections.
|
||||
IPFamily string `toml:"ip_family"` // ipv4(默认,PTR/SPF 最可靠)| ipv6 | auto
|
||||
@@ -189,7 +197,8 @@ func defaultConfig() *Config {
|
||||
AttachDir: filepath.Join(bd, "attachments"),
|
||||
},
|
||||
Web: WebConfig{
|
||||
Addr: DefaultWebPort,
|
||||
Addr: DefaultWebPort,
|
||||
CookieSecure: true,
|
||||
},
|
||||
SMTP: SMTPConfig{
|
||||
Addr: fmt.Sprintf(":%d", DefaultSMTPPort),
|
||||
@@ -435,11 +444,15 @@ func loadConfigFrom(path string) (*Config, error) {
|
||||
return nil, fmt.Errorf("解析配置文件失败: %w", err)
|
||||
}
|
||||
|
||||
// relay_starttls defaults to true for safety; the raw file is checked
|
||||
// because TOML decoding cannot distinguish an absent bool from false.
|
||||
// relay_starttls 与 web.cookie_secure 默认值为 true for safety;
|
||||
// the raw file is checked because TOML decoding cannot distinguish
|
||||
// an absent bool from false.
|
||||
if !strings.Contains(string(data), "relay_starttls") {
|
||||
cfg.Outbound.RelayStartTLS = defaults.Outbound.RelayStartTLS
|
||||
}
|
||||
if !strings.Contains(string(data), "cookie_secure") {
|
||||
cfg.Web.CookieSecure = defaults.Web.CookieSecure
|
||||
}
|
||||
|
||||
// 会话密钥缺失或不安全时补发随机密钥(随下面的写回一并落盘)
|
||||
if err := ensureSecretKey(cfg); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user