审计修复:后端(安全 / 数据一致性 / 性能)

audit-2026-09-21.md 里的后端条目落地。每条都配了回归守卫,并逐条做过变异验证
(注入原 bug → 确认按正确理由变红 → 还原 → 变绿)。

安全
- CSRF 只加同源 Origin/Referer 守卫(csrf.go),不动模板
- 会话 cookie 的 Secure 按请求判断;两步验证两闸(loginGuard + 验证码一次性)
- 跳转收口到 safeInternalPath;clientIP 只认 XFF 最后一段
- GPX 按属主圈定且查 RowsAffected;下载名走 downloadDisposition
- 上传口统一 MaxBytesReader;落盘后的失败路径清掉半成品

数据一致性
- 区域判定两端各过一遍再 AND(apiTrack 与 movementsWhere 对齐)
- Filter.Verdict 全域一致(下推到 visitsWhere);聚合刷新必须返回 error
- 附属数据改挂坐标键,Go 与 SQL 两套表达式逐位一致
- 脏备份的伪时间/越界坐标不再进领域模型;rows 循环统一 drainRows
- 合并与导出:writeRawRow 上抛错误、悬空引用清理、原生列补全

性能
- placeCols 相关子查询带 dataset_id 前导列;findSourceArchive 先按大小筛
- 批量勾选改 ANY($3::bigint[]);fillupCache 加上限;同一查询不再查两遍

另:新增「全新库」冒烟测试 —— 空库跑完 Migrate 再用应用侧读查询真打一遍,
迁移块排到依赖表之前这种错只有新库会炸,老库毫无症状。
This commit is contained in:
tamakyi
2026-09-24 20:37:16 +08:00
parent 78865fc736
commit b4c2a8452f
70 changed files with 5417 additions and 360 deletions
+39
View File
@@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"strings"
"sync"
"time"
)
@@ -98,6 +99,44 @@ func urlEscape(s string) string {
return b.String()
}
// ---------- 验证码重放 ----------
// replayTTL 要盖过 VerifyTOTP 的整个接受窗口(前后各一步 = 90 秒),
// 否则同一步长的码会在条目过期后又被收下。
const replayTTL = 3 * totpStep
// CodeReplay 记住最近用过的验证码,拦住「同一个码在窗口内被用第二次」。
// VerifyTOTP 只判断「这个码此刻对不对」,不含一次性语义;被偷看到或被日志
// 截获的码,在它过期前都能再换一个会话。
type CodeReplay struct {
mu sync.Mutex
used map[string]time.Time
now func() time.Time
}
func NewCodeReplay() *CodeReplay {
return &CodeReplay{used: map[string]time.Time{}, now: time.Now}
}
// Consume 在验证码**已经校验通过**之后登记一次使用;已被用过则返回 false。
// 键里带用户号:不同账号的同一个数字码互不影响。
func (r *CodeReplay) Consume(userID int64, code string) bool {
now := r.now()
key := fmt.Sprintf("%d|%s", userID, code)
r.mu.Lock()
defer r.mu.Unlock()
for k, t := range r.used {
if now.Sub(t) > replayTTL {
delete(r.used, k)
}
}
if _, ok := r.used[key]; ok {
return false
}
r.used[key] = now
return true
}
// ---------- 用户秘钥存取 ----------
// TOTPState 返回用户的两步验证状态。