forked from Junirezz/YieldVault-RWA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrateLimiter.property.test.ts
More file actions
177 lines (148 loc) · 6.64 KB
/
Copy pathrateLimiter.property.test.ts
File metadata and controls
177 lines (148 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @file rateLimiter.property.test.ts
* Property-based tests for the Redis-backed rate limiter.
*
* Uses fast-check to verify universal correctness properties across
* generated inputs. Each property runs a minimum of 100 iterations.
*/
import * as fc from 'fast-check';
import express, { Request, Response } from 'express';
import request from 'supertest';
// Helper: build a minimal express app with a fresh in-memory limiter
function buildApp(max: number, windowMs = 60000) {
jest.resetModules();
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { createLimiter } = require('../rateLimiter');
const app = express();
app.use(express.json());
const limiter = createLimiter({ routePrefix: '/prop-test', max, windowMs });
app.get('/test', limiter, (_req: Request, res: Response) => {
res.json({ ok: true });
});
return app;
}
// ─── Property 5: Requests beyond the limit receive 429 with required headers/body ──
// Feature: redis-rate-limiting, Property 5: 429 headers and body
describe('Property 5: Requests beyond the limit receive 429 with required headers and body', () => {
it('holds for randomly generated limit values', async () => {
await fc.assert(
fc.asyncProperty(
fc.integer({ min: 1, max: 20 }),
async (limit) => {
const app = buildApp(limit);
const key = `wallet-p5-${limit}-${Math.random()}`;
// Make exactly `limit` requests (all should succeed)
for (let i = 0; i < limit; i++) {
await request(app).get('/test').set('x-api-key', key);
}
// The (limit+1)th request must be 429
const res = await request(app).get('/test').set('x-api-key', key);
if (res.status !== 429) return false;
// Required headers
if (!res.headers['retry-after']) return false;
if (!res.headers['ratelimit-limit']) return false;
if (!res.headers['ratelimit-remaining']) return false;
if (!res.headers['ratelimit-reset']) return false;
// Required body fields
const body = res.body as Record<string, unknown>;
if (!body.error) return false;
if (body.status !== 429) return false;
if (!body.message) return false;
if (typeof body.retryAfter !== 'number') return false;
return true;
}
),
{ numRuns: 10 } // keep fast; each run makes limit+1 HTTP requests
);
});
});
// ─── Property 6: Requests within the limit include rate-limit headers ────────
// Feature: redis-rate-limiting, Property 6: 200 includes rate-limit headers
describe('Property 6: Requests within the limit include rate-limit headers', () => {
it('holds for randomly generated request counts within limit', async () => {
await fc.assert(
fc.asyncProperty(
fc.integer({ min: 1, max: 10 }),
async (count) => {
const limit = count + 5; // ensure we stay within limit
const app = buildApp(limit);
const key = `wallet-p6-${count}-${Math.random()}`;
for (let i = 0; i < count; i++) {
const res = await request(app).get('/test').set('x-api-key', key);
if (res.status !== 200) return false;
if (!res.headers['ratelimit-limit']) return false;
if (!res.headers['ratelimit-remaining']) return false;
if (!res.headers['ratelimit-reset']) return false;
}
return true;
}
),
{ numRuns: 10 }
);
});
});
// ─── Property 7: Counter initialises to 1 on first request in a window ───────
// Feature: redis-rate-limiting, Property 7: Counter initialises to 1
describe('Property 7: Counter initialises to 1 on first request in a window', () => {
it('RateLimit-Remaining equals limit-1 after first request', async () => {
await fc.assert(
fc.asyncProperty(
fc.integer({ min: 2, max: 30 }),
fc.string({ minLength: 5, maxLength: 20 }),
async (limit, walletSuffix) => {
const app = buildApp(limit);
const key = `wallet-p7-${walletSuffix}-${Math.random()}`;
const res = await request(app).get('/test').set('x-api-key', key);
if (res.status !== 200) return false;
const remaining = parseInt(res.headers['ratelimit-remaining'] as string, 10);
return remaining === limit - 1;
}
),
{ numRuns: 10 }
);
});
});
// ─── Property 8: Log fields and PII masking ───────────────────────────────────
// Feature: redis-rate-limiting, Property 8: Log fields and PII masking
describe('Property 8: Rate-limit log entries contain required fields without exposing full wallet address', () => {
const originalEnv = process.env;
afterEach(() => {
process.env = originalEnv;
jest.resetModules();
});
it('log contains required fields and masks wallet in production', async () => {
await fc.assert(
fc.asyncProperty(
fc.string({ minLength: 10, maxLength: 40 }),
async (walletAddress) => {
process.env = { ...originalEnv, NODE_ENV: 'production' };
jest.resetModules();
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { createLimiter } = require('../rateLimiter');
const logEntries: Record<string, unknown>[] = [];
const consoleSpy = jest.spyOn(console, 'log').mockImplementation((msg: string) => {
try { logEntries.push(JSON.parse(msg)); } catch { /* ignore non-JSON */ }
});
const app = express();
app.use(express.json());
const limiter = createLimiter({ routePrefix: '/prop-p8', max: 1, windowMs: 60000 });
app.get('/test', limiter, (_req: Request, res: Response) => res.json({ ok: true }));
// First request succeeds, second triggers 429 log
await request(app).get('/test').set('x-wallet-address', walletAddress);
await request(app).get('/test').set('x-wallet-address', walletAddress);
consoleSpy.mockRestore();
const rateLimitedLog = logEntries.find((e) => e.event === 'rate_limited');
if (!rateLimitedLog) return false;
// Required fields present
if (!rateLimitedLog.path) return false;
if (rateLimitedLog.resetTime === undefined) return false;
if (!rateLimitedLog.key) return false;
// Full wallet address must NOT appear in log key in production
if (walletAddress.length > 8 && rateLimitedLog.key === walletAddress) return false;
return true;
}
),
{ numRuns: 10 }
);
});
});