@proteinjs/user-server 1.26.0 → 1.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/dist/generated/index.js +1 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/src/routes/signup.d.ts +8 -3
- package/dist/src/routes/signup.d.ts.map +1 -1
- package/dist/src/routes/signup.js +55 -15
- package/dist/src/routes/signup.js.map +1 -1
- package/dist/src/services/DisposableEmailDomains.d.ts +24 -0
- package/dist/src/services/DisposableEmailDomains.d.ts.map +1 -0
- package/dist/src/services/DisposableEmailDomains.js +129 -0
- package/dist/src/services/DisposableEmailDomains.js.map +1 -0
- package/dist/src/services/Roles.d.ts +4 -3
- package/dist/src/services/Roles.d.ts.map +1 -1
- package/dist/src/services/Roles.js +3 -3
- package/dist/src/services/Roles.js.map +1 -1
- package/dist/src/services/SignupGuard.d.ts +55 -0
- package/dist/src/services/SignupGuard.d.ts.map +1 -0
- package/dist/src/services/SignupGuard.js +13 -0
- package/dist/src/services/SignupGuard.js.map +1 -0
- package/dist/test/Roles.integration.test.js +71 -0
- package/dist/test/Roles.integration.test.js.map +1 -1
- package/dist/test/SignupGuardSeam.test.d.ts +2 -0
- package/dist/test/SignupGuardSeam.test.d.ts.map +1 -0
- package/dist/test/SignupGuardSeam.test.js +474 -0
- package/dist/test/SignupGuardSeam.test.js.map +1 -0
- package/dist/test/UserServerTestEnvironment.d.ts.map +1 -1
- package/dist/test/UserServerTestEnvironment.js +4 -0
- package/dist/test/UserServerTestEnvironment.js.map +1 -1
- package/generated/index.ts +1 -1
- package/index.ts +10 -0
- package/package.json +2 -2
- package/src/routes/signup.ts +58 -9
- package/src/services/DisposableEmailDomains.ts +125 -0
- package/src/services/Roles.ts +12 -6
- package/src/services/SignupGuard.ts +58 -0
- package/test/Roles.integration.test.ts +42 -1
- package/test/SignupGuardSeam.test.ts +299 -0
- package/test/UserServerTestEnvironment.ts +4 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `POST /user/signup`'s guard seam and disposable-domain rule, on the NO-INVITE road.
|
|
5
|
+
*
|
|
6
|
+
* A consumer registers a `DefaultSignupGuard` (the `DefaultInviteConfigFactory` pattern) and the
|
|
7
|
+
* route hands it the submission's facts; the guard answers `allow`, `refuse(sentence)` or `drop`.
|
|
8
|
+
* Covered here, outcomes only (rows written, session minted, the body sent), against the Spanner
|
|
9
|
+
* emulator:
|
|
10
|
+
* - no guard registered: the account is created and the session established (today's behavior);
|
|
11
|
+
* - `refuse`: the body is `{ error: sentence }`, no account row, no session;
|
|
12
|
+
* - `drop`: the body is exactly the success shape, no account row, no session — the caller cannot tell;
|
|
13
|
+
* - the invite road never consults the guard: an invited address was chosen by an admin;
|
|
14
|
+
* - the facts the guard receives: the lowercased address, the form's honeypot and render token, the
|
|
15
|
+
* coarse client-address hash (never the address), the user agent's presence;
|
|
16
|
+
* - the disposable-domain list: a throwaway domain and a subdomain of one refused with the one
|
|
17
|
+
* sentence before the guard runs; a real domain allowed.
|
|
18
|
+
*
|
|
19
|
+
* The email transport is stubbed at the module boundary (SignupRoute.test.ts pattern); the db
|
|
20
|
+
* stays real. The guard is seeded into the SourceRepository cache by its qualified name, exactly
|
|
21
|
+
* where the generated source graph would put a consumer's registration.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const sendEmail = jest.fn();
|
|
25
|
+
|
|
26
|
+
jest.mock('@proteinjs/email-server', () => ({
|
|
27
|
+
EmailSender: jest.fn().mockImplementation(() => ({ sendEmail })),
|
|
28
|
+
getDefaultInviteEmailConfigFactory: () => ({
|
|
29
|
+
getConfig: () => ({ getEmailContent: () => ({ text: 'invite', html: '<p>invite</p>' }) }),
|
|
30
|
+
}),
|
|
31
|
+
getDefaultSignupConfirmationEmailConfigFactory: () => ({
|
|
32
|
+
getConfig: () => ({
|
|
33
|
+
getNewUserEmailContent: () => ({ text: 'welcome', html: '<p>welcome</p>' }),
|
|
34
|
+
getExistingUserEmailContent: () => ({ text: 'exists', html: '<p>exists</p>' }),
|
|
35
|
+
}),
|
|
36
|
+
}),
|
|
37
|
+
}));
|
|
38
|
+
|
|
39
|
+
import { getDbAsSystem, Reference } from '@proteinjs/db';
|
|
40
|
+
import { SourceRepository } from '@proteinjs/reflection';
|
|
41
|
+
import { tables } from '@proteinjs/user';
|
|
42
|
+
import { RequestDigests } from '@proteinjs/util-node';
|
|
43
|
+
import { signup } from '../src/routes/signup';
|
|
44
|
+
import { createPassportRequest } from './passportSessionHarness';
|
|
45
|
+
import { UserServerTestEnvironment } from './UserServerTestEnvironment';
|
|
46
|
+
|
|
47
|
+
const testEnv = new UserServerTestEnvironment();
|
|
48
|
+
|
|
49
|
+
const INVITE_CONFIG_KEY = '@proteinjs/user-server/DefaultInviteConfigFactory';
|
|
50
|
+
const GUARD_KEY = '@proteinjs/user-server/DefaultSignupGuard';
|
|
51
|
+
const DISPOSABLE_REFUSAL = 'Use an email address you can read mail at.';
|
|
52
|
+
const CLIENT_ADDRESS = '203.0.113.7';
|
|
53
|
+
|
|
54
|
+
type ObjectCache = { objectCache: Record<string, unknown[]> };
|
|
55
|
+
|
|
56
|
+
/** A guard a consumer would register: answers what it is told to, and keeps the facts it was handed. */
|
|
57
|
+
type RecordingGuard = {
|
|
58
|
+
check: (facts: Record<string, unknown>) => unknown;
|
|
59
|
+
received: Record<string, unknown>[];
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const guardAnswering = (verdict: unknown): RecordingGuard => {
|
|
63
|
+
const guard: RecordingGuard = {
|
|
64
|
+
received: [],
|
|
65
|
+
check: (facts) => {
|
|
66
|
+
guard.received.push(facts);
|
|
67
|
+
return verdict;
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
return guard;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** Seed the guard lookup (tests don't load the generated source graph); none = no consumer registration. */
|
|
74
|
+
const setGuard = (guards: unknown[]) => {
|
|
75
|
+
(SourceRepository.get() as unknown as ObjectCache).objectCache[GUARD_KEY] = guards;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const setInviteConfig = (factories: unknown[]) => {
|
|
79
|
+
(SourceRepository.get() as unknown as ObjectCache).objectCache[INVITE_CONFIG_KEY] = factories;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
type RouteOutcome = {
|
|
83
|
+
loggedInAs?: string;
|
|
84
|
+
sessionEvents: string[];
|
|
85
|
+
body?: { error?: string };
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const invokeSignup = async (
|
|
89
|
+
body: Record<string, unknown>,
|
|
90
|
+
headers: Record<string, string> = { 'user-agent': 'a browser' }
|
|
91
|
+
): Promise<RouteOutcome> => {
|
|
92
|
+
const { request, events } = await createPassportRequest({
|
|
93
|
+
body,
|
|
94
|
+
headers,
|
|
95
|
+
socket: { remoteAddress: CLIENT_ADDRESS },
|
|
96
|
+
});
|
|
97
|
+
const outcome: RouteOutcome = { sessionEvents: events };
|
|
98
|
+
const response = {
|
|
99
|
+
status() {
|
|
100
|
+
return this;
|
|
101
|
+
},
|
|
102
|
+
send(body?: unknown) {
|
|
103
|
+
outcome.body = body as RouteOutcome['body'];
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
await signup.onRequest(request as never, response as never);
|
|
107
|
+
outcome.loggedInAs = request.session.passport?.user;
|
|
108
|
+
return outcome;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const getUserRow = async (email: string) => await getDbAsSystem().get(tables.User, { email });
|
|
112
|
+
const userRowCount = async () => (await getDbAsSystem().query(tables.User, {})).length;
|
|
113
|
+
|
|
114
|
+
const seedInvite = async (email: string, token: string) => {
|
|
115
|
+
await getDbAsSystem().insert(tables.Invite, {
|
|
116
|
+
email,
|
|
117
|
+
token,
|
|
118
|
+
tokenExpiresAt: moment().add(1, 'day'),
|
|
119
|
+
invitedBy: new Reference(tables.User.name, 'inviter-1'),
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
describe('signup route — the guard seam on the no-invite road', () => {
|
|
124
|
+
beforeAll(async () => {
|
|
125
|
+
await testEnv.beforeAll();
|
|
126
|
+
}, 120000);
|
|
127
|
+
|
|
128
|
+
afterAll(async () => {
|
|
129
|
+
delete (SourceRepository.get() as unknown as ObjectCache).objectCache[GUARD_KEY];
|
|
130
|
+
await testEnv.afterAll();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
beforeEach(async () => {
|
|
134
|
+
jest.clearAllMocks();
|
|
135
|
+
setInviteConfig([]); // invite optional: the no-invite road is open
|
|
136
|
+
setGuard([]);
|
|
137
|
+
const db = getDbAsSystem();
|
|
138
|
+
await db.delete(tables.Invite, {});
|
|
139
|
+
await db.delete(tables.User, {});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('with no guard registered, a no-invite signup creates the account and establishes the session', async () => {
|
|
143
|
+
const outcome = await invokeSignup({ name: 'Open Door', email: 'Open.Door@Test.local', password: 'pw-open-1' });
|
|
144
|
+
|
|
145
|
+
expect(outcome.body).toEqual({});
|
|
146
|
+
expect(outcome.loggedInAs).toBe('open.door@test.local');
|
|
147
|
+
expect(outcome.sessionEvents).toEqual(['regenerate', 'login', 'save']);
|
|
148
|
+
expect(await getUserRow('open.door@test.local')).toBeDefined();
|
|
149
|
+
expect(sendEmail).toHaveBeenCalledTimes(1);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it(`a guard answering refuse: the body carries the guard's sentence, no account, no session`, async () => {
|
|
153
|
+
setGuard([guardAnswering({ verdict: 'refuse', sentence: 'Too many sign-ups from here. Try again in an hour.' })]);
|
|
154
|
+
|
|
155
|
+
const outcome = await invokeSignup({ name: 'Refused', email: 'refused@test.local', password: 'pw-refused-1' });
|
|
156
|
+
|
|
157
|
+
expect(outcome.body).toEqual({ error: 'Too many sign-ups from here. Try again in an hour.' });
|
|
158
|
+
expect(outcome.loggedInAs).toBeUndefined();
|
|
159
|
+
expect(outcome.sessionEvents).toEqual([]);
|
|
160
|
+
expect(await getUserRow('refused@test.local')).toBeUndefined();
|
|
161
|
+
expect(await userRowCount()).toBe(0);
|
|
162
|
+
expect(sendEmail).not.toHaveBeenCalled();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('a guard answering drop: the body is exactly the success shape, no account, no session', async () => {
|
|
166
|
+
setGuard([guardAnswering({ verdict: 'drop' })]);
|
|
167
|
+
|
|
168
|
+
const outcome = await invokeSignup({
|
|
169
|
+
name: 'Bot',
|
|
170
|
+
email: 'dropped@test.local',
|
|
171
|
+
password: 'pw-dropped-1',
|
|
172
|
+
website: 'https://filled.by.a.bot',
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
expect(outcome.body).toEqual({});
|
|
176
|
+
expect(outcome.loggedInAs).toBeUndefined();
|
|
177
|
+
expect(outcome.sessionEvents).toEqual([]);
|
|
178
|
+
expect(await getUserRow('dropped@test.local')).toBeUndefined();
|
|
179
|
+
expect(await userRowCount()).toBe(0);
|
|
180
|
+
expect(sendEmail).not.toHaveBeenCalled();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('a guard answering allow lets the signup through as if none were registered', async () => {
|
|
184
|
+
const guard = guardAnswering({ verdict: 'allow' });
|
|
185
|
+
setGuard([guard]);
|
|
186
|
+
|
|
187
|
+
const outcome = await invokeSignup({ name: 'Allowed', email: 'allowed@test.local', password: 'pw-allowed-1' });
|
|
188
|
+
|
|
189
|
+
expect(outcome.body).toEqual({});
|
|
190
|
+
expect(outcome.loggedInAs).toBe('allowed@test.local');
|
|
191
|
+
expect(await getUserRow('allowed@test.local')).toBeDefined();
|
|
192
|
+
expect(guard.received).toHaveLength(1);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('the invite road never consults the guard: a refusing guard, an invited signup, the account is created', async () => {
|
|
196
|
+
const guard = guardAnswering({ verdict: 'refuse', sentence: 'Never shown on the invite road.' });
|
|
197
|
+
setGuard([guard]);
|
|
198
|
+
await seedInvite('invited@test.local', 'invite-token-guarded');
|
|
199
|
+
|
|
200
|
+
const outcome = await invokeSignup({ name: 'Invited', password: 'pw-invited-1', token: 'invite-token-guarded' });
|
|
201
|
+
|
|
202
|
+
expect(outcome.body).toEqual({});
|
|
203
|
+
expect(outcome.loggedInAs).toBe('invited@test.local');
|
|
204
|
+
expect(outcome.sessionEvents).toEqual(['regenerate', 'login', 'save']);
|
|
205
|
+
const created = await getUserRow('invited@test.local');
|
|
206
|
+
expect(created).toBeDefined();
|
|
207
|
+
expect(created!.emailVerified).toBe(true);
|
|
208
|
+
expect(await getDbAsSystem().get(tables.Invite, { token: 'invite-token-guarded' })).toBeUndefined();
|
|
209
|
+
expect(guard.received).toEqual([]);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('the guard receives the facts: the lowercased address, the honeypot, the render token, the coarse device hash, the user agent', async () => {
|
|
213
|
+
const guard = guardAnswering({ verdict: 'allow' });
|
|
214
|
+
setGuard([guard]);
|
|
215
|
+
|
|
216
|
+
await invokeSignup({
|
|
217
|
+
name: 'Facts',
|
|
218
|
+
email: ' Facts.Seen@Test.local ',
|
|
219
|
+
password: 'pw-facts-1',
|
|
220
|
+
website: '',
|
|
221
|
+
formToken: '1700000000000.abc',
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
expect(guard.received).toEqual([
|
|
225
|
+
{
|
|
226
|
+
email: 'facts.seen@test.local',
|
|
227
|
+
honeypot: '',
|
|
228
|
+
formToken: '1700000000000.abc',
|
|
229
|
+
clientDevice: new RequestDigests().coarseIp(CLIENT_ADDRESS),
|
|
230
|
+
hasUserAgent: true,
|
|
231
|
+
},
|
|
232
|
+
]);
|
|
233
|
+
// The device is a keyed digest of the address, never the address.
|
|
234
|
+
expect(JSON.stringify(guard.received)).not.toContain(CLIENT_ADDRESS);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('a form without the honeypot or the token, from a client without a user agent, says so in the facts', async () => {
|
|
238
|
+
const guard = guardAnswering({ verdict: 'allow' });
|
|
239
|
+
setGuard([guard]);
|
|
240
|
+
|
|
241
|
+
await invokeSignup({ name: 'Bare', email: 'bare@test.local', password: 'pw-bare-1' }, {});
|
|
242
|
+
|
|
243
|
+
expect(guard.received).toHaveLength(1);
|
|
244
|
+
expect(guard.received[0].honeypot).toBeUndefined();
|
|
245
|
+
expect(guard.received[0].formToken).toBeUndefined();
|
|
246
|
+
expect(guard.received[0].hasUserAgent).toBe(false);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('a throwaway-mail domain is refused with the one sentence before the guard runs — no account, no session', async () => {
|
|
250
|
+
const guard = guardAnswering({ verdict: 'allow' });
|
|
251
|
+
setGuard([guard]);
|
|
252
|
+
|
|
253
|
+
const outcome = await invokeSignup({ name: 'Throwaway', email: 'someone@Mailinator.com', password: 'pw-throw-1' });
|
|
254
|
+
|
|
255
|
+
expect(outcome.body).toEqual({ error: DISPOSABLE_REFUSAL });
|
|
256
|
+
expect(outcome.loggedInAs).toBeUndefined();
|
|
257
|
+
expect(outcome.sessionEvents).toEqual([]);
|
|
258
|
+
expect(await userRowCount()).toBe(0);
|
|
259
|
+
expect(sendEmail).not.toHaveBeenCalled();
|
|
260
|
+
expect(guard.received).toEqual([]);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('a subdomain of a listed domain is refused with it', async () => {
|
|
264
|
+
const outcome = await invokeSignup({
|
|
265
|
+
name: 'Throwaway',
|
|
266
|
+
email: 'someone@mail.mailinator.com',
|
|
267
|
+
password: 'pw-throw-2',
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
expect(outcome.body).toEqual({ error: DISPOSABLE_REFUSAL });
|
|
271
|
+
expect(outcome.loggedInAs).toBeUndefined();
|
|
272
|
+
expect(await userRowCount()).toBe(0);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('a domain that merely ends with a listed name, and a real domain, are allowed', async () => {
|
|
276
|
+
const lookalike = await invokeSignup({
|
|
277
|
+
name: 'Lookalike',
|
|
278
|
+
email: 'someone@notmailinator.com',
|
|
279
|
+
password: 'pw-look-1',
|
|
280
|
+
});
|
|
281
|
+
expect(lookalike.body).toEqual({});
|
|
282
|
+
expect(await getUserRow('someone@notmailinator.com')).toBeDefined();
|
|
283
|
+
|
|
284
|
+
const real = await invokeSignup({ name: 'Real', email: 'someone@example.com', password: 'pw-real-1' });
|
|
285
|
+
expect(real.body).toEqual({});
|
|
286
|
+
expect(real.loggedInAs).toBe('someone@example.com');
|
|
287
|
+
expect(await getUserRow('someone@example.com')).toBeDefined();
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('an invited throwaway address is not refused by the list: the invite road is unchanged', async () => {
|
|
291
|
+
await seedInvite('someone@mailinator.com', 'invite-token-throwaway');
|
|
292
|
+
|
|
293
|
+
const outcome = await invokeSignup({ name: 'Invited', password: 'pw-invited-2', token: 'invite-token-throwaway' });
|
|
294
|
+
|
|
295
|
+
expect(outcome.body).toEqual({});
|
|
296
|
+
expect(outcome.loggedInAs).toBe('someone@mailinator.com');
|
|
297
|
+
expect(await getUserRow('someone@mailinator.com')).toBeDefined();
|
|
298
|
+
});
|
|
299
|
+
});
|
|
@@ -53,6 +53,10 @@ export class UserServerTestEnvironment {
|
|
|
53
53
|
// No shared window store registered: the counted throttle windows use the library's own
|
|
54
54
|
// per-process store (the suites load src, never the generated source graph).
|
|
55
55
|
objectCache['@proteinjs/user-server/DefaultThrottleWindowStoreFactory'] = [];
|
|
56
|
+
// No sign-up guard registered: the signup route's no-invite road allows every submission
|
|
57
|
+
// (the library's own default), as a deployment with no registration does — a suite that
|
|
58
|
+
// registers one seeds this key itself.
|
|
59
|
+
objectCache['@proteinjs/user-server/DefaultSignupGuard'] = [];
|
|
56
60
|
Session.setData({
|
|
57
61
|
sessionId: 'test-session',
|
|
58
62
|
user: 'guest',
|