|
| 1 | +import * as request from 'supertest'; |
| 2 | +import { INestApplication } from '@nestjs/common'; |
| 3 | +import { AuthWalletThrottlerGuard } from '../../../../src/modules/auth/auth-throttler.guard'; |
| 4 | +import { buildTestApp, InMemoryStore } from '../../helpers/test-setup'; |
| 5 | + |
| 6 | +// Use the real stellar-sdk implementation even when a manual mock exists at |
| 7 | +// test/__mocks__/stellar-sdk.js (which is auto-applied for `jest.mock` in |
| 8 | +// unit tests). `jest.requireActual` bypasses the mock and gives us a |
| 9 | +// Keypair that generates distinct random wallets per call. |
| 10 | +const { Keypair: RealKeypair } = jest.requireActual('stellar-sdk') as typeof import('stellar-sdk'); |
| 11 | +type RealKeypairType = InstanceType<typeof RealKeypair>; |
| 12 | +function createTestKeypair(): RealKeypairType { |
| 13 | + return RealKeypair.random() as unknown as RealKeypairType; |
| 14 | +} |
| 15 | +function signMessage(keypair: RealKeypairType, message: string): string { |
| 16 | + return (keypair as unknown as { sign: (b: Buffer) => Buffer }).sign(Buffer.from(message)).toString('base64'); |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * E2E coverage for the two gaps identified by the audit bot: |
| 21 | + * - atomic nonce consumption under genuine concurrency (parallel identical |
| 22 | + * POST /auth/verify must yield exactly one 200, the other 401) |
| 23 | + * - per-wallet throttling on POST /auth/verify (6 rapid requests from the |
| 24 | + * same wallet must yield 429 on the 6th) |
| 25 | + * |
| 26 | + * Uses the InMemoryStore mock (via buildTestApp) rather than a real Postgres |
| 27 | + * instance. The store's UPDATE ... is('used_at', null) predicate is evaluated |
| 28 | + * in-memory, so the second concurrent claim correctly sees count 0 / empty |
| 29 | + * data and is rejected as AUTH_NONCE_NOT_FOUND. This is the user-visible |
| 30 | + * contract even though the underlying atomicity is ultimately provided by |
| 31 | + * Postgres `UPDATE ... WHERE used_at IS NULL` in production. |
| 32 | + */ |
| 33 | +describe('Auth verify — atomic claim & per-wallet throttling (e2e)', () => { |
| 34 | + let app: INestApplication; |
| 35 | + let mockDb: InMemoryStore; |
| 36 | + |
| 37 | + beforeAll(async () => { |
| 38 | + const built = await buildTestApp(); |
| 39 | + app = built.app; |
| 40 | + mockDb = built.mockDb; |
| 41 | + }); |
| 42 | + |
| 43 | + afterAll(async () => { |
| 44 | + await app.close(); |
| 45 | + }); |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + mockDb.clear(); |
| 49 | + AuthWalletThrottlerGuard.clearStorage(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('parallel double-verify with same (wallet, nonce, signature) yields exactly one success (atomic claim)', async () => { |
| 53 | + const keypair = createTestKeypair(); |
| 54 | + const wallet = keypair.publicKey(); |
| 55 | + |
| 56 | + const nonceRes = await request(app.getHttpServer()) |
| 57 | + .post('/auth/nonce') |
| 58 | + .send({ wallet }) |
| 59 | + .expect(201); |
| 60 | + |
| 61 | + const nonce: string = nonceRes.body.nonce; |
| 62 | + expect(nonce).toHaveLength(64); |
| 63 | + |
| 64 | + // Legacy raw scheme: signature over the nonce hex bytes. |
| 65 | + const signature = signMessage(keypair, nonce); |
| 66 | + |
| 67 | + const results = await Promise.allSettled([ |
| 68 | + request(app.getHttpServer()).post('/auth/verify').send({ wallet, nonce, signature }), |
| 69 | + request(app.getHttpServer()).post('/auth/verify').send({ wallet, nonce, signature }), |
| 70 | + ]); |
| 71 | + |
| 72 | + // supertest always fulfills; inspect HTTP status directly |
| 73 | + const statuses = results.map((r) => |
| 74 | + r.status === 'fulfilled' ? (r.value as request.Response).status : 0, |
| 75 | + ); |
| 76 | + |
| 77 | + const successes = statuses.filter((s) => s === 200); |
| 78 | + const notFounds = statuses.filter((s) => s === 401); |
| 79 | + |
| 80 | + expect(successes).toHaveLength(1); |
| 81 | + expect(notFounds).toHaveLength(1); |
| 82 | + |
| 83 | + // A third sequential replay must also fail with 401 (nonce stays burned) |
| 84 | + await request(app.getHttpServer()) |
| 85 | + .post('/auth/verify') |
| 86 | + .send({ wallet, nonce, signature }) |
| 87 | + .expect(401); |
| 88 | + }); |
| 89 | + |
| 90 | + it('expired nonce is rejected and stays burned — second attempt is NOT_FOUND, not success', async () => { |
| 91 | + const keypair = createTestKeypair(); |
| 92 | + const wallet = keypair.publicKey(); |
| 93 | + |
| 94 | + const nonceRes = await request(app.getHttpServer()) |
| 95 | + .post('/auth/nonce') |
| 96 | + .send({ wallet }) |
| 97 | + .expect(201); |
| 98 | + |
| 99 | + const nonce: string = nonceRes.body.nonce; |
| 100 | + const signature = signMessage(keypair, nonce); |
| 101 | + |
| 102 | + // Manually expire the nonce row in the mock store (bulk update via store) |
| 103 | + // The mock store holds rows in memory; find and mutate. |
| 104 | + const rows = mockDb.dump('nonces'); |
| 105 | + const row = rows.find((r) => r.nonce === nonce); |
| 106 | + if (row) { |
| 107 | + row.expires_at = new Date(Date.now() - 1000).toISOString(); |
| 108 | + } |
| 109 | + |
| 110 | + // First verify: claim succeeds but expiry check fails -> 401 AUTH_NONCE_EXPIRED |
| 111 | + await request(app.getHttpServer()) |
| 112 | + .post('/auth/verify') |
| 113 | + .send({ wallet, nonce, signature }) |
| 114 | + .expect(401); |
| 115 | + |
| 116 | + // Second verify: nonce already claimed/burned -> 401 AUTH_NONCE_NOT_FOUND |
| 117 | + // (burn-on-failure semantics) |
| 118 | + await request(app.getHttpServer()) |
| 119 | + .post('/auth/verify') |
| 120 | + .send({ wallet, nonce, signature }) |
| 121 | + .expect(401); |
| 122 | + }); |
| 123 | + |
| 124 | + it('per-wallet throttling: 6 rapid POST /auth/verify from the same wallet yields 429 on the 6th', async () => { |
| 125 | + const wallet = createTestKeypair().publicKey(); |
| 126 | + const fakeNonce = 'a'.repeat(64); |
| 127 | + const fakeSig = Buffer.alloc(64).toString('base64'); |
| 128 | + |
| 129 | + // First 5 requests: throttler allows them (service returns 401 AUTH_NONCE_NOT_FOUND, |
| 130 | + // but throttler does not block) |
| 131 | + for (let i = 0; i < 5; i++) { |
| 132 | + await request(app.getHttpServer()) |
| 133 | + .post('/auth/verify') |
| 134 | + .send({ wallet, nonce: fakeNonce, signature: fakeSig }) |
| 135 | + .expect(401); |
| 136 | + } |
| 137 | + |
| 138 | + // 6th request from same wallet: per-wallet guard must reject with 429 |
| 139 | + await request(app.getHttpServer()) |
| 140 | + .post('/auth/verify') |
| 141 | + .send({ wallet, nonce: fakeNonce, signature: fakeSig }) |
| 142 | + .expect(429); |
| 143 | + }); |
| 144 | + |
| 145 | + it('per-wallet throttling is isolated — a different wallet is not throttled by the first wallet’s quota', async () => { |
| 146 | + const walletA = createTestKeypair().publicKey(); |
| 147 | + const walletB = createTestKeypair().publicKey(); |
| 148 | + const fakeNonce = 'b'.repeat(64); |
| 149 | + const fakeSig = Buffer.alloc(64).toString('base64'); |
| 150 | + |
| 151 | + for (let i = 0; i < 5; i++) { |
| 152 | + await request(app.getHttpServer()) |
| 153 | + .post('/auth/verify') |
| 154 | + .send({ wallet: walletA, nonce: fakeNonce, signature: fakeSig }) |
| 155 | + .expect(401); |
| 156 | + } |
| 157 | + // walletA exhausted |
| 158 | + await request(app.getHttpServer()) |
| 159 | + .post('/auth/verify') |
| 160 | + .send({ wallet: walletA, nonce: fakeNonce, signature: fakeSig }) |
| 161 | + .expect(429); |
| 162 | + |
| 163 | + // walletB should still be allowed (gets 401, not 429) |
| 164 | + await request(app.getHttpServer()) |
| 165 | + .post('/auth/verify') |
| 166 | + .send({ wallet: walletB, nonce: fakeNonce, signature: fakeSig }) |
| 167 | + .expect(401); |
| 168 | + }); |
| 169 | +}); |
0 commit comments