Skip to content

fix(appauth): key app passwords by a stable user id, not UserId.String() - #755

Closed
paul43210 wants to merge 2 commits into
owncloud:mainfrom
paul43210:fix/appauth-stable-user-key
Closed

paul43210 wants to merge 2 commits into
owncloud:mainfrom
paul43210:fix/appauth-stable-user-key

Conversation

@paul43210

Copy link
Copy Markdown

Summary

pkg/appauth/manager/json stores each user's app passwords under UserId.String(). That is the protobuf text format, and protobuf-go deliberately varies its whitespace between binaries (internal/detrand: one or two spaces between fields, chosen per binary) precisely so that nobody depends on it. The result: a bucket written by one build is not found by the next, and every existing app password silently stops working after an upgrade — still in the file, not expired, password not found.

Seen in production on 2026-09-30: an oCIS 8.0.0 build wrote idp:"…" opaque_id:"…" type:USER_TYPE_PRIMARY (two spaces); the 8.2.0 build looks up the one-space form. A service account's one-year token minted hours before the upgrade returned 401 right after it. The state file already held the same users under both key shapes from an earlier build change.

Changes

  • userKey(id) = <idp>|<opaque id> replaces every userID.String() lookup.
  • migrateLegacyKeys() runs once on load: buckets whose key parses as protobuf text (prototext.Unmarshal into UserId) are re-keyed, buckets that differ only in whitespace are merged, the file is rewritten. Existing tokens keep working across the upgrade that ships this.
  • Tests: existing tables now build state with userKey(...); new TestLegacyKeysAreMigrated loads a file holding one-space, two-space and already-stable keys for the same user and asserts all three tokens authenticate and list, only the stable key survives on disk, and new tokens land under it.

How tested

  • go test ./pkg/appauth/... — passes; TestLegacyKeysAreMigrated added.
  • golangci-lint run --new-from-rev=origin/main ./pkg/appauth/... with this repo's .golangci.yaml — no new findings (the Makefile's pinned v1.61.0 refuses to run against the current Go toolchain locally; CI will run its own).
  • Manual: reproduced the 401 on production against the unmodified store, confirmed both key shapes in appauth.json, confirmed the detrand branch in vendor/google.golang.org/protobuf/internal/encoding/text/encode.go.

Related

🤖 Generated with Claude Code

https://claude.ai/code/session_01WpoeSoKYEdgQcWjEzTVKpT

@paul43210
paul43210 requested a review from a team as a code owner October 1, 2026 01:28
@paul43210
paul43210 force-pushed the fix/appauth-stable-user-key branch from 9105c73 to 573cbc2 Compare October 1, 2026 01:28
@kw-security

kw-security commented Oct 1, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues
✅ Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@2403905 2403905 self-assigned this Oct 14, 2026
Comment thread pkg/appauth/manager/json/json.go Outdated
Comment on lines +124 to +126
if mgr.passwords[newKey] == nil {
mgr.passwords[newKey] = make(map[string]*apppb.AppPassword)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Mutating a map while ranging over it migrateLegacyKeys adds new keys to mgr.passwords while iterating over that same map rather than building the merged result in a separate map and swapping it in afterward. It is not a robust solution.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed. The startup migration is gone (see the top-level comment). The per-user replacement builds the merged bucket in a separate map and swaps it in after the loop (4d8cbe6).

Comment thread pkg/appauth/manager/json/json.go Outdated
defer mgr.Unlock()

mgr.purgeExpiredUserTokens(userID.String())
mgr.purgeExpiredUserTokens(userKey(userID))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could the uKey := userKey(userID) be called once and then the uKey?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done, uKey is computed once in each method (4d8cbe6).

@jvillafanez

Copy link
Copy Markdown
Member

Something to note is that the migration code will run on each startup, whether we want it or not. While it makes sense now, it won't in some months when all the keys have been migrated.

I think a better approach is, first, to start using the new format unconditionally, and second, when we check for a key, check the new format first and then the old format.
On the long run, the result is that successful checks aren't impacted, and wrong checks will be delayed (we'd need to check at least 2 different keys). While not a real brute force protection, it might be fine if wrong checks takes more time. We might not care if it takes 2ms or 2s more.

We still need to deal with the space difference. Do we have any data on the usage of the app-auth? As annoying as it is, the easiest solution is to let the users regenerate the app passwords if something goes wrong.
Alternatively, it might be better to move the migration code into a console command. Something like ocis auth-app format-keys that would reformat the keys, possible into different formats, not just yours but also any other future formats that could be considered more secure if needed. This idea will definitely require more planning though, but it won't be a one-and-done command.

paul43210 and others added 2 commits October 17, 2026 23:57
The json manager keyed each user's bucket by UserId.String(), the protobuf
text format. protobuf-go deliberately varies that output's whitespace from
one binary to the next (internal/detrand), so a bucket written by one build
was not found by the next and every existing app password silently stopped
working after an upgrade, although still present in the file and unexpired.
State files written by several builds held the same user under several keys.

Key buckets by <idp>|<opaque id>. On load, parse buckets stored under the
old text-format keys, re-key and merge them, and persist the result.

Signed-off-by: Paul Faure <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
…artup

Review feedback on owncloud#755: the load-time migration ran on every startup and
changed the passwords map while ranging over it.

- New app passwords are always stored under userKey.
- Legacy UserId.String() keys are matched by parsing them back into a user id
  (the text cannot be recomputed, since its whitespace varies between builds).
- GetAppPassword checks the stable key first; only a miss falls back to the
  user's legacy buckets. Generate, List, Invalidate and a successful Get move
  that user's legacy buckets under userKey (built in a separate map, then
  swapped in) and save.
- Nothing is rewritten on load; the expired-token purge is as before.
- userKey is computed once per call.

Tests cover load (no rewrite), Get, a wrong password and another user's
token, List, Invalidate and Generate; the adoption tests fail with the
legacy lookup disabled. go test -race and go vet pass.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Paul Faure <[email protected]>
@paul43210
paul43210 force-pushed the fix/appauth-stable-user-key branch from 573cbc2 to 4d8cbe6 Compare October 18, 2026 04:25
@paul43210

Copy link
Copy Markdown
Author

@jvillafanez thanks, I agree. I've followed your first suggestion, with one twist that the old key needs (4d8cbe6):

  • New app passwords are always stored under the new key, and nothing runs at startup anymore.
  • GetAppPassword checks the new key first, so a correct password under it is never slowed down. Only a miss looks at the old keys.
  • The twist: the old key can't simply be rebuilt and looked up. The old text-format key is UserId.String(), and protobuf-go varies its whitespace from build to build, which is the bug this PR fixes. A lookup by String() would miss on the very next build. So an old key is matched by parsing it back into a user id.
  • The first time a user generates, lists, invalidates or uses an app password, that user's old buckets are moved under the new key, so each user pays the fallback once. List and invalidate see the old tokens too, so users can still find and revoke them.

On usage data: I don't have any beyond our own server. If you'd rather not carry the fallback, letting users regenerate is fine with me too. I'd then drop the legacy lookup and say so in the changelog. A console command could come later if more key formats are ever needed.

Also rebased onto main.

@2403905

2403905 commented Oct 20, 2026

Copy link
Copy Markdown

I would like to include the fix in the recent releases, but I would prefer a different implementation. @paul43210, thank you for your contribution.
#777

@2403905

2403905 commented Oct 21, 2026

Copy link
Copy Markdown

Closed it to favor #777

@2403905 2403905 closed this Oct 21, 2026
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.

4 participants