-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix(auth): avoid stale runtime state when re-adding disabled auth files #1945
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
Open
excelwang
wants to merge
3
commits into
router-for-me:main
Choose a base branch
from
excelwang:pr/upstream-auth-remove-readd-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−1
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import ( | |
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/router-for-me/CLIProxyAPI/v6/internal/config" | ||
|
|
@@ -127,3 +128,72 @@ func TestDeleteAuthFile_FallbackToAuthDirPath(t *testing.T) { | |
| t.Fatalf("expected auth file to be removed from auth dir, stat err: %v", errStat) | ||
| } | ||
| } | ||
|
|
||
| func TestDeleteAuthFile_AllowsReaddingSameNameWithoutStaleState(t *testing.T) { | ||
| t.Setenv("MANAGEMENT_PASSWORD", "") | ||
| gin.SetMode(gin.TestMode) | ||
|
|
||
| authDir := t.TempDir() | ||
| fileName := "codex-readd.json" | ||
| filePath := filepath.Join(authDir, fileName) | ||
| fileData := []byte(`{"type":"codex","email":"[email protected]"}`) | ||
| if errWrite := os.WriteFile(filePath, fileData, 0o600); errWrite != nil { | ||
| t.Fatalf("failed to write auth file: %v", errWrite) | ||
| } | ||
|
|
||
| store := &memoryAuthStore{} | ||
| manager := coreauth.NewManager(store, nil, nil) | ||
| authID := fileName | ||
| if _, errRegister := manager.Register(context.Background(), &coreauth.Auth{ | ||
| ID: authID, | ||
| FileName: fileName, | ||
| Provider: "codex", | ||
| Metadata: map[string]any{"type": "codex", "email": "[email protected]"}, | ||
| Attributes: map[string]string{ | ||
| "path": filePath, | ||
| }, | ||
| ModelStates: map[string]*coreauth.ModelState{ | ||
| "gpt-5.3-codex": { | ||
| Status: coreauth.StatusError, | ||
| Unavailable: true, | ||
| NextRetryAfter: time.Now().Add(30 * time.Minute), | ||
| }, | ||
| }, | ||
| }); errRegister != nil { | ||
| t.Fatalf("register auth: %v", errRegister) | ||
| } | ||
|
|
||
| h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, manager) | ||
| h.tokenStore = store | ||
|
|
||
| deleteRec := httptest.NewRecorder() | ||
| deleteCtx, _ := gin.CreateTestContext(deleteRec) | ||
| deleteReq := httptest.NewRequest(http.MethodDelete, "/v0/management/auth-files?name="+url.QueryEscape(fileName), nil) | ||
| deleteCtx.Request = deleteReq | ||
| h.DeleteAuthFile(deleteCtx) | ||
|
|
||
| if deleteRec.Code != http.StatusOK { | ||
| t.Fatalf("expected delete status %d, got %d with body %s", http.StatusOK, deleteRec.Code, deleteRec.Body.String()) | ||
| } | ||
| if _, ok := manager.GetByID(authID); ok { | ||
| t.Fatal("expected auth to be fully removed from manager") | ||
| } | ||
|
|
||
| if errWrite := os.WriteFile(filePath, fileData, 0o600); errWrite != nil { | ||
| t.Fatalf("failed to recreate auth file: %v", errWrite) | ||
| } | ||
| if errRegister := h.registerAuthFromFile(context.Background(), filePath, fileData); errRegister != nil { | ||
| t.Fatalf("re-register auth from file: %v", errRegister) | ||
| } | ||
|
|
||
| updated, ok := manager.GetByID(authID) | ||
| if !ok || updated == nil { | ||
| t.Fatal("expected re-added auth to exist") | ||
| } | ||
| if updated.Disabled || updated.Status == coreauth.StatusDisabled { | ||
| t.Fatalf("expected re-added auth to be active, got disabled=%v status=%s", updated.Disabled, updated.Status) | ||
| } | ||
| if len(updated.ModelStates) != 0 { | ||
| t.Fatalf("expected stale model state to be cleared, got %#v", updated.ModelStates) | ||
| } | ||
| } | ||
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
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
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
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.
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.
Calling
m.store.Delete(ctx, id)here assumes auth IDs are valid delete keys, but file-backed auth IDs can contain path separators (e.g. nested IDs liketeam/user.json). InFileTokenStore.resolveDeletePath, any ID containing a separator is treated as a direct filesystem path rather than being rooted underAuthDir, so removal can target the wrong location and leave the actual auth file undeleted (or delete an unrelated relative path) when an auth is removed and later reloaded. This regression is introduced by the newRemovepersistence path and is triggered whenever removed auth IDs are not simple basename IDs.Useful? React with 👍 / 👎.