Repository navigation
Expand file tree
/
Copy pathglobal-setup.ts
More file actions
161 lines (148 loc) · 6.5 KB
/
Copy pathglobal-setup.ts
File metadata and controls
161 lines (148 loc) · 6.5 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
/**
* End-to-end global setup.
*
* A full run signs up a dozen accounts from one address, which is exactly what
* the `auth.signup.ip` limit (5 per hour) exists to stop — so without this the
* suite spends most of its time proving its own rate limiter works.
*
* The limits are NOT disabled. They are raised through the product's own
* `rate_limit_overrides` system setting, which means:
*
* - the limiter still runs on every request, so a regression that removed it
* would still be caught elsewhere;
* - the override mechanism itself gets exercised on every CI run;
* - `rate-limit.spec.ts` can set a deliberately tiny override and assert that
* blocking still happens.
*
* It also refuses to touch anything that is not obviously a local database.
*
* WHAT IT PUTS BACK
* -----------------
* Whatever `rate_limit_overrides` held before the run is written to
* `OVERRIDE_BACKUP` and restored by `global-teardown.ts`, including the case
* where it held nothing — then the row is removed again rather than left at the
* test values.
*
* That teardown was missing for a long time and the consequence was not
* theoretical: a developer database was found sitting at 500 signups per hour
* per IP instead of 5, weeks after the run that raised it, because the suite
* loosened the limiter and nothing ever tightened it again. Manual testing
* after any run was therefore happening against abuse controls that were
* effectively off. The limits are a security control; a test must hand them
* back exactly as it found them.
*/
import { config } from "dotenv";
import pg from "pg";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { SEEDED_CODE, SEEDED_CODE_LAST4 } from "./support";
config({ path: ".env", quiet: true });
export const LOCAL_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "postgres"]);
/**
* Where the pre-run value of `rate_limit_overrides` is parked.
*
* A file rather than another settings row: the teardown has to be able to tell
* "there was no override before" from "the override was empty", and a row that
* itself needs cleaning up is the problem this is meant to solve.
*/
export const OVERRIDE_BACKUP = path.join(os.tmpdir(), "inkloom-e2e-rate-limit-backup.json");
/** Generous enough for a whole suite; still finite. */
const E2E_OVERRIDES = {
"auth.signup.ip": { limit: 500 },
"auth.login.ip": { limit: 500 },
"auth.login.account": { limit: 200 },
"auth.forgot_password.email": { limit: 100 },
"auth.forgot_password.ip": { limit: 200 },
"auth.resend_verification.account": { limit: 100 },
"code.redeem.user": { limit: 200 },
"code.redeem.ip": { limit: 500 },
"support.submit.user": { limit: 100 },
"support.submit.ip": { limit: 200 },
};
export default async function globalSetup() {
/*
* A DEPLOYED target prepares nothing, and must not.
*
* This setup raises the rate limits and clears the counters so a full suite
* is not throttled by the limiter it shares with production. That is right
* for a local run, which creates dozens of accounts. It is wrong for the
* post-deploy smoke test, which makes about twenty read-only requests
* against a live environment — rewriting that environment's abuse controls
* to check that the marketing pages render is a far larger action than the
* check itself, and it would leave them raised if the run were interrupted.
*
* So a run pointed at E2E_BASE_URL with no database simply skips this. A run
* with a database still goes through every guard below, including the
* refusal to touch anything non-local.
*/
const url = process.env.DATABASE_URL;
if (!url && process.env.E2E_BASE_URL) {
console.log(
` e2e: testing ${process.env.E2E_BASE_URL} as-is — no database, so rate limits are left alone`,
);
return;
}
if (!url) throw new Error("DATABASE_URL is not set; cannot prepare the e2e environment.");
const parsed = new URL(url);
if (!LOCAL_HOSTS.has(parsed.hostname)) {
throw new Error(
`Refusing to prepare e2e against a non-local database (${parsed.hostname}). ` +
"These tests create accounts and rewrite settings.",
);
}
const client = new pg.Client({ connectionString: url });
await client.connect();
try {
// Start from a clean slate so a previous run's counters do not carry over.
await client.query("DELETE FROM rate_limit_events");
/*
* Save whatever is there before touching it. `rows[0]` being undefined is a
* meaningful state, not an error: it means no override existed, and the
* teardown must then delete the row rather than restore an empty object.
*/
const existing = await client.query(
"SELECT value FROM system_settings WHERE key = 'rate_limit_overrides'",
);
fs.writeFileSync(
OVERRIDE_BACKUP,
JSON.stringify({ existed: existing.rowCount > 0, value: existing.rows[0]?.value ?? null }),
);
await client.query(
`INSERT INTO system_settings (id, key, value, description, high_risk)
VALUES ($1, 'rate_limit_overrides', $2::jsonb,
'Per-bucket overrides for the rate limiter', true)
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = now()`,
[`set_e2e_${Date.now()}`, JSON.stringify(E2E_OVERRIDES)],
);
// Every feature flag at its declared default, so one test flipping a flag
// cannot leave the next run in a surprising state.
await client.query(`
UPDATE feature_flags SET enabled = true
WHERE key IN ('signup_enabled','code_redemption_enabled','promotional_grants_enabled',
'support_form_enabled','analytics_enabled','early_access_open')
`);
await client.query(`
UPDATE feature_flags SET enabled = false
WHERE key IN ('generation_enabled','payments_enabled','maintenance_banner_enabled')
`);
// The suite redeems this repeatedly; make sure it exists and is open.
const campaign = await client.query(
"SELECT id, status FROM access_code_campaigns WHERE code_last4 = $1 LIMIT 1",
[SEEDED_CODE_LAST4],
);
if (campaign.rowCount === 0) {
throw new Error(
`The ${SEEDED_CODE} campaign is missing (no campaign ends '${SEEDED_CODE_LAST4}'). ` +
"Run `pnpm db:seed` before the e2e suite.",
);
}
await client.query(
"UPDATE access_code_campaigns SET status = 'enabled', paused_at = NULL WHERE id = $1",
[campaign.rows[0].id],
);
console.log(` e2e: rate-limit overrides applied to ${parsed.pathname.slice(1)}`);
} finally {
await client.end();
}
}