@starci/hfs 4.9.8 → 4.9.9

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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.9.9 - 2026-10-29
4
+
5
+ - Fixed: the back-end skeleton's `RateLimitGuard` counts in a store every replica shares (BE-AUTH-4), not a process-local map. Each hit is one upsert of the caller's window (tier and address) in the primary database, answering its hits; a hit that opens a fresh window prunes up to 100 ended windows (`FOR UPDATE SKIP LOCKED`). `platform/http-security` owns the table `http_security_rate_windows`: in full through its `persistence/` entity and migration (`httpSecurityEntities`, `httpSecurityMigrations`, registered on the primary connection by the api and cli apps), in lite in the Supabase baseline with forced RLS and `app_be` policies. The lite starter now ships `platform/database/database.sql.ts` (with `DATABASE_IDENTIFIER_REJECTED`) and exports `sql`, `InjectPrimaryEntityManager` and `PRIMARY_ENTITY_MANAGER`, which the guard and its spec consume; a lite starter leaves out a platform capability's TypeORM schema files. Needs `@starci/eslint-canon-be` 3.6.5, which admits `http-security` as a persistence capability.
6
+ - Unchanged on purpose: `platform/http` and `platform/http-security` still export only what the skeleton consumes (R25 HFS_UNUSED_EXPORT); code that first consumes `InjectHttpClient` or `RateLimit`/`RateTier` adds the export line to the owner entry in the same change, as the webhook generators already do.
7
+ - Changed: the bundled canon pins name hfs 4.9.9 and eslint-canon-be 3.6.5.
8
+ - Changed: the bundled canon pins name the Grammar family 0.15.7 (grammar, eslint-grammar-core, eslint-grammar-offset-pop) and eslint-canon-fe 9.4.6.
9
+ - Changed: the bundled canon pins name the Grammar family 0.15.8 (grammar, eslint-grammar-core, eslint-grammar-offset-pop), eslint-canon-fe 9.4.7 and eslint-canon-be 3.6.6.
10
+
3
11
  ## 4.9.8 - 2026-10-28
4
12
 
5
13
  - Added: `starci app add page <route> --tabs a,b` puts the page tabs in the `Page` header as link tabs (Grammar 0.15.6): the first tab is the route itself, every other one its sub-route `<route>/<tab>`; the server owner resolves the labels (`<page>.tabs.label` and `<page>.tabs.items.<tab>` in every catalog) and passes no state or handler, and the stories carry the tabs. Fewer than two or repeated tab ids are refused (`HFS_ADD_TABS_INVALID`).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@starci/hfs",
3
- "version": "4.9.8",
3
+ "version": "4.9.9",
4
4
  "description": "The StarCi app command implementation for scaffolding, linting, checking and synchronizing one product repository.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -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.6
34
+ version: 0.15.8
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.6.4
41
+ version: 3.6.6
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.4.5
48
+ version: 9.4.7
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.6
55
+ version: 0.15.8
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.6
62
+ version: 0.15.8
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.9.8
109
+ version: 4.9.9
110
110
  group: starci
111
111
  install: registry
112
112
  side: both
package/scaffold/app.mjs CHANGED
@@ -407,11 +407,12 @@ const declaresCliApp = app => app.sides.be.apps.some(entry => entry.kind === 'cl
407
407
 
408
408
  const LITE_APP_REUSED = new Set(['.editorconfig', '.gitattributes', '.nvmrc']);
409
409
  const LITE_DEFERRED_GENERATOR_FILES = new Set([
410
- 'src/modules/platform/database/database.sql.ts',
411
410
  'src/modules/platform/database/migration-runner.service.ts',
412
411
  'src/modules/platform/database/seed-runner.service.ts',
413
412
  'src/modules/platform/primitives/sequence.policy.ts',
414
413
  ]);
414
+ /** The TypeORM schema files of a platform capability: lite's schema authority is supabase/migrations (be.persistence lite overlay). */
415
+ const LITE_TYPEORM_SCHEMA = /^src\/modules\/platform\/[^/]+\/persistence\/(?:connection\.ts$|entities\/|migrations\/)/;
415
416
  const liteBaseSource = (rel) => !rel.startsWith('apps/cli/')
416
417
  && !rel.startsWith('src/features/cli/')
417
418
  && !rel.startsWith('src/modules/domain/note/')
@@ -419,6 +420,7 @@ const liteBaseSource = (rel) => !rel.startsWith('apps/cli/')
419
420
  && rel !== 'src/modules/platform/database/migrate-connections.client.ts'
420
421
  && rel !== 'src/modules/platform/database/seed-connections.client.ts'
421
422
  && !LITE_DEFERRED_GENERATOR_FILES.has(rel)
423
+ && !LITE_TYPEORM_SCHEMA.test(rel)
422
424
  && !rel.startsWith('src/tests/');
423
425
  /** The be files the lite overlay replaces: a base unit spec of such a file tests the full variant, so lite carries its own. */
424
426
  const liteOverlaid = (() => {
@@ -62,3 +62,44 @@ grant usage on schema public to app_be;
62
62
  revoke all on table public.profiles from public, anon, authenticated;
63
63
  grant select, insert on table public.profiles to app_be;
64
64
  grant update (display_name) on table public.profiles to app_be;
65
+
66
+ -- The rate limit windows of platform/http-security: every api replica counts a caller's hits in the same row, and a hit
67
+ -- that opens a fresh window prunes a bounded batch of the ended ones.
68
+ create table public.http_security_rate_windows (
69
+ key varchar(128) primary key,
70
+ hits integer not null check (hits > 0),
71
+ reset_at timestamptz not null
72
+ );
73
+
74
+ create index http_security_rate_windows_reset_at_idx on public.http_security_rate_windows (reset_at);
75
+
76
+ alter table public.http_security_rate_windows enable row level security;
77
+ alter table public.http_security_rate_windows force row level security;
78
+
79
+ create policy http_security_rate_windows_app_be_select
80
+ on public.http_security_rate_windows
81
+ for select
82
+ to app_be
83
+ using (current_user = 'app_be');
84
+
85
+ create policy http_security_rate_windows_app_be_insert
86
+ on public.http_security_rate_windows
87
+ for insert
88
+ to app_be
89
+ with check (current_user = 'app_be');
90
+
91
+ create policy http_security_rate_windows_app_be_update
92
+ on public.http_security_rate_windows
93
+ for update
94
+ to app_be
95
+ using (current_user = 'app_be')
96
+ with check (current_user = 'app_be');
97
+
98
+ create policy http_security_rate_windows_app_be_delete
99
+ on public.http_security_rate_windows
100
+ for delete
101
+ to app_be
102
+ using (current_user = 'app_be');
103
+
104
+ revoke all on table public.http_security_rate_windows from public, anon, authenticated;
105
+ grant select, insert, update, delete on table public.http_security_rate_windows to app_be;
@@ -6,7 +6,13 @@ import { CONFIG_ERROR_KINDS } from "@modules/platform/config"
6
6
  import { CqrsModule } from "@modules/platform/cqrs"
7
7
  import { DATABASE_ERROR_KINDS, DatabaseModule } from "@modules/platform/database"
8
8
  import { ERRORS_MESSAGES, ErrorsModule } from "@modules/platform/errors"
9
- import { HTTP_SECURITY_ERROR_KINDS, HTTP_SECURITY_MESSAGES, HttpSecurityModule } from "@modules/platform/http-security"
9
+ import {
10
+ HTTP_SECURITY_ERROR_KINDS,
11
+ HTTP_SECURITY_MESSAGES,
12
+ HttpSecurityModule,
13
+ httpSecurityEntities,
14
+ httpSecurityMigrations,
15
+ } from "@modules/platform/http-security"
10
16
  import { I18nModule } from "@modules/platform/i18n"
11
17
  import { IdsModule } from "@modules/platform/ids"
12
18
  import { HTTP_ERROR_KINDS, HTTP_MESSAGES, HttpModule } from "@modules/platform/http"
@@ -44,6 +50,12 @@ export const platformModules = (options: {{appPascal}}Options): Array<DynamicMod
44
50
  HttpSecurityModule.register({ isGlobal: true, ...options.httpSecurity }),
45
51
  DatabaseModule.register({
46
52
  isGlobal: true,
47
- connections: [{ ...options.database, entities: noteEntities, migrations: noteMigrations }],
53
+ connections: [
54
+ {
55
+ ...options.database,
56
+ entities: [...httpSecurityEntities, ...noteEntities],
57
+ migrations: [...httpSecurityMigrations, ...noteMigrations],
58
+ },
59
+ ],
48
60
  }),
49
61
  ]
@@ -5,14 +5,15 @@ import { DatabaseModule } from "@modules/platform/database"
5
5
  import type { DatabaseConnectionConfig, DatabaseConnectionOptions } from "@modules/platform/database"
6
6
  import { IdsModule } from "@modules/platform/ids"
7
7
  import { HttpModule } from "@modules/platform/http"
8
+ import { httpSecurityEntities, httpSecurityMigrations } from "@modules/platform/http-security"
8
9
  import { LoggingModule } from "@modules/platform/logging"
9
10
  import type { CliAppOptions } from "../cli.options"
10
11
 
11
12
  /** Binds the primary connection to its owners' entities and migrations during composition. */
12
13
  const primaryConnectionOf = (primary: DatabaseConnectionConfig): DatabaseConnectionOptions => ({
13
14
  ...primary,
14
- entities: noteEntities,
15
- migrations: noteMigrations,
15
+ entities: [...httpSecurityEntities, ...noteEntities],
16
+ migrations: [...httpSecurityMigrations, ...noteMigrations],
16
17
  })
17
18
 
18
19
  /** The clock, logger and declared database connections shared by CLI commands. */
@@ -4,6 +4,7 @@ export { HttpSecurityModule } from "./http-security.module"
4
4
  export type { HttpSecurityOptions } from "./http-security.options"
5
5
  export { HTTP_SECURITY_MESSAGES } from "./messages/http-security.messages"
6
6
  export { OriginGuard } from "./origin.guard"
7
+ export { httpSecurityEntities, httpSecurityMigrations } from "./persistence/connection"
7
8
  export { requestOf } from "./execution-request.mapper"
8
9
  export { RateLimitGuard } from "./rate-limit.guard"
9
10
  export { applyHttpSecurity } from "./edge-security.service"
@@ -0,0 +1,8 @@
1
+ import { RateWindowEntity } from "./entities/rate-window.entity"
2
+ import { CreateRateWindows1789900000000 } from "./migrations/1789900000000-create-rate-windows"
3
+
4
+ /** The entities of the http-security capability, for the primary connection its rate limit counts in. */
5
+ export const httpSecurityEntities = [RateWindowEntity]
6
+
7
+ /** The migrations of the http-security capability, in the order they run. */
8
+ export const httpSecurityMigrations = [CreateRateWindows1789900000000]
@@ -0,0 +1,17 @@
1
+ import { Column, Entity, PrimaryColumn } from "typeorm"
2
+
3
+ @Entity("http_security_rate_windows")
4
+ /** One rate limit window of one caller and tier: the hits it counted and when it ends. */
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
+ }
@@ -0,0 +1,24 @@
1
+ import type { MigrationInterface, QueryRunner } from "typeorm"
2
+
3
+ /** Creates the rate limit windows every api replica counts in. */
4
+ export class CreateRateWindows1789900000000 implements MigrationInterface {
5
+ name = "CreateRateWindows1789900000000"
6
+
7
+ /** Creates the windows table and the index the prune reads. */
8
+ async up(queryRunner: QueryRunner): Promise<void> {
9
+ await queryRunner.query(`CREATE TABLE http_security_rate_windows (
10
+ key varchar(128) PRIMARY KEY,
11
+ hits integer NOT NULL CHECK (hits > 0),
12
+ reset_at timestamptz NOT NULL
13
+ )`)
14
+ await queryRunner.query(
15
+ `CREATE INDEX ix_http_security_rate_windows_reset_at ON http_security_rate_windows (reset_at)`,
16
+ )
17
+ }
18
+
19
+ /** Drops the index and the table. */
20
+ async down(queryRunner: QueryRunner): Promise<void> {
21
+ await queryRunner.query(`DROP INDEX ix_http_security_rate_windows_reset_at`)
22
+ await queryRunner.query(`DROP TABLE http_security_rate_windows`)
23
+ }
24
+ }
@@ -0,0 +1,5 @@
1
+ /** The row HIT_RATE_WINDOW answers. */
2
+ export interface RateWindowRow {
3
+ /** The hits of the caller's current window, this one included. */
4
+ readonly hits: number
5
+ }
@@ -0,0 +1,20 @@
1
+ import { sql } from "@modules/platform/database"
2
+
3
+ /**
4
+ * Counts one hit in the caller's window and answers the hits of that window: a missing or expired window opens afresh at one
5
+ * hit and ends at $3 ($1 tier and address key, $2 instant, $3 end of a fresh window).
6
+ */
7
+ export const HIT_RATE_WINDOW = sql`INSERT INTO http_security_rate_windows (key, hits, reset_at)
8
+ VALUES ($1::varchar, 1, $3::timestamptz)
9
+ ON CONFLICT (key) DO UPDATE SET
10
+ hits = CASE WHEN http_security_rate_windows.reset_at <= $2::timestamptz THEN 1
11
+ ELSE http_security_rate_windows.hits + 1 END,
12
+ reset_at = CASE WHEN http_security_rate_windows.reset_at <= $2::timestamptz THEN EXCLUDED.reset_at
13
+ ELSE http_security_rate_windows.reset_at END
14
+ RETURNING hits`
15
+
16
+ /** Deletes a bounded batch of the windows that ended by $1; a second replica skips the rows this one holds. */
17
+ export const PRUNE_RATE_WINDOWS = sql`DELETE FROM http_security_rate_windows WHERE key IN (
18
+ SELECT key FROM http_security_rate_windows WHERE reset_at <= $1::timestamptz
19
+ LIMIT 100 FOR UPDATE SKIP LOCKED
20
+ )`
@@ -3,29 +3,34 @@ import type { ExecutionContext, Type } from "@nestjs/common"
3
3
  import type { Request } from "express"
4
4
  import { Reflector } from "@nestjs/core"
5
5
  import { Test } from "@nestjs/testing"
6
- import { FakeClock, mock } from "@starci/jest-preset"
6
+ import { FakeClock, mock, mockEntityManager } from "@starci/jest-preset"
7
+ import type { MockEntityManager } from "@starci/jest-preset"
7
8
  import { CLOCK } from "@modules/platform/clock"
9
+ import { PRIMARY_ENTITY_MANAGER } from "@modules/platform/database"
8
10
  import { HttpSecurityErrorCode } from "./errors/http-security.error"
9
11
  import { HTTP_SECURITY_OPTIONS } from "./http-security.decorators"
10
12
  import type { HttpSecurityOptions } from "./http-security.options"
13
+ import { HIT_RATE_WINDOW, PRUNE_RATE_WINDOWS } from "./persistence/rate-limit.sql"
11
14
  import { RateLimit, RateLimitGuard, RateTier } from "./rate-limit.guard"
12
15
 
13
16
  const AT = "2026-02-03T04:05:06.000Z"
17
+ const RESET_AT = "2026-02-03T04:06:06.000Z"
14
18
  const options: HttpSecurityOptions = {
15
19
  allowedOrigins: [],
16
20
  rateLimit: { windowMs: 60_000, defaultLimit: 2, strictLimit: 1 },
17
21
  webhooks: {},
18
22
  }
19
23
 
20
- const build = async (clock: FakeClock, tier?: RateTier) => {
24
+ const build = async (entityManager: MockEntityManager, tier?: RateTier) => {
21
25
  const reflector = mock<Reflector>()
22
26
  reflector.getAllAndOverride.mockReturnValue(tier)
23
27
  const moduleRef = await Test.createTestingModule({
24
28
  providers: [
25
29
  RateLimitGuard,
26
30
  { provide: Reflector, useValue: reflector },
27
- { provide: CLOCK, useValue: clock },
31
+ { provide: CLOCK, useValue: new FakeClock(AT) },
28
32
  { provide: HTTP_SECURITY_OPTIONS, useValue: options },
33
+ { provide: PRIMARY_ENTITY_MANAGER, useValue: entityManager },
29
34
  ],
30
35
  }).compile()
31
36
  return { guard: moduleRef.get(RateLimitGuard), reflector }
@@ -33,79 +38,100 @@ const build = async (clock: FakeClock, tier?: RateTier) => {
33
38
 
34
39
  /** An execution context of one POST from `ip` on the door; the handler and the class carry the tier metadata. */
35
40
  const contextOf = (ip: string | undefined, handler = () => undefined, door: Type<unknown> = class Door {}) => {
36
- let address = ip
37
41
  const http = mock<ReturnType<ExecutionContext["switchToHttp"]>>()
38
- http.getRequest.mockImplementation(() => mock<Request>({ headers: {}, method: "POST", ip: address }))
42
+ http.getRequest.mockImplementation(() => mock<Request>({ headers: {}, method: "POST", ip }))
39
43
  const context = mock<ExecutionContext>()
40
44
  context.switchToHttp.mockReturnValue(http)
41
45
  context.getHandler.mockReturnValue(handler)
42
46
  context.getClass.mockReturnValue(door)
43
- return { context, setIp: (value: string | undefined) => (address = value) }
47
+ return context
44
48
  }
45
49
 
46
50
  describe("RateLimit", () => {
47
51
  it("marks a door with the tier it declares, and the guard counts it under that tier's limit", async () => {
48
52
  class Door {}
49
53
  RateLimit(RateTier.Strict)(Door)
50
- const { guard, reflector } = await build(new FakeClock(AT), RateTier.Strict)
54
+ const entityManager = mockEntityManager({ query: [HIT_RATE_WINDOW, [{ hits: 2 }]] })
55
+ const { guard, reflector } = await build(entityManager, RateTier.Strict)
51
56
  const handler = () => undefined
52
- const { context } = contextOf("127.0.0.1", handler, Door)
53
57
 
54
58
  expect(Reflect.getMetadata("platform.http-security.rate-tier", Door)).toBe(RateTier.Strict)
55
- expect(guard.canActivate(context)).toBe(true)
56
- expect(() => guard.canActivate(context)).toThrow(HttpSecurityErrorCode.RateLimited)
59
+ await expect(guard.canActivate(contextOf("127.0.0.1", handler, Door))).rejects.toMatchObject({
60
+ code: HttpSecurityErrorCode.RateLimited,
61
+ })
57
62
  expect(reflector.getAllAndOverride).toHaveBeenCalledWith("platform.http-security.rate-tier", [handler, Door])
63
+ expect(entityManager.query).toHaveBeenCalledWith(HIT_RATE_WINDOW, [
64
+ "strict:127.0.0.1",
65
+ new Date(AT),
66
+ new Date(RESET_AT),
67
+ ])
58
68
  })
59
69
  })
60
70
 
61
71
  describe("RateLimitGuard", () => {
62
- it("counts an unmarked door under the default limit", async () => {
63
- const { guard } = await build(new FakeClock(AT))
64
- const { context } = contextOf("127.0.0.1")
65
-
66
- expect(guard.canActivate(context)).toBe(true)
67
- expect(guard.canActivate(context)).toBe(true)
68
- expect(() => guard.canActivate(context)).toThrow(HttpSecurityErrorCode.RateLimited)
72
+ it("admits a caller under the default limit of an unmarked door, counting in the shared window", async () => {
73
+ const entityManager = mockEntityManager({ query: [HIT_RATE_WINDOW, [{ hits: 2 }]] })
74
+ const { guard } = await build(entityManager)
75
+
76
+ await expect(guard.canActivate(contextOf("127.0.0.1"))).resolves.toBe(true)
77
+ expect(entityManager.query).toHaveBeenCalledWith(HIT_RATE_WINDOW, [
78
+ "default:127.0.0.1",
79
+ new Date(AT),
80
+ new Date(RESET_AT),
81
+ ])
69
82
  })
70
83
 
71
- it("opens a fresh window for a caller whose bucket expired", async () => {
72
- const clock = new FakeClock(AT)
73
- const { guard } = await build(clock)
74
- const { context } = contextOf("127.0.0.1")
75
-
76
- guard.canActivate(context)
77
- guard.canActivate(context)
78
- expect(() => guard.canActivate(context)).toThrow(HttpSecurityErrorCode.RateLimited)
84
+ it("refuses a caller whose shared window is over the default limit", async () => {
85
+ const { guard } = await build(mockEntityManager({ query: [HIT_RATE_WINDOW, [{ hits: 3 }]] }))
79
86
 
80
- clock.advance(60_001)
87
+ await expect(guard.canActivate(contextOf("127.0.0.1"))).rejects.toMatchObject({
88
+ code: HttpSecurityErrorCode.RateLimited,
89
+ })
90
+ })
81
91
 
82
- expect(guard.canActivate(context)).toBe(true)
92
+ it("prunes the expired windows when a hit opens a fresh one", async () => {
93
+ const entityManager = mockEntityManager({
94
+ query: [
95
+ [HIT_RATE_WINDOW, [{ hits: 1 }]],
96
+ [PRUNE_RATE_WINDOWS, []],
97
+ ],
98
+ })
99
+ const { guard } = await build(entityManager)
100
+
101
+ await expect(guard.canActivate(contextOf("127.0.0.1"))).resolves.toBe(true)
102
+ expect(entityManager.query).toHaveBeenCalledWith(PRUNE_RATE_WINDOWS, [new Date(AT)])
83
103
  })
84
104
 
85
- it("counts a request without an address under the unknown bucket", async () => {
86
- const { guard } = await build(new FakeClock(AT))
87
- const { context } = contextOf(undefined)
105
+ it("counts a request without an address under the unknown key", async () => {
106
+ const entityManager = mockEntityManager({ query: [HIT_RATE_WINDOW, [{ hits: 2 }]] })
107
+ const { guard } = await build(entityManager)
88
108
 
89
- guard.canActivate(context)
90
- guard.canActivate(context)
91
- expect(() => guard.canActivate(context)).toThrow(HttpSecurityErrorCode.RateLimited)
109
+ await expect(guard.canActivate(contextOf(undefined))).resolves.toBe(true)
110
+ expect(entityManager.query).toHaveBeenCalledWith(HIT_RATE_WINDOW, [
111
+ "default:unknown",
112
+ new Date(AT),
113
+ new Date(RESET_AT),
114
+ ])
92
115
  })
93
116
 
94
- it("prunes the expired buckets when the map outgrows the bucket cap", async () => {
95
- const clock = new FakeClock(AT)
96
- const { guard } = await build(clock)
97
- const { context, setIp } = contextOf("0.0.0.0")
117
+ it("bounds the window key to the length the table stores", async () => {
118
+ const entityManager = mockEntityManager({ query: [HIT_RATE_WINDOW, [{ hits: 2 }]] })
119
+ const { guard } = await build(entityManager)
98
120
 
99
- for (let index = 0; index <= 10_000; index += 1) {
100
- setIp(`10.${(index >> 16) % 256}.${(index >> 8) % 256}.${index % 256}`)
101
- guard.canActivate(context)
102
- }
121
+ await guard.canActivate(contextOf("f".repeat(300)))
122
+
123
+ expect(entityManager.query).toHaveBeenCalledWith(HIT_RATE_WINDOW, [
124
+ `default:${"f".repeat(120)}`,
125
+ new Date(AT),
126
+ new Date(RESET_AT),
127
+ ])
128
+ })
103
129
 
104
- clock.advance(60_001)
105
- setIp("127.0.0.1")
130
+ it("refuses the request when the store answers no window", async () => {
131
+ const { guard } = await build(mockEntityManager({ query: [HIT_RATE_WINDOW, []] }))
106
132
 
107
- expect(guard.canActivate(context)).toBe(true)
108
- expect(guard.canActivate(context)).toBe(true)
109
- expect(() => guard.canActivate(context)).toThrow(HttpSecurityErrorCode.RateLimited)
133
+ await expect(guard.canActivate(contextOf("127.0.0.1"))).rejects.toMatchObject({
134
+ code: HttpSecurityErrorCode.RateLimited,
135
+ })
110
136
  })
111
137
  })
@@ -1,13 +1,17 @@
1
1
  import { Injectable, SetMetadata } from "@nestjs/common"
2
2
  import type { CanActivate, ExecutionContext } from "@nestjs/common"
3
3
  import type { Reflector } from "@nestjs/core"
4
+ import type { EntityManager } from "typeorm"
4
5
  import { InjectClock } from "@modules/platform/clock"
5
6
  import type { Clock } from "@modules/platform/clock"
6
7
  import { InjectReflector } from "@modules/platform/composition"
8
+ import { InjectPrimaryEntityManager } from "@modules/platform/database"
7
9
  import { HttpSecurityError, HttpSecurityErrorCode } from "./errors/http-security.error"
8
10
  import { requestOf } from "./execution-request.mapper"
9
11
  import { InjectHttpSecurityOptions } from "./http-security.decorators"
10
12
  import type { HttpSecurityOptions } from "./http-security.options"
13
+ import type { RateWindowRow } from "./persistence/rate-limit.rows"
14
+ import { HIT_RATE_WINDOW, PRUNE_RATE_WINDOWS } from "./persistence/rate-limit.sql"
11
15
 
12
16
  /** The rate limit tiers of a door. */
13
17
  export enum RateTier {
@@ -18,29 +22,28 @@ export enum RateTier {
18
22
  }
19
23
 
20
24
  const RATE_TIER_KEY = "platform.http-security.rate-tier"
21
- const BUCKETS_MAX = 10_000
22
-
23
- interface Bucket {
24
- count: number
25
- resetAt: number
26
- }
25
+ /** The longest window key the table stores: the tier, a colon and the caller address. */
26
+ const KEY_MAX = 128
27
27
 
28
28
  /** Marks a door with a rate limit tier other than the default. */
29
29
  export const RateLimit = (tier: RateTier): ReturnType<typeof SetMetadata> => SetMetadata(RATE_TIER_KEY, tier)
30
30
 
31
31
  @Injectable()
32
- /** The first app guard: a fixed-window counter per caller address and tier, kept in process memory and stamped by the Clock. */
32
+ /**
33
+ * The first app guard: a fixed-window counter per caller address and tier, stamped by the Clock and kept in the primary
34
+ * database, so every replica counts against the same window. A hit that opens a fresh window also prunes a bounded batch of
35
+ * expired windows.
36
+ */
33
37
  export class RateLimitGuard implements CanActivate {
34
- private readonly buckets = new Map<string, Bucket>()
35
-
36
38
  constructor(
37
39
  @InjectReflector() private readonly reflector: Reflector,
38
40
  @InjectClock() private readonly clock: Clock,
39
41
  @InjectHttpSecurityOptions() private readonly options: HttpSecurityOptions,
42
+ @InjectPrimaryEntityManager() private readonly entityManager: EntityManager,
40
43
  ) {}
41
44
 
42
45
  /** Counts the request and refuses it when the caller is over the limit of the door tier. */
43
- canActivate(context: ExecutionContext): boolean {
46
+ async canActivate(context: ExecutionContext): Promise<boolean> {
44
47
  const tier =
45
48
  this.reflector.getAllAndOverride<RateTier | undefined>(RATE_TIER_KEY, [
46
49
  context.getHandler(),
@@ -48,21 +51,13 @@ export class RateLimitGuard implements CanActivate {
48
51
  ]) ?? RateTier.Default
49
52
  const limit =
50
53
  tier === RateTier.Strict ? this.options.rateLimit.strictLimit : this.options.rateLimit.defaultLimit
51
- const now = this.clock.now().getTime()
52
- const key = `${tier}:${requestOf(context).ip ?? "unknown"}`
53
- const current = this.buckets.get(key)
54
- const bucket =
55
- current && current.resetAt > now ? current : { count: 0, resetAt: now + this.options.rateLimit.windowMs }
56
- bucket.count += 1
57
- this.buckets.set(key, bucket)
58
- if (this.buckets.size > BUCKETS_MAX) this.prune(now)
59
- if (bucket.count > limit) throw new HttpSecurityError({ code: HttpSecurityErrorCode.RateLimited })
54
+ const now = this.clock.now()
55
+ const key = `${tier}:${requestOf(context).ip ?? "unknown"}`.slice(0, KEY_MAX)
56
+ const resetAt = new Date(now.getTime() + this.options.rateLimit.windowMs)
57
+ const rows: Array<RateWindowRow> = await this.entityManager.query(HIT_RATE_WINDOW, [key, now, resetAt])
58
+ const hits = rows[0]?.hits ?? Number.POSITIVE_INFINITY
59
+ if (hits === 1) await this.entityManager.query(PRUNE_RATE_WINDOWS, [now])
60
+ if (hits > limit) throw new HttpSecurityError({ code: HttpSecurityErrorCode.RateLimited })
60
61
  return true
61
62
  }
62
-
63
- private prune(now: number): void {
64
- for (const [key, bucket] of this.buckets) {
65
- if (bucket.resetAt <= now) this.buckets.delete(key)
66
- }
67
- }
68
63
  }
@@ -5,11 +5,14 @@ import type { ErrorKind } from "@modules/platform/errors"
5
5
  export enum DatabaseErrorCode {
6
6
  /** A configured connection has no declared EntityManager injection token. */
7
7
  ConnectionUndeclared = "DATABASE_CONNECTION_UNDECLARED",
8
+ /** A dynamic SQL identifier was not on the names its caller allows. */
9
+ IdentifierRejected = "DATABASE_IDENTIFIER_REJECTED",
8
10
  }
9
11
 
10
12
  /** How each database code travels. */
11
13
  export const DATABASE_ERROR_KINDS: Record<DatabaseErrorCode, ErrorKind> = {
12
14
  [DatabaseErrorCode.ConnectionUndeclared]: "internal",
15
+ [DatabaseErrorCode.IdentifierRejected]: "internal",
13
16
  }
14
17
 
15
18
  /** The one error class of the database capability. */
@@ -1,7 +1,8 @@
1
1
  export { DatabaseModule } from "./database.module"
2
- export { DATABASE_PROBE, InjectDatabaseProbe } from "./primary.decorators"
2
+ export { DATABASE_PROBE, InjectDatabaseProbe, InjectPrimaryEntityManager, PRIMARY_ENTITY_MANAGER } from "./primary.decorators"
3
3
  export { DatabaseProbeService } from "./database-probe.service"
4
4
  export type { DatabaseConnectionOptions } from "./database.options"
5
5
  export { PRIMARY_DATA_SOURCE } from "./database.port"
6
+ export { sql } from "./database.sql"
6
7
  export { DATABASE_ERROR_KINDS } from "./errors/database.error"
7
8
  export { parsePrimaryDatabaseConfig } from "./primary.config"