Skip to content

Verify row ownership in Redis upstream token read - #6876

Merged
ChrisJBurns merged 1 commit into
stacklok:mainfrom
hellouz818:fix/6273-verify-user-row-ownership
Oct 20, 2026
Merged

ChrisJBurns merged 1 commit into
stacklok:mainfrom
hellouz818:fix/6273-verify-user-row-ownership

Conversation

@hellouz818

@hellouz818 hellouz818 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

GetLatestUpstreamTokensForUser on the Redis backend trusted the user-upstream index to name only rows belonging to the requested user. parseUserUpstreamEntry rejected a mismatched ProviderID but never compared the stored UserID, so a foreign-owned row named by that set would be decoded and could win the expiry-based selection.

This is hardening, not a reachable bug. Index members are prefix:upstream:{sessionID}:{providerName}, sessionID is a per-flow rand.Text() value, and every writer files its row under its own UserID, so no caller can produce a foreign row in another user's set today. It is worth closing anyway:

  • The user-upstream set has no TTL and is never garbage collected — the existing TODO above warnOnCleanupErr names KeyTypeUserUpstream as one of the sets awaiting a cleanup job — making it the one index where dead members accumulate.
  • The memory backend already recomputes ownership while scanning (memory.go), so Redis was the asymmetric one: it pushed the user filter into the index key and never re-checked the row.
  • maybeCarryForwardRefreshToken already depends on this postcondition, and once session identifiers stop being random per authorize flow, this check becomes the main protection rather than a backup.

What changed:

  • parseUserUpstreamEntry takes the requested userID and rejects rows whose stored UserID differs, alongside the existing ProviderID check.
  • Two regression subtests in TestRedisStorage_GetLatestUpstreamTokensForUser.

Ownership validation on the DeleteUser cascade is deliberately left out, per the scope agreed on the issue; it belongs with the user-keyed storage work.

Closes #6273

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

task test, task lint and task docs-verify all exit 0 on this branch, rebased onto main at 89b1bd2.

The two new subtests plant a foreign-owned member directly with mr.SAdd on redisSetKey(keyPrefix, KeyTypeUserUpstream, "user-A"), following the existing precedent in this file:

  • foreign_owned_member_not_returned — a foreign row alone must yield ErrNotFound.
  • foreign_owned_member_does_not_win_selection — a foreign row with a later ExpiresAt competing against the user's legitimate row must not win the tie-break. The first test alone cannot catch that.

Mutation-checked: removing the UserID comparison fails exactly these two subtests and no others.

Does this introduce a user-facing change?

No.

Special notes for reviewers

Both backends already carry a different_user_not_matched subtest, and the Redis one has a comment saying it passes via the empty-SMEMBERS short-circuit rather than a per-row check — so the gap was known, and no existing test asserted the postcondition at row level.

The comparison is folded into the existing ProviderID condition rather than added as a separate block, since both are the same "row does not match the request" rejection and the rationale lives in the function's doc comment. Happy to split it if you'd prefer the check called out on its own.

GetLatestUpstreamTokensForUser trusted the user-upstream index to name
only rows belonging to the requested user. parseUserUpstreamEntry
rejected a mismatched ProviderID but never compared the stored UserID,
so a foreign-owned row named by that set would be decoded and could win
the expiry-based selection.

No caller can reach that state today: the index is keyed by user and
every writer files its row under its own UserID. The check is worth
making anyway. The user-upstream set has no TTL and is never garbage
collected, so it is the one index where dead members accumulate, and
the memory backend already recomputes ownership while scanning. Once
session identifiers stop being random per authorize flow, this becomes
the main protection rather than a backup.

Ownership validation on the DeleteUser cascade is left out; it belongs
with the user-keyed storage work.

Closes stacklok#6273

Signed-off-by: hellouz818 <[email protected]>
@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.77%. Comparing base (89b1bd2) to head (e437774).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6876      +/-   ##
==========================================
- Coverage   79.77%   79.77%   -0.01%     
==========================================
  Files         820      820              
  Lines       83723    83723              
==========================================
- Hits        66791    66787       -4     
- Misses      16927    16931       +4     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ChrisJBurns
ChrisJBurns merged commit d8af1f3 into stacklok:main Oct 20, 2026
44 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 22, 2026
2 tasks
@hellouz818
hellouz818 deleted the fix/6273-verify-user-row-ownership branch November 23, 2026 05:10
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.

Redis user-keyed upstream token read does not verify row ownership

2 participants