Fix: Improve auth flow and state validation (#4786)

Improves the authentication flow and state validation process.  We no longer reset the auth nounce after each sign in as AuthService is a singleton and there is no risk of nonce collision between different users as only one user can be signed in at a time.

Changes:
    - The `authNonce` is now generated once during `AuthService` instantiation and stored as a read-only property. This ensures that the nonce remains consistent throughout the authentication process.
    - The `resetAuthNonce` method has been removed, as the nonce is no longer meant to be reset.
    - The `createAuthRequest` method now uses the URL object for more graceful query construction.
- **Controller:**
    - The `validateAuthState` method has been simplified to directly compare the provided state with the stored `authNonce`.
- **Extension:**
    - The extension now prompts the user for confirmation if the state parameter in the auth callback does not match the stored `authNonce`. This allows sign-ins initiated from outside the extension (e.g., Cline web) to be handled correctly.

Issue: The issue is that the authNonce is being reset in the validateAuthState method in the Controller, but the extension.ts is directly accessing authService.authNonce without going through the validation method. This creates a race condition where:

User initiates auth, nonce is generated
Auth callback comes back with the state
If there are multiple auth attempts or the callback is processed multiple times, the nonce might be reset before the validation in extension.ts happens
User gets "Invalid auth state" error

Co-authored-by: abeatrix <beatrix@cline.bot>
This commit is contained in:
Beeandabeatrix authored and GitHub committed 2025-07-10 21:53:36 +05:30
1 parent d0069eb7cb
commit 190d3bd2dc
3 files changed
+33 -36

No files matched your search

+1 -6
View File
@@ -458,12 +458,7 @@ export class Controller {
// Auth
public async validateAuthState(state: string | null): Promise<boolean> {
const storedNonce = this.authService.authNonce
if (!state || state !== storedNonce) {
return false
}
this.authService.resetAuthNonce() // Clear the nonce after validation
return true
return state === this.authService.authNonce
}
async handleAuthCallback(customToken: string, provider: string | null = null) {