@starci/hfs 4.11.11 → 4.11.12
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 +9 -0
- package/package.json +1 -1
- package/runtime/knowledge/hfs/canon-pins.yaml +6 -6
- package/runtime/knowledge/hfs/rules.yaml +1 -1
- package/runtime/modules/kernel/failure-codes.yaml +5 -5
- package/runtime/scripts/hfs/path-findings.mjs +4 -2
- package/scaffold/add-inbox-lite.mjs +6 -10
- package/scaffold/add-queue-lite.mjs +7 -2
- package/scaffold/add-table.mjs +5 -0
- package/scaffold/app.mjs +15 -3
- package/scaffold/lite-exports.mjs +23 -0
- package/templates/be/skeleton-lite/src/modules/platform/database/database.options.ts +4 -1
- package/templates/be/skeleton-lite/src/modules/platform/database/entities.ts +4 -1
- package/templates/be/skeleton-lite/src/modules/platform/database/index.ts +0 -2
- package/templates/be/skeleton-lite/src/modules/platform/http-security/index.ts +1 -0
- package/templates/be/skeleton-lite/src/modules/platform/http-security/persistence/entities/index.ts +4 -0
- package/templates/be/skeleton-lite/src/modules/platform/http-security/persistence/entities/rate-window.entity.ts +17 -0
- package/templates/be/skeleton-lite/src/modules/platform/http-security/rate-limit.guard.spec.ts +260 -0
- package/templates/be/skeleton-lite/src/modules/platform/http-security/rate-limit.guard.ts +133 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 4.11.12 - 2026-10-29
|
|
4
|
+
|
|
5
|
+
- Changed: the skeleton canon gate (`src/scaffold-template-canon-be.spec.mjs`) lints every file under `be/src/modules/platform` of both editions with the starci-be rules, no longer `platform/http` alone; both editions are green.
|
|
6
|
+
- Fixed: the lite skeleton's `platform/http-security` reads and writes its rate limit windows through `RateWindowEntity` on the EntityManager (R334), no longer through full's `rate-limit.sql.ts` and `.query(`: a live window grows in one atomic `increment`, an ended one restarts with `update`, a missing one opens with an `orIgnore` insert builder, and the prune deletes a bounded `find` batch. The entity maps `public.http_security_rate_windows` of the baseline migration (`persistence/entities/rate-window.entity.ts`, `httpSecurityEntities` in its barrel, listed in `PRIMARY_ENTITIES`); a lite platform capability takes its `persistence/` from the lite overlay alone. The guard spec uses the kit's `mockEntityManager` and `queueQueryBuilders`.
|
|
7
|
+
- Changed: a fresh lite app no longer carries the raw SQL helpers nothing in it calls: `platform/database/database.sql.ts` and `database.client.ts` (with its spec) are written by the first generator that needs them (`add table` the sql helpers, `add queue`, `add job` and `add webhook` both), and the barrel exports `sql`, `callDbFunction` and `advisoryXactLock` are registered by those generators (`jobs` registers `advisoryXactLock`); `notify` is no longer exported by the starter.
|
|
8
|
+
- Fixed: `source-suffix` (BE_SOURCE_FORM, R89) admits the one file a file slot names by its path, read from the slot manifest: the lite entity registry `src/modules/platform/database/entities.ts` (slot `be.platform.database.entities`) was refused for having no role suffix.
|
|
9
|
+
- Fixed: the lite skeleton types its entity lists with `EntityClass` (`new () => object`, declared in `platform/database/database.options.ts` as full declares it): `DatabaseOptions.entities` and `PRIMARY_ENTITIES` of `entities.ts` were `ReadonlyArray<Function>`, which `starci-be/no-function-or-eval` refuses.
|
|
10
|
+
- Changed: R334 `BE_PERSISTENCE_NO_RAW_SQL` covers test code with one owner exception (ruling lite-test-sql-owner): specs, builders and fixtures seed and assert through entities on `world.db.<connection>`, and only `src/tests/world` holds raw SQL beside `platform/database`, as named world helpers for what entities cannot express. The bundled catalog law and the failure-code hint say so; the bundled canon pins name hfs 4.11.12, eslint-canon-be 3.8.11, eslint-canon-fe 9.5.13 and the Grammar family 0.15.24.
|
|
11
|
+
|
|
3
12
|
## 4.11.11 - 2026-10-29
|
|
4
13
|
|
|
5
14
|
- Changed: `server-key <env> --backup github` stores the server identity as the secret `SOPS_AGE_KEY` of the GitHub Environment `<env>` (`BACKUP_SECRET`, `gh secret set SOPS_AGE_KEY --env <env>`), the name the deploy workflows read: an Environment secret overrides a repository secret of the same name for every job bound to that Environment, so the deploy of `<env>` decrypts with that environment's own server key. `backupSecretName` and the `SOPS_AGE_KEY_<ENV>` name are gone.
|
package/package.json
CHANGED
|
@@ -31,35 +31,35 @@ pins:
|
|
|
31
31
|
source: packages/event-stream/package.json
|
|
32
32
|
why: 'the one server-sent-events client of every front end (fe-stack event-stream, owner:event-stream-package): the transport client layer (modules/api/sse/clients) creates one instance for the app with the bearer token source and the stream operations follow on it; it carries Last-Event-ID resume, the capped doubling backoff, the heartbeat timeout, the single 401 renewal and the useSWRSubscription adapter, so no app keeps a hand-written reader.'
|
|
33
33
|
'@starci/grammar':
|
|
34
|
-
version: 0.15.
|
|
34
|
+
version: 0.15.24
|
|
35
35
|
group: starci
|
|
36
36
|
install: registry
|
|
37
37
|
side: fe
|
|
38
38
|
source: packages/grammar/package.json
|
|
39
39
|
why: 'the grammar front ends read: the brand layer sets `--font-sans` and `--font-mono` and the grammar reads them; it carries the Input tel kind, the copyable and read-only Input, the IconButton disclosure props and count badge, ChatBubble, FillViewport, Textarea mentions and Enter-to-send, the leading PrimaryRailLayout rail, the plate-less Image, ListRow, the soft Alert, Progress steps, the floating primary IconButton, FlowGraph, the STATE-8 loading views and the bordered selectable TagGroup chips. The runtime source version wins for a @starci package.'
|
|
40
40
|
'@starci/eslint-canon-be':
|
|
41
|
-
version: 3.8.
|
|
41
|
+
version: 3.8.11
|
|
42
42
|
group: starci
|
|
43
43
|
install: registry
|
|
44
44
|
side: be
|
|
45
45
|
source: packages/eslint-canon-be/package.json
|
|
46
46
|
why: 'the back-end ESLint canon: `loadHfs(import.meta.url)` of be/eslint.config.mjs finds the app-root hfs.json (kind app) and gives every linted file the view of the be side, and the project graph is built per side; its bundled runtime copies (slots, canon-pins, failure codes, the architecture machine) put .starcistacks and .sops.yaml at the app root; `starciBeConfig({ hfs: loadHfs(import.meta.url) })` is the typed factory and the BE-CONVENTION laws are its rules.'
|
|
47
47
|
'@starci/eslint-canon-fe':
|
|
48
|
-
version: 9.5.
|
|
48
|
+
version: 9.5.13
|
|
49
49
|
group: starci
|
|
50
50
|
install: registry
|
|
51
51
|
side: fe
|
|
52
52
|
source: packages/eslint-canon-fe/package.json
|
|
53
53
|
why: 'the front-end ESLint canon: `loadHfs(import.meta.url)` of fe/eslint.config.mjs finds the app-root hfs.json (kind app) and gives every linted file the view of the fe side, and the project graph is built per side; its bundled runtime copies (slots, canon-pins, failure codes, the architecture machine) put .starcistacks and .sops.yaml at the app root; the front end has no tests (FE_NO_TESTS R97 in hfs) and no-vietnamese-in-source (R91) applies.'
|
|
54
54
|
'@starci/eslint-grammar-core':
|
|
55
|
-
version: 0.15.
|
|
55
|
+
version: 0.15.24
|
|
56
56
|
group: starci
|
|
57
57
|
install: registry
|
|
58
58
|
side: fe
|
|
59
59
|
source: packages/eslint-grammar-core/package.json
|
|
60
60
|
why: 'the StarCi Grammar component API lint, versioned with @starci/grammar; front ends compose this family package with the universal FE canon.'
|
|
61
61
|
'@starci/eslint-grammar-offset-pop':
|
|
62
|
-
version: 0.15.
|
|
62
|
+
version: 0.15.24
|
|
63
63
|
group: starci
|
|
64
64
|
install: registry
|
|
65
65
|
side: fe
|
|
@@ -106,7 +106,7 @@ pins:
|
|
|
106
106
|
source: packages/ai-router/package.json
|
|
107
107
|
why: 'the AI pool router of a back end: a NestJS module that routes OpenAI-compatible chat, stream and embeddings calls across keys, models and providers, with @starci/ai-router/testing for its specs; @starci/jest-preset compiles it as an ESM-only dependency.'
|
|
108
108
|
'@starci/hfs':
|
|
109
|
-
version: 4.11.
|
|
109
|
+
version: 4.11.12
|
|
110
110
|
group: starci
|
|
111
111
|
install: registry
|
|
112
112
|
side: both
|
|
@@ -5828,7 +5828,7 @@ rules:
|
|
|
5828
5828
|
ruling: "Lite entities only map tables the migrations create, so each entity column is checked against the migrated schema by starci app db drift, which CI runs in the build job and the release gate runs as its db-drift row."
|
|
5829
5829
|
- id: R334
|
|
5830
5830
|
code: "BE_PERSISTENCE_NO_RAW_SQL"
|
|
5831
|
-
law: "Lite domain and
|
|
5831
|
+
law: "Lite domain, feature and test code reads and writes through TypeORM entities on an EntityManager (domain code the injected one, integration and e2e specs, builders and fixtures the world's `world.db.<connection>`) (find, findOne, findBy, count, exists, insert, save, update, delete, upsert, increment, and createQueryBuilder(Entity, alias) with named parameters for joins, aggregates, FOR UPDATE / SKIP LOCKED and jsonb or vector operators; never a table-name builder, a builder with no entity or a Repository); no .query( call and no sql template outside the two raw SQL owners, platform/database and the test world src/tests/world, which wraps only what entities cannot express (a trigger, a database function, a row-level-security probe, a pgmq queue) in a named, typed world helper specs call; a database function TypeORM cannot express is called, in domain and test code alike, through the callDbFunction, advisoryXactLock and notify helpers of platform/database/database.client.ts, and callDbFunction names only a function ruleParams.be.dbFunctions or hfs.json sides.be.dbFunctions lists with a reason: a function of a migration schema-qualified (public.job_claim), a pg built-in bare (pg_notify, called in pg_catalog). That file alone composes the call statement, from the name parts its validator checked and double-quotes and a numbered parameter list. Entities only map tables db/migrations created (synchronize stays false); entity-versus-schema drift is BE_ENTITY_SCHEMA_DRIFT of starci app db drift."
|
|
5832
5832
|
kinds: [lint]
|
|
5833
5833
|
gates: [pre-commit, settle, land, ci]
|
|
5834
5834
|
failureCodes: ["BE_PERSISTENCE_NO_RAW_SQL"]
|
|
@@ -5581,12 +5581,12 @@ LITE_DB_MIGRATION:
|
|
|
5581
5581
|
kind: check-finding
|
|
5582
5582
|
|
|
5583
5583
|
BE_PERSISTENCE_NO_RAW_SQL:
|
|
5584
|
-
title: "Lite domain code runs raw SQL instead of TypeORM entities"
|
|
5585
|
-
title_vi: "Mã domain của app lite chạy SQL thô thay vì entity TypeORM"
|
|
5586
|
-
meaning_vi: "File `<path>` gọi `.query(` hoặc dùng template `sql` ngoài platform/database và
|
|
5584
|
+
title: "Lite domain, feature or test code runs raw SQL instead of TypeORM entities"
|
|
5585
|
+
title_vi: "Mã domain, feature hoặc test của app lite chạy SQL thô thay vì entity TypeORM"
|
|
5586
|
+
meaning_vi: "File `<path>` gọi `.query(` hoặc dùng template `sql` ngoài hai chủ của SQL thô là platform/database và test world src/tests/world, hoặc gọi callDbFunction với một hàm không có trong ruleParams.be.dbFunctions hay hfs.json sides.be.dbFunctions."
|
|
5587
5587
|
causes_vi:
|
|
5588
|
-
- "Vi phạm R334: dữ liệu của app lite đi qua entity TypeORM trên EntityManager (find, save, update, upsert, createQueryBuilder)."
|
|
5589
|
-
nextStep_vi: "
|
|
5588
|
+
- "Vi phạm R334: dữ liệu của app lite, kể cả spec, builder và fixture, đi qua entity TypeORM trên EntityManager (find, save, update, upsert, createQueryBuilder); spec dùng world.db.<connection>."
|
|
5589
|
+
nextStep_vi: "Đọc/ghi bằng entity trên EntityManager hoặc createQueryBuilder với tham số có tên (trong test là world.db.<connection>); hàm database TypeORM không diễn đạt được thì gọi qua callDbFunction với hàm đã khai kèm lý do; điều entity không diễn đạt được trong test (trigger, thăm dò row-level security, hàng đợi pgmq) thì gọi một helper có tên của src/tests/world."
|
|
5590
5590
|
owner: op-retry
|
|
5591
5591
|
kind: check-finding
|
|
5592
5592
|
|
|
@@ -29,6 +29,8 @@ function sourceFormContext(file, resolver, suffixes, bannedSuffixes) {
|
|
|
29
29
|
const base = path.posix.basename(file);
|
|
30
30
|
if (FREE_NAMES.has(base)) return null;
|
|
31
31
|
const slot = resolver.slot(c.slot);
|
|
32
|
+
// A file slot declares its one file by its path (be.platform.database.entities: src/modules/platform/database/entities.ts).
|
|
33
|
+
if (slot?.path === c.path) return null;
|
|
32
34
|
if ([...(slot?.requires ?? []), ...(slot?.allows ?? [])].includes(base)) return null;
|
|
33
35
|
const admitted = allowsFile(resolver, file);
|
|
34
36
|
// Environment parsing is an app slot role, not a source suffix admitted outside that matching app file.
|
|
@@ -57,8 +59,8 @@ function sourceFormFinding(file, { c, base, suffixes, bannedSuffixes }, boundSuf
|
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
/**
|
|
60
|
-
* BE_SOURCE_FORM (R89): every tracked src/ or apps/ TypeScript file of a back end is index.ts, main.ts, a
|
|
61
|
-
* be.persistence, the matching <app>.env.ts explicitly admitted by its app slot, or <kebab-name>.<suffix>.ts with <suffix> in the closed vocabulary ruleParams.be.suffixes (a name such
|
|
62
|
+
* BE_SOURCE_FORM (R89): every tracked src/ or apps/ TypeScript file of a back end is index.ts, main.ts, the one file a file
|
|
63
|
+
* slot names by its path (the lite entity registry src/modules/platform/database/entities.ts), a migration of be.persistence, the matching <app>.env.ts explicitly admitted by its app slot, or <kebab-name>.<suffix>.ts with <suffix> in the closed vocabulary ruleParams.be.suffixes (a name such
|
|
62
64
|
* as order.service.spec.ts keeps its inner words kebab-case). A suffix of ruleParams.be.bannedSuffixes anywhere in the
|
|
63
65
|
* name is refused by name. Paths no slot owns are HFS_SLOT_UNDECLARED's, not this code's.
|
|
64
66
|
*/
|
|
@@ -233,17 +233,13 @@ export function addLiteWebhookInbox({
|
|
|
233
233
|
);
|
|
234
234
|
if (!hasHandoffStorage)
|
|
235
235
|
planned[0].text = `${template("platform/migration.sql.tpl", renderValues)}\n${template("platform/client-operation-outcomes.sql.tpl", renderValues)}\n${planned[0].text}`;
|
|
236
|
-
|
|
237
|
-
|
|
236
|
+
// The sql tag and the database-function call (with its spec) the inbox and the event bus use; the lite starter leaves them out.
|
|
237
|
+
for (const file of ["database.sql.ts", "database.client.ts", "database.client.spec.ts"]) {
|
|
238
|
+
const relative = `be/src/modules/platform/database/${file}`;
|
|
239
|
+
if (fs.existsSync(path.join(root, ...relative.split("/")))) continue;
|
|
238
240
|
planned.push({
|
|
239
|
-
relative
|
|
240
|
-
text: render(
|
|
241
|
-
fs.readFileSync(
|
|
242
|
-
path.join(TEMPLATES_DIR, "be", "skeleton-lite", "src", "modules", "platform", "database", "database.sql.ts"),
|
|
243
|
-
"utf8",
|
|
244
|
-
),
|
|
245
|
-
{},
|
|
246
|
-
),
|
|
241
|
+
relative,
|
|
242
|
+
text: render(fs.readFileSync(path.join(TEMPLATES_DIR, "be", "skeleton-lite", "src", "modules", "platform", "database", file), "utf8"), {}),
|
|
247
243
|
});
|
|
248
244
|
}
|
|
249
245
|
const databaseClient = path.join(
|
|
@@ -84,12 +84,14 @@ const DATABASE_ERROR = 'be/src/modules/platform/database/errors/database.error.t
|
|
|
84
84
|
|
|
85
85
|
/** Skeleton files the lite starter leaves out and the queue capabilities use, each from its skeleton template. */
|
|
86
86
|
const SKELETON_FILES = Object.freeze([
|
|
87
|
+
['be/src/modules/platform/database/database.client.ts', 'skeleton-lite/src/modules/platform/database/database.client.ts'],
|
|
88
|
+
['be/src/modules/platform/database/database.client.spec.ts', 'skeleton-lite/src/modules/platform/database/database.client.spec.ts'],
|
|
87
89
|
['be/src/modules/platform/database/database.sql.ts', 'skeleton-lite/src/modules/platform/database/database.sql.ts'],
|
|
88
90
|
['be/src/modules/platform/primitives/sequence.policy.ts', 'skeleton/src/modules/platform/primitives/sequence.policy.ts'],
|
|
89
91
|
['be/src/modules/platform/primitives/sequence.policy.spec.ts', 'skeleton/src/modules/platform/primitives/sequence.policy.spec.ts'],
|
|
90
92
|
]);
|
|
91
93
|
|
|
92
|
-
/** The sql tag of the outbox and claim statements (with its identifier error) and the in-order relay the queue adapters walk their rows with. */
|
|
94
|
+
/** The database-function call and the sql tag of the outbox and claim statements (with its identifier error) and the in-order relay the queue adapters walk their rows with. */
|
|
93
95
|
function skeletonFiles(repoRoot) {
|
|
94
96
|
const created = SKELETON_FILES.filter(([relative]) => !fs.existsSync(path.join(repoRoot, ...relative.split('/'))))
|
|
95
97
|
.map(([relative, source]) => ({ relative, text: render(template(`be/${source}`), {}) }));
|
|
@@ -364,7 +366,10 @@ export function planLiteQueue({ repoRoot, repo, noun, patterns, now }) {
|
|
|
364
366
|
fs.writeFileSync(target, entry.text);
|
|
365
367
|
}
|
|
366
368
|
for (const patch of patches) fs.writeFileSync(patch.file, patch.text);
|
|
367
|
-
const exported =
|
|
369
|
+
const exported = [
|
|
370
|
+
...registerLiteExports({ root: repoRoot, generator: 'queue' }),
|
|
371
|
+
...(patterns.includes('fenced-job') ? registerLiteExports({ root: repoRoot, generator: 'jobs' }) : []),
|
|
372
|
+
];
|
|
368
373
|
const sealed = sealRedisMembers(repoRoot, stack.environments);
|
|
369
374
|
const changed = patches.map((patch) => path.relative(repoRoot, patch.file).split(path.sep).join('/'));
|
|
370
375
|
return [...new Set([...created.map((entry) => entry.relative), ...changed, ...exported, ...sealed])];
|
package/scaffold/add-table.mjs
CHANGED
|
@@ -472,9 +472,14 @@ function writeTableFiles({ root, planned, edit }, created) {
|
|
|
472
472
|
|
|
473
473
|
}
|
|
474
474
|
|
|
475
|
+
/** The lite sql helpers of platform/database (requireOwnedEntity, the bounds and the sql tag). */
|
|
476
|
+
const DATABASE_SQL_FILE = "be/src/modules/platform/database/database.sql.ts";
|
|
477
|
+
|
|
475
478
|
function tableRegistrations({ root, repo, fe, edit }) {
|
|
476
479
|
const errorFile = "be/src/modules/platform/database/errors/database.error.ts";
|
|
477
480
|
if (repo.edition === "lite" && fs.existsSync(at(root, errorFile))) edit(errorFile, withSqlIdentifierError);
|
|
481
|
+
// requireOwnedEntity lives in the sql helpers the lite starter leaves out; the first table writes them.
|
|
482
|
+
if (repo.edition === "lite") edit(DATABASE_SQL_FILE, (text) => text || render(fs.readFileSync(path.join(TEMPLATES_DIR, "be", "skeleton-lite", "src", "modules", "platform", "database", "database.sql.ts"), "utf8"), {}));
|
|
478
483
|
const registrations = [
|
|
479
484
|
...(repo.edition === "lite" ? ["table"] : []),
|
|
480
485
|
...(fe ? ["api", "graphql"] : []),
|
package/scaffold/app.mjs
CHANGED
|
@@ -353,13 +353,25 @@ const CLI_APP_DIR = 'apps/cli/';
|
|
|
353
353
|
const declaresCliApp = app => app.sides.be.apps.some(entry => entry.kind === 'cli' && entry.name === 'cli');
|
|
354
354
|
|
|
355
355
|
const LITE_APP_REUSED = new Set(['.editorconfig', '.gitattributes', '.nvmrc']);
|
|
356
|
+
/**
|
|
357
|
+
* The lite be files a generator writes when it first needs them, never the starter: the cli's runners, the in-order relay, and the
|
|
358
|
+
* raw SQL helpers of platform/database (the sql tag, and the database-function call statement with its spec), which only the table,
|
|
359
|
+
* queue, jobs and webhook capabilities call; a fresh app reads and writes through entities alone (R334).
|
|
360
|
+
*/
|
|
356
361
|
const LITE_DEFERRED_GENERATOR_FILES = new Set([
|
|
362
|
+
'src/modules/platform/database/database.client.ts',
|
|
363
|
+
'src/modules/platform/database/database.client.spec.ts',
|
|
364
|
+
'src/modules/platform/database/database.sql.ts',
|
|
357
365
|
'src/modules/platform/database/migration-runner.service.ts',
|
|
358
366
|
'src/modules/platform/database/seed-runner.service.ts',
|
|
359
367
|
'src/modules/platform/primitives/sequence.policy.ts',
|
|
360
368
|
]);
|
|
361
|
-
/**
|
|
362
|
-
|
|
369
|
+
/**
|
|
370
|
+
* The persistence folder of a full platform capability (TypeORM migrations, connection arrays, `sql`-tagged statements and rows):
|
|
371
|
+
* lite's schema authority is db/migrations and its code reads and writes through entities (be.persistence lite overlay, R334), so a
|
|
372
|
+
* lite platform capability takes its persistence/ from the lite overlay alone.
|
|
373
|
+
*/
|
|
374
|
+
const FULL_PLATFORM_PERSISTENCE = /^src\/modules\/platform\/[^/]+\/persistence\//;
|
|
363
375
|
const liteBaseSource = (rel) => !rel.startsWith('apps/cli/')
|
|
364
376
|
&& !rel.startsWith('src/features/cli/')
|
|
365
377
|
&& !rel.startsWith('src/modules/domain/note/')
|
|
@@ -367,7 +379,7 @@ const liteBaseSource = (rel) => !rel.startsWith('apps/cli/')
|
|
|
367
379
|
&& rel !== 'src/modules/platform/database/migrate-connections.client.ts'
|
|
368
380
|
&& rel !== 'src/modules/platform/database/seed-connections.client.ts'
|
|
369
381
|
&& !LITE_DEFERRED_GENERATOR_FILES.has(rel)
|
|
370
|
-
&& !
|
|
382
|
+
&& !FULL_PLATFORM_PERSISTENCE.test(rel)
|
|
371
383
|
&& !rel.startsWith('src/tests/');
|
|
372
384
|
/** The be files the lite overlay replaces: a base unit spec of such a file tests the full variant, so lite carries its own. */
|
|
373
385
|
const liteOverlaid = (() => {
|
|
@@ -51,6 +51,22 @@ const REGISTRATIONS = Object.freeze({
|
|
|
51
51
|
names: ["sql"],
|
|
52
52
|
beforeSource: "./errors/database.error",
|
|
53
53
|
},
|
|
54
|
+
{
|
|
55
|
+
// The outbox write and the relay call the queue functions of their migration (R334).
|
|
56
|
+
file: "be/src/modules/platform/database/index.ts",
|
|
57
|
+
source: "./database.client",
|
|
58
|
+
names: ["callDbFunction"],
|
|
59
|
+
beforeSource: "./primary.decorators",
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
// The jobs capability (add job, the fenced-job pattern): its tick takes a transaction-scoped lock before it claims the minute (R335).
|
|
63
|
+
jobs: [
|
|
64
|
+
{
|
|
65
|
+
file: "be/src/modules/platform/database/index.ts",
|
|
66
|
+
source: "./database.client",
|
|
67
|
+
names: ["advisoryXactLock", "callDbFunction"],
|
|
68
|
+
beforeSource: "./primary.decorators",
|
|
69
|
+
},
|
|
54
70
|
],
|
|
55
71
|
webhook: [
|
|
56
72
|
{
|
|
@@ -59,6 +75,13 @@ const REGISTRATIONS = Object.freeze({
|
|
|
59
75
|
names: ["BATCH_ROWS", "sql"],
|
|
60
76
|
beforeSource: "./errors/database.error",
|
|
61
77
|
},
|
|
78
|
+
{
|
|
79
|
+
// The inbox claim and the lite event bus call the functions of their migration (R334).
|
|
80
|
+
file: "be/src/modules/platform/database/index.ts",
|
|
81
|
+
source: "./database.client",
|
|
82
|
+
names: ["callDbFunction"],
|
|
83
|
+
beforeSource: "./primary.decorators",
|
|
84
|
+
},
|
|
62
85
|
{
|
|
63
86
|
// PRIMARY_ENTITY_MANAGER: the token the inbox service's unit spec provides its EntityManager double under.
|
|
64
87
|
file: "be/src/modules/platform/database/index.ts",
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { Secret } from "@modules/platform/config"
|
|
2
2
|
|
|
3
|
+
/** An entity class as TypeORM registers it. */
|
|
4
|
+
export type EntityClass = new () => object
|
|
5
|
+
|
|
3
6
|
/** What the primary connection parser reads from the environment. */
|
|
4
7
|
export interface DatabaseConnectionConfig {
|
|
5
8
|
/** The logical connection name declared in `hfs.json`. */
|
|
@@ -21,5 +24,5 @@ export interface DatabaseOptions {
|
|
|
21
24
|
/** The connections this app opens; one per physical database. */
|
|
22
25
|
readonly connections: ReadonlyArray<DatabaseConnectionOptions>
|
|
23
26
|
/** The TypeORM entities the connections map: PRIMARY_ENTITIES of `@modules/platform/database/entities`, passed by the app's composition root only. */
|
|
24
|
-
readonly entities: ReadonlyArray<
|
|
27
|
+
readonly entities: ReadonlyArray<EntityClass>
|
|
25
28
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { httpSecurityEntities } from "@modules/platform/http-security"
|
|
2
|
+
import type { EntityClass } from "./database.options"
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* The entity list of the primary connection: each capability's `<c>Entities` array (its `persistence/entities/index.ts`), by
|
|
3
6
|
* name, no glob (the entity registry, slot be.platform.database.entities). Only app composition and the test world import this
|
|
4
7
|
* file, from the deep path `@modules/platform/database/entities`; DatabaseModule and the database barrel never do, since the
|
|
5
8
|
* capability barrels import the database capability back.
|
|
6
9
|
*/
|
|
7
|
-
export const PRIMARY_ENTITIES: ReadonlyArray<
|
|
10
|
+
export const PRIMARY_ENTITIES: ReadonlyArray<EntityClass> = [...httpSecurityEntities]
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
export { DatabaseModule } from "./database.module"
|
|
2
|
-
export { advisoryXactLock, callDbFunction, notify } from "./database.client"
|
|
3
2
|
export { DATABASE_PROBE, InjectDatabaseProbe, InjectPrimaryEntityManager, PRIMARY_ENTITY_MANAGER } from "./primary.decorators"
|
|
4
3
|
export { DatabaseProbeService } from "./database-probe.service"
|
|
5
4
|
export type { DatabaseConnectionOptions } from "./database.options"
|
|
6
5
|
export { PRIMARY_DATA_SOURCE } from "./database.port"
|
|
7
|
-
export { sql } from "./database.sql"
|
|
8
6
|
export { DATABASE_ERROR_KINDS } from "./errors/database.error"
|
|
9
7
|
export { parsePrimaryDatabaseConfig } from "./primary.config"
|
|
@@ -5,6 +5,7 @@ export { HttpSecurityModule } from "./http-security.module"
|
|
|
5
5
|
export type { HttpSecurityOptions } from "./http-security.options"
|
|
6
6
|
export { HTTP_SECURITY_MESSAGES } from "./messages/http-security.messages"
|
|
7
7
|
export { OriginGuard, ServerToServer } from "./origin.guard"
|
|
8
|
+
export { httpSecurityEntities } from "./persistence/entities"
|
|
8
9
|
export { RateLimitGuard } from "./rate-limit.guard"
|
|
9
10
|
export { applyHttpSecurity } from "./edge-security.service"
|
|
10
11
|
export { ReadinessService } from "./readiness.service"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Column, Entity, PrimaryColumn } from "typeorm"
|
|
2
|
+
|
|
3
|
+
@Entity({ schema: "public", name: "http_security_rate_windows" })
|
|
4
|
+
/** One rate limit window of one caller and tier, mapped from public.http_security_rate_windows of the baseline migration in db/migrations. */
|
|
5
|
+
export class RateWindowEntity {
|
|
6
|
+
/** The tier and caller address the window counts. */
|
|
7
|
+
@PrimaryColumn({ name: "key", type: "varchar", length: 128 })
|
|
8
|
+
key!: string
|
|
9
|
+
|
|
10
|
+
/** The hits counted in the window. */
|
|
11
|
+
@Column({ name: "hits", type: "integer" })
|
|
12
|
+
hits!: number
|
|
13
|
+
|
|
14
|
+
/** When the window ends; a hit after it opens a fresh one. */
|
|
15
|
+
@Column({ name: "reset_at", type: "timestamptz" })
|
|
16
|
+
resetAt!: Date
|
|
17
|
+
}
|
package/templates/be/skeleton-lite/src/modules/platform/http-security/rate-limit.guard.spec.ts
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import "reflect-metadata"
|
|
2
|
+
import type { ExecutionContext, Type } from "@nestjs/common"
|
|
3
|
+
import type { Request, Response } from "express"
|
|
4
|
+
import { Reflector } from "@nestjs/core"
|
|
5
|
+
import { Test } from "@nestjs/testing"
|
|
6
|
+
import { In, LessThanOrEqual, MoreThan } from "typeorm"
|
|
7
|
+
import { FakeClock, mock, mockEntityManager, queueQueryBuilders } from "@starci/jest-preset"
|
|
8
|
+
import type { MockEntityManager } from "@starci/jest-preset"
|
|
9
|
+
import { CLOCK } from "@modules/platform/clock"
|
|
10
|
+
import { PRIMARY_ENTITY_MANAGER } from "@modules/platform/database"
|
|
11
|
+
import { HttpSecurityErrorCode } from "./errors/http-security.error"
|
|
12
|
+
import { HTTP_SECURITY_OPTIONS } from "./http-security.decorators"
|
|
13
|
+
import type { HttpSecurityOptions } from "./http-security.options"
|
|
14
|
+
import { RateWindowEntity } from "./persistence/entities/rate-window.entity"
|
|
15
|
+
import { RateLimit, RateLimitGuard, RateTier } from "./rate-limit.guard"
|
|
16
|
+
import type { RateLimitExceeded } from "./rate-limit.guard"
|
|
17
|
+
|
|
18
|
+
const AT = "2026-02-03T04:05:06.000Z"
|
|
19
|
+
const RESET_AT = "2026-02-03T04:06:06.000Z"
|
|
20
|
+
const options: HttpSecurityOptions = {
|
|
21
|
+
allowedOrigins: [],
|
|
22
|
+
rateLimit: { windowMs: 60_000, defaultLimit: 2, strictLimit: 1 },
|
|
23
|
+
webhooks: {},
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The caller's live window holding `hits`. */
|
|
27
|
+
const windowOf = (hits: number): RateWindowEntity => ({ key: "default:127.0.0.1", hits, resetAt: new Date(RESET_AT) })
|
|
28
|
+
|
|
29
|
+
/** The answers of a store whose live window of the caller holds `hits` after this hit's increment. */
|
|
30
|
+
const liveWindow = (hits: number) =>
|
|
31
|
+
({ increment: [RateWindowEntity, { affected: 1 }], findOneBy: [RateWindowEntity, windowOf(hits)] }) as const
|
|
32
|
+
|
|
33
|
+
/** The answers of a store where this hit opens the caller's window (no live or ended one) and the prune finds none ended. */
|
|
34
|
+
const OPENING_WINDOW = {
|
|
35
|
+
increment: [RateWindowEntity, { affected: 0 }],
|
|
36
|
+
update: [RateWindowEntity, { affected: 0 }],
|
|
37
|
+
find: [RateWindowEntity, []],
|
|
38
|
+
} as const
|
|
39
|
+
|
|
40
|
+
const build = async (entityManager: MockEntityManager, tier?: RateTier, exceeded?: RateLimitExceeded) => {
|
|
41
|
+
const reflector = mock<Reflector>()
|
|
42
|
+
reflector.getAllAndOverride.mockImplementation((key: unknown) =>
|
|
43
|
+
key === "platform.http-security.rate-exceeded" ? exceeded : tier,
|
|
44
|
+
)
|
|
45
|
+
const moduleRef = await Test.createTestingModule({
|
|
46
|
+
providers: [
|
|
47
|
+
RateLimitGuard,
|
|
48
|
+
{ provide: Reflector, useValue: reflector },
|
|
49
|
+
{ provide: CLOCK, useValue: new FakeClock(AT) },
|
|
50
|
+
{ provide: HTTP_SECURITY_OPTIONS, useValue: options },
|
|
51
|
+
{ provide: PRIMARY_ENTITY_MANAGER, useValue: entityManager },
|
|
52
|
+
],
|
|
53
|
+
}).compile()
|
|
54
|
+
return { guard: moduleRef.get(RateLimitGuard), reflector }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** An execution context of one POST from `ip` on the door; the handler and the class carry the tier metadata. */
|
|
58
|
+
const contextOf = (
|
|
59
|
+
ip: string | undefined,
|
|
60
|
+
handler = () => undefined,
|
|
61
|
+
door: Type<unknown> = class Door {},
|
|
62
|
+
response = mock<Response>(),
|
|
63
|
+
) => {
|
|
64
|
+
const http = mock<ReturnType<ExecutionContext["switchToHttp"]>>()
|
|
65
|
+
http.getRequest.mockImplementation(() => mock<Request>({ headers: {}, method: "POST", ip }))
|
|
66
|
+
http.getResponse.mockReturnValue(response)
|
|
67
|
+
const context = mock<ExecutionContext>()
|
|
68
|
+
context.getType.mockReturnValue("http")
|
|
69
|
+
context.switchToHttp.mockReturnValue(http)
|
|
70
|
+
context.getHandler.mockReturnValue(handler)
|
|
71
|
+
context.getClass.mockReturnValue(door)
|
|
72
|
+
return context
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
describe("RateLimit", () => {
|
|
76
|
+
it("marks a door with the tier it declares, and the guard counts it under that tier's limit", async () => {
|
|
77
|
+
class Door {}
|
|
78
|
+
RateLimit(RateTier.Strict)(Door)
|
|
79
|
+
const entityManager = mockEntityManager(liveWindow(2))
|
|
80
|
+
const { guard, reflector } = await build(entityManager, RateTier.Strict)
|
|
81
|
+
const handler = () => undefined
|
|
82
|
+
|
|
83
|
+
expect(Reflect.getMetadata("platform.http-security.rate-tier", Door)).toBe(RateTier.Strict)
|
|
84
|
+
await expect(guard.canActivate(contextOf("127.0.0.1", handler, Door))).rejects.toMatchObject({
|
|
85
|
+
code: HttpSecurityErrorCode.RateLimited,
|
|
86
|
+
})
|
|
87
|
+
expect(reflector.getAllAndOverride).toHaveBeenCalledWith("platform.http-security.rate-tier", [handler, Door])
|
|
88
|
+
expect(entityManager.increment).toHaveBeenCalledWith(
|
|
89
|
+
RateWindowEntity,
|
|
90
|
+
{ key: "strict:127.0.0.1", resetAt: MoreThan(new Date(AT)) },
|
|
91
|
+
"hits",
|
|
92
|
+
1,
|
|
93
|
+
)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it("marks a redirect-only door with its exceeded redirect beside the tier", () => {
|
|
97
|
+
class Door {}
|
|
98
|
+
RateLimit(RateTier.Strict, { redirect: "/sign-in?auth=error" })(Door)
|
|
99
|
+
|
|
100
|
+
expect(Reflect.getMetadata("platform.http-security.rate-tier", Door)).toBe(RateTier.Strict)
|
|
101
|
+
expect(Reflect.getMetadata("platform.http-security.rate-exceeded", Door)).toEqual({
|
|
102
|
+
redirect: "/sign-in?auth=error",
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it.each(["https://foreign.test/sign-in", "//foreign.test/sign-in", "/\\foreign.test", "sign-in"])(
|
|
107
|
+
"refuses an exceeded redirect %s that is not a same-site path",
|
|
108
|
+
(redirect) => {
|
|
109
|
+
expect(() => RateLimit(RateTier.Strict, { redirect })).toThrow(HttpSecurityErrorCode.RequestInvalid)
|
|
110
|
+
},
|
|
111
|
+
)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
describe("RateLimitGuard", () => {
|
|
115
|
+
it("admits a caller under the default limit of an unmarked door, counting in the shared live window", async () => {
|
|
116
|
+
const entityManager = mockEntityManager(liveWindow(2))
|
|
117
|
+
const { guard } = await build(entityManager)
|
|
118
|
+
|
|
119
|
+
await expect(guard.canActivate(contextOf("127.0.0.1"))).resolves.toBe(true)
|
|
120
|
+
expect(entityManager.findOneBy).toHaveBeenCalledWith(RateWindowEntity, { key: "default:127.0.0.1" })
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it("refuses a caller whose shared window is over the default limit", async () => {
|
|
124
|
+
const { guard } = await build(mockEntityManager(liveWindow(3)))
|
|
125
|
+
|
|
126
|
+
await expect(guard.canActivate(contextOf("127.0.0.1"))).rejects.toMatchObject({
|
|
127
|
+
code: HttpSecurityErrorCode.RateLimited,
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it("refuses the request when the counted window vanished before it was read", async () => {
|
|
132
|
+
const entityManager = mockEntityManager({
|
|
133
|
+
increment: [RateWindowEntity, { affected: 1 }],
|
|
134
|
+
findOneBy: [RateWindowEntity, null],
|
|
135
|
+
})
|
|
136
|
+
const { guard } = await build(entityManager)
|
|
137
|
+
|
|
138
|
+
await expect(guard.canActivate(contextOf("127.0.0.1"))).rejects.toMatchObject({
|
|
139
|
+
code: HttpSecurityErrorCode.RateLimited,
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
describe("RateLimitGuard opening a window", () => {
|
|
145
|
+
it("restarts an ended window at one hit and prunes the ended windows", async () => {
|
|
146
|
+
const ended = windowOf(5)
|
|
147
|
+
const entityManager = mockEntityManager({
|
|
148
|
+
increment: [RateWindowEntity, {}],
|
|
149
|
+
update: [RateWindowEntity, { affected: 1 }],
|
|
150
|
+
find: [RateWindowEntity, [ended]],
|
|
151
|
+
delete: [RateWindowEntity, { affected: 1 }],
|
|
152
|
+
})
|
|
153
|
+
const { guard } = await build(entityManager)
|
|
154
|
+
|
|
155
|
+
await expect(guard.canActivate(contextOf("127.0.0.1"))).resolves.toBe(true)
|
|
156
|
+
expect(entityManager.update).toHaveBeenCalledWith(
|
|
157
|
+
RateWindowEntity,
|
|
158
|
+
{ key: "default:127.0.0.1", resetAt: LessThanOrEqual(new Date(AT)) },
|
|
159
|
+
{ hits: 1, resetAt: new Date(RESET_AT) },
|
|
160
|
+
)
|
|
161
|
+
expect(entityManager.find).toHaveBeenCalledWith(RateWindowEntity, {
|
|
162
|
+
select: { key: true },
|
|
163
|
+
where: { resetAt: LessThanOrEqual(new Date(AT)) },
|
|
164
|
+
order: { resetAt: "ASC" },
|
|
165
|
+
take: 100,
|
|
166
|
+
})
|
|
167
|
+
expect(entityManager.delete).toHaveBeenCalledWith(RateWindowEntity, {
|
|
168
|
+
key: In([ended.key]),
|
|
169
|
+
resetAt: LessThanOrEqual(new Date(AT)),
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it("opens a missing window at one hit, ignoring a concurrent opener, and deletes nothing when no window ended", async () => {
|
|
174
|
+
const entityManager = mockEntityManager(OPENING_WINDOW)
|
|
175
|
+
const [opened] = queueQueryBuilders(entityManager, [{ execute: {} }])
|
|
176
|
+
const { guard } = await build(entityManager)
|
|
177
|
+
|
|
178
|
+
await expect(guard.canActivate(contextOf("127.0.0.1"))).resolves.toBe(true)
|
|
179
|
+
expect(opened?.callsOf("values")).toEqual([
|
|
180
|
+
[{ key: "default:127.0.0.1", hits: 1, resetAt: new Date(RESET_AT) }],
|
|
181
|
+
])
|
|
182
|
+
expect(opened?.callsOf("orIgnore")).toEqual([[]])
|
|
183
|
+
expect(entityManager.delete).not.toHaveBeenCalled()
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it("opens a window when the restart answers no affected count", async () => {
|
|
187
|
+
const entityManager = mockEntityManager({
|
|
188
|
+
increment: [RateWindowEntity, { affected: 0 }],
|
|
189
|
+
update: [RateWindowEntity, {}],
|
|
190
|
+
find: [RateWindowEntity, []],
|
|
191
|
+
})
|
|
192
|
+
const [opened] = queueQueryBuilders(entityManager, [{ execute: {} }])
|
|
193
|
+
const { guard } = await build(entityManager)
|
|
194
|
+
|
|
195
|
+
await expect(guard.canActivate(contextOf("127.0.0.1"))).resolves.toBe(true)
|
|
196
|
+
expect(opened?.callsOf("insert")).toEqual([[]])
|
|
197
|
+
})
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
describe("RateLimitGuard keys", () => {
|
|
201
|
+
it("counts a request without an address under the unknown key", async () => {
|
|
202
|
+
const entityManager = mockEntityManager(liveWindow(2))
|
|
203
|
+
const { guard } = await build(entityManager)
|
|
204
|
+
|
|
205
|
+
await expect(guard.canActivate(contextOf(undefined))).resolves.toBe(true)
|
|
206
|
+
expect(entityManager.findOneBy).toHaveBeenCalledWith(RateWindowEntity, { key: "default:unknown" })
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
it("bounds the window key to the length the table stores", async () => {
|
|
210
|
+
const entityManager = mockEntityManager(liveWindow(2))
|
|
211
|
+
const { guard } = await build(entityManager)
|
|
212
|
+
|
|
213
|
+
await guard.canActivate(contextOf("f".repeat(300)))
|
|
214
|
+
|
|
215
|
+
expect(entityManager.findOneBy).toHaveBeenCalledWith(RateWindowEntity, { key: `default:${"f".repeat(120)}` })
|
|
216
|
+
})
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
describe("RateLimitGuard on a redirect-only door", () => {
|
|
220
|
+
const EXCEEDED: RateLimitExceeded = { redirect: "/sign-in?auth=error" }
|
|
221
|
+
|
|
222
|
+
/** A response whose redirect is a plain double, so the spec reads the call without the deprecated overload. */
|
|
223
|
+
const responseOf = () => {
|
|
224
|
+
const redirect = jest.fn()
|
|
225
|
+
return { response: mock<Response>({ redirect }), redirect }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
it("answers the door's own 303 when the caller is over the limit, and still refuses", async () => {
|
|
229
|
+
const { guard } = await build(mockEntityManager(liveWindow(2)), RateTier.Strict, EXCEEDED)
|
|
230
|
+
const { response, redirect } = responseOf()
|
|
231
|
+
|
|
232
|
+
await expect(
|
|
233
|
+
guard.canActivate(contextOf("127.0.0.1", () => undefined, class Door {}, response)),
|
|
234
|
+
).rejects.toMatchObject({ code: HttpSecurityErrorCode.RateLimited })
|
|
235
|
+
expect(redirect).toHaveBeenCalledWith(303, "/sign-in?auth=error")
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
it("writes no redirect while the caller is under the limit", async () => {
|
|
239
|
+
const entityManager = mockEntityManager(OPENING_WINDOW)
|
|
240
|
+
queueQueryBuilders(entityManager, [{ execute: {} }])
|
|
241
|
+
const { guard } = await build(entityManager, RateTier.Strict, EXCEEDED)
|
|
242
|
+
const { response, redirect } = responseOf()
|
|
243
|
+
|
|
244
|
+
await expect(guard.canActivate(contextOf("127.0.0.1", () => undefined, class Door {}, response))).resolves.toBe(
|
|
245
|
+
true,
|
|
246
|
+
)
|
|
247
|
+
expect(redirect).not.toHaveBeenCalled()
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
it("refuses a GraphQL operation over the limit without writing a redirect", async () => {
|
|
251
|
+
const { guard } = await build(mockEntityManager(liveWindow(2)), RateTier.Strict, EXCEEDED)
|
|
252
|
+
const { response, redirect } = responseOf()
|
|
253
|
+
const context = contextOf("127.0.0.1", () => undefined, class Door {}, response)
|
|
254
|
+
context.getType.mockReturnValue("graphql")
|
|
255
|
+
context.getArgs.mockReturnValue([{}, {}, { req: mock<Request>({ headers: {}, ip: "127.0.0.1" }) }, {}])
|
|
256
|
+
|
|
257
|
+
await expect(guard.canActivate(context)).rejects.toMatchObject({ code: HttpSecurityErrorCode.RateLimited })
|
|
258
|
+
expect(redirect).not.toHaveBeenCalled()
|
|
259
|
+
})
|
|
260
|
+
})
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Injectable, SetMetadata, applyDecorators } from "@nestjs/common"
|
|
2
|
+
import type { CanActivate, ExecutionContext } from "@nestjs/common"
|
|
3
|
+
import type { Reflector } from "@nestjs/core"
|
|
4
|
+
import type { Response } from "express"
|
|
5
|
+
import { In, LessThanOrEqual, MoreThan } from "typeorm"
|
|
6
|
+
import type { EntityManager } from "typeorm"
|
|
7
|
+
import { InjectClock } from "@modules/platform/clock"
|
|
8
|
+
import type { Clock } from "@modules/platform/clock"
|
|
9
|
+
import { InjectReflector } from "@modules/platform/composition"
|
|
10
|
+
import type { DoorDecorator } from "@modules/platform/composition"
|
|
11
|
+
import { InjectPrimaryEntityManager } from "@modules/platform/database"
|
|
12
|
+
import { HttpSecurityError, HttpSecurityErrorCode } from "./errors/http-security.error"
|
|
13
|
+
import { requestOf } from "./execution-request.mapper"
|
|
14
|
+
import { InjectHttpSecurityOptions } from "./http-security.decorators"
|
|
15
|
+
import type { HttpSecurityOptions } from "./http-security.options"
|
|
16
|
+
import { RateWindowEntity } from "./persistence/entities/rate-window.entity"
|
|
17
|
+
|
|
18
|
+
/** The rate limit tiers of a door. */
|
|
19
|
+
export enum RateTier {
|
|
20
|
+
/** The global default tier. */
|
|
21
|
+
Default = "default",
|
|
22
|
+
/** The tight tier every authentication handshake and signed webhook carries. */
|
|
23
|
+
Strict = "strict",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** What a door answers when its caller is over the limit, when that is not the 429 error body. */
|
|
27
|
+
export interface RateLimitExceeded {
|
|
28
|
+
/** A same-site path the door's own contract answers with a 303 (an OAuth callback's error redirect). */
|
|
29
|
+
readonly redirect: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const RATE_TIER_KEY = "platform.http-security.rate-tier"
|
|
33
|
+
const RATE_EXCEEDED_KEY = "platform.http-security.rate-exceeded"
|
|
34
|
+
/** The longest window key the table stores: the tier, a colon and the caller address. */
|
|
35
|
+
const KEY_MAX = 128
|
|
36
|
+
/** The most ended windows one fresh window prunes. */
|
|
37
|
+
const PRUNE_BATCH = 100
|
|
38
|
+
|
|
39
|
+
/** A same-site path: one leading slash, never a protocol-relative `//` or `/\` that leaves the site. */
|
|
40
|
+
const isSameSitePath = (location: string): boolean =>
|
|
41
|
+
location.startsWith("/") && !location.startsWith("//") && !location.startsWith("/\\")
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Marks a door with a rate limit tier other than the default. A door whose contract answers only redirects (an OAuth
|
|
45
|
+
* callback) names its error redirect in `exceeded`, and a caller over the limit gets that 303 instead of a 429 body.
|
|
46
|
+
*/
|
|
47
|
+
export const RateLimit = (tier: RateTier, exceeded?: RateLimitExceeded): DoorDecorator => {
|
|
48
|
+
if (exceeded === undefined) return applyDecorators(SetMetadata(RATE_TIER_KEY, tier))
|
|
49
|
+
if (!isSameSitePath(exceeded.redirect))
|
|
50
|
+
throw new HttpSecurityError({
|
|
51
|
+
code: HttpSecurityErrorCode.RequestInvalid,
|
|
52
|
+
params: { fields: "RATE_LIMIT_REDIRECT" },
|
|
53
|
+
})
|
|
54
|
+
return applyDecorators(SetMetadata(RATE_TIER_KEY, tier), SetMetadata(RATE_EXCEEDED_KEY, exceeded))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@Injectable()
|
|
58
|
+
/**
|
|
59
|
+
* The first app guard: a fixed-window counter per caller address and tier, stamped by the Clock and kept in the primary
|
|
60
|
+
* database through its entity, so every replica counts against the same window. A hit that opens a fresh window also prunes
|
|
61
|
+
* a bounded batch of expired windows.
|
|
62
|
+
*/
|
|
63
|
+
export class RateLimitGuard implements CanActivate {
|
|
64
|
+
constructor(
|
|
65
|
+
@InjectReflector() private readonly reflector: Reflector,
|
|
66
|
+
@InjectClock() private readonly clock: Clock,
|
|
67
|
+
@InjectHttpSecurityOptions() private readonly options: HttpSecurityOptions,
|
|
68
|
+
@InjectPrimaryEntityManager() private readonly entityManager: EntityManager,
|
|
69
|
+
) {}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Counts the request and refuses it when the caller is over the limit of the door tier. A door that declares its
|
|
73
|
+
* exceeded redirect has the 303 written here; the refusal still travels, and the errors filter leaves the sent
|
|
74
|
+
* response as it is.
|
|
75
|
+
*/
|
|
76
|
+
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
77
|
+
const targets = [context.getHandler(), context.getClass()]
|
|
78
|
+
const tier = this.reflector.getAllAndOverride<RateTier | undefined>(RATE_TIER_KEY, targets) ?? RateTier.Default
|
|
79
|
+
const limit =
|
|
80
|
+
tier === RateTier.Strict ? this.options.rateLimit.strictLimit : this.options.rateLimit.defaultLimit
|
|
81
|
+
const now = this.clock.now()
|
|
82
|
+
const key = `${tier}:${requestOf(context).ip ?? "unknown"}`.slice(0, KEY_MAX)
|
|
83
|
+
const resetAt = new Date(now.getTime() + this.options.rateLimit.windowMs)
|
|
84
|
+
const hits = await this.hit(key, now, resetAt)
|
|
85
|
+
if (hits === 1) await this.prune(now)
|
|
86
|
+
if (hits <= limit) return true
|
|
87
|
+
const exceeded = this.reflector.getAllAndOverride<RateLimitExceeded | undefined>(RATE_EXCEEDED_KEY, targets)
|
|
88
|
+
if (exceeded !== undefined && context.getType() === "http")
|
|
89
|
+
context.switchToHttp().getResponse<Response>().redirect(303, exceeded.redirect)
|
|
90
|
+
throw new HttpSecurityError({ code: HttpSecurityErrorCode.RateLimited })
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Counts one hit in the caller's window and answers the hits it holds: a live window grows by one in one atomic increment,
|
|
95
|
+
* an ended window restarts at one hit ending at `resetAt`, and a missing one opens at one hit (a concurrent opener's row
|
|
96
|
+
* wins and this hit is its first). A window that vanished between the writes counts as over every limit.
|
|
97
|
+
*/
|
|
98
|
+
private async hit(key: string, now: Date, resetAt: Date): Promise<number> {
|
|
99
|
+
const counted = await this.entityManager.increment(RateWindowEntity, { key, resetAt: MoreThan(now) }, "hits", 1)
|
|
100
|
+
if ((counted.affected ?? 0) > 0) {
|
|
101
|
+
const window = await this.entityManager.findOneBy(RateWindowEntity, { key })
|
|
102
|
+
return window?.hits ?? Number.POSITIVE_INFINITY
|
|
103
|
+
}
|
|
104
|
+
const restarted = await this.entityManager.update(
|
|
105
|
+
RateWindowEntity,
|
|
106
|
+
{ key, resetAt: LessThanOrEqual(now) },
|
|
107
|
+
{ hits: 1, resetAt },
|
|
108
|
+
)
|
|
109
|
+
if ((restarted.affected ?? 0) > 0) return 1
|
|
110
|
+
await this.entityManager
|
|
111
|
+
.createQueryBuilder(RateWindowEntity, "window")
|
|
112
|
+
.insert()
|
|
113
|
+
.values({ key, hits: 1, resetAt })
|
|
114
|
+
.orIgnore()
|
|
115
|
+
.execute()
|
|
116
|
+
return 1
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Deletes a bounded batch of the windows that ended by `now`; a window a hit restarted meanwhile is kept. */
|
|
120
|
+
private async prune(now: Date): Promise<void> {
|
|
121
|
+
const ended = await this.entityManager.find(RateWindowEntity, {
|
|
122
|
+
select: { key: true },
|
|
123
|
+
where: { resetAt: LessThanOrEqual(now) },
|
|
124
|
+
order: { resetAt: "ASC" },
|
|
125
|
+
take: PRUNE_BATCH,
|
|
126
|
+
})
|
|
127
|
+
if (ended.length === 0) return
|
|
128
|
+
await this.entityManager.delete(RateWindowEntity, {
|
|
129
|
+
key: In(ended.map((window) => window.key)),
|
|
130
|
+
resetAt: LessThanOrEqual(now),
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
}
|