-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathuser_defined_rules.js
243 lines (213 loc) · 7.17 KB
/
user_defined_rules.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
const test = require('tap').test;
const TestServer = require('../test_server');
const Promise = require('bluebird');
const restifyClients = Promise.promisifyAll(require('restify-clients'));
const mcHelper = require('../cache-helper');
Promise.promisifyAll(mcHelper, { multiArgs: true });
function randomEmail() {
return Math.floor(Math.random() * 10000) + '@email.com';
}
function randomIp() {
function getSubnet() {
return Math.floor(Math.random() * 255);
}
return [getSubnet(), getSubnet(), getSubnet(), getSubnet()].join('.');
}
function randomUid() {
return Math.floor(Math.random() * 10000) + '';
}
const config = require('../../lib/config').getProperties();
config.userDefinedRateLimitRules.totpCodeRules.limits.max = 2;
config.userDefinedRateLimitRules.totpCodeRules.limits.periodMs = 1000;
config.userDefinedRateLimitRules.totpCodeRules.limits.rateLimitIntervalMs = 1000;
config.userDefinedRateLimitRules.tokenCodeRules.limits.max = 2;
config.userDefinedRateLimitRules.tokenCodeRules.limits.periodMs = 1000;
config.userDefinedRateLimitRules.tokenCodeRules.limits.rateLimitIntervalMs = 1000;
config.userDefinedRateLimitRules.recoveryPhoneSendSetupCodeRules.limits.max = 5;
config.userDefinedRateLimitRules.recoveryPhoneSendSetupCodeRules.limits.periodMs = 5000;
config.userDefinedRateLimitRules.recoveryPhoneSendSetupCodeRules.limits.rateLimitIntervalMs = 1000;
config.userDefinedRateLimitRules.recoveryPhoneSendSigninCodeRules.limits.max = 5;
config.userDefinedRateLimitRules.recoveryPhoneSendSigninCodeRules.limits.periodMs = 5000;
config.userDefinedRateLimitRules.recoveryPhoneSendSigninCodeRules.limits.rateLimitIntervalMs = 1000;
const ACTIONS = ['verifyTotpCode', 'verifyTokenCode'];
const testServer = new TestServer(config);
const client = restifyClients.createJsonClient({
url: 'http://localhost:' + config.listen.port,
});
Promise.promisifyAll(client, { multiArgs: true });
test('startup', async function (t) {
await testServer.start();
t.type(testServer.server, 'object', 'test server was started');
t.end();
});
ACTIONS.forEach((action) => {
test('clear everything', (t) => {
mcHelper.clearEverything((err) => {
t.notOk(err, 'no errors were returned');
t.end();
});
});
test('/check `' + action + '` by email', (t) => {
const email = randomEmail();
const ip = randomIp();
// Send requests until throttled
return (
client
.postAsync('/check', { ip, action, email })
.spread((req, res, obj) => {
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, false, 'not rate limited');
return client.postAsync('/check', { ip, action, email });
})
.spread((req, res, obj) => {
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, false, 'not rate limited');
return client.postAsync('/check', { ip, action, email });
})
.spread((req, res, obj) => {
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, true, 'rate limited');
t.equal(obj.retryAfter, 1, 'rate limit retry amount');
// Delay ~1s for rate limit to go away
return Promise.delay(1010);
})
// Reissue requests to verify that throttling is disabled
.then(() => {
return client.postAsync('/check', { ip, action, email });
})
.spread((req, res, obj) => {
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, false, 'not rate limited');
t.end();
})
.catch((err) => {
t.fail(err);
t.end();
})
);
});
test('/check `' + action + '` does not rate limit other actions', (t) => {
const email = randomEmail();
const ip = randomIp();
return client
.postAsync('/check', { ip, action, email })
.spread(() => {
return client.postAsync('/check', { ip, action, email });
})
.spread(() => {
return client.postAsync('/check', { ip, action, email });
})
.spread((req, res, obj) => {
t.equal(obj.block, true, 'rate limited');
return client.postAsync('/check', {
ip,
action: 'accountLogin',
email,
});
})
.spread((req, res, obj) => {
t.equal(obj.block, false, 'not rate limited');
return client.postAsync('/check', {
ip,
action: 'accountLogin',
email: randomEmail(),
});
})
.spread((req, res, obj) => {
t.equal(obj.block, false, 'not rate limited');
return client.postAsync('/check', {
ip: randomIp(),
action: 'accountLogin',
email,
});
})
.spread((req, res, obj) => {
t.equal(obj.block, false, 'not rate limited');
})
.catch((err) => {
t.fail(err);
t.end();
});
});
});
const recoveryPhoneActions = [
'recoveryPhoneSendSetupCode',
'recoveryPhoneSendSigninCode',
];
recoveryPhoneActions.forEach((action) => {
test(`clear everything for ${action}`, async (t) => {
try {
await mcHelper.clearEverythingAsync();
t.pass('cleared redis');
} catch (err) {
t.fail(err);
} finally {
t.end();
}
});
test('/checkAuthenticated `' + action + '` by uid', async (t) => {
const uid = randomUid();
const ip = randomIp();
// Send requests until throttled
let [req, res, obj] = await client.postAsync('/checkAuthenticated', {
ip,
action,
uid,
});
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, false, 'not rate limited');
[req, res, obj] = await client.postAsync('/checkAuthenticated', {
ip,
action,
uid,
});
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, false, 'not rate limited');
[req, res, obj] = await client.postAsync('/checkAuthenticated', {
ip,
action,
uid,
});
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, false, 'not rate limited');
[req, res, obj] = await client.postAsync('/checkAuthenticated', {
ip,
action,
uid,
});
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, false, 'not rate limited');
[req, res, obj] = await client.postAsync('/checkAuthenticated', {
ip,
action,
uid,
});
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, false, 'not rate limited');
[req, res, obj] = await client.postAsync('/checkAuthenticated', {
ip,
action,
uid,
});
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, true, 'rate limited');
t.equal(obj.retryAfter, 1, 'rate limit retry amount');
// Wait for limit to expire
await Promise.delay(1010);
[req, res, obj] = await client.postAsync('/checkAuthenticated', {
ip,
action,
uid,
});
t.equal(res.statusCode, 200, 'returns a 200');
t.equal(obj.block, false, 'not rate limited');
t.pass(req);
t.end();
});
});
test('teardown', async function (t) {
await testServer.stop();
t.end();
});