@proteinjs/user-server 1.26.1 → 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 +11 -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/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/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/SignupGuard.ts +58 -0
- package/test/SignupGuardSeam.test.ts +299 -0
- package/test/UserServerTestEnvironment.ts +4 -0
package/src/routes/signup.ts
CHANGED
|
@@ -3,10 +3,26 @@ import { Logger } from '@proteinjs/logger';
|
|
|
3
3
|
import { RequestDigests } from '@proteinjs/util-node';
|
|
4
4
|
import { UserSignup, routes } from '@proteinjs/user';
|
|
5
5
|
import { establishSession } from '../authentication/establishSession';
|
|
6
|
+
import { DisposableEmailDomains } from '../services/DisposableEmailDomains';
|
|
6
7
|
import { Signup } from '../services/Signup';
|
|
8
|
+
import { getDefaultSignupGuard, SignupGuardFacts } from '../services/SignupGuard';
|
|
9
|
+
import { ClientAddress } from '../throttle/ClientAddress';
|
|
7
10
|
|
|
8
11
|
const logger = new Logger({ name: 'signup' });
|
|
9
12
|
|
|
13
|
+
/**
|
|
14
|
+
* The request body: `UserSignup` (+ an optional invite `token`; the invite carries the email on
|
|
15
|
+
* that path), and the two fields a form may carry for the consumer's guard — read here, judged
|
|
16
|
+
* by the guard, never by this route.
|
|
17
|
+
*/
|
|
18
|
+
type SignupSubmission = Partial<UserSignup> & {
|
|
19
|
+
token?: string;
|
|
20
|
+
/** The honeypot field — no person sees it, so a value in it is bot-shaped. */
|
|
21
|
+
website?: unknown;
|
|
22
|
+
/** The render-moment token a form carries when the consumer's posture mints one. */
|
|
23
|
+
formToken?: unknown;
|
|
24
|
+
};
|
|
25
|
+
|
|
10
26
|
/**
|
|
11
27
|
* `POST /user/signup` — create the account AND establish its session in the same request
|
|
12
28
|
* (auto-login), so a new user lands in the app instead of being bounced to the login form to
|
|
@@ -14,37 +30,61 @@ const logger = new Logger({ name: 'signup' });
|
|
|
14
30
|
* because session establishment is a request-level concern services never see; the domain flow
|
|
15
31
|
* (invite validation, account creation, notification emails) stays owned by `Signup.createUser`.
|
|
16
32
|
*
|
|
17
|
-
* Body: `
|
|
18
|
-
*
|
|
19
|
-
* user-readable copy otherwise.
|
|
33
|
+
* Body: `SignupSubmission`. Response mirrors the login route's contract: 200 with `{}` on
|
|
34
|
+
* success, `{ error }` with user-readable copy otherwise.
|
|
20
35
|
*
|
|
21
36
|
* When the email is already registered the response is byte-identical to success but NO session
|
|
22
37
|
* is minted — auto-login must never hand out a session for an account the caller didn't just
|
|
23
38
|
* create; the caller falls through to the login screen exactly as before. Existence is reported
|
|
24
39
|
* to the mailbox owner by email, never to the caller.
|
|
40
|
+
*
|
|
41
|
+
* The NO-INVITE road passes two doors before any write, in this order: the throwaway-mail list
|
|
42
|
+
* (`DisposableEmailDomains`, refused with its one sentence) and the consumer's registered
|
|
43
|
+
* `DefaultSignupGuard`, handed the submission's facts (`SignupGuardFacts`) — `refuse` answers
|
|
44
|
+
* `{ error: sentence }`, `drop` answers exactly as success, and neither writes an account or
|
|
45
|
+
* mints a session. The invite road passes neither: an invited address was chosen by an admin.
|
|
25
46
|
*/
|
|
26
47
|
export const signup: Route = {
|
|
27
48
|
path: routes.signup.path,
|
|
28
49
|
method: routes.signup.method,
|
|
29
50
|
onRequest: async (request: any, response): Promise<void> => {
|
|
30
|
-
const body:
|
|
51
|
+
const body: SignupSubmission = request.body ?? {};
|
|
31
52
|
if (!body.name || !body.password) {
|
|
32
53
|
response.send({ error: 'Name and password cannot be blank' });
|
|
33
54
|
return;
|
|
34
55
|
}
|
|
35
56
|
|
|
57
|
+
const digests = new RequestDigests();
|
|
58
|
+
const email = typeof body.email === 'string' ? body.email.trim() : undefined;
|
|
59
|
+
if (!body.token && email) {
|
|
60
|
+
if (new DisposableEmailDomains().isDisposable(email)) {
|
|
61
|
+
logger.info({ message: 'Signup refused: throwaway-mail domain', obj: { account: digests.account(email) } });
|
|
62
|
+
response.send({ error: DisposableEmailDomains.REFUSAL });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const facts = signupFacts(request, body, email);
|
|
67
|
+
const verdict = await getDefaultSignupGuard().check(facts);
|
|
68
|
+
if (verdict.verdict !== 'allow') {
|
|
69
|
+
logger.info({
|
|
70
|
+
message: `Signup ${verdict.verdict === 'refuse' ? 'refused' : 'dropped'} by the guard`,
|
|
71
|
+
obj: { account: digests.account(email), device: facts.clientDevice },
|
|
72
|
+
});
|
|
73
|
+
// A drop answers exactly as success: the caller learns nothing it can iterate against.
|
|
74
|
+
response.send(verdict.verdict === 'refuse' ? { error: verdict.sentence } : {});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
36
79
|
let result;
|
|
37
80
|
try {
|
|
38
|
-
result = await new Signup().createUser(
|
|
39
|
-
{ name: body.name, email: body.email, password: body.password },
|
|
40
|
-
body.token
|
|
41
|
-
);
|
|
81
|
+
result = await new Signup().createUser({ name: body.name, email, password: body.password }, body.token);
|
|
42
82
|
} catch (error: any) {
|
|
43
83
|
// createUser throws plain-words errors deliberately (invite expired / invite required /
|
|
44
84
|
// email missing); the message is the user-facing contract, same as the RPC layer's. The log
|
|
45
85
|
// gets the error through the digest door: a concurrent double sign-up's unique-index refusal
|
|
46
86
|
// names the address in the database's own words.
|
|
47
|
-
logger.error({ message: 'Signup failed', error:
|
|
87
|
+
logger.error({ message: 'Signup failed', error: digests.redactError(error) });
|
|
48
88
|
response.send({ error: error instanceof Error ? error.message : 'Sign up failed.' });
|
|
49
89
|
return;
|
|
50
90
|
}
|
|
@@ -56,3 +96,12 @@ export const signup: Route = {
|
|
|
56
96
|
response.send({});
|
|
57
97
|
},
|
|
58
98
|
};
|
|
99
|
+
|
|
100
|
+
/** The facts the guard judges — the address lowercased, the form's two optional fields as sent, the device as a digest. */
|
|
101
|
+
const signupFacts = (request: any, body: SignupSubmission, email: string): SignupGuardFacts => ({
|
|
102
|
+
email: email.toLowerCase(),
|
|
103
|
+
honeypot: typeof body.website === 'string' ? body.website : undefined,
|
|
104
|
+
formToken: typeof body.formToken === 'string' ? body.formToken : undefined,
|
|
105
|
+
clientDevice: new RequestDigests().coarseIp(new ClientAddress().of(request)),
|
|
106
|
+
hasUserAgent: Boolean(request.headers?.['user-agent']),
|
|
107
|
+
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The common throwaway-mail domains — inboxes anyone can open or that are minted on demand with
|
|
3
|
+
* nobody behind them — so a sign-up from one is an account whose mail no one reads. A short list
|
|
4
|
+
* in code, no vendor, applied on the no-invite road before the guard: a deliverability check
|
|
5
|
+
* refuses only a domain that takes no mail, and a throwaway on a real domain passes it; this list
|
|
6
|
+
* is the other half. Written 2026-10-28.
|
|
7
|
+
*
|
|
8
|
+
* The rule for adding a domain: it goes on the list when its service describes itself as
|
|
9
|
+
* temporary, disposable or anonymous mail — addresses with no owner, read by whoever asks — and
|
|
10
|
+
* never when people hold their own mailboxes there (a webmail, a school, a company, a forwarding
|
|
11
|
+
* service with an owner behind each alias). Keep it short: the list carries the names that
|
|
12
|
+
* actually show up at the door, and a consumer's guard bounds the rest. Entries are lowercase
|
|
13
|
+
* registrable domains, one per line; a subdomain of a listed domain is refused with it.
|
|
14
|
+
*/
|
|
15
|
+
export class DisposableEmailDomains {
|
|
16
|
+
/** The one sentence a refused address is told — read by the person, shown in full. */
|
|
17
|
+
static readonly REFUSAL = 'Use an email address you can read mail at.';
|
|
18
|
+
|
|
19
|
+
private static readonly DOMAINS: ReadonlySet<string> = new Set([
|
|
20
|
+
'10minemail.com',
|
|
21
|
+
'10minutemail.com',
|
|
22
|
+
'10minutemail.net',
|
|
23
|
+
'anonbox.net',
|
|
24
|
+
'burnermail.io',
|
|
25
|
+
'cool.fr.nf',
|
|
26
|
+
'courriel.fr.nf',
|
|
27
|
+
'crazymailing.com',
|
|
28
|
+
'discard.email',
|
|
29
|
+
'dispostable.com',
|
|
30
|
+
'dropmail.me',
|
|
31
|
+
'emailfake.com',
|
|
32
|
+
'emailondeck.com',
|
|
33
|
+
'fakeinbox.com',
|
|
34
|
+
'fakemail.net',
|
|
35
|
+
'generator.email',
|
|
36
|
+
'getairmail.com',
|
|
37
|
+
'getnada.com',
|
|
38
|
+
'grr.la',
|
|
39
|
+
'guerrillamail.biz',
|
|
40
|
+
'guerrillamail.com',
|
|
41
|
+
'guerrillamail.de',
|
|
42
|
+
'guerrillamail.net',
|
|
43
|
+
'guerrillamail.org',
|
|
44
|
+
'guerrillamailblock.com',
|
|
45
|
+
'harakirimail.com',
|
|
46
|
+
'inboxkitten.com',
|
|
47
|
+
'jetable.fr.nf',
|
|
48
|
+
'jetable.org',
|
|
49
|
+
'mail-temp.com',
|
|
50
|
+
'mail7.io',
|
|
51
|
+
'mailcatch.com',
|
|
52
|
+
'maildrop.cc',
|
|
53
|
+
'mailexpire.com',
|
|
54
|
+
'mailin8r.com',
|
|
55
|
+
'mailinater.com',
|
|
56
|
+
'mailinator.com',
|
|
57
|
+
'mailinator2.com',
|
|
58
|
+
'mailnesia.com',
|
|
59
|
+
'mailpoof.com',
|
|
60
|
+
'mailsac.com',
|
|
61
|
+
'mailtemp.net',
|
|
62
|
+
'mega.zik.dj',
|
|
63
|
+
'meltmail.com',
|
|
64
|
+
'mintemail.com',
|
|
65
|
+
'mohmal.com',
|
|
66
|
+
'moncourrier.fr.nf',
|
|
67
|
+
'monemail.fr.nf',
|
|
68
|
+
'monmail.fr.nf',
|
|
69
|
+
'mytemp.email',
|
|
70
|
+
'nomail.xl.cx',
|
|
71
|
+
'nospam.ze.tc',
|
|
72
|
+
'pokemail.net',
|
|
73
|
+
'sharklasers.com',
|
|
74
|
+
'sogetthis.com',
|
|
75
|
+
'spam4.me',
|
|
76
|
+
'spambox.us',
|
|
77
|
+
'speed.1s.fr',
|
|
78
|
+
'temp-mail.io',
|
|
79
|
+
'temp-mail.org',
|
|
80
|
+
'tempail.com',
|
|
81
|
+
'tempinbox.com',
|
|
82
|
+
'tempmail.com',
|
|
83
|
+
'tempmail.dev',
|
|
84
|
+
'tempmailo.com',
|
|
85
|
+
'tempr.email',
|
|
86
|
+
'throwawaymail.com',
|
|
87
|
+
'trashmail.com',
|
|
88
|
+
'trashmail.me',
|
|
89
|
+
'trashmail.net',
|
|
90
|
+
'yopmail.com',
|
|
91
|
+
'yopmail.fr',
|
|
92
|
+
'yopmail.net',
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
/** Whether the address's domain is a listed domain or a subdomain of one; the case of the address does not matter. */
|
|
96
|
+
isDisposable(email: string): boolean {
|
|
97
|
+
const domain = this.domainOf(email);
|
|
98
|
+
if (!domain) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// `mail.mailinator.com` is refused with `mailinator.com`: every suffix at a label boundary
|
|
103
|
+
// is looked up, so a domain that merely ends with a listed name (`notmailinator.com`) is not.
|
|
104
|
+
const labels = domain.split('.');
|
|
105
|
+
for (let start = 0; start < labels.length - 1; start++) {
|
|
106
|
+
if (DisposableEmailDomains.DOMAINS.has(labels.slice(start).join('.'))) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The part after the last `@`, trimmed and lowercased; undefined when there is none. */
|
|
114
|
+
private domainOf(email: string): string | undefined {
|
|
115
|
+
const at = email.lastIndexOf('@');
|
|
116
|
+
if (at < 0) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
const domain = email
|
|
120
|
+
.slice(at + 1)
|
|
121
|
+
.trim()
|
|
122
|
+
.toLowerCase();
|
|
123
|
+
return domain || undefined;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Loadable, SourceRepository } from '@proteinjs/reflection';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What the signup route hands the guard about a no-invite submission: request facts only the
|
|
5
|
+
* route can read, reduced to what an abuse posture needs and never anything that identifies the
|
|
6
|
+
* client on its own.
|
|
7
|
+
*/
|
|
8
|
+
export type SignupGuardFacts = {
|
|
9
|
+
/** The address the form carries, trimmed and lowercased — the account's address if the signup goes through. */
|
|
10
|
+
email: string;
|
|
11
|
+
/**
|
|
12
|
+
* The form's honeypot field (`website`) when the form carries one — a field no person sees, so
|
|
13
|
+
* a value in it is bot-shaped. Undefined when the form has no such field.
|
|
14
|
+
*/
|
|
15
|
+
honeypot?: string;
|
|
16
|
+
/** The render-moment token the form carries, when it carries one — a consumer's time trap verifies it. */
|
|
17
|
+
formToken?: string;
|
|
18
|
+
/**
|
|
19
|
+
* The coarse hash of the client's address (`RequestDigests.coarseIp` of the address `ClientAddress`
|
|
20
|
+
* reads): the key a per-device window counts on and the name a log line gives a device. Never
|
|
21
|
+
* the address itself.
|
|
22
|
+
*/
|
|
23
|
+
clientDevice: string;
|
|
24
|
+
/** Whether the request carried a User-Agent header at all. */
|
|
25
|
+
hasUserAgent: boolean;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The guard's answer, one of three:
|
|
30
|
+
* - `allow` — the signup continues: the account is created and its session established;
|
|
31
|
+
* - `refuse` — the route answers `{ error: sentence }` with no account and no session; the
|
|
32
|
+
* sentence is read by the person, so it is plain words in full;
|
|
33
|
+
* - `drop` — the route answers exactly as it answers success (`{}`) with no account and no
|
|
34
|
+
* session, and the caller cannot tell: the silent drop for a bot-shaped submission, which gets
|
|
35
|
+
* nothing to iterate against.
|
|
36
|
+
*/
|
|
37
|
+
export type SignupGuardVerdict = { verdict: 'allow' } | { verdict: 'refuse'; sentence: string } | { verdict: 'drop' };
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The seam a consumer plugs its sign-up abuse posture into — a honeypot, a time trap, a
|
|
41
|
+
* per-device throttle, a daily breaker, whatever it holds — by registering a class that
|
|
42
|
+
* implements this interface (the `DefaultInviteConfigFactory` pattern; the first registration
|
|
43
|
+
* found is the guard). The signup route consults it on the NO-INVITE road only: an invited
|
|
44
|
+
* address was chosen by an admin, and the invite road is unchanged. With no registration every
|
|
45
|
+
* submission is allowed.
|
|
46
|
+
*/
|
|
47
|
+
export interface DefaultSignupGuard extends Loadable {
|
|
48
|
+
check(facts: SignupGuardFacts): SignupGuardVerdict | Promise<SignupGuardVerdict>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const getDefaultSignupGuard = (): DefaultSignupGuard => {
|
|
52
|
+
const allowEvery: DefaultSignupGuard = {
|
|
53
|
+
check: (): SignupGuardVerdict => ({ verdict: 'allow' }),
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const guard = SourceRepository.get().object<DefaultSignupGuard>('@proteinjs/user-server/DefaultSignupGuard');
|
|
57
|
+
return guard || allowEvery;
|
|
58
|
+
};
|
|
@@ -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',
|