Skip to content

fix(profiles): enforce per-account profile name uniqueness - #342

Merged
Quick104 merged 3 commits into
Silo-Server:mainfrom
RXWatcher:fix/profile-name-uniqueness
Jul 8, 2026
Merged

Quick104 merged 3 commits into
Silo-Server:mainfrom
RXWatcher:fix/profile-name-uniqueness

Conversation

@RXWatcher

@RXWatcher RXWatcher commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Profile create and rename accept any name, so a single account can hold unlimited identically-named profiles ("Laura", "laura", "Laura " — or five literal "Laura"s). Found during the 2026-07-08 Android profile QA pass. No client validates either, so every platform exposes the bug.

Approach

Reject a create or rename whose trimmed, case-insensitive name matches another profile on the same account, with 409 name_conflict ("A profile with this name already exists"), following the existing profile_limit_reached pattern.

  • Per-account scoping by construction: the check compares against a single user's profile store (storeProvider.ForUser), so different accounts can still each have a "Laura". Cross-account names are never visible to it.
  • Renames exclude the profile being updated, so re-saving a profile under its own name (avatar-only edits resubmit the name) still succeeds.
  • Adjacent fix: whitespace-only names (" ") previously passed the name == "" check on create, and renames had no blank check at all; both now reject with 400.

Additive-only per the v1 API rules: a new error condition on existing endpoints, no field or status-code changes to success paths.

Tests

  • TestHandleCreateProfile_RejectsDuplicateName — " main " vs seeded "Main" → 409, nothing created
  • TestHandleUpdateProfile_RejectsDuplicateNameOnRename — rename "Kids" → "MAIN" → 409, name unchanged
  • TestHandleUpdateProfile_AllowsKeepingOwnNameOnUpdate — self-name resave → 200

go test ./internal/api/handlers/ passes; go build ./..., go vet, gofmt clean.

Risks / follow-up

  • Existing duplicate profiles are untouched; the rule applies to new creates/renames only. Editing one of two existing "Laura"s without renaming still saves (self-exclusion); renaming it to "Laura" again is rejected.
  • Android surfaces the 409 message directly and pre-checks client-side (companion branch in silo-android); iOS/web inherit enforcement but may show their generic error text until they surface message.

No linked scope issue — small correctness fix out of device QA rather than a capability epic.

AI-use disclosure

Implemented with Claude Code (Claude Fable 5); reviewed and test-verified locally.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Enforced profile-name uniqueness ignoring case and leading/trailing spaces.
    • Profile creation/renaming to a conflicting name now returns a conflict error (409, name_conflict).
    • Profile names that are empty after trimming whitespace are rejected (400).
    • When a name is provided, the stored value is the trimmed version.
  • Tests

    • Added coverage for duplicate detection, whitespace trimming, and update scenarios (including keeping the same name on rename).

Profile create and rename accepted any name, so one account could hold
unlimited profiles all called "Laura" (every client allowed it too).
Reject a create or rename whose trimmed, case-insensitive name matches
another profile on the same account with 409 name_conflict. Scoping is
per account by construction — the check runs against a single user's
profile store, so different accounts can still each have a "Laura".

Renames exclude the profile being updated, so re-saving a profile under
its own name (e.g. avatar-only edits that resubmit the name) still works.

Also reject whitespace-only names on create and rename; a name of " "
previously passed the blank check.

Additive-only per the v1 API rules: new 409 error code on existing
endpoints, following the profile_limit_reached pattern.

Co-Authored-By: Claude Fable 5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8fbb823a-7ac7-426f-b0e0-3ae91e2bc3aa

📥 Commits

Reviewing files that changed from the base of the PR and between 564ce2e and 77dc373.

📒 Files selected for processing (2)
  • internal/api/handlers/profiles.go
  • internal/api/handlers/profiles_test.go

📝 Walkthrough

Walkthrough

Adds trimmed, case-insensitive profile-name validation on create and update, returning HTTP 409 name_conflict for collisions and storing normalized names. Tests cover duplicate rejection, trimming, and allowed updates that keep the current name.

Changes

Profile name conflict enforcement

Layer / File(s) Summary
Conflict detection helper
internal/api/handlers/profiles.go
Adds strings import and profileNameConflicts helper comparing trimmed, case-insensitive names while excluding a given profile ID.
Create-profile validation and conflict check
internal/api/handlers/profiles.go, internal/api/handlers/profiles_test.go
Requires trimmed non-empty name on create, rejects duplicate names with 409 name_conflict, stores the trimmed name, and adds tests for duplicate rejection and trimming.
Update-profile validation and conflict check
internal/api/handlers/profiles.go, internal/api/handlers/profiles_test.go
Requires trimmed non-empty name on update, rejects duplicate names excluding the current profile with 409 name_conflict, and adds tests for rename rejection and allowed no-op renames.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: enforcing per-account profile name uniqueness in profiles.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f944063fcc

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

}
}

if profileNameConflicts(existingProfiles, req.Name, "") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Make profile name uniqueness atomic

When two profile creates (or renames) for the same account arrive concurrently with the same trimmed/case-folded name, both requests can pass this ListProfiles snapshot before either write commits, and then both writes succeed because the SQLite/Postgres profile stores only enforce the profile ID primary key, not normalized-name uniqueness. That leaves duplicate names despite the new name_conflict contract; the conflict check needs to be enforced in the store transaction or with a database constraint/unique index so one of the concurrent requests fails.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed the snapshot check isn't atomic — two concurrent same-name writes on one account can both pass it, since neither store constrains normalized names. Keeping this PR at the API layer (it fixes the user-facing bug and grandfathers existing duplicates safely) and tracking store-level enforcement in both backends as #343, including the typed conflict error and the pre-existing-duplicates migration concern.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/api/handlers/profiles.go`:
- Around line 363-372: Trim the profile name before persisting it so storage
matches the conflict check behavior. In the profile handler code around the
create/update paths (including the logic using profileNameConflicts), normalize
req.Name with whitespace trimming before assigning it to the stored profile
object, and make sure both the create flow and the update flow use the trimmed
value consistently so names like " main " are not saved with extra spaces.
- Around line 363-372: The name uniqueness check in CreateProfile/UpdateProfile
is only a handler pre-check, so concurrent writes can still create duplicate
profile names. Move the invariant into the store/DB layer behind the profiles
persistence path used by CreateProfile and UpdateProfile, enforcing a trimmed
case-insensitive unique name per user with a unique index or equivalent atomic
conflict handling, and keep the handler’s profileNameConflicts check only as a
fast early validation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 9cb4ad21-1067-4139-ace1-9c14f2753f36

📥 Commits

Reviewing files that changed from the base of the PR and between f330d55 and f944063.

📒 Files selected for processing (2)
  • internal/api/handlers/profiles.go
  • internal/api/handlers/profiles_test.go

Comment thread internal/api/handlers/profiles.go Resolved
The conflict check compared trimmed names but create/rename persisted
the raw input, so " Laura " could land with stray whitespace and render
inconsistently. Normalize to the trimmed form before storage on both
paths. Addresses the CodeRabbit review finding on PR Silo-Server#342.

Co-Authored-By: Claude Fable 5 <[email protected]>
@RXWatcher

Copy link
Copy Markdown
Contributor Author

Pushed 564ce2e addressing the CodeRabbit finding: names are now normalized to their trimmed form before storage on both create and rename, with a test pinning it (TestHandleCreateProfile_TrimsStoredName). The Codex atomicity point is tracked as #343.

Also document the check-then-write race in profileNameConflicts: the
userstore backends carry no unique index on name, so concurrent creates
can still race past the guard, same as profile_limit_reached.

Co-Authored-By: Claude Fable 5 <[email protected]>
@Quick104

Quick104 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Pushed 77dc373 addressing the remaining review items:

  • Whitespace-only name tests: create and update both had the trim-then-reject behavior but no coverage; added TestHandleCreateProfile_RejectsWhitespaceOnlyName and TestHandleUpdateProfile_RejectsWhitespaceOnlyName (400, nothing created/changed).
  • Rename trim coverage: TestHandleUpdateProfile_TrimsStoredNameOnRename locks in that " Laura " persists as "Laura" on rename, matching the create-side test added in 564ce2e.
  • TOCTOU documented: profileNameConflicts now notes the check-then-write race (no store-level unique index across the Postgres/SQLite userstore backends) and that a functional unique index is the fix if interactive-use assumptions ever stop holding — recorded rather than rediscovered later.

gofmt clean, go vet clean, go test ./internal/api/handlers/ passes.

@Quick104
Quick104 merged commit 93c525c into Silo-Server:main Jul 8, 2026
1 check was pending
RXWatcher added a commit to Silo-Server/silo-android that referenced this pull request Jul 8, 2026
A single account could create unlimited identically-named profiles.
Silo-Server/silo-server#342 enforces per-account uniqueness server-side
(409 name_conflict); this is the client half:

- Shared List<Profile>.hasProfileNamed mirrors the server rule (trimmed,
  case-insensitive, per account) so create/edit forms on phone and TV
  fail fast before the network round-trip — and still guard against
  servers that predate the rule. Edit excludes the profile being
  renamed, so re-saving under your own name works.
- Create and edit VMs on both platforms surface the server's error
  message (name_conflict, profile_limit_reached) instead of generic
  "Failed to create/update profile".

Co-Authored-By: Claude Fable 5 <[email protected]>
Quick104 added a commit that referenced this pull request Jul 10, 2026
* fix(profiles): enforce per-account profile name uniqueness

Profile create and rename accepted any name, so one account could hold
unlimited profiles all called "Laura" (every client allowed it too).
Reject a create or rename whose trimmed, case-insensitive name matches
another profile on the same account with 409 name_conflict. Scoping is
per account by construction — the check runs against a single user's
profile store, so different accounts can still each have a "Laura".

Renames exclude the profile being updated, so re-saving a profile under
its own name (e.g. avatar-only edits that resubmit the name) still works.

Also reject whitespace-only names on create and rename; a name of " "
previously passed the blank check.

Additive-only per the v1 API rules: new 409 error code on existing
endpoints, following the profile_limit_reached pattern.

Co-Authored-By: Claude Fable 5 <[email protected]>

* fix(profiles): store the trimmed profile name

The conflict check compared trimmed names but create/rename persisted
the raw input, so " Laura " could land with stray whitespace and render
inconsistently. Normalize to the trimmed form before storage on both
paths. Addresses the CodeRabbit review finding on PR #342.

Co-Authored-By: Claude Fable 5 <[email protected]>

* test(profiles): cover whitespace-only rejection and rename trimming

Also document the check-then-write race in profileNameConflicts: the
userstore backends carry no unique index on name, so concurrent creates
can still race past the guard, same as profile_limit_reached.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: rxwatcher <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
Co-authored-by: Quick <[email protected]>
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.

2 participants