XnLemon commented on code in PR #1087:
URL: https://github.com/apache/dubbo-go-pixiu/pull/1087#discussion_r4528798208


##########
admin/dao/impl/guest.go:
##########
@@ -57,33 +55,43 @@ func (d *GuestDao) Login(username, password string) (bool, 
int) {
        return true, id
 }
 
-func (d *GuestDao) Register(username, password string) error {
+func (d *GuestDao) EnsureDefaultAdmin(username, password string) error {
+       _, err := d.db.Exec(`
+               INSERT INTO pixiu_user (username, password, role)
+               SELECT ?, ?, 1 FROM DUAL
+               WHERE NOT EXISTS (SELECT 1 FROM pixiu_user WHERE username = ?)
+       `, username, password, username)
+       return err
+}
 
-       if username == "" {
-               return errors.New("void username")
+func (d *GuestDao) Register(username, password string) error {
+       tx, err := d.db.Begin()
+       if err != nil {
+               return err
        }
+       defer tx.Rollback()
 
-       db := d.db
-       var id int
-       err := db.QueryRow("SELECT id FROM pixiu_user WHERE username = ?", 
username).Scan(&id)
+       var userID int64
+       err = tx.QueryRow("SELECT id FROM pixiu_user WHERE username = ? LIMIT 
1", username).Scan(&userID)

Review Comment:
   **[P2] Enforce username uniqueness atomically**
   
   **Evidence chain**
   - **Head:** `8f54356173678fa0681c38816cd978a047d55823`.
   - **Trigger:** two authenticated administrators concurrently register the 
same previously unused username.
   - **Path and observation:** `admin/dao/impl/guest.go:75` performs a plain 
SELECT inside the transaction, then line 83 inserts when it sees no row. The 
current `pixiu_user` schema at `admin/resources/sql/pixiu_demo.sql:28-36` has 
no unique constraint on `username`. Under InnoDB's default REPEATABLE READ, 
these nonlocking reads can both observe the username as absent; both inserts 
can then commit. [MySQL isolation 
documentation](https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html).
   - **Contract:** the duplicate check in `Register` intends to reject an 
already-used username.
   - **Impact:** concurrent requests can create multiple rows for one login 
name, making username-based profile, authorization, and password lookups 
ambiguous.
   - **Validation:** traced the check/insert/commit path and inspected the 
table DDL; the reported Go tests cover handler authorization and validation, 
not concurrent database registration.
   - **Request:** enforce username uniqueness atomically (including a safe 
migration strategy for existing data), handle duplicate-key errors, and add a 
concurrent-registration regression test.
   
   **中文**
   两个已认证管理员并发注册同一个尚未使用的用户名时,`admin/dao/impl/guest.go:75` 
的普通查询可能都读到“用户不存在”,随后都在第 83 行插入并提交。当前 `pixiu_user` 
表(`admin/resources/sql/pixiu_demo.sql:28-36`)没有用户名唯一约束;InnoDB 默认 REPEATABLE 
READ 
下的非锁定读不会保护这个不存在的用户名。因此会产生同名账号,导致按用户名进行的个人信息、权限和密码查询出现歧义。请通过数据库级原子唯一性约束(并考虑已有数据的安全迁移)处理重复键,同时增加并发注册回归测试。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

  • [PR] feat(adm... XnLemon (via GitHub) via notifications
    • Re: [PR]... XnLemon (via GitHub) via notifications
    • Re: [PR]... XnLemon (via GitHub) via notifications
    • Re: [PR]... sonarqubecloud[bot] (via GitHub) via notifications
    • Re: [PR]... github-advanced-security[bot] (via GitHub) via notifications
    • Re: [PR]... sonarqubecloud[bot] (via GitHub) via notifications
    • Re: [PR]... sonarqubecloud[bot] (via GitHub) via notifications
    • Re: [PR]... XnLemon (via GitHub) via notifications
    • Re: [PR]... sonarqubecloud[bot] (via GitHub) via notifications
    • Re: [PR]... sonarqubecloud[bot] (via GitHub) via notifications
    • Re: [PR]... Alanxtl (via GitHub) via notifications
    • Re: [PR]... codecov-commenter (via GitHub) via notifications

Reply via email to