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

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
+24 -3
View File
@@ -31,11 +31,29 @@ type fillupHint struct {
var (
fillupMu sync.Mutex
fillupCache = map[int64]fillupHint{}
fillupOrder []int64 // 键的插入顺序,超限时据此淘汰最旧的一批
)
// fillupCacheMax 是内存提示缓存的条目上限。
//
// 个人站点的地点数是有限的,这里兜的是「站点长期运行、数据集换了一轮又一轮」时的无界增长:
// 光写不清的全局 map 会一路涨到把 1C1G 的机器吃干净。超限就丢掉最旧的四分之一,
// 让缓存自己收敛——比整表清空温和,不会一次性把所有高德建议都丢掉。
const fillupCacheMax = 2048
func fillupCachePut(placeID int64, h fillupHint) {
fillupMu.Lock()
defer fillupMu.Unlock()
if _, ok := fillupCache[placeID]; !ok {
if len(fillupOrder) >= fillupCacheMax {
n := fillupCacheMax / 4
for _, k := range fillupOrder[:n] {
delete(fillupCache, k)
}
fillupOrder = append(fillupOrder[:0], fillupOrder[n:]...)
}
fillupOrder = append(fillupOrder, placeID)
}
fillupCache[placeID] = h
}
@@ -381,7 +399,10 @@ func (s *Server) adminFillupApply(w http.ResponseWriter, r *http.Request) {
actByName[name] = actID
newActs++
}
isHome, isWork := s.activityFlags(ctx, ex, actID)
isHome, isWork, err := s.activityFlags(ctx, ex, actID)
if err != nil {
return err
}
res, err := ex.ExecContext(ctx, `UPDATE visits v SET activity_id=$1, is_home=$2, is_work=$3
WHERE v.dataset_id=$4 AND v.activity_id IS NULL
AND v.place_id = (SELECT p.id FROM places p WHERE p.dataset_id=$4 AND p.id=$5)`,
@@ -451,8 +472,8 @@ func (s *Server) fillupTargets(ctx context.Context, ex execer, datasetID int64)
return out, rows.Err()
}
func (s *Server) activityFlags(ctx context.Context, ex execer, actID int64) (isHome, isWork bool) {
ex.QueryRowContext(ctx, `SELECT is_home, is_work FROM activities WHERE id=$1`, actID).Scan(&isHome, &isWork)
func (s *Server) activityFlags(ctx context.Context, ex execer, actID int64) (isHome, isWork bool, err error) {
err = ex.QueryRowContext(ctx, `SELECT is_home, is_work FROM activities WHERE id=$1`, actID).Scan(&isHome, &isWork)
return
}