Skip to content

fix(auth-app): skip expired tokens before bcrypt and purge on load - #11998

Closed
paul43210 wants to merge 1 commit into
owncloud:masterfrom
paul43210:fix/auth-app-token-validation-perf
Closed

paul43210 wants to merge 1 commit into
owncloud:masterfrom
paul43210:fix/auth-app-token-validation-perf

Conversation

@paul43210

Copy link
Copy Markdown
Contributor

Summary

  • Skip expired tokens before bcrypt in GetAppPassword — avoids expensive (~200ms each) bcrypt comparisons on tokens that are already expired
  • Purge expired tokens on file load and during token creation — prevents unbounded accumulation of expired tokens in appauth.json
  • Use sync.RWMutex instead of sync.Mutex — allows concurrent read operations (GetAppPassword, ListAppPasswords) without blocking each other

Problem

The GetAppPassword function in the JSON appauth manager performs a linear scan with bcrypt.CompareHashAndPassword over every stored token for a user. On a production system, one user accumulated 1,123 tokens in appauth.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, GenerateAppPassword creates 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:

  1. GetAppPassword calls bcrypt.CompareHashAndPassword on every token (including expired ones) before checking expiration
  2. A global sync.Mutex blocks all concurrent operations during the entire scan
  3. No code path ever removes expired tokens from the JSON file

Changes

File: vendor/github.com/owncloud/reva/v2/pkg/appauth/manager/json/json.go

  1. GetAppPassword: Check isExpired() before calling bcrypt; use RLock for the scan phase and only acquire write lock for the Utime update
  2. loadOrCreate: Call purgeExpiredTokens() after loading the JSON file to clean up on startup
  3. GenerateAppPassword: Call purgeExpiredUserTokens() to opportunistically clean up when creating new tokens
  4. ListAppPasswords: Use RLock instead of Lock (read-only operation)
  5. New helpers: isExpired(), purgeExpiredUserTokens(), purgeExpiredTokens()

File: vendor/github.com/owncloud/reva/v2/pkg/appauth/manager/json/json_test.go (new)

  • Tests for expired token skipping, valid token validation, nil/zero expiration handling, purge on load, purge during token creation, and isExpired unit tests

Test plan

  • Unit tests pass (go test -mod=vendor -v ./vendor/github.com/owncloud/reva/v2/pkg/appauth/manager/json/)
  • Full oCIS binary builds successfully
  • Deployed and verified on test server — service starts cleanly with no errors
  • Verify auth-app token validation works in web UI
  • Verify token creation still works
  • Verify expired tokens are cleaned up after restart

🤖 Generated with Claude Code

@update-docs

update-docs Bot commented Feb 7, 2026

Copy link
Copy Markdown

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]>
@paul43210
paul43210 force-pushed the fix/auth-app-token-validation-perf branch from 713479d to 693099a Compare February 8, 2026 20:53
@sonarqubecloud

sonarqubecloud Bot commented Feb 8, 2026

Copy link
Copy Markdown

// 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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@jvillafanez

Copy link
Copy Markdown
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants