Add startup validation for TOTPHandler UsedCodes misconfiguration#259
Merged
Conversation
Copilot created this pull request from a session on behalf of
veverkap
May 11, 2026 17:43
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a startup-time validation hook to TOTPHandler so deployments fail fast when the replay-protection cache (UsedCodes) is not configured, avoiding runtime panics on enroll/verify paths.
Changes:
- Added
(*TOTPHandler).Validate()that errors whenUsedCodesis nil. - Added unit tests covering the new validation contract.
- Updated README + TOTP handler docs to show calling
Validate()at startup.
Show a summary per file
| File | Description |
|---|---|
| README.md | Documents calling TOTPHandler.Validate() during startup to catch misconfiguration early. |
| handler/totp.go | Introduces Validate() to enforce non-nil UsedCodes. |
| handler/totp_test.go | Adds tests asserting Validate() fails when UsedCodes is nil and succeeds when configured. |
| docs/handler/totp.md | Updates TOTP handler documentation example to include startup validation. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 4/4 changed files
- Comments generated: 0
veverkap
reviewed
May 11, 2026
Contributor
veverkap
left a comment
There was a problem hiding this comment.
Fixed: added nil checks for h.TOTP and h.Users in Validate() so all three required dependencies are caught at startup before any request handler can panic.
This was referenced May 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug Fix
TOTP lacked the same startup validation guard used by other handlers. If
UsedCodeswas nil, replay-protection paths (isReplay,recordUsed) could panic at runtime instead of failing fast at boot.What was the bug?
TOTPHandlerhad noValidate()method, so a missingUsedCodesdependency was only discovered when handling enroll/verify requests.How did you fix it?
Added
TOTPHandler.Validate()to enforce non-nilUsedCodeswith a clear startup error:TOTPHandler misconfigured: UsedCodes is requiredh.Validate()at startup, matching other handlers’ configuration pattern.Testing
Added focused unit coverage for the new validation contract:
UsedCodesis nilUsedCodesis configuredGreptile Summary
This PR adds
TOTPHandler.Validate()to catch missing dependencies (TOTP,Users,UsedCodes) at server startup rather than panicking on the first request. Documentation and README examples are updated to callh.Validate()after construction, matching the pattern already used by other handlers in the codebase.handler/totp.go: NewValidate()method checks all three required fields and returns a descriptive error for each, consistent with the existing nil-guard panics inisReplayandrecordUsed.handler/totp_test.go: Adds two tests covering theUsedCodes == nilerror case and the fully-configured happy path, but leaves theTOTP == nilandUsers == nilbranches untested.README.md/docs/handler/totp.md: Updated quickstart snippets to include the startupValidate()call.Confidence Score: 5/5
Safe to merge — the new Validate() method correctly guards all three required fields and the change is additive with no effect on existing request-handling paths.
The implementation is correct and consistent with the existing nil-panic guards already in place. The only gap is that test cases for the nil-TOTP and nil-Users branches were not added alongside the nil-UsedCodes test, leaving those two code paths without direct coverage.
handler/totp_test.go — the two new Validate() error branches (nil TOTP, nil Users) have no corresponding test cases.
Important Files Changed
Validate()checking all three required fields (TOTP,Users,UsedCodes); implementation is correct and consistent with the panic guards already present inisReplay/recordUsed.UsedCodes == niland happy-path cases; nil-TOTP and nil-Users guards introduced in this PR have no corresponding test cases.h.Validate()startup call to the TOTP setup example, matching the pattern used by other handlers.h.Validate()call to the docs quickstart snippet; no issues.Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[Server startup] --> B["h := &TOTPHandler{...}"] B --> C["h.Validate()"] C --> D{TOTP == nil?} D -- yes --> E["return error: TOTP is required"] D -- no --> F{Users == nil?} F -- yes --> G["return error: Users is required"] F -- no --> H{UsedCodes == nil?} H -- yes --> I["return error: UsedCodes is required"] H -- no --> J[return nil — handler is ready] J --> K[Register HTTP routes]Prompt To Fix All With AI
Reviews (2): Last reviewed commit: "fix(totp): validate TOTP and Users depen..." | Re-trigger Greptile