fix(agent):第 19 条 token 回填守卫兼容 SQL NULL 老行(token_plain IS NULL OR = '')
This commit is contained in:
@@ -83,11 +83,16 @@ func (a *AgentController) findAgent(token string) (*model.AgentNode, bool) {
|
||||
// Item 19 backfill: the token has just passed the hash check, so for
|
||||
// rows created before plaintext storage existed, adopt the presented
|
||||
// token as the recoverable copy. The guarded update only fires while
|
||||
// token_plain is empty — an authenticated request can never overwrite
|
||||
// an already-stored token (only issuance writes a fresh one).
|
||||
// token_plain is unset — an authenticated request can never overwrite
|
||||
// an already-stored token (only issuance writes a fresh one). "Unset"
|
||||
// must cover SQL NULL as well as '': AutoMigrate added the column to
|
||||
// pre-existing rows as NULL, and NULL = '' is never true, so an
|
||||
// equality-only guard silently matched zero legacy rows in
|
||||
// production (the GORM-scanned value reads as "" either way, which is
|
||||
// why the in-memory check above cannot distinguish them).
|
||||
if agent.TokenPlain == "" {
|
||||
if res := database.GetDB().Model(&model.AgentNode{}).
|
||||
Where("id = ? AND token_plain = ''", agent.Id).
|
||||
Where("id = ? AND (token_plain IS NULL OR token_plain = '')", agent.Id).
|
||||
Update("token_plain", token); res.Error == nil && res.RowsAffected > 0 {
|
||||
agent.TokenPlain = token
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user