Repository navigation
Conversation
|
Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would create a changelog item based on your changes. |
The JSON appauth manager performed a linear scan with bcrypt comparison over every stored token, including expired ones. With many accumulated tokens this caused token validation to take minutes and block all auth-app operations due to a global mutex. - Skip expired tokens before calling bcrypt in GetAppPassword - Purge expired tokens on startup and persist cleaned state to disk - Purge expired user tokens during token creation - Use sync.RWMutex to allow concurrent read operations - Add unit tests for expiration handling and purge behavior Co-Authored-By: Claude Opus 4.6 <[email protected]>
713479d to
693099a
Compare
|
| // Purge expired tokens and persist the cleaned state. | ||
| manager.purgeExpiredTokens() | ||
| if err := manager.save(); err != nil { | ||
| return nil, errors.Wrap(err, "error saving purged tokens") |
There was a problem hiding this comment.
I assume that, if this happens, it will crash the service.
Technically, the service can run. There might not be expired token, or there could be just a few, but the service can crash because the manager failed to write the updated file. It seems too much.
I'd ignore this error. You can try to log the error message if possible.
| @@ -207,31 +215,44 @@ func (mgr *jsonManager) InvalidateAppPassword(ctx context.Context, password stri | |||
| } | |||
|
|
|||
| func (mgr *jsonManager) GetAppPassword(ctx context.Context, userID *userpb.UserId, password string) (*apppb.AppPassword, error) { | |||
There was a problem hiding this comment.
This will need an in-depth analysis.. not sure how much it will improve the situation.
- You're still traversing the list of passwords. While the list might be "small" due to the regular purging, a user could have a good bunch of passwords. I'd consider around 100 active passwords to be on the safe side.
- Previously, if you used an expired password, you might get the expired error quick if the password was at the beginning of the list. Now, you'll traverse the whole list. It's true that the list is expected to be smaller, but the behavior doesn't look good.
- The save operation is against the FS, which might take some time. This can delay other password operations. The result of this function might still take a while.
It will likely improve the speed, but it might still be slow on some scenarios.
| nowSec := uint64(time.Now().Unix()) | ||
| for hash, pw := range tokens { | ||
| if isExpired(pw, nowSec) { | ||
| delete(tokens, hash) |
There was a problem hiding this comment.
deleting items from a slice while traversing it is dangerous in a lot of languages. Please, provide official evidence that the operation is safe, because as far as I know, it isn't, and I'd prefer to avoid potential problems.
| for uid, tokens := range mgr.passwords { | ||
| for hash, pw := range tokens { | ||
| if isExpired(pw, nowSec) { | ||
| delete(tokens, hash) |
|
Although the issue needs to be solved, I'm closing the PR because the code changes need to be in https://github.com/owncloud/reva . Please, re-submit the PR there (preferably with the suggested changes). Thanks. |



Summary
GetAppPassword— avoids expensive (~200ms each) bcrypt comparisons on tokens that are already expiredappauth.jsonsync.RWMutexinstead ofsync.Mutex— allows concurrent read operations (GetAppPassword,ListAppPasswords) without blocking each otherProblem
The
GetAppPasswordfunction in the JSON appauth manager performs a linear scan withbcrypt.CompareHashAndPasswordover every stored token for a user. On a production system, one user accumulated 1,123 tokens inappauth.json. At ~200ms per bcrypt call (cost 11), token validation took minutes and effectively hung all auth-app operations due to the global mutex.Additionally,
GenerateAppPasswordcreates new tokens but never cleans up expired ones, and no other code path removes them either, so tokens accumulate indefinitely.Root Cause
In
vendor/github.com/owncloud/reva/v2/pkg/appauth/manager/json/json.go:GetAppPasswordcallsbcrypt.CompareHashAndPasswordon every token (including expired ones) before checking expirationsync.Mutexblocks all concurrent operations during the entire scanChanges
File:
vendor/github.com/owncloud/reva/v2/pkg/appauth/manager/json/json.goGetAppPassword: CheckisExpired()before calling bcrypt; useRLockfor the scan phase and only acquire write lock for theUtimeupdateloadOrCreate: CallpurgeExpiredTokens()after loading the JSON file to clean up on startupGenerateAppPassword: CallpurgeExpiredUserTokens()to opportunistically clean up when creating new tokensListAppPasswords: UseRLockinstead ofLock(read-only operation)isExpired(),purgeExpiredUserTokens(),purgeExpiredTokens()File:
vendor/github.com/owncloud/reva/v2/pkg/appauth/manager/json/json_test.go(new)isExpiredunit testsTest plan
go test -mod=vendor -v ./vendor/github.com/owncloud/reva/v2/pkg/appauth/manager/json/)🤖 Generated with Claude Code