Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

Addresses review comments on PR #157 which implements username conflict recovery for Discord headset linking.

Changes

Error Detection Simplification

  • Removed redundant string check in isAlreadyExistsError(): "code = AlreadyExists" || "AlreadyExists""AlreadyExists"
  • Second check is substring of first, making it redundant

Security Documentation

Added 21-line SECURITY NOTE documenting automatic custom_id linking tradeoffs:

  • Concern: Attacker could pre-create account with victim's Discord username
  • Mitigations: Comprehensive audit logging, unlinked-only recovery, unique usernames per server, duplicate rejection
  • Race condition: LinkCustom fails with AlreadyExists if concurrent link occurs
  • Justification: Discord auth is trusted, conflicts are legacy migrations, logging enables abuse detection

Test Fix

Example

// Simplified error detection
func (d *DiscordAppBot) isAlreadyExistsError(err error) bool {
    if err == nil {
        return false
    }
    st, ok := status.FromError(err)
    if ok && st.Code() == codes.AlreadyExists {
        return true
    }
    // Single check covers both "code = AlreadyExists" and "AlreadyExists"
    return strings.Contains(err.Error(), "AlreadyExists")
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Handle linking errors for "username already exists"</issue_title>
<issue_description>### Problem
When linking a Discord account, if Nakama returns a rpc error: code = AlreadyExists desc = Username is already in use. error (as in:

{"level":"error","ts":"2025-11-15T20:35:59.335Z","caller":"server/evr_discord_appbot_linking.go:79","msg":"Failed to link headset","runtime":"go","system":"discordAppBot","discord_id":"1380019639354130534","username":"someuser","guild_id":"779349159852769310","channel_id":"1227795372244729926","user_id":"","group_id":"147afc9d-2819-4197-926d-5b3f92790edc","discord_id":"1234123412341234","link_code":"RTTD","error":"failed to authenticate (or create) user 1234123412341234: rpc error: code = AlreadyExists desc = Username is already in use.","source":"github.com/heroiclabs/nakama/v3/server/evr_discord_appbot_linking.go:79"}

the process fails without further recovery options.

Proposed Solution

Implement a new handler for this linking case in Go using the Nakama server runtime APIs:

  • On catching this AlreadyExists error, look up the Nakama account with the conflicting username.
    • If the discord user exists:
      • Update the Nakama username to the correct value and re-attempt linking.
    • If the discord user no longer exists:
      • Change the Nakama custom_id to the Discord ID of the user currently trying to link the headset.
      • Retry linking.
  • If the link still fails, return a user error and guide them to file a support ticket.

Reference Nakama Go runtime API docs for authentication and account update calls.

Acceptance Criteria

  • Graceful handling of username conflicts for Discord linking using a recovery handler
  • Logic matches the error context given above, references server/evr_discord_appbot_linking.go
  • Properly documents the control flow with code comments
  • Returns actionable info to user on repeated failure
  • Code samples/references from Nakama docs and error log are included for maintainers
    </issue_description>

<agent_instructions>review #157 carefully, implement the review suggestions.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- Simplify isAlreadyExistsError to remove redundant string check
- Add comprehensive security documentation for automatic linking
- Document race condition risks and mitigations
- Fix test compilation issue with operatorFromTag/operatorFromStatField

Co-authored-by: thesprockee <962164+thesprockee@users.noreply.github.com>
Copilot AI changed the title [WIP] Handle linking errors for existing usernames Implement PR #157 review feedback: simplify error detection and document security tradeoffs Nov 20, 2025
Copilot AI requested a review from thesprockee November 20, 2025 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle linking errors for "username already exists"

2 participants