fixed a bug with NUL in strings

This commit is contained in:
Нурлыгаянова Екатерина
2026-10-29 14:34:10 +03:00
parent 98ffd9ff45
commit 5021df4ea8
10 changed files with 177 additions and 36 deletions
+5 -3
View File
@@ -24,7 +24,7 @@ func (u *Users) CreateUser(ctx context.Context, username, displayName, passwordH
INSERT INTO users (username, display_name, password_hash)
VALUES ($1, $2, $3)
RETURNING id::text, created_at`,
username, displayName, passwordHash,
username, encodeText(displayName), passwordHash,
).Scan(&user.ID, &user.CreatedAt)
if err != nil {
return domain.User{}, mapErr(err)
@@ -51,6 +51,7 @@ func (u *Users) UserByID(ctx context.Context, id string) (domain.User, error) {
}
return domain.User{}, err
}
user.DisplayName = decodeText(user.DisplayName)
user.CreatedAt = user.CreatedAt.UTC()
return user, nil
}
@@ -65,11 +66,12 @@ func (u *Users) CredentialsByUsername(ctx context.Context, username string) (dom
SELECT id::text, username, display_name, created_at, password_hash
FROM users
WHERE username = $1`,
username,
encodeText(username),
).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash)
if err != nil {
return domain.User{}, "", mapErr(err)
}
user.DisplayName = decodeText(user.DisplayName)
user.CreatedAt = user.CreatedAt.UTC()
return user, hash, nil
}
@@ -81,7 +83,7 @@ func (u *Users) UsernameExists(ctx context.Context, username string) bool {
var exists bool
err := u.pool.QueryRow(ctx,
`SELECT EXISTS (SELECT 1 FROM users WHERE username = $1)`,
username,
encodeText(username),
).Scan(&exists)
return err == nil && exists
}