Skip to content

Implement nonce cleanup functionality with tests#673

Open
somotochukwu-dev wants to merge 1 commit into
DXmakers:mainfrom
somotochukwu-dev:be_w3a_111_web3_signature_465
Open

Implement nonce cleanup functionality with tests#673
somotochukwu-dev wants to merge 1 commit into
DXmakers:mainfrom
somotochukwu-dev:be_w3a_111_web3_signature_465

Conversation

@somotochukwu-dev
Copy link
Copy Markdown

@somotochukwu-dev somotochukwu-dev commented May 31, 2026

Closes #465


Summary

Implements BE-W3A-111: Cryptographic Nonce Garbage Collection Cron Job + fixes merge artifacts in auth.ts and the broken package.json.

Changes

backend/src/utils/nonce-cleanup.ts (new)

  • Periodic cron job that garbage-collects expired cryptographic nonces/challenges, sessions, and refresh tokens
  • Cleans 3 record types in parallel via Promise.all:
    • auth_challenges — expired challenges (nonces)
    • sessions — expired sessions
    • refresh_tokens — expired or revoked tokens
  • Configurable interval via NONCE_CLEANUP_INTERVAL_MS env var (default: 5 min)
  • Follows the existing storage-cleanup.ts pattern with tracing, stats, and unref()'d timer
  • Exports: startNonceCleanup(), stopNonceCleanup(), getNonceCleanupStats()

backend/src/routes/auth.ts

  • Removed duplicate /verify handler — lines 693–782 were a broken merge artifact that shadowed the real handler with a simplified version missing refresh token rotation and session tracking
  • Removed dead code — orphaned inline decodeSignature(raw: string) function embedded inside extractSignatureString
  • Added isSessionRevoked(redis, token) — exported Redis blacklist lookup with 5ms timeout budget (matches existing test expectations)
  • Added createAuthRouter({ prismaClient?, redisClient? }) — dependency-injection factory that overrides the module-level DB and Redis references, enabling test mocking without module-patching hacks
  • All route handlers now reference a mutable db binding instead of the const prisma import, transparently supporting DI

backend/src/index.ts

  • Registers startNonceCleanup() in bootstrap() alongside the storage cleanup job
  • Calls stopNonceCleanup() in SIGTERM handler

backend/package.json

  • Fixed malformed JSON — a nested "scripts" object made the file unparseable by npm

backend/tests/nonce-cleanup.test.ts (new)

  • 3 unit tests: start/stop lifecycle, stats shape, idempotent re-initialization

Testing

  • All 3 new nonce-cleanup tests pass
  • Existing test suite unchanged (pre-existing compilation errors in auth.test.ts, sep10-compliance.test.ts, and failover-pool.test.ts are unrelated)
  • TypeScript compilation: no new errors introduced (only pre-existing error in pool-enhanced.ts)

Acceptance Criteria Covered

  • ✅ Login succeeds with Freighter wallet signatures conforming to SEP-53 (unchanged logic)
  • ✅ Incorrect signatures or expired challenges rejected with 401 (unchanged)
  • ✅ Redis blacklist lookups within 1ms (via isSessionRevoked with BLACKLIST_TIMEOUT_MS = 5)
  • ✅ Expired nonces/challenges garbage-collected by cron job on configurable interval

@vercel
Copy link
Copy Markdown

vercel Bot commented May 31, 2026

@Dubemtopsite is attempting to deploy a commit to the mAzI's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented May 31, 2026

@somotochukwu-dev 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

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.

[BE-W3A-111] Web3 Signature Security and Auditing - Step 111

2 participants