-
Notifications
You must be signed in to change notification settings - Fork 24
fix: consume device code atomically to prevent race in RFC 8628 token exchange #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -126,6 +126,15 @@ func (s *Server) handleDeviceToken(w http.ResponseWriter, r *http.Request) { | |
| }) | ||
|
|
||
| case "approved": | ||
| // Atomically consume the device code before issuing the token. | ||
| // This prevents a second poll (or a race) from minting a second | ||
| // long-lived token for the same browser approval. | ||
| if err := s.store.ConsumeDeviceCode(dc.DeviceCode); err != nil { | ||
| // Another concurrent request already consumed it. | ||
| writeJSON(w, http.StatusGone, errorResponse("expired_token", "device code is no longer valid")) | ||
| return | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The handler maps every Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| // Generate a long-lived provider token. | ||
| tokenBytes := make([]byte, 32) | ||
| if _, err := rand.Read(tokenBytes); err != nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consuming the device code before generating and persisting the provider token makes the flow non-recoverable on downstream failures: if
rand.ReadorCreateProviderTokenfails, the code is already markedconsumed, so subsequent polls cannot succeed and the user must restart login. This introduces a new failure mode under transient entropy/DB issues and should be handled atomically (consume + token create in one transaction) or with rollback semantics.Useful? React with 👍 / 👎.