create-tigra 3.3.0 → 4.0.1

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.
Files changed (207) hide show
  1. package/CHANGELOG.md +151 -0
  2. package/README.md +264 -56
  3. package/bin/create-tigra.js +497 -472
  4. package/lib/patchers/email-verification.patcher.js +72 -10
  5. package/modules/email-verification/client/hooks/useVerification.ts +4 -1
  6. package/modules/email-verification/server/__tests__/verification.integration.test.ts +115 -0
  7. package/modules/email-verification/server/verification.controller.ts +3 -1
  8. package/modules/email-verification/server/verification.service.ts +44 -13
  9. package/package.json +5 -3
  10. package/template/.github/dependabot.yml +47 -0
  11. package/template/.github/workflows/audit.yml +36 -0
  12. package/template/.github/workflows/ci.yml +79 -0
  13. package/template/AGENTS.md +18 -9
  14. package/template/README.md +79 -0
  15. package/template/client/.dockerignore +1 -0
  16. package/template/client/.env.example.production +13 -2
  17. package/template/client/AGENTS.md +13 -5
  18. package/template/client/Dockerfile +186 -14
  19. package/template/client/README.md +92 -21
  20. package/template/client/next.config.ts +1 -1
  21. package/template/client/package-lock.json +15983 -14181
  22. package/template/client/package.json +56 -48
  23. package/template/client/scripts/check-sharp.mjs +53 -0
  24. package/template/client/scripts/next-dev.cjs +5 -1
  25. package/template/client/src/__tests__/proxy.test.ts +92 -0
  26. package/template/client/src/app/healthz/route.ts +16 -0
  27. package/template/client/src/app/layout.tsx +9 -3
  28. package/template/client/src/app/page.tsx +2 -2
  29. package/template/client/src/app/providers.tsx +23 -4
  30. package/template/client/src/components/layout/Header.tsx +7 -3
  31. package/template/client/src/features/admin/hooks/useAdminSessions.ts +2 -1
  32. package/template/client/src/features/admin/hooks/useAdminUsers.ts +3 -1
  33. package/template/client/src/features/admin/types/admin.types.ts +2 -3
  34. package/template/client/src/features/auth/components/AuthInitializer.tsx +35 -18
  35. package/template/client/src/features/auth/components/ForgotPasswordForm.tsx +107 -0
  36. package/template/client/src/features/auth/components/LimitNotice.tsx +28 -0
  37. package/template/client/src/features/auth/components/LoginForm.tsx +13 -2
  38. package/template/client/src/features/auth/components/RegisterForm.tsx +23 -7
  39. package/template/client/src/features/auth/hooks/useAuth.ts +11 -1
  40. package/template/client/src/features/auth/hooks/usePasswordReset.ts +7 -1
  41. package/template/client/src/features/auth/services/auth.service.ts +8 -3
  42. package/template/client/src/features/auth/store/authSlice.ts +7 -2
  43. package/template/client/src/lib/api/__tests__/axios.config.test.ts +183 -0
  44. package/template/client/src/lib/api/api.types.ts +14 -0
  45. package/template/client/src/lib/api/axios.config.ts +101 -15
  46. package/template/client/src/lib/api/hard-redirect.ts +8 -0
  47. package/template/client/src/lib/api/session-events.ts +29 -0
  48. package/template/client/src/lib/constants/routes.ts +12 -0
  49. package/template/client/src/lib/observability/sentry-scrub.ts +1 -1
  50. package/template/client/src/lib/utils/__tests__/error.test.ts +119 -0
  51. package/template/client/src/lib/utils/__tests__/form.test.ts +42 -0
  52. package/template/client/src/lib/utils/__tests__/paths.test.ts +43 -0
  53. package/template/client/src/lib/utils/__tests__/security.test.ts +40 -0
  54. package/template/client/src/lib/utils/__tests__/session-marker.test.ts +27 -0
  55. package/template/client/src/lib/utils/error.ts +81 -0
  56. package/template/client/src/lib/utils/form.ts +31 -0
  57. package/template/client/src/lib/utils/paths.ts +18 -0
  58. package/template/client/src/lib/utils/session-marker.ts +24 -0
  59. package/template/client/src/{middleware.ts → proxy.ts} +25 -11
  60. package/template/client/src/store/__tests__/store.test.ts +44 -0
  61. package/template/client/src/store/index.ts +26 -8
  62. package/template/client/vitest.config.mts +48 -0
  63. package/template/server/.env.example +280 -270
  64. package/template/server/.env.example.production +283 -242
  65. package/template/server/AGENTS.md +13 -4
  66. package/template/server/Dockerfile +176 -14
  67. package/template/server/README.md +108 -0
  68. package/template/server/docker-compose.yml +4 -2
  69. package/template/server/docs/deployment.md +63 -0
  70. package/template/server/docs/logging.md +66 -4
  71. package/template/server/docs/rate-limits.md +153 -0
  72. package/template/server/docs/testing.md +81 -0
  73. package/template/server/eslint.config.mjs +19 -0
  74. package/template/server/package-lock.json +8784 -8743
  75. package/template/server/package.json +79 -75
  76. package/template/server/prisma/migrations/0_init/migration.sql +82 -0
  77. package/template/server/prisma/migrations/migration_lock.toml +3 -0
  78. package/template/server/prisma/schema.prisma +0 -3
  79. package/template/server/scripts/check-heic.mjs +40 -0
  80. package/template/server/scripts/check-sharp.mjs +53 -0
  81. package/template/server/src/__tests__/routes.integration.test.ts +280 -0
  82. package/template/server/src/app.ts +248 -346
  83. package/template/server/src/config/__tests__/env.test.ts +219 -0
  84. package/template/server/src/config/__tests__/rate-limit.config.test.ts +158 -0
  85. package/template/server/src/config/env.ts +326 -182
  86. package/template/server/src/config/rate-limit.config.ts +157 -106
  87. package/template/server/src/jobs/__tests__/cleanup-deleted-accounts.job.test.ts +60 -8
  88. package/template/server/src/jobs/__tests__/cleanup-expired-auth.job.test.ts +44 -0
  89. package/template/server/src/jobs/__tests__/jobs.integration.test.ts +88 -0
  90. package/template/server/src/jobs/__tests__/schedule.test.ts +92 -0
  91. package/template/server/src/jobs/cleanup-deleted-accounts.job.ts +38 -36
  92. package/template/server/src/jobs/cleanup-expired-auth.job.ts +32 -24
  93. package/template/server/src/jobs/index.ts +12 -2
  94. package/template/server/src/jobs/refresh-cloudflare-ranges.job.ts +28 -0
  95. package/template/server/src/jobs/schedule.ts +50 -0
  96. package/template/server/src/libs/__tests__/auth.test.ts +98 -7
  97. package/template/server/src/libs/__tests__/client-ip.test.ts +257 -121
  98. package/template/server/src/libs/__tests__/cloudflare-ranges.test.ts +79 -0
  99. package/template/server/src/libs/__tests__/cookies.test.ts +53 -2
  100. package/template/server/src/libs/__tests__/email-quota.test.ts +81 -0
  101. package/template/server/src/libs/__tests__/email.test.ts +68 -0
  102. package/template/server/src/libs/__tests__/error-handler.test.ts +162 -0
  103. package/template/server/src/libs/__tests__/health-db-down.integration.test.ts +37 -0
  104. package/template/server/src/libs/__tests__/health-redis-down.integration.test.ts +45 -0
  105. package/template/server/src/libs/__tests__/health-routes.test.ts +83 -0
  106. package/template/server/src/libs/__tests__/health.integration.test.ts +118 -0
  107. package/template/server/src/libs/__tests__/http.test.ts +74 -2
  108. package/template/server/src/libs/__tests__/ip-block.test.ts +179 -62
  109. package/template/server/src/libs/__tests__/logger.test.ts +50 -0
  110. package/template/server/src/libs/__tests__/monitoring.test.ts +185 -0
  111. package/template/server/src/libs/__tests__/outbound.integration.test.ts +48 -0
  112. package/template/server/src/libs/__tests__/prisma-client.test.ts +28 -0
  113. package/template/server/src/libs/__tests__/query-counter.integration.test.ts +53 -0
  114. package/template/server/src/libs/__tests__/rate-limiting.test.ts +146 -0
  115. package/template/server/src/libs/__tests__/rate-limits.integration.test.ts +53 -0
  116. package/template/server/src/libs/__tests__/redis-required.test.ts +30 -0
  117. package/template/server/src/libs/__tests__/redis.test.ts +38 -2
  118. package/template/server/src/libs/__tests__/request-id.test.ts +25 -0
  119. package/template/server/src/libs/__tests__/requestLogger.test.ts +126 -0
  120. package/template/server/src/libs/__tests__/safe-http.test.ts +138 -0
  121. package/template/server/src/libs/__tests__/sentry-scrub.test.ts +82 -80
  122. package/template/server/src/libs/__tests__/shutdown.test.ts +89 -0
  123. package/template/server/src/libs/__tests__/url-safety.test.ts +130 -80
  124. package/template/server/src/libs/auth.ts +39 -10
  125. package/template/server/src/libs/client-ip.ts +158 -77
  126. package/template/server/src/libs/cloudflare-ranges.ts +132 -0
  127. package/template/server/src/libs/cookies.ts +53 -14
  128. package/template/server/src/libs/email-quota.ts +74 -0
  129. package/template/server/src/libs/email.ts +46 -9
  130. package/template/server/src/libs/error-handler.ts +111 -0
  131. package/template/server/src/libs/health-routes.ts +53 -0
  132. package/template/server/src/libs/http.ts +139 -47
  133. package/template/server/src/libs/ip-address.ts +37 -0
  134. package/template/server/src/libs/ip-block.ts +284 -221
  135. package/template/server/src/libs/logger.ts +41 -12
  136. package/template/server/src/libs/monitoring.ts +127 -41
  137. package/template/server/src/libs/observability/sentry.ts +1 -1
  138. package/template/server/src/libs/origin-check.ts +9 -7
  139. package/template/server/src/libs/prisma.ts +47 -4
  140. package/template/server/src/libs/query-counter.ts +71 -59
  141. package/template/server/src/libs/rate-limiting.ts +110 -0
  142. package/template/server/src/libs/redis-required.ts +28 -0
  143. package/template/server/src/libs/redis.ts +27 -1
  144. package/template/server/src/libs/request-id.ts +18 -0
  145. package/template/server/src/libs/requestLogger.ts +112 -17
  146. package/template/server/src/libs/safe-http.ts +101 -0
  147. package/template/server/src/libs/shutdown.ts +87 -0
  148. package/template/server/src/libs/storage/__tests__/fixtures/tiny-heic.ts +13 -0
  149. package/template/server/src/libs/storage/__tests__/image-optimizer.test.ts +59 -0
  150. package/template/server/src/libs/storage/__tests__/multipart-limits.test.ts +91 -0
  151. package/template/server/src/libs/storage/__tests__/upload-persistence.test.ts +52 -0
  152. package/template/server/src/libs/storage/file-storage.service.ts +94 -84
  153. package/template/server/src/libs/storage/file-validator.ts +48 -46
  154. package/template/server/src/libs/storage/image-optimizer.service.ts +16 -5
  155. package/template/server/src/libs/token-hash.ts +18 -0
  156. package/template/server/src/libs/url-safety.ts +127 -121
  157. package/template/server/src/libs/validation-errors.ts +86 -0
  158. package/template/server/src/modules/admin/__tests__/admin-actions.integration.test.ts +255 -0
  159. package/template/server/src/modules/admin/__tests__/admin.health.test.ts +82 -0
  160. package/template/server/src/modules/admin/__tests__/admin.repo.test.ts +55 -0
  161. package/template/server/src/modules/admin/__tests__/admin.schemas.test.ts +38 -0
  162. package/template/server/src/modules/admin/__tests__/admin.service.test.ts +162 -0
  163. package/template/server/src/modules/admin/admin.controller.ts +66 -49
  164. package/template/server/src/modules/admin/admin.repo.ts +49 -10
  165. package/template/server/src/modules/admin/admin.routes.ts +48 -2
  166. package/template/server/src/modules/admin/admin.schemas.ts +18 -6
  167. package/template/server/src/modules/admin/admin.service.ts +66 -4
  168. package/template/server/src/modules/auth/__tests__/auth.controller.test.ts +39 -0
  169. package/template/server/src/modules/auth/__tests__/auth.integration.test.ts +9 -3
  170. package/template/server/src/modules/auth/__tests__/auth.service.dummy-hash.test.ts +34 -0
  171. package/template/server/src/modules/auth/__tests__/auth.service.test.ts +197 -37
  172. package/template/server/src/modules/auth/__tests__/session-cleanup.test.ts +39 -0
  173. package/template/server/src/modules/auth/__tests__/sessions.integration.test.ts +246 -0
  174. package/template/server/src/modules/auth/__tests__/token-fingerprints.test.ts +119 -0
  175. package/template/server/src/modules/auth/auth.controller.ts +133 -128
  176. package/template/server/src/modules/auth/auth.repo.ts +44 -44
  177. package/template/server/src/modules/auth/auth.schemas.ts +4 -2
  178. package/template/server/src/modules/auth/auth.service.ts +167 -96
  179. package/template/server/src/modules/auth/session.repo.ts +12 -0
  180. package/template/server/src/modules/files/__tests__/files.integration.test.ts +5 -5
  181. package/template/server/src/modules/users/__tests__/avatar-upload.test.ts +120 -0
  182. package/template/server/src/modules/users/__tests__/name-rules.test.ts +46 -0
  183. package/template/server/src/modules/users/__tests__/users.integration.test.ts +295 -0
  184. package/template/server/src/modules/users/__tests__/users.repo.test.ts +37 -0
  185. package/template/server/src/modules/users/__tests__/users.service.test.ts +129 -0
  186. package/template/server/src/modules/users/users.controller.ts +9 -38
  187. package/template/server/src/modules/users/users.repo.ts +23 -7
  188. package/template/server/src/modules/users/users.routes.ts +3 -22
  189. package/template/server/src/modules/users/users.schemas.ts +15 -9
  190. package/template/server/src/modules/users/users.service.ts +105 -93
  191. package/template/server/src/scripts/make-admin.ts +48 -0
  192. package/template/server/src/server.ts +48 -26
  193. package/template/server/src/shared/errors/AppError.ts +27 -1
  194. package/template/server/src/shared/errors/errors.ts +42 -5
  195. package/template/server/src/shared/responses/successResponse.ts +22 -3
  196. package/template/server/src/shared/types/index.ts +7 -0
  197. package/template/server/src/test/__tests__/integration-services.test.ts +66 -0
  198. package/template/server/src/test/integration-helpers.ts +343 -0
  199. package/template/server/src/test/integration-services.ts +74 -0
  200. package/template/server/src/test/integration.global-setup.ts +144 -0
  201. package/template/server/src/test/integration.setup.ts +35 -158
  202. package/template/server/src/test/setup.ts +3 -10
  203. package/template/server/tsconfig.build.json +1 -1
  204. package/template/server/tsconfig.json +1 -0
  205. package/template/server/vitest.config.ts +17 -4
  206. package/template/server/vitest.integration.config.ts +18 -13
  207. package/template/server/src/libs/storage/filename-sanitizer.ts +0 -71
@@ -0,0 +1,153 @@
1
+ # Rate limits, login lockout and IP blocks
2
+
3
+ Everything that slows down or stops a visitor, in one place. The limits themselves live in `src/config/rate-limit.config.ts`, the single source; the wiring is in `src/libs/rate-limiting.ts`.
4
+
5
+ ## How counting works
6
+
7
+ - **Per visitor IP.** The key is the verified client IP (`getClientIp`, see `src/libs/client-ip.ts`), never an address the visitor wrote into a header.
8
+ - **Per route.** Every route keeps its own counter, even when two routes share a limit entry.
9
+ - **Shared across server processes.** Counters live in Redis.
10
+ - **Every "slow down" answer says how long to wait** in `retryAfterSeconds` (whole seconds) inside the JSON body. The client runs on another origin and CORS does not expose the `Retry-After` header to it.
11
+
12
+ ## Route limits
13
+
14
+ | Route | Limit (per IP) | Name |
15
+ |---|---|---|
16
+ | `POST /auth/register` | 40 per hour | `AUTH_REGISTER` |
17
+ | `POST /auth/login` | 100 per 15 min | `AUTH_LOGIN` |
18
+ | `POST /auth/logout` | 200 per 15 min | `AUTH_LOGOUT` |
19
+ | `POST /auth/refresh` | 200 per 15 min | `AUTH_REFRESH` |
20
+ | `POST /auth/forgot-password` | 20 per 15 min (+ email cap) | `AUTH_FORGOT_PASSWORD` |
21
+ | `POST /auth/reset-password` | 100 per 15 min | `AUTH_RESET_PASSWORD` |
22
+ | `GET /auth/me` | 200 per minute | `AUTH_ME` |
23
+ | `GET /auth/sessions` | 30 per minute | `AUTH_SESSIONS` |
24
+ | `POST /auth/logout-all` | 10 per 15 min | `AUTH_LOGOUT_ALL` |
25
+ | `POST /auth/send-verification` (email module) | 10 per 15 min (+ email cap) | `AUTH_SEND_VERIFICATION` |
26
+ | `POST /auth/verify-account` (email module) | 10 per 15 min | `AUTH_VERIFY_ACCOUNT` |
27
+ | `PATCH /users/me`, `PATCH /users/:userId` | 30 per minute | `USERS_UPDATE_PROFILE` |
28
+ | `PATCH /users/me/password`, `PATCH /users/:userId/password` | 10 per 15 min | `USERS_CHANGE_PASSWORD` |
29
+ | `DELETE /users/me`, `DELETE /users/:userId` | 10 per minute | `USERS_DELETE_ACCOUNT` |
30
+ | `POST /users/avatar`, `POST /users/:userId/avatar` | 10 per minute | `USERS_UPLOAD_AVATAR` |
31
+ | `DELETE /users/avatar`, `DELETE /users/:userId/avatar` | 30 per minute | `USERS_DELETE_AVATAR` |
32
+ | `GET /files/:filename` | 100 per minute | `FILES_GET` |
33
+ | every `/admin/*` route | 30 per minute | `ADMIN_DEFAULT` |
34
+ | **any other route** (including routes you add) | 200 per minute | `DEFAULT` |
35
+ | unknown URLs (404s) | 200 per minute, one shared counter | `DEFAULT` |
36
+
37
+ All paths are under `/api/v1`. Password change checks the current password, so it stays low: someone holding a stolen session cannot guess that password. An avatar upload decodes an image of up to 50 MP, which is real CPU work.
38
+
39
+ **Never limited:**
40
+ - `GET /api/v1/health`, `/api/v1/ready` and `/api/v1/live` (health probes come from infrastructure IPs);
41
+ - static `/uploads/*` (one page can load dozens of avatars);
42
+ - CORS preflights (`OPTIONS`).
43
+
44
+ A new route gets `DEFAULT` automatically. Give it its own entry in `RATE_LIMITS` and `config: { rateLimit: RATE_LIMITS.MY_ROUTE }` when it needs a different number.
45
+
46
+ ## Email cap
47
+
48
+ At most **3 emails per hour to one address**, on two separate counters so a stranger can never use up the owner's emails:
49
+
50
+ | Counter | Emails it covers | Over the cap |
51
+ |---|---|---|
52
+ | public | forgot-password, send-verification (anyone can type an address) | `429 EMAIL_RATE_LIMITED` with the wait time |
53
+ | sign-in | the verification link re-sent when an unverified user signs in with the **right password** | email skipped and logged; the user is still told to verify |
54
+
55
+ - **Not counted at all:** the single verification email sent at register. An address can register only once, so it cannot flood anyone, and strangers who used up the public counter cannot stop it.
56
+ - Why the sign-in re-send is still capped: whoever registered an address knows its password and could otherwise flood that inbox by signing in again and again.
57
+ - Both counters are charged **before** looking the account up, so unknown addresses count exactly like real ones and the answer reveals nothing.
58
+ - Redis keys are a SHA-256 of the address; raw emails are never stored there.
59
+
60
+ The IP limits alone cannot do this: someone with many IPs could flood one inbox and burn the sender's quota and reputation.
61
+
62
+ ## Login lockout
63
+
64
+ This is separate from the route limits, and counted per **email + IP pair**. An attacker therefore only locks out their own address and can never lock the real user out remotely.
65
+
66
+ - The 15th failed sign-in locks that pair for **15 minutes**.
67
+ - The 30th failure (and every 15th after it) locks it for **1 hour**.
68
+ - The failure count is forgotten an hour after the last failure, and a successful sign-in clears it.
69
+ - While locked, every attempt, even with the right password, gets `429 LOGIN_LOCKED`: "Too many failed sign-in attempts. Try again in N minutes."
70
+ - Unknown emails are counted and locked the same way and take the same password-check time (a dummy hash). Neither the lock nor the timing reveals which emails are registered.
71
+ - A wrong password outside a lock still says "Invalid email or password".
72
+
73
+ ## IP auto-block
74
+
75
+ - Every request **over a limit** on a non-auth route records one strike against the IP. Requests to `/api/v1/auth/*` add no strikes, so a mistyped password can never ban a whole network.
76
+ - `IP_AUTO_BLOCK_THRESHOLD` strikes (default 20) within `IP_AUTO_BLOCK_WINDOW_SECONDS` (default 300) block the IP on **every** route for `IP_AUTO_BLOCK_DURATION_SECONDS` (default 3600). The answer is `403 IP_BLOCKED` with `retryAfterSeconds`.
77
+ - Private, loopback and reserved addresses (your own proxy) are never auto-blocked. Health probes are never blocked.
78
+ - Admins can add permanent blocks (`/admin/blocked-ips`). Those answer `403 IP_BLOCKED` "Access from your network is blocked." with no wait time.
79
+
80
+ Before you shorten a window, read the note at the top of `rate-limit.config.ts`. A client that keeps hammering a short-window route after its 429 bans itself within minutes.
81
+
82
+ ## Response shape
83
+
84
+ ```json
85
+ { "success": false, "error": { "code": "LOGIN_LOCKED", "message": "Too many failed sign-in attempts. Try again in 14 minutes.", "retryAfterSeconds": 840 } }
86
+ ```
87
+
88
+ | Code | Status | When |
89
+ |---|---|---|
90
+ | `RATE_LIMIT_EXCEEDED` | 429 | a route limit (or the default) was exceeded |
91
+ | `LOGIN_LOCKED` | 429 | too many failed sign-ins for this email + IP |
92
+ | `EMAIL_RATE_LIMITED` | 429 | too many emails to this address this hour |
93
+ | `IP_BLOCKED` | 403 | the IP is auto-blocked (`retryAfterSeconds` set) or permanently blocked (not set) |
94
+
95
+ **The client** (`client/src/lib/api/axios.config.ts`):
96
+ - It shows one toast with the wait time for these codes.
97
+ - The login, register and forgot-password forms show the message inline instead.
98
+ - React Query never retries these answers, because a retry would only earn another strike.
99
+ - A limited `/auth/refresh` never logs the user out.
100
+
101
+ ## Changing limits (ENV)
102
+
103
+ | Variable | Default | Meaning |
104
+ |---|---|---|
105
+ | `RATE_LIMIT_ENABLED` | `true` | Master switch. `false` makes every limit 1,000,000 (the email cap too). |
106
+ | `RATE_LIMIT_MULTIPLIER` | `1` | Scales every max, e.g. `10` for local development. |
107
+ | `RATE_LIMIT_OVERRIDES` | (empty) | Change single limits: `NAME=MAX/WINDOW,...` with `WINDOW` like `30s`, `15m` or `1h`. |
108
+
109
+ Example: `RATE_LIMIT_OVERRIDES=AUTH_LOGIN=200/15m,DEFAULT=300/1m,EMAIL_PER_ADDRESS=5/1h`
110
+
111
+ - `NAME` is any name from the table above (`RATE_LIMITS` keys), `DEFAULT` or `EMAIL_PER_ADDRESS`.
112
+ - An override replaces that limit's max and window; the multiplier still applies on top.
113
+ - An unknown name or a malformed value stops the server at boot with the list of valid names, so a typo never goes unnoticed.
114
+ - The old `RATE_LIMIT_AUTH_LOGIN_MAX` and `RATE_LIMIT_AUTH_REGISTER_MAX` are no longer read. If one is still set, the server logs a warning at boot and ignores it.
115
+
116
+ ## When Redis is down
117
+
118
+ The app keeps serving, but while Redis is unreachable:
119
+ - rate limits, the email cap and the login lockout are off;
120
+ - IP blocks are not enforced;
121
+ - forgot-password, reset-password, send-verification and verify-account answer `503 SERVICE_UNAVAILABLE` ("Password reset is temporarily unavailable. Please try again in a few minutes."). Their tokens live in Redis, so they cannot work. The check runs before any account lookup, so the answer is the same for every address and reveals nothing (`src/libs/redis-required.ts`).
122
+
123
+ `/api/v1/health` reports `degraded`; admins see that it is Redis at `/api/v1/admin/health`. Everything returns on its own when Redis is back; permanent blocks are re-loaded from the database on reconnect.
124
+
125
+ A missing or unreachable `REDIS_URL` at boot is different from an outage: in production the server refuses to start without it, and also when it cannot reach Redis within 10 s of starting (a wrong host or password), because it would otherwise run with every protection above switched off while its health check still passes. On Coolify that failed start never becomes healthy, so a rolling deploy keeps the old container serving. Outside production it defaults to `redis://localhost:6379` and starts without Redis.
126
+
127
+ ## Behind Cloudflare
128
+
129
+ Cloudflare requests that cannot be verified as your own zone's traffic share **one** counter. This happens in two cases:
130
+ - an origin secret is set without its Transform Rule;
131
+ - the traffic comes through a Cloudflare Worker.
132
+
133
+ On a correct setup this never happens. The server logs `[CLIENT-IP] Cloudflare requests ... share one rate-limit bucket` (at most every 10 minutes) when it does. See `docs/client-ip-behind-proxies.md` in the create-tigra repository.
134
+
135
+ ## Known limits (read before relying on these)
136
+
137
+ - **Anyone can use up an address's public email counter.** Three anonymous forgot-password or send-verification requests in an hour use up that address's public counter. For the rest of the hour nobody gets a reset link through that address. A determined stranger can keep doing this every hour, keeping one person from resetting a password by email. Verification is **not** blocked: the register email is not capped, and signing in with the right password re-sends the link on its own counter. A user who knows their password is not affected.
138
+ - **Whether an email has an account can be found out.** Sign-up answers `409 EMAIL_ALREADY_EXISTS` for an existing account (and for one deleted recently, while its email stays reserved), and forgot-password gives the same answer for every address but takes noticeably longer for a real account, because it sends the email before replying. This is accepted on purpose (most apps do the same, and hiding it would make sign-up confusing). Sign-in and the lockout still reveal nothing: unknown emails cost the same time and lock the same way.
139
+ - **An unverified sign-up keeps its email.** With email verification on, someone can sign up with another person's address and never verify it. The real owner then gets `409 EMAIL_ALREADY_EXISTS` when signing up. They are not locked out: "forgot password" sends the reset link to their own inbox, and after resetting, signing in sends them the verification link. Accounts are never deleted automatically for staying unverified.
140
+ - **Parallel sign-in attempts can get ahead of the lockout.** A burst of simultaneous failed sign-ins from one IP is checked before any of them is counted, so it can try more than 15 passwords. It is still capped by the login route limit (100 per 15 minutes per IP).
141
+ - **`HEAD` requests count separately from `GET`.** Fastify adds a `HEAD` route for each `GET`, and it gets its own counter.
142
+ - **IP blocks are single addresses.** An IPv6 visitor usually controls a whole /64 network and can switch to another address in it, so blocking one IPv6 address only slows such a visitor down. Ranges (CIDR) are not supported. Blocks are stored in one canonical spelling, so `2001:DB8::1` and `2001:0db8:0:0::1` are the same block. An admin cannot block their own current address, nor a private or reserved one.
143
+
144
+ ## Porting to an app made with an older create-tigra
145
+
146
+ 1. Copy `src/config/rate-limit.config.ts`, `src/libs/rate-limiting.ts` and `src/libs/email-quota.ts`. Then copy the `app.ts` changes (`registerRateLimiting`, `registerNotFoundHandler`, `rateLimit: false` on the health routes, `getIpBlock`), `TooManyRequestsError` and `retryAfterSeconds` in `shared/errors`, `errorResponse`, and the auth-service lockout.
147
+ 2. Replace `RATE_LIMIT_AUTH_LOGIN_MAX` / `RATE_LIMIT_AUTH_REGISTER_MAX` in every environment with `RATE_LIMIT_OVERRIDES`, for example `AUTH_LOGIN=100/15m`. Remember that the old variables are now ignored.
148
+ 3. Drop the two dead lockout columns with a migration. Nothing reads them any more:
149
+ ```sql
150
+ ALTER TABLE `users` DROP COLUMN `failedLoginAttempts`, DROP COLUMN `lockedUntil`;
151
+ ```
152
+ These are the template's column names (the fields have no `@map`). Or remove `failedLoginAttempts` and `lockedUntil` from the `User` model and let `prisma migrate dev` generate the migration.
153
+ 4. Client: copy `getLimitInfo` / `isLimitError` / `formatWait` (`lib/utils/error.ts`), the interceptor branch and the refresh check (`lib/api/axios.config.ts`), the `retry` function (`app/providers.tsx`), `LimitNotice` and the form changes.
@@ -0,0 +1,81 @@
1
+ # Testing
2
+
3
+ The server has two suites, both Vitest (the client's unit tests are at the end):
4
+
5
+ | Suite | Command | Needs | What it proves |
6
+ |---|---|---|---|
7
+ | Unit | `npm test` | nothing (no database, no network) | the logic of each module, with the database and Redis mocked |
8
+ | Integration | `npm run test:integration` | a MySQL and a Redis (see below) | the real app answering real requests (`app.inject`) against a real MySQL and Redis |
9
+
10
+ Run both before you push. Coverage: `npm run test:coverage` (unit suite).
11
+
12
+ ## Integration tests: where MySQL and Redis come from
13
+
14
+ **Testcontainers (default, and what CI uses).** With neither variable below set, the suite starts a throwaway
15
+ MySQL 8.4 and Redis 7 in Docker, once per run, and removes them afterwards. Docker must be running.
16
+
17
+ **Services you already run.** Set both:
18
+
19
+ ```bash
20
+ INTEGRATION_DATABASE_URL="mysql://user:password@127.0.0.1:3306/myapp_test" \
21
+ INTEGRATION_REDIS_URL="redis://127.0.0.1:6379/15" \
22
+ npm run test:integration
23
+ ```
24
+
25
+ No container is started. **The suite drops every table in that database and flushes that Redis database**, so it
26
+ refuses anything that does not look disposable:
27
+
28
+ - the database name must end in `_test` (create it first, for example `CREATE DATABASE myapp_test`);
29
+ - the Redis URL must name a database from 1 to 15 (database 0, the default, is refused);
30
+ - setting only one of the two variables is refused.
31
+
32
+ The messages name the rule, never the URL.
33
+
34
+ Either way the schema comes from `prisma/migrations` (`prisma migrate deploy`), the same migrations production
35
+ applies, so a migration that does not match the code fails here first.
36
+
37
+ ## Writing integration tests
38
+
39
+ - Name the file `*.integration.test.ts`; `npm test` skips those.
40
+ - Use the helpers in `src/test/integration-helpers.ts`: `buildTestApp()`, `registerUser()` / `registerAdmin()`
41
+ (signed in, with a cookie jar per device), `call()` for requests, `db()` / `redis()` for checking stored state,
42
+ `uploadAvatar()`, and `startEmailStub()`, a local stand-in for Resend that catches every email (nothing is sent).
43
+ - Every new route gets a row in `src/__tests__/routes.integration.test.ts`: who may call it (401/403), what wrong
44
+ input answers (422) and its success answer. Deeper behaviour goes in the area's own integration file.
45
+ - That file checks itself: its last test fails, naming the route, when a registered route is never called in it.
46
+ A route another file already tests goes in its `COVERED_ELSEWHERE` list with that file's name:
47
+
48
+ ```text
49
+ × every registered route was called above, or is in COVERED_ELSEWHERE
50
+ + "GET /api/v1/reports/:id"
51
+ ```
52
+
53
+ Run it on its own with `-t` and it skips itself (the other tests did not run).
54
+ - Import the app lazily, inside `beforeAll` (`const { buildApp } = await import('@/app.js')`): the environment is
55
+ set by `src/test/integration.setup.ts` and must be in place before `env.ts` is loaded.
56
+ - All files share one database and Redis and run one after another. Create your own data with unique emails; never
57
+ rely on what another file left behind.
58
+ - Rate limits and email verification are off in this suite. A file that tests them switches them on in its
59
+ `process.env` before importing the app (each file runs with fresh modules).
60
+
61
+ ## CI
62
+
63
+ `.github/workflows/ci.yml` (project root) runs on every push and pull request: typecheck, lint,
64
+ `npm run test:coverage`, the server build, `npm run test:integration` (Testcontainers on GitHub's Ubuntu runners)
65
+ and the client's typecheck, lint, `npm run test:coverage` and build. It never deploys.
66
+
67
+ ## Coverage
68
+
69
+ `npm run test:coverage` counts every file in `src/` (type-only files and test helpers excluded) and fails below the
70
+ thresholds in `vitest.config.ts`. They are a ratchet: the real numbers when they were set, rounded down. Raise them
71
+ when you add tests; never lower them to make a change pass. Routes and controllers are tested by the integration
72
+ suite, which the coverage gate does not count.
73
+
74
+ ## Client tests
75
+
76
+ `client/` has its own Vitest unit suite (`npm test`, `npm run test:coverage` with its floor in
77
+ `client/vitest.config.mts`). It covers the logic under the screens: the API client's session rules (one refresh for
78
+ parallel 401s, a refresh race lost to another tab, a ban mid-session), the proxy's CSP and redirects, redirect
79
+ safety, path matching, the session marker, error and field-error parsing, and the store. The template ships no pages;
80
+ when you add them, keep their logic in functions under `client/src/lib/` and test it there.
81
+
@@ -11,6 +11,25 @@ export default tseslint.config(
11
11
  '@typescript-eslint/no-explicit-any': 'error',
12
12
  },
13
13
  },
14
+ // Promises that nobody awaits. An unhandled rejection ends the process with
15
+ // exit code 1 (src/libs/shutdown.ts), so a forgotten `await` is a crash
16
+ // waiting for its first error; an async function passed where a plain
17
+ // callback is expected (a misused promise) loses its errors the same way.
18
+ // Both rules need type information: projectService reads tsconfig.json, which
19
+ // makes lint a little slower. Mark a deliberate fire-and-forget with `void`.
20
+ {
21
+ files: ['src/**/*.ts'],
22
+ languageOptions: {
23
+ parserOptions: {
24
+ projectService: true,
25
+ tsconfigRootDir: import.meta.dirname,
26
+ },
27
+ },
28
+ rules: {
29
+ '@typescript-eslint/no-floating-promises': 'error',
30
+ '@typescript-eslint/no-misused-promises': 'error',
31
+ },
32
+ },
14
33
  {
15
34
  ignores: ['dist/', 'node_modules/', 'prisma/'],
16
35
  }