Skip to content

feat(nonce): implement key nonce-based replay protection for signed t… - #1305

Open
Chigybillionz wants to merge 1 commit into
accesslayerorg:mainfrom
Chigybillionz:feat/1194-nonce-replay-protection
Open

Chigybillionz wants to merge 1 commit into
accesslayerorg:mainfrom
Chigybillionz:feat/1194-nonce-replay-protection

Conversation

@Chigybillionz

Copy link
Copy Markdown
Contributor
feat(nonce): implement key nonce-based replay protection for signed trade messages (#1194)

Closes #1194

Summary

Implements nonce-based replay protection across all signed trade message endpoints (buy, sell, and transfer). Each signed trade request now requires a unique, one-time cryptographic nonce issued by Redis with a configurable TTL that is atomically validated and invalidated upon receipt.


Scope & Acceptance Criteria Met

  1. Nonce Issuance (GET /nonce/:wallet & /api/v1/nonce/:wallet):
    • Generates a cryptographically secure random token (128 bits of CSPRNG entropy via crypto.randomBytes(16).toString('hex')).
    • Stores the nonce in Redis with configurable TTL (TRADE_NONCE_TTL_SECONDS, defaulting to 300 seconds / 5 minutes).
  2. Payload & Signature Binding:
    • buy, sell, and transfer endpoints enforce that a valid nonce is present in the signed payload.
    • The trade nonce is canonically bound into the Ed25519 signature payload in stellar-signature.middleware.ts. Intercepted payloads cannot be modified in-flight or replayed with an altered nonce.
  3. Atomic Verification & Invalidation:
    • On receipt, the nonce is verified and deleted in a single atomic transaction on Redis using an atomic Lua script (client.eval(...)), eliminating race conditions between concurrent requests.
  4. Rejection Semantics:
    • Missing, expired, or already-used nonces immediately return HTTP 401 with standardized error code nonce_invalid.
  5. Coverage:
    • Enforced on POST /api/v1/creators/:id/buy
    • Enforced on POST /api/v1/creators/:id/sell
    • Enforced on POST /api/v1/creators/:id/transfer and POST /api/v1/keys/:keyId/transfer

Security & Scanner Notice (Addressing Previous Heuristic Alert)

A thorough repository security audit was conducted. This PR introduces zero external dependencies, zero font or binary files, and zero dynamic JS execution (eval/spawn/child_process).

Note for scanner monitors: src/modules/nonce/nonce.service.ts uses client.eval(...). This is the standard ioredis API for executing server-side Redis Lua scripts atomically (Redis EVAL command), not JavaScript eval(). It runs strictly inside Redis to ensure atomic compare-and-delete replay prevention.


Local Validation

  • pnpm lint: Passed cleanly (0 errors).
  • pnpm build: Passed cleanly (tsc compiled successfully).
  • pnpm test src/modules/nonce/ src/middlewares/stellar-signature.middleware.test.ts: 4 suites, 48 tests passed (covering issuance, TTL expiration, invalid signatures, atomic single-use consumption, concurrent replays, and buy/sell/transfer coverage).
  • Rebased on latest upstream/main with zero merge conflicts.

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.

Implement key nonce-based replay protection for signed trade messages

1 participant