Repository navigation
fix(legacy): prevent concurrent token refreshes from reusing a rotated token #194
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8895dfe
test: rotate refresh tokens in the mock auth server
pjcdawkins 7bb1941
fix(legacy): reload the session from storage before refreshing a token
pjcdawkins 61ea0c1
fix(legacy): use OS file locks in FileLock
pjcdawkins 75f7d25
fix(legacy): save the refreshed token before releasing the refresh lock
pjcdawkins f64d20e
fix(legacy): do not refresh a token without the refresh lock
pjcdawkins 0c99301
fix(legacy): hold the refresh lock until the middleware saves the token
pjcdawkins c667ee4
fix(legacy): require the refresh lock before using a stored token
pjcdawkins 565a780
chore(deps): update platformsh/client and platformsh/oauth2
pjcdawkins ba12621
refactor(legacy): remove token refresh workarounds fixed upstream
pjcdawkins e351aca
chore(deps): require platformsh/oauth2 1.0.0-beta4 or later
pjcdawkins 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "net/http/httptest" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/upsun/cli/pkg/mockapi" | ||
| ) | ||
|
|
||
| // TestAuthRefresh_Concurrent runs several processes that load the same expired session. | ||
| // Only one should use the refresh token: the others must use the token it saved. | ||
| func TestAuthRefresh_Concurrent(t *testing.T) { | ||
| authServer := mockapi.NewAuthServer(t) | ||
| defer authServer.Close() | ||
| // Keep the first refresh in progress while the other processes start. | ||
| authServer.SetRefreshDelay(2 * time.Second) | ||
| authServer.AddRefreshToken("initial-refresh-token") | ||
|
|
||
| apiHandler := mockapi.NewHandler(t) | ||
| apiHandler.SetMyUser(&mockapi.User{ID: "u1"}) | ||
| apiServer := httptest.NewServer(apiHandler) | ||
| defer apiServer.Close() | ||
|
|
||
| f := newCommandFactory(t, apiServer.URL, authServer.URL) | ||
| f.extraEnv = append(f.extraEnv, EnvPrefix+"TOKEN=") | ||
| // Initialize before running commands concurrently. | ||
| f.dir = t.TempDir() | ||
| getCommandName(t) | ||
| writeOAuthSession(t, f.home, "default", map[string]any{ | ||
| "accessToken": "expired-token", | ||
| "tokenType": "bearer", | ||
| "expires": time.Now().Add(-time.Hour).Unix(), | ||
| "refreshToken": "initial-refresh-token", | ||
| }) | ||
|
|
||
| const processes = 3 | ||
| type result struct { | ||
| stdout, stderr string | ||
| err error | ||
| } | ||
| results := make([]result, processes) | ||
| var wg sync.WaitGroup | ||
| for i := range processes { | ||
| wg.Go(func() { | ||
| stdout, stderr, err := f.RunCombinedOutput("auth:token", "--no-warn") | ||
| results[i] = result{stdout, stderr, err} | ||
| }) | ||
| } | ||
| wg.Wait() | ||
|
|
||
| for i, r := range results { | ||
| if assert.NoError(t, r.err, "process %d stderr: %s", i, r.stderr) { | ||
| assert.Equal(t, "access-token-1", strings.TrimSpace(r.stdout), "process %d", i) | ||
| } | ||
| } | ||
| assert.False(t, authServer.ReuseDetected(), "a rotated refresh token was reused") | ||
| } |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.