数据:收窄账号列表与所有权消费方

This commit is contained in:
Christ committed 2026-08-14 11:39:37 +08:00
1 parent 5bb60cabcd
commit d54377d2ef
6 files changed
+27 -25

No files matched your search

+4 -2
View File
@@ -109,7 +109,8 @@ app shell / routes
- 已完成阶段 2 第一个切片:建立不读取或解密敏感字段的 `CookieSummary`、`ListOwnedIDs`、`ExistsOwned`,并覆盖跨用户和无效 user ID 回归测试;
- 已完成阶段 2 第二个切片:新增 `GetOwnerID`、原子 `GetValueOwned`,将 server 的账号所有权检查和订单凭证读取迁移到窄查询,纯所有权 handler 不再解密完整账号详情,并保留 404/403 行为回归;
- 已完成阶段 2 第三个切片:账号列表、运行状态、非敏感详情和单账号详情迁移到 `ListOwnedIDs`、`ListSummaries`、`GetSummaryOwned`,刷新资料先做窄所有权过滤,消除列表批量解密和详情 N+1 完整凭证查询;
- 下一最小工作项:盘点 `internal/server` 其余 `AllForUser` 调用,将只需要账号 ID 或所有权判断的流程迁移到窄查询;
- 已完成阶段 2 第四个切片:聊天账号校验、商品列表、关键词回复列表、卡券关联校验及管理员删除用户的账号停止流程迁移到 `ListOwnedIDs` 或 `ExistsOwned`,商品凭证读取迁移到 `GetValueOwned`;
- 下一最小工作项:拆分 `internal/server/order_handlers.go` 中订单刷新、手动发货和订单导入的账号列表与凭证读取,分别使用账号 ID 列表、所有权判断和单值凭证接口;
- 随后工作项:再处理明确需要平台凭证的业务流程,统一使用按用户和账号 ID 过滤的单值凭证接口;
- 禁止跳过当前入口直接开始 Engine、Automation 或 DB 的大规模拆分。
@@ -204,7 +205,8 @@ app shell / routes
`GetOwnerID` 只返回所有者 ID;`GetSummaryOwned` 返回指定用户的单个非敏感摘要;`GetValueOwned` 在同一条带 user_id 过滤的查询中读取并解密单个 Cookie,避免
所有权检查与凭证读取之间的竞态窗口。测试使用故意无效的密文值验证摘要查询和所有权检查不会触发解密,
并使用正常加密值覆盖单值凭证读取。账号列表与详情 handler 已不再通过 `AllForUser` 或完整 `GetDetails` 读取敏感字段,
其余 `AllForUser` 和 `GetDetails` 调用方继续按消费场景逐个替换并保留行为回归测试。
目前 server 生产代码仅剩订单刷新、手动发货和订单导入的 3 处 `AllForUser`,这些流程同时编排凭证和订单业务,
将作为独立切片拆分,避免在一次变更中混合多个订单行为。
逐步替换使用 `AllForUser` 进行所有权检查以及使用 `GetDetails` 获取非敏感字段的调用。
+2 -2
View File
@@ -65,8 +65,8 @@ func (s *Server) adminDeleteUser(w http.ResponseWriter, r *http.Request) {
return
}
if s.Manager != nil {
if accounts, listErr := s.Store.Cookies.AllForUser(r.Context(), uid); listErr == nil {
for cookieID := range accounts {
if accountIDs, listErr := s.Store.Cookies.ListOwnedIDs(r.Context(), uid); listErr == nil { // accountIDs 是待停止的账号 ID。
for _, cookieID := range accountIDs {
s.Manager.Stop(cookieID)
}
}
+3 -3
View File
@@ -167,11 +167,11 @@ func (s *Server) itemOwnedByUser(r *http.Request, userID int64, cookieID, itemID
if itemID == "" {
return true
}
all, err := s.Store.Cookies.AllForUser(r.Context(), userID)
if err != nil || cookieID == "" {
if cookieID == "" {
return false
}
if _, ok := all[cookieID]; !ok {
owned, err := s.Store.Cookies.ExistsOwned(r.Context(), userID, cookieID) // owned 和 err 表示账号归属及查询错误。
if err != nil || !owned {
return false
}
_, err = s.Store.Items.Get(r.Context(), cookieID, itemID)
+6 -6
View File
@@ -434,14 +434,14 @@ func (s *Server) ownsAccount(r *http.Request, accountID string) bool {
return false
}
sess := auth.SessionFromContext(r.Context())
accounts, err := s.Store.Cookies.AllForUser(r.Context(), sess.UserID)
if err != nil {
return false
}
_, ok := accounts[accountID]
return ok
owned, err := s.Store.Cookies.ExistsOwned(r.Context(), sess.UserID, accountID) // owned 和 err 表示账号归属及查询错误。
return err == nil && owned
}
/*
账号查询已采用所有权窄接口。
*/
// parsePositiveInt 将正整数文本转换为整数,无法解析时返回备用值。
func parsePositiveInt(raw string, fallback int) int {
value, err := strconv.Atoi(raw)
if err != nil || value <= 0 {
+10 -10
View File
@@ -317,21 +317,21 @@ func parseMoneyCents(raw string) (int64, error) {
func (s *Server) listItems(w http.ResponseWriter, r *http.Request) {
sess := auth.SessionFromContext(r.Context())
all, err := s.Store.Cookies.AllForUser(r.Context(), sess.UserID)
cookieIDs, err := s.Store.Cookies.ListOwnedIDs(r.Context(), sess.UserID) // cookieIDs 和 err 是账号 ID 列表及查询错误。
if err != nil {
writeErr(w, http.StatusInternalServerError, "查询失败")
return
}
cookieID := strings.TrimSpace(r.URL.Query().Get("cookie_id"))
if cookieID != "" {
if _, ok := all[cookieID]; !ok {
if !s.cookieOwnedByUser(r.Context(), sess.UserID, cookieID) {
writeErr(w, http.StatusForbidden, "无权限操作该账号")
return
}
all = map[string]string{cookieID: all[cookieID]}
cookieIDs = []string{cookieID}
}
result := []map[string]any{}
for cid := range all {
for _, cid := range cookieIDs {
items, _ := s.Store.Items.AllForCookie(r.Context(), cid)
for _, it := range items {
result = append(result, itemToMap(it))
@@ -538,16 +538,15 @@ func (s *Server) syncItemsPageFromAccount(w http.ResponseWriter, r *http.Request
func (s *Server) cookieForCurrentUser(w http.ResponseWriter, r *http.Request, cookieID string) (string, int64, bool) {
sess := auth.SessionFromContext(r.Context())
all, err := s.Store.Cookies.AllForUser(r.Context(), sess.UserID)
value, err := s.Store.Cookies.GetValueOwned(r.Context(), sess.UserID, cookieID) // value 和 err 是单个 Cookie 明文及读取错误。
if err != nil {
if errors.Is(err, db.ErrNotFound) {
writeErr(w, http.StatusForbidden, "无权限操作该账号")
return "", 0, false
}
writeErr(w, http.StatusInternalServerError, "查询账号失败")
return "", 0, false
}
value, ok := all[cookieID]
if !ok {
writeErr(w, http.StatusForbidden, "无权限操作该账号")
return "", 0, false
}
if value == "" {
writeErr(w, http.StatusBadRequest, "账号 cookie 为空")
return "", 0, false
@@ -555,6 +554,7 @@ func (s *Server) cookieForCurrentUser(w http.ResponseWriter, r *http.Request, co
return value, sess.UserID, true
}
// saveSyncedItems 以下历史同步流程保持原有行为。
func (s *Server) saveSyncedItems(ctx context.Context, cookieID string, items []mtop.ItemListItem) int {
saved := 0
for _, item := range items {
+2 -2
View File
@@ -299,9 +299,9 @@ func (s *Server) mountItemRepliesReal(r chi.Router) {
func (s *Server) listItemReplies(w http.ResponseWriter, r *http.Request) {
sess := auth.SessionFromContext(r.Context())
all, _ := s.Store.Cookies.AllForUser(r.Context(), sess.UserID)
cookieIDs, _ := s.Store.Cookies.ListOwnedIDs(r.Context(), sess.UserID) // cookieIDs 是当前用户拥有的账号 ID。
var result []map[string]any
for cid := range all {
for _, cid := range cookieIDs {
rows, _ := s.Store.ItemReps.AllForUser(r.Context(), cid)
for _, ir := range rows {
result = append(result, map[string]any{