fix: credential data corruption - Create handler mutating store pointer

- CreateCredential was setting cred.Data = cred.Masked directly on the
  store pointer, corrupting stored credentials with masked data
- Fixed by using a value copy for API response
- Also fix nextID starts at 1 to avoid ID=0 issue
This commit is contained in:
2026-07-24 19:16:35 +08:00
parent 471fe76384
commit 6498903a7c
2 changed files with 7 additions and 5 deletions
+3 -2
View File
@@ -71,8 +71,9 @@ var CredStore *CredentialStore
// InitCredentialStore initializes the credential store // InitCredentialStore initializes the credential store
func InitCredentialStore(cfg *Config) { func InitCredentialStore(cfg *Config) {
CredStore = &CredentialStore{ CredStore = &CredentialStore{
data: make(map[uint]*Credential), data: make(map[uint]*Credential),
path: cfg.DataDir + "/credentials.json", path: cfg.DataDir + "/credentials.json",
nextID: 1,
} }
if err := CredStore.Load(); err != nil { if err := CredStore.Load(); err != nil {
+4 -3
View File
@@ -89,9 +89,10 @@ func (h *CredentialHandler) CreateCredential(c *gin.Context) {
return return
} }
// Return without secrets // Return without secrets (use a copy to avoid mutating store)
cred.Data = cred.Masked resp := *cred
c.JSON(http.StatusCreated, cred) resp.Data = cred.Masked
c.JSON(http.StatusCreated, &resp)
} }
// UpdateCredential updates a credential // UpdateCredential updates a credential