Skip to content

fix(profiles): store requested PIN atomically on creation - #2708

Merged
Quick104 merged 4 commits into
mainfrom
fix/c08-atomic-profile-pin-creation
Oct 20, 2026
Merged

Quick104 merged 4 commits into
mainfrom
fix/c08-atomic-profile-pin-creation

Conversation

@Quick104

@Quick104 Quick104 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #2707
Related issue: #1179
Validation tasks: changes #1180 C1

Creating a profile with a PIN commits an unlocked row before storing its hash. A concurrent household caller can select that row without proof. Through v1, a PIN longer than bcrypt's 72-byte limit also leaves the profile unlocked after the request fails.

The requested PIN now protects the profile from its first committed state, and invalid input leaves no profile behind.

Approach

Hash the PIN before opening the existing profile/preferences transaction and include it in the initial insert. Reject overlong PINs in the shared handler. V1 returns 400 bad_request; v2 retains its existing 422 validation_failed response. Successful contracts stay the same.

Apple and Android already send the PIN in the create request. Jellyfin reads the same stored hash. No client changes are needed. The user manual already promises this protection; developer API documentation records the atomicity and byte limit.

Validation

  • Live synthetic requests on an isolated development deployment reproduced the persistent v1 failure at 365e3d436fc2 and confirmed the fix at 2012545299d5. Invalid ASCII/UTF-8 inputs leave no row; valid PINs deny missing proof, reject wrong PINs, and accept correct proof. Intentional PIN-less profiles remain unlocked.
  • The transient window was reproduced with a deterministic test against a real SQLite transaction, rather than a live HTTP race. Before-fix tests fail; focused race tests pass at the final head, including creation authorization, bootstrap, and settings rollback controls.
  • Go build, vet, formatting, changed-line lint, contract and repository hygiene checks pass. Web lint, formatting, build, and bundle budget pass. Prometheus rules pass with promtool 3.5.0 using a cached image; the local image pull failed DNS resolution.
  • The full web suite passes on re-run (896 files, 8,367 tests). Four initial failures also pass in focused checks on clean main and this head.
  • The full Go suite fails six cases also failing on clean main: system-resource capabilities, two Jellyfin playback-planning cases, and three NFO filesystem cases. Two additional initial playback timing/probe failures pass on focused main/head rechecks. The complete touched handler package recheck passes.
  • Multi-profile / PIN / kids — Server #1180 C1 (PIN enforcement) needs a minor re-check of creating a PIN profile after merge. The client C3 cases (remembered PIN profile relaunch and protected-profile switching) don't create profiles and keep the same token checks, so they are not listed. Direct validation tasks were read; the board could not be enumerated because the token lacks read:project. No human acceptance or board update is claimed.
  • Review check on Postgres: a local server build against a throwaway database showed, at the base commit, all 15 of 15 PIN creates visible as committed rows without a PIN hash (polled every 5 ms) and overlong v1 PINs answering 500 with an unlocked profile left behind. At this branch: none of 15 visible without a hash, overlong v1 PINs answer 400 with no row, v2 answers 422, and new PIN profiles deny access without proof and accept the right PIN.

Evidence

Evidence: https://evidence.siloserver.org/r/silo-server/issue-2707/

Risks

New protected profiles start with PIN revision 0. Fresh profile IDs and the existing account/session/profile/revision binding keep proofs scoped correctly; subsequent PIN changes retain revision invalidation.

PR #2704 separately guards household authority freshness. Its patch applies cleanly over this change; preserve its transaction guard when integrating either order. This PR does not include that fix.

The show_forced_subtitles: false write in the same function still runs after the create transaction commits. It predates this PR and is tracked in #2743, whose fix should follow this one.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: T3 Code through the Codex harness; Claude Agent (claudeAgent) for cross-provider reviews.
  • Tool(s): Codex, Claude Agent, T3 delegated reviews, GitHub CLI, Modern Go Guidelines CLI, Go test, Silo sandbox and evidence tools.
  • Model(s): gpt-6.1-sol (xhigh) for implementation and independent code review; claude-opus-5-5 (high) for plan and adversarial review.
  • Involvement: Fully AI-generated; no human verification claimed.
  • Adversarial review: claude-opus-5-5 reproduced the failing tests and checked the plan, including initial PIN revision safety and both stores; no blocking objections. Independent gpt-6.1-sol reviews checked callers, contracts, failure boundaries, tests, and validation impact with no findings at 67cc03455; final 201254529 only flattens test assertions. Background claude-opus-5-5 review checked the complete final diff at 201254529, traced transaction/revision/contract behavior through both stores, and passed focused race tests. It found no defects introduced by this change and posted nothing.
  • Review: /silo review with Claude Code (claude-opus-5-5) and Claude subagents, plus a read-only Codex pass (gpt-6.1-sol, xhigh). It traced both stores, token revision checks, the v1/v2 contract, and the tests. One low finding (a test assertion that only exercised bcrypt) was fixed in 196fbdf; Codex reported no findings. A pre-existing partial write was filed as [bug] Profile creation with show_forced_subtitles false can return 500 after committing the profile #2743.

@silo-triage silo-triage Bot added the size/M 50–199 added + deleted lines (all files); change volume, not priority. label Oct 19, 2026
@silo-kody

silo-kody Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@silo-triage silo-triage Bot added bug Something isn't working go Pull requests that update go code impact: security Auth, privacy, or permission boundary priority: P0 Fix/merge first: data-loss, security, or widespread breakage labels Oct 19, 2026
@macroscopeapp

macroscopeapp Bot commented Oct 19, 2026

Copy link
Copy Markdown

The Validation tasks line overstates the impact. #1180 C1 records PIN-proof and creation-authorization checks; the client C3 results cover reopening or switching existing PIN-protected profiles. A valid PIN is persisted before the create response both before and after this diff, while none of these case steps races profile creation or submits a PIN over 72 bytes. The verification and selection paths remain unchanged. The tvOS C2 failure on main is separately recorded as passing with Apple PR #722; this server diff does not affect it.

Suggested replacement: Validation tasks: none

Automated check: Macroscope check run agent (gpt-6-luna). No validation was performed.

Posted via Macroscope — v1 validation impact

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dc18c6fa-421a-4681-9401-885af11e29eb
📥 Commits

Reviewing files that changed from the base of the PR and between 2012545 and 196fbdf.

📒 Files selected for processing: 1
  • internal/api/handlers/profiles_create_pin_test.go
💤 Files with no reviewable changes: 1
  • internal/api/handlers/profiles_create_pin_test.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

📝 Walkthrough

Profile creation now validates a requested PIN’s byte length and hashes it before writing the profile. The profile and preferences are committed with the PIN hash. Tests check PIN-protected and PIN-less profiles, and the API documentation describes the behavior.

Changes: Profile PIN creation

Layer / File(s) Summary
Validate and store PIN during creation
internal/api/handlers/profiles.go, internal/api/handlers/profiles_create_pin_test.go, docs/auth-api.md
CreateProfile rejects PINs over 72 bytes and stores the bcrypt hash in the initial profile write. Tests cover committed profile state and byte limits. The API documentation describes version-specific errors and PIN-less creation.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant CreateProfile
  participant bcrypt
  participant ProfileAndPreferencesTransaction
  Client->>CreateProfile: Submit profile and optional PIN
  CreateProfile->>bcrypt: Hash supplied PIN
  bcrypt-->>CreateProfile: Return PIN hash
  CreateProfile->>ProfileAndPreferencesTransaction: Write profile with PIN hash and preferences
  ProfileAndPreferencesTransaction-->>Client: Commit profile creation
Loading
Merge Risk: ⚪ Minimal · up to 196fb

No actionable issue identified in the selected change; it is mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: storing the requested profile PIN atomically during creation.
Description check Passed The description is directly related to the changeset and explains the problem, implementation, validation, risks, and scope.
Linked Issues check Passed The changes meet the coding requirements in directly linked issue #2707. CreateProfile validates the 72-byte PIN limit before writes, hashes a supplied PIN before the transaction, and stores `PINHas…
Out of Scope Changes check Passed The changes stay within #2707. The handler change closes the initial unlocked state and the invalid-input persistence failure. The tests verify these behaviors. The documentation describes the changed…
Full details: Linked Issues check

Explanation

The changes meet the coding requirements in directly linked issue #2707. CreateProfile validates the 72-byte PIN limit before writes, hashes a supplied PIN before the transaction, and stores PINHash in the initial profile insert. The tests cover first-commit protection, preferences visibility, PIN-less behavior, and invalid-input persistence for v1. The shared handler covers v1 and v2; the documentation records the v2 response contract.

Full details: Out of Scope Changes check

Explanation

The changes stay within #2707. The handler change closes the initial unlocked state and the invalid-input persistence failure. The tests verify these behaviors. The documentation describes the changed API behavior. No unrelated implementation or client change is shown.

  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@Quick104 Quick104 left a comment

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.

Review of 2012545 (PIN in the create transaction, v1 byte limit, docs, tests). One low-severity test finding, inline. I'm fixing it in a commit on this branch, so there's no need to change it yourself.

I found no defect in the production change. Both stores already insert pin_hash from the profile, so the hash is in the first committed row on Postgres and SQLite. A PIN profile now starts at PIN revision 0 instead of 1; token checks compare the revision for equality and the profile ID is new, so no earlier proof can match it. Dropping the follow-up UpdateProfile also drops a Postgres access_policy_revision bump, which only invalidated the household's existing theme-song grants as a side effect. The summary comment follows once the fix is pushed and checked on a sandbox.

AI disclosure: reviewed with Claude Code (claude-opus-5-5) and Claude subagents, with an independent read-only pass by Codex (gpt-6.1-sol, xhigh), which reported no findings.

@silo-kody

silo-kody Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@Quick104

Quick104 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Review summary

Fit: 9/10 · Readiness: 9/10 — ready

Reviewed head: 196fbdf (findings reviewed at 2012545, plus the one test fix below).

A profile created with a PIN is now protected from its first committed state, and an overlong v1 PIN no longer leaves an unlocked profile behind. I found no defect in the production change.

Findings

  • Low, test value: an assertion in TestCreateProfilePINProtectedAtFirstCommit only exercised bcrypt (a hash of "1234" never matches "wrong"). Removed in 196fbdf; the thread is resolved.

Verification

  • Live check on Postgres: a local server build against a throwaway database. At the base commit (365e3d4), 15 of 15 PIN creates were visible as committed rows with an empty PIN hash while polling every 5 ms, and v1 creates with a 73-byte ASCII PIN or a 74-byte UTF-8 PIN answered 500 and left unlocked profiles. At this branch: 0 of 15 seen without a hash, both overlong v1 PINs answer 400 bad_request with no row, and v2 answers 422 validation_failed. New PIN profiles on v1 and v2 refuse access without proof (403), answer valid: false for a wrong PIN, and accept the right PIN's token. A profile created without a PIN stays unlocked.
  • The two new tests fail on the pre-fix code (778198d) for the intended reasons and pass at head. Focused handler tests, go vet, and make lint-changed pass.
  • Both stores insert pin_hash from the profile, so SQLite and Postgres behave the same. New PIN profiles start at PIN revision 0 instead of 1. Token checks compare the revision for equality and the profile ID is new, so no earlier proof can match. Dropping the follow-up update also drops a Postgres access_policy_revision bump that only invalidated the household's existing theme-song grants.
  • An independent read-only Codex review (gpt-6.1-sol) reported no findings and agreed on the validation line below.

Remaining notes (not blocking)

Validated features

Validation tasks: changes #1180 C1 (updated in the PR body). #1180 C1 covers PIN enforcement for profile changes; creating a PIN profile is part of that path, and every recorded C1 outcome still holds, so it needs only a minor re-check. The client C3 cases (#1181, silo-apple#322 and #323, silo-android#332 and #333) relaunch a remembered PIN profile and switch between protected profiles. They don't create profiles, and the token checks they rely on are unchanged, so I removed them from the line. This agrees with the bot's reading for C3. For C1 I kept a minor re-check rather than none, because C1 exercises PIN enforcement on profiles that a manager creates through this path. No validated case regresses.

Other checks

  • User manual: no change needed. The manual describes PIN prompts and Jellyfin password#PIN sign-in but says nothing about PIN length or creation, and this fix makes behavior match what it implies.
  • Apple and Android: no change needed. Both already send the PIN in the create request, and successful responses are unchanged.
  • Jellyfin compatibility: it has no profile-creation path and reads the same stored hash.
  • API contract: the v1 change from 500 to 400 for an overlong PIN fits the bridge-window critical-fix rule; the request never succeeded before. v2 is unchanged.

Scores

  • Fit 9/10: a security fix in the core household profile experience, at the shared create handler both API versions use, with no new surface or setting.
  • Readiness 9/10: the bug was reproduced on Postgres before and gone after, the tests fail before and pass after, and the only finding is fixed. CI on 196fbdf is green.

AI disclosure: review by Claude Code (claude-opus-5-5) with Claude subagents and an independent read-only Codex pass (gpt-6.1-sol, xhigh). The fix commit was written by claude-opus-5-5.

@Quick104
Quick104 merged commit 387c497 into main Oct 20, 2026
18 checks passed
@Quick104
Quick104 deleted the fix/c08-atomic-profile-pin-creation branch October 20, 2026 01:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code impact: security Auth, privacy, or permission boundary priority: P0 Fix/merge first: data-loss, security, or widespread breakage size/M 50–199 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Profile creation can leave a requested PIN unprotected

1 participant