Skip to content

fix(auth): implement proactive TTL eviction for pending SEP-10 challenges - #875

Merged
Just-Bamford merged 3 commits into
Sorokit:mainfrom
Gezziy:expired-challenges
Oct 20, 2026
Merged

Just-Bamford merged 3 commits into
Sorokit:mainfrom
Gezziy:expired-challenges

Conversation

@Gezziy

@Gezziy Gezziy commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

closes #763

Summary of Changes

  • Proactive Background TTL Eviction: Introduced an automatic eviction timer in src/integration/sep10Auth.ts that removes expired challenge contexts as soon as CHALLENGE_TTL_MS (5 minutes) elapses, preventing memory accumulation in idle deployments.
  • $\mathcal{O}(K)$ Early-Exit Optimization: Because all challenges share identical uniform TTLs, insertion order in the internal map corresponds to chronological expiration. The eviction scan terminates early on the first unexpired entry without scanning the entire map.
  • Process Safety: Called .unref() on the background timer handle in Node.js/Bun environments so pending timers do not block clean process exit or CLI operations. When pendingChallenges is empty, background timers are cleanly cleared.
  • Eager Cleanup on Operations:
    • initiateSep10Auth: Prunes expired entries before evaluating MAX_PENDING_CHALLENGES (128) and inserting new challenges.
    • completeSep10Auth: Accurately evaluates caller-supplied options.now ?? Date.now(). If expired, rejects immediately with code INVALID_AUTH and message "SEP-10 challenge has expired." while purging expired entries.
  • Observability & Testing Helpers: Exported pruneExpiredChallenges, getPendingChallengesCount, clearPendingChallenges, CHALLENGE_TTL_MS, and MAX_PENDING_CHALLENGES from src/integration/ and root src/index.ts.
  • Test Coverage: Added comprehensive test cases in src/integration/sep10Auth.test.ts verifying idle background eviction via fake timers, expired-but-not-evicted error handling, FIFO limit enforcement at 128 challenges, and staggered multi-challenge expirations.

Reason for Changes

Resolves issue #763. Previously, pendingChallenges only evicted expired entries when a new challenge was requested via initiateSep10Auth. In idle deployments, stale challenge contexts remained pinned in memory indefinitely. Furthermore, under traffic bursts approaching the 128-challenge limit, stale entries could cause valid in-flight challenges to be prematurely evicted via FIFO.

…nges (Sorokit#763)

- Add automatic background timer eviction for expired SEP-10 challenge contexts
- Implement O(K) early-exit pruning leveraging uniform TTL insertion monotonicity
- Eagerly clean expired challenges on initiateSep10Auth and completeSep10Auth
- Evaluate caller-supplied options.now in completeSep10Auth with clear error on expiry
- Unreference background timer handle to prevent holding the Node.js event loop
- Export pruneExpiredChallenges, getPendingChallengesCount, and clearPendingChallenges helpers
- Add unit and integration tests covering idle background eviction and FIFO limits
@drips-wave

drips-wave Bot commented Oct 20, 2026

Copy link
Copy Markdown

@Gezziy Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Just-Bamford
Just-Bamford merged commit ed37494 into Sorokit:main Oct 20, 2026
5 checks passed
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.

pendingChallenges in sep10Auth.ts uses weak TTL eviction — expired challenges persist until MAX_PENDING_CHALLENGES limit hit

3 participants