create-tigra 3.3.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -12
- package/bin/create-tigra.js +495 -472
- package/lib/patchers/email-verification.patcher.js +72 -10
- package/modules/email-verification/client/hooks/useVerification.ts +4 -1
- package/modules/email-verification/server/__tests__/verification.integration.test.ts +115 -0
- package/modules/email-verification/server/verification.controller.ts +3 -1
- package/modules/email-verification/server/verification.service.ts +44 -13
- package/package.json +3 -2
- package/template/.github/dependabot.yml +47 -0
- package/template/.github/workflows/audit.yml +36 -0
- package/template/.github/workflows/ci.yml +79 -0
- package/template/AGENTS.md +18 -9
- package/template/client/.dockerignore +1 -0
- package/template/client/.env.example.production +13 -2
- package/template/client/AGENTS.md +13 -5
- package/template/client/Dockerfile +186 -14
- package/template/client/next.config.ts +1 -1
- package/template/client/package-lock.json +15983 -14181
- package/template/client/package.json +56 -48
- package/template/client/scripts/check-sharp.mjs +53 -0
- package/template/client/scripts/next-dev.cjs +5 -1
- package/template/client/src/__tests__/proxy.test.ts +92 -0
- package/template/client/src/app/healthz/route.ts +16 -0
- package/template/client/src/app/layout.tsx +9 -3
- package/template/client/src/app/page.tsx +2 -2
- package/template/client/src/app/providers.tsx +23 -4
- package/template/client/src/components/layout/Header.tsx +7 -3
- package/template/client/src/features/admin/hooks/useAdminSessions.ts +2 -1
- package/template/client/src/features/admin/hooks/useAdminUsers.ts +3 -1
- package/template/client/src/features/admin/types/admin.types.ts +2 -3
- package/template/client/src/features/auth/components/AuthInitializer.tsx +35 -18
- package/template/client/src/features/auth/components/ForgotPasswordForm.tsx +107 -0
- package/template/client/src/features/auth/components/LimitNotice.tsx +28 -0
- package/template/client/src/features/auth/components/LoginForm.tsx +13 -2
- package/template/client/src/features/auth/components/RegisterForm.tsx +23 -7
- package/template/client/src/features/auth/hooks/useAuth.ts +11 -1
- package/template/client/src/features/auth/hooks/usePasswordReset.ts +7 -1
- package/template/client/src/features/auth/services/auth.service.ts +8 -3
- package/template/client/src/features/auth/store/authSlice.ts +7 -2
- package/template/client/src/lib/api/__tests__/axios.config.test.ts +183 -0
- package/template/client/src/lib/api/api.types.ts +14 -0
- package/template/client/src/lib/api/axios.config.ts +101 -15
- package/template/client/src/lib/api/hard-redirect.ts +8 -0
- package/template/client/src/lib/api/session-events.ts +29 -0
- package/template/client/src/lib/constants/routes.ts +12 -0
- package/template/client/src/lib/observability/sentry-scrub.ts +1 -1
- package/template/client/src/lib/utils/__tests__/error.test.ts +119 -0
- package/template/client/src/lib/utils/__tests__/form.test.ts +42 -0
- package/template/client/src/lib/utils/__tests__/paths.test.ts +43 -0
- package/template/client/src/lib/utils/__tests__/security.test.ts +40 -0
- package/template/client/src/lib/utils/__tests__/session-marker.test.ts +27 -0
- package/template/client/src/lib/utils/error.ts +81 -0
- package/template/client/src/lib/utils/form.ts +31 -0
- package/template/client/src/lib/utils/paths.ts +18 -0
- package/template/client/src/lib/utils/session-marker.ts +24 -0
- package/template/client/src/{middleware.ts → proxy.ts} +25 -11
- package/template/client/src/store/__tests__/store.test.ts +44 -0
- package/template/client/src/store/index.ts +26 -8
- package/template/client/vitest.config.mts +48 -0
- package/template/server/.env.example +280 -270
- package/template/server/.env.example.production +283 -242
- package/template/server/AGENTS.md +13 -4
- package/template/server/Dockerfile +176 -14
- package/template/server/docker-compose.yml +4 -2
- package/template/server/docs/deployment.md +63 -0
- package/template/server/docs/logging.md +52 -4
- package/template/server/docs/rate-limits.md +153 -0
- package/template/server/docs/testing.md +63 -0
- package/template/server/eslint.config.mjs +19 -0
- package/template/server/package-lock.json +8784 -8743
- package/template/server/package.json +79 -75
- package/template/server/prisma/migrations/0_init/migration.sql +82 -0
- package/template/server/prisma/migrations/migration_lock.toml +3 -0
- package/template/server/prisma/schema.prisma +0 -3
- package/template/server/scripts/check-heic.mjs +40 -0
- package/template/server/scripts/check-sharp.mjs +53 -0
- package/template/server/src/__tests__/routes.integration.test.ts +280 -0
- package/template/server/src/app.ts +248 -346
- package/template/server/src/config/__tests__/env.test.ts +219 -0
- package/template/server/src/config/__tests__/rate-limit.config.test.ts +158 -0
- package/template/server/src/config/env.ts +326 -182
- package/template/server/src/config/rate-limit.config.ts +157 -106
- package/template/server/src/jobs/__tests__/cleanup-deleted-accounts.job.test.ts +60 -8
- package/template/server/src/jobs/__tests__/cleanup-expired-auth.job.test.ts +44 -0
- package/template/server/src/jobs/__tests__/jobs.integration.test.ts +88 -0
- package/template/server/src/jobs/__tests__/schedule.test.ts +92 -0
- package/template/server/src/jobs/cleanup-deleted-accounts.job.ts +38 -36
- package/template/server/src/jobs/cleanup-expired-auth.job.ts +32 -24
- package/template/server/src/jobs/index.ts +12 -2
- package/template/server/src/jobs/refresh-cloudflare-ranges.job.ts +28 -0
- package/template/server/src/jobs/schedule.ts +50 -0
- package/template/server/src/libs/__tests__/auth.test.ts +98 -7
- package/template/server/src/libs/__tests__/client-ip.test.ts +257 -121
- package/template/server/src/libs/__tests__/cloudflare-ranges.test.ts +79 -0
- package/template/server/src/libs/__tests__/cookies.test.ts +53 -2
- package/template/server/src/libs/__tests__/email-quota.test.ts +81 -0
- package/template/server/src/libs/__tests__/email.test.ts +68 -0
- package/template/server/src/libs/__tests__/error-handler.test.ts +162 -0
- package/template/server/src/libs/__tests__/health-db-down.integration.test.ts +37 -0
- package/template/server/src/libs/__tests__/health-redis-down.integration.test.ts +45 -0
- package/template/server/src/libs/__tests__/health-routes.test.ts +83 -0
- package/template/server/src/libs/__tests__/health.integration.test.ts +118 -0
- package/template/server/src/libs/__tests__/http.test.ts +74 -2
- package/template/server/src/libs/__tests__/ip-block.test.ts +179 -62
- package/template/server/src/libs/__tests__/logger.test.ts +50 -0
- package/template/server/src/libs/__tests__/monitoring.test.ts +185 -0
- package/template/server/src/libs/__tests__/outbound.integration.test.ts +48 -0
- package/template/server/src/libs/__tests__/prisma-client.test.ts +28 -0
- package/template/server/src/libs/__tests__/query-counter.integration.test.ts +53 -0
- package/template/server/src/libs/__tests__/rate-limiting.test.ts +146 -0
- package/template/server/src/libs/__tests__/rate-limits.integration.test.ts +53 -0
- package/template/server/src/libs/__tests__/redis-required.test.ts +30 -0
- package/template/server/src/libs/__tests__/redis.test.ts +38 -2
- package/template/server/src/libs/__tests__/request-id.test.ts +25 -0
- package/template/server/src/libs/__tests__/requestLogger.test.ts +126 -0
- package/template/server/src/libs/__tests__/safe-http.test.ts +138 -0
- package/template/server/src/libs/__tests__/sentry-scrub.test.ts +82 -80
- package/template/server/src/libs/__tests__/shutdown.test.ts +89 -0
- package/template/server/src/libs/__tests__/url-safety.test.ts +130 -80
- package/template/server/src/libs/auth.ts +39 -10
- package/template/server/src/libs/client-ip.ts +158 -77
- package/template/server/src/libs/cloudflare-ranges.ts +132 -0
- package/template/server/src/libs/cookies.ts +53 -14
- package/template/server/src/libs/email-quota.ts +74 -0
- package/template/server/src/libs/email.ts +46 -9
- package/template/server/src/libs/error-handler.ts +111 -0
- package/template/server/src/libs/health-routes.ts +53 -0
- package/template/server/src/libs/http.ts +139 -47
- package/template/server/src/libs/ip-address.ts +37 -0
- package/template/server/src/libs/ip-block.ts +284 -221
- package/template/server/src/libs/logger.ts +41 -12
- package/template/server/src/libs/monitoring.ts +127 -41
- package/template/server/src/libs/observability/sentry.ts +1 -1
- package/template/server/src/libs/origin-check.ts +9 -7
- package/template/server/src/libs/prisma.ts +47 -4
- package/template/server/src/libs/query-counter.ts +71 -59
- package/template/server/src/libs/rate-limiting.ts +110 -0
- package/template/server/src/libs/redis-required.ts +28 -0
- package/template/server/src/libs/redis.ts +27 -1
- package/template/server/src/libs/request-id.ts +18 -0
- package/template/server/src/libs/requestLogger.ts +112 -17
- package/template/server/src/libs/safe-http.ts +101 -0
- package/template/server/src/libs/shutdown.ts +87 -0
- package/template/server/src/libs/storage/__tests__/fixtures/tiny-heic.ts +13 -0
- package/template/server/src/libs/storage/__tests__/image-optimizer.test.ts +59 -0
- package/template/server/src/libs/storage/__tests__/multipart-limits.test.ts +91 -0
- package/template/server/src/libs/storage/__tests__/upload-persistence.test.ts +52 -0
- package/template/server/src/libs/storage/file-storage.service.ts +94 -84
- package/template/server/src/libs/storage/file-validator.ts +48 -46
- package/template/server/src/libs/storage/image-optimizer.service.ts +16 -5
- package/template/server/src/libs/token-hash.ts +18 -0
- package/template/server/src/libs/url-safety.ts +127 -121
- package/template/server/src/libs/validation-errors.ts +86 -0
- package/template/server/src/modules/admin/__tests__/admin-actions.integration.test.ts +255 -0
- package/template/server/src/modules/admin/__tests__/admin.health.test.ts +82 -0
- package/template/server/src/modules/admin/__tests__/admin.repo.test.ts +55 -0
- package/template/server/src/modules/admin/__tests__/admin.schemas.test.ts +38 -0
- package/template/server/src/modules/admin/__tests__/admin.service.test.ts +162 -0
- package/template/server/src/modules/admin/admin.controller.ts +66 -49
- package/template/server/src/modules/admin/admin.repo.ts +49 -10
- package/template/server/src/modules/admin/admin.routes.ts +48 -2
- package/template/server/src/modules/admin/admin.schemas.ts +18 -6
- package/template/server/src/modules/admin/admin.service.ts +66 -4
- package/template/server/src/modules/auth/__tests__/auth.controller.test.ts +39 -0
- package/template/server/src/modules/auth/__tests__/auth.integration.test.ts +9 -3
- package/template/server/src/modules/auth/__tests__/auth.service.dummy-hash.test.ts +34 -0
- package/template/server/src/modules/auth/__tests__/auth.service.test.ts +197 -37
- package/template/server/src/modules/auth/__tests__/session-cleanup.test.ts +39 -0
- package/template/server/src/modules/auth/__tests__/sessions.integration.test.ts +246 -0
- package/template/server/src/modules/auth/__tests__/token-fingerprints.test.ts +119 -0
- package/template/server/src/modules/auth/auth.controller.ts +133 -128
- package/template/server/src/modules/auth/auth.repo.ts +44 -44
- package/template/server/src/modules/auth/auth.schemas.ts +4 -2
- package/template/server/src/modules/auth/auth.service.ts +167 -96
- package/template/server/src/modules/auth/session.repo.ts +12 -0
- package/template/server/src/modules/files/__tests__/files.integration.test.ts +5 -5
- package/template/server/src/modules/users/__tests__/avatar-upload.test.ts +120 -0
- package/template/server/src/modules/users/__tests__/name-rules.test.ts +46 -0
- package/template/server/src/modules/users/__tests__/users.integration.test.ts +295 -0
- package/template/server/src/modules/users/__tests__/users.repo.test.ts +37 -0
- package/template/server/src/modules/users/__tests__/users.service.test.ts +129 -0
- package/template/server/src/modules/users/users.controller.ts +9 -38
- package/template/server/src/modules/users/users.repo.ts +23 -7
- package/template/server/src/modules/users/users.routes.ts +3 -22
- package/template/server/src/modules/users/users.schemas.ts +15 -9
- package/template/server/src/modules/users/users.service.ts +105 -93
- package/template/server/src/scripts/make-admin.ts +48 -0
- package/template/server/src/server.ts +48 -26
- package/template/server/src/shared/errors/AppError.ts +27 -1
- package/template/server/src/shared/errors/errors.ts +42 -5
- package/template/server/src/shared/responses/successResponse.ts +22 -3
- package/template/server/src/shared/types/index.ts +7 -0
- package/template/server/src/test/__tests__/integration-services.test.ts +66 -0
- package/template/server/src/test/integration-helpers.ts +343 -0
- package/template/server/src/test/integration-services.ts +74 -0
- package/template/server/src/test/integration.global-setup.ts +144 -0
- package/template/server/src/test/integration.setup.ts +35 -158
- package/template/server/src/test/setup.ts +3 -10
- package/template/server/tsconfig.build.json +1 -1
- package/template/server/tsconfig.json +1 -0
- package/template/server/vitest.config.ts +17 -4
- package/template/server/vitest.integration.config.ts +18 -13
- package/template/server/src/libs/storage/filename-sanitizer.ts +0 -71
package/template/AGENTS.md
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Agent instructions
|
|
2
|
+
|
|
3
|
+
**Read the scoped file first.** Before changing anything in `server/`, read `server/AGENTS.md`; before changing anything in `client/`, read `client/AGENTS.md`. Claude Code loads them by itself when it opens files there. Codex does not: it loads only the AGENTS.md files from the project root down to the folder it was started in, so started here it never sees them.
|
|
2
4
|
|
|
3
5
|
## MUST FOLLOW — comment important code
|
|
4
6
|
|
|
@@ -33,30 +35,37 @@ Every implementation change MUST include a human-readability pass. Add or update
|
|
|
33
35
|
|
|
34
36
|
- Use functional kebab-case for project names and anything surfaced in CLIs, logs, queues, jobs, or worker/process names.
|
|
35
37
|
- Use camelCase for TypeScript values/functions, PascalCase for components/types/classes, and UPPER_SNAKE_CASE for true constants.
|
|
36
|
-
- Keep domain folders and route paths lowercase and functional;
|
|
38
|
+
- Keep domain folders and route paths lowercase and functional; the file naming rules of each app are in `server/AGENTS.md` and `client/AGENTS.md`.
|
|
37
39
|
|
|
38
40
|
## Deployment boundaries
|
|
39
41
|
|
|
40
42
|
- The Dockerfile in each app is its production contract. Re-check it when changing ports, entry points, build output, native dependencies, runtime files, health routes, or environment variables.
|
|
41
|
-
- The client must retain Next.js `output: "standalone"`; every `NEXT_PUBLIC_*` value is public and must be available at Docker build time.
|
|
42
|
-
- The server container applies `prisma migrate deploy` before startup and exposes unauthenticated `GET /api/v1/live` for orchestration.
|
|
43
|
+
- The client must retain Next.js `output: "standalone"`; every `NEXT_PUBLIC_*` value is public and must be available at Docker build time. The client build stops when `NEXT_PUBLIC_API_BASE_URL` is empty. Its health check is `GET /healthz` (`client/src/app/healthz/route.ts`), which must keep answering 200 without rendering a page.
|
|
44
|
+
- The server container applies `prisma migrate deploy` before startup and exposes unauthenticated `GET /api/v1/live` for orchestration. It refuses to start when the image has no migrations (the database would have no tables), so keep `server/prisma/migrations/` committed, starting with the shipped `0_init`, and never switch to `prisma db push` for production. In production it also refuses to start when Redis is not reachable within 10 s: rate limits, login lockout and IP blocks run on Redis.
|
|
45
|
+
- Every deploy applies the new migrations while the old version is still serving, so a migration must work with the code that is still running. Follow expand / deploy / contract (`server/docs/deployment.md`): never drop, rename, retype or make a column required in the same deploy as the code that stops using it.
|
|
46
|
+
- Both Dockerfiles build sharp against a pinned libvips so images run on CPUs without x86-64-v2. When upgrading sharp (or Next, which brings its own sharp), check the new sharp's `config.libvips` range; if `LIBVIPS_VERSION` no longer satisfies it, bump `LIBVIPS_VERSION` and `LIBVIPS_SHA256` in both Dockerfiles. The build fails with a clear message if they do not match.
|
|
47
|
+
- Write `package-lock.json` with npm 11.19 or newer (`npm -v`). The images run `npm ci` with npm 11.19.1, which rejects lockfiles from npm 10 and from early npm 11 (11.6.1 was seen failing).
|
|
43
48
|
- `server/docker-compose.yml` is local-development infrastructure, not a production topology. Keep host ports loopback-bound and admin UIs opt-in through the `tools` profile.
|
|
44
49
|
|
|
45
50
|
## Non-obvious security rules
|
|
46
51
|
|
|
47
52
|
- Never commit `.env` files, credentials, tokens, or production data. Never place a secret in `NEXT_PUBLIC_*` or client-side code.
|
|
48
53
|
- Authentication uses server-issued httpOnly cookies. Do not copy tokens into localStorage, sessionStorage, Redux, logs, URLs, or response bodies.
|
|
49
|
-
- Validate every external input at the server boundary with Zod, use Prisma parameterization, enforce authorization in server services/routes, and return user-safe errors.
|
|
54
|
+
- Validate every external input at the server boundary with Zod, use Prisma parameterization, enforce authorization in server services/routes, and return user-safe errors. Wrong input always answers 422 `VALIDATION_FAILED` with `error.fields` (field + message, never the submitted values); the client reads them with `getFieldErrors`.
|
|
50
55
|
- Preserve origin/CORS checks for state-changing cookie-authenticated requests, rate limits on public/auth-sensitive endpoints, and the split between statically served public uploads and authenticated owner-scoped private files.
|
|
51
56
|
|
|
52
|
-
## Required
|
|
57
|
+
## Required security workflow
|
|
53
58
|
|
|
54
59
|
- For every meaningful server implementation, bug fix, refactor, dependency update, or configuration change, use the repository-local `security-audit` skill in guidance mode while designing and reviewing the change. Treat authentication, authorization, validation, database access, outbound requests, uploads, cookies, CORS/origin handling, rate limits, environment configuration, and deployment behavior as security-sensitive surfaces.
|
|
55
|
-
-
|
|
56
|
-
-
|
|
60
|
+
- The same workflow applies to client changes in its security code: `client/src/proxy.ts` (security headers, CSP, route protection), the sign-in flow and its redirects (`client/src/features/auth/`), the shared API client (`client/src/lib/api/axios.config.ts`), and anything that renders HTML or untrusted URLs.
|
|
61
|
+
- Before completion, review the patch you are handing over: `git diff HEAD` plus every untracked file (`git status --porcelain`). In Codex, use the Codex Security plugin's `codex-security:security-diff-scan` skill when it is installed. In Claude Code, run the bundled `security-audit` skill in guidance mode on that diff. Claude Code's `/security-review` is not this check: it reviews only `git diff origin/HEAD...` (commits since the remote branch), so it never sees uncommitted work, and a project that was never pushed has no `origin/HEAD` at all. Scope the review to the changed behavior and the supporting code needed to establish impact; do not expand it into an unrelated repository audit.
|
|
62
|
+
- If Codex's scan is unavailable, perform the same focused patch review with the bundled `security-audit` skill and explicitly report that the scan was unavailable. Do not skip the security gate silently.
|
|
57
63
|
- A full or deep repository audit remains explicit work: do not start one merely because ordinary server code changed. Fix confirmed in-scope findings, rerun affected checks, and disclose any unresolved or unverified security concern before claiming completion.
|
|
58
64
|
- Documentation-only, comment-only, and formatting-only changes may skip the patch scan when they cannot affect runtime behavior, permissions, deployment, or security policy.
|
|
59
65
|
|
|
60
66
|
## Verification
|
|
61
67
|
|
|
62
|
-
|
|
68
|
+
- Server (`server/`): `npm run lint`, `npm run typecheck`, `npm test` (unit), `npm run test:coverage` (fails below the floor in `vitest.config.ts`; do not lower it to make a change pass), `npm run test:integration`, `npm run build`. Client (`client/`): `npm run lint`, `npm run typecheck`, `npm test` (unit), `npm run test:coverage` (same floor rule, `vitest.config.mts`), `npm run build`. Run the ones that cover what you changed.
|
|
69
|
+
- `npm run test:integration` needs a real MySQL and Redis: either `INTEGRATION_DATABASE_URL` (a database whose name ends in `_test`) and `INTEGRATION_REDIS_URL` (Redis database 1-15), or Docker for Testcontainers. If neither is available, say that the integration tests did not run instead of reporting the change as tested.
|
|
70
|
+
- Never run `npm run prisma:reset` (`prisma migrate reset`) or `prisma db push` against a database that was not created for the test: they delete or rewrite its data. The integration suite empties its own test database.
|
|
71
|
+
- Database and deployment changes also require migration/Docker compatibility checks.
|
|
@@ -1,5 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ===================================================================
|
|
2
|
+
# CLIENT APP (in Coolify: the client application only, not the server)
|
|
3
|
+
# ===================================================================
|
|
4
|
+
# Every NEXT_PUBLIC_* value is PUBLIC: Next.js bakes it into the JavaScript
|
|
5
|
+
# that every visitor downloads, at BUILD time. So, unlike the server's
|
|
6
|
+
# variables (which must never be available at build time), each of these
|
|
7
|
+
# needs "Available at Buildtime" ticked in Coolify (keep it available at
|
|
8
|
+
# runtime too). Never put a secret here. Changing one needs a redeploy, which
|
|
9
|
+
# rebuilds the image; a restart is not enough.
|
|
10
|
+
|
|
11
|
+
# The server's public API address, ending in /api/v1. Required: the image
|
|
12
|
+
# build stops when it is empty (client/Dockerfile).
|
|
2
13
|
NEXT_PUBLIC_API_BASE_URL=https://api.yourdomain.com/api/v1
|
|
3
14
|
|
|
4
|
-
# App name
|
|
15
|
+
# App name shown in the UI. Optional (defaults to "My App").
|
|
5
16
|
NEXT_PUBLIC_APP_NAME={{PROJECT_DISPLAY_NAME}}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Client
|
|
1
|
+
# Client agent instructions
|
|
2
2
|
|
|
3
3
|
## Architecture and naming
|
|
4
4
|
|
|
@@ -10,14 +10,22 @@
|
|
|
10
10
|
## Sources of truth
|
|
11
11
|
|
|
12
12
|
- Server-rendered page data belongs in Server Components; client-fetched server data belongs in React Query; Redux is limited to the authenticated-user lifecycle; local and URL state stay local or in search params.
|
|
13
|
-
- `src/
|
|
13
|
+
- Redux has one store per visitor (`makeStore` in `src/store/index.ts`, created in `Providers`). Never import a store instance into a module: the API client reports a session that ended through `src/lib/api/session-events.ts`, and `AuthInitializer` clears the user.
|
|
14
|
+
- `src/lib/constants/routes.ts` and `api-endpoints.ts` own application and API paths. `PROTECTED_PATHS` and `AUTH_PATHS` there are the one list each that the proxy and `AuthInitializer` use, matched by whole path segments (`src/lib/utils/paths.ts`): add your protected areas there.
|
|
15
|
+
- The start-up "who am I" check runs only when the browser holds the server's `auth_session` marker (`src/lib/utils/session-marker.ts`), so anonymous visitors make no API calls.
|
|
14
16
|
- `src/styles/themes/default.css` owns color values. Components use semantic Tailwind tokens; do not hardcode palette values or create competing theme variables.
|
|
15
17
|
- If a `next/font` CSS variable is referenced by a token declared on `:root`, apply that font's generated variable class to `<html>`. Never attach it only to `<body>`, where it cannot resolve the root-scoped token.
|
|
16
|
-
- `src/
|
|
18
|
+
- `src/proxy.ts` (Next 16's name for middleware) is the single source of truth for security headers and nonce-based CSP. Do not add a second CSP in `next.config.ts`.
|
|
17
19
|
|
|
18
20
|
## Security and deployment
|
|
19
21
|
|
|
20
|
-
- Treat client validation as UX only; the server must validate and authorize every request.
|
|
22
|
+
- Treat client validation as UX only; the server must validate and authorize every request. Forms put the server's 422 messages on their own fields with `applyFieldErrors` (`src/lib/utils/form.ts`); keep `RegisterForm`'s rules equal to the server's `registerSchema`.
|
|
21
23
|
- Use the shared credentialed API client. Auth tokens remain in httpOnly cookies and must never enter browser storage or Redux.
|
|
22
24
|
- Sanitize any intentionally rendered HTML and untrusted URLs. Never log passwords, tokens, credentials, or complete sensitive user objects.
|
|
23
|
-
- Preserve `output: "standalone"`, the non-root container, and the
|
|
25
|
+
- Preserve `output: "standalone"`, the non-root container, and the `/healthz` health check (`src/app/healthz/route.ts`, used by the Dockerfile HEALTHCHECK; keep it answering 200 without rendering a page). Add every new public environment variable to the Docker builder as `ARG` + `ENV`; public variables are compiled into the bundle. The build stops when `NEXT_PUBLIC_API_BASE_URL` is empty (in Coolify it needs "Available at Buildtime").
|
|
26
|
+
- Changes to `src/proxy.ts`, the sign-in flow and its redirects (`src/features/auth/`), the shared API client (`src/lib/api/axios.config.ts`), or anything that renders HTML or untrusted URLs go through the root `AGENTS.md` security workflow.
|
|
27
|
+
|
|
28
|
+
## Tests
|
|
29
|
+
|
|
30
|
+
- `npm test` (and `npm run test:coverage`, which CI runs and which fails below the floor in `vitest.config.mts`) runs the unit tests in `src/**/__tests__/`. Keep logic in functions under `src/lib/` and test it there; a test file that needs `document` or `window` starts with `// @vitest-environment jsdom`.
|
|
31
|
+
- The template ships no pages on purpose. Screens are proven by hand in a browser against a running server; the logic under them is what the unit tests cover.
|
|
@@ -8,11 +8,86 @@
|
|
|
8
8
|
# across builds, so a lockfile/dep-change rebuild re-installs from cache
|
|
9
9
|
# (measured ~2.8x: 49s->18s on a real create-tigra client) instead of
|
|
10
10
|
# re-downloading every tarball. Requires the `# syntax` line above (BuildKit).
|
|
11
|
+
#
|
|
12
|
+
# Every stage uses the same pinned node:24-alpine image (Node 24.22.0,
|
|
13
|
+
# npm 11.19.1, Alpine 3.24). Node 24 is the template's runtime (package.json
|
|
14
|
+
# engines >=24). `npm ci` below fails when package-lock.json was written by an
|
|
15
|
+
# older npm: npm 10 always, and early npm 11 too (11.6.1 leaves out the
|
|
16
|
+
# @emnapi/* entries this npm requires). Write lockfiles with npm 11.19 or newer.
|
|
17
|
+
#
|
|
18
|
+
# next/image optimizes images at runtime with sharp (GET /_next/image), so the
|
|
19
|
+
# client needs the same CPU-safe sharp as the server: stages 1-2 below.
|
|
11
20
|
|
|
12
21
|
# ===================================================================
|
|
13
|
-
# Stage 1:
|
|
22
|
+
# Stage 1: libvips that runs on any x86-64 CPU
|
|
14
23
|
# ===================================================================
|
|
15
|
-
|
|
24
|
+
# Why this stage exists: sharp's prebuilt addon and the libvips it bundles
|
|
25
|
+
# need an x86-64-v2 CPU (SSE4.2/POPCNT). Many VPS hosts present an older
|
|
26
|
+
# virtual CPU (e.g. "QEMU Virtual CPU"), where the prebuilt sharp refuses to
|
|
27
|
+
# load and the server crashes at start. So we compile a pinned libvips release
|
|
28
|
+
# for the generic x86-64 baseline and build sharp's addon against it (stage 2).
|
|
29
|
+
#
|
|
30
|
+
# KEEP THIS STAGE BYTE-IDENTICAL in server/Dockerfile and client/Dockerfile,
|
|
31
|
+
# so one rule set builds both images. Identical text also lets a plain
|
|
32
|
+
# `docker build` host reuse the compiled layer across the two apps. On
|
|
33
|
+
# Coolify (measured): redeploys of the same app on the same server reused it,
|
|
34
|
+
# but the two apps did not share it, and a change to a build-time variable
|
|
35
|
+
# compiled it again (Coolify passes those into the build). An uncached
|
|
36
|
+
# compile takes about 6-7 min on a small shared VPS.
|
|
37
|
+
#
|
|
38
|
+
# Version coupling: every sharp release needs a minimum libvips (the
|
|
39
|
+
# "config.libvips" range in sharp's package.json, e.g. ">=8.18.7" for sharp
|
|
40
|
+
# 0.35.5). When a sharp upgrade needs more than LIBVIPS_VERSION, bump
|
|
41
|
+
# LIBVIPS_VERSION and LIBVIPS_SHA256 (from the libvips release page) in BOTH
|
|
42
|
+
# Dockerfiles. Stage 2 stops the build with a clear message if you forget.
|
|
43
|
+
FROM node:24-alpine@sha256:44fce696f38ba8c987c4db89954051000bbd05ffd8d8659ba15ed4a8602e937f AS libvips
|
|
44
|
+
|
|
45
|
+
ARG LIBVIPS_VERSION=8.18.7
|
|
46
|
+
ARG LIBVIPS_SHA256=5baaead3b0bb20ffdb9e9ff09aa9fda08620923df77b63b436654cb5e0b3bf94
|
|
47
|
+
|
|
48
|
+
# Compilers and codec headers. python3 serves node-gyp in the stage that
|
|
49
|
+
# builds FROM this one. Every -dev package here needs its runtime library in
|
|
50
|
+
# the production stage's apk list (keep the two lists in sync).
|
|
51
|
+
RUN apk add --no-cache \
|
|
52
|
+
build-base python3 curl xz meson ninja pkgconf \
|
|
53
|
+
glib-dev expat-dev libjpeg-turbo-dev libpng-dev libwebp-dev \
|
|
54
|
+
tiff-dev lcms2-dev libheif-dev libexif-dev
|
|
55
|
+
|
|
56
|
+
ENV PKG_CONFIG_PATH=/opt/vips/lib/pkgconfig \
|
|
57
|
+
LD_LIBRARY_PATH=/opt/vips/lib
|
|
58
|
+
|
|
59
|
+
# --auto-features=disabled turns every optional dependency off; only the
|
|
60
|
+
# formats we use are enabled: JPEG/PNG/WebP/HEIF/TIFF (plus the built-in GIF
|
|
61
|
+
# loader, which Next's image optimizer accepts), colour profiles, and EXIF
|
|
62
|
+
# (needed to read orientation and strip metadata). The rarely used PPM,
|
|
63
|
+
# Analyze and Radiance loaders are off to shrink the parser attack surface.
|
|
64
|
+
# -march=x86-64 is the baseline every x86-64 CPU supports (other
|
|
65
|
+
# architectures keep the compiler default). -j 2 bounds memory on small
|
|
66
|
+
# build hosts.
|
|
67
|
+
RUN set -eu; \
|
|
68
|
+
if [ "$(uname -m)" = "x86_64" ]; then \
|
|
69
|
+
export CFLAGS="-march=x86-64 -mtune=generic" CXXFLAGS="-march=x86-64 -mtune=generic"; \
|
|
70
|
+
fi; \
|
|
71
|
+
curl -fsSL --retry 3 -o /tmp/vips.tar.xz \
|
|
72
|
+
"https://github.com/libvips/libvips/releases/download/v${LIBVIPS_VERSION}/vips-${LIBVIPS_VERSION}.tar.xz"; \
|
|
73
|
+
echo "${LIBVIPS_SHA256} /tmp/vips.tar.xz" | sha256sum -c -; \
|
|
74
|
+
tar -xJf /tmp/vips.tar.xz -C /tmp; \
|
|
75
|
+
meson setup /tmp/vips-build "/tmp/vips-${LIBVIPS_VERSION}" \
|
|
76
|
+
--prefix=/opt/vips --libdir=lib --buildtype=release --auto-features=disabled \
|
|
77
|
+
-Djpeg=enabled -Dpng=enabled -Dwebp=enabled -Dheif=enabled -Dtiff=enabled \
|
|
78
|
+
-Dlcms=enabled -Dzlib=enabled -Dexif=enabled \
|
|
79
|
+
-Dexamples=false -Dppm=false -Danalyze=false -Dradiance=false; \
|
|
80
|
+
meson compile -C /tmp/vips-build -j 2; \
|
|
81
|
+
meson install -C /tmp/vips-build; \
|
|
82
|
+
test "$(pkg-config --modversion vips-cpp)" = "${LIBVIPS_VERSION}"; \
|
|
83
|
+
rm -rf /tmp/vips-build "/tmp/vips-${LIBVIPS_VERSION}" /tmp/vips.tar.xz
|
|
84
|
+
|
|
85
|
+
# ===================================================================
|
|
86
|
+
# Stage 2: Dependencies + CPU-safe sharp addon
|
|
87
|
+
# ===================================================================
|
|
88
|
+
# Built FROM the libvips stage because compiling sharp's addon needs the
|
|
89
|
+
# compilers and every codec -dev package that vips-cpp.pc references.
|
|
90
|
+
FROM libvips AS dependencies
|
|
16
91
|
|
|
17
92
|
WORKDIR /app
|
|
18
93
|
|
|
@@ -29,17 +104,66 @@ RUN --mount=type=cache,target=/root/.npm \
|
|
|
29
104
|
npm ci; \
|
|
30
105
|
fi
|
|
31
106
|
|
|
107
|
+
# Build sharp's native addon against /opt/vips. sharp loads
|
|
108
|
+
# node_modules/sharp/src/build/Release/sharp-<platform>-<version>.node BEFORE
|
|
109
|
+
# its prebuilt addon and without the prebuilt's x86-64-v2 check, so this file
|
|
110
|
+
# is what makes sharp work on older CPUs. Keep this block identical in
|
|
111
|
+
# server/Dockerfile and client/Dockerfile.
|
|
112
|
+
# - It runs after every npm install above, so it targets the sharp version
|
|
113
|
+
# actually installed (read from its package.json, never hard-coded).
|
|
114
|
+
# - SHARP_FORCE_GLOBAL_LIBVIPS makes sharp skip its own libvips version
|
|
115
|
+
# check, so the block checks sharp's "config.libvips" range itself.
|
|
116
|
+
# - node-addon-api and node-gyp are build tools sharp requires but does not
|
|
117
|
+
# install. They go to /tmp/sharp-build (exact pins inside sharp's declared
|
|
118
|
+
# ranges; --ignore-scripts, since neither needs an install script and their
|
|
119
|
+
# unlocked transitive packages must not run code here) and never reach the
|
|
120
|
+
# lockfile or the runtime image.
|
|
121
|
+
# - npm_config_nodedir points node-gyp at this image's own Node headers, so
|
|
122
|
+
# nothing is downloaded and the addon matches the runtime Node exactly.
|
|
123
|
+
# - Intermediate build files are dropped; only the .node file stays, also
|
|
124
|
+
# staged in /opt/sharp-addon for stages that copy it explicitly.
|
|
125
|
+
RUN --mount=type=cache,target=/root/.npm \
|
|
126
|
+
set -eu; \
|
|
127
|
+
npm install --prefix /tmp/sharp-build --no-save --no-audit --no-fund --ignore-scripts \
|
|
128
|
+
node-addon-api@8.9.2 node-gyp@12.4.0; \
|
|
129
|
+
if [ ! -d node_modules/sharp ]; then \
|
|
130
|
+
echo "ERROR: node_modules/sharp is missing. If this app no longer uses sharp, delete this build step and the libvips stage." >&2; \
|
|
131
|
+
exit 1; \
|
|
132
|
+
fi; \
|
|
133
|
+
cd node_modules/sharp; \
|
|
134
|
+
version="$(node -p "require('./package.json').version")"; \
|
|
135
|
+
need="$(node -p "require('./package.json').config.libvips")"; \
|
|
136
|
+
have="$(pkg-config --modversion vips-cpp)"; \
|
|
137
|
+
if ! node -e "process.exit(require('semver').satisfies(process.argv[1], process.argv[2]) ? 0 : 1)" "$have" "$need"; then \
|
|
138
|
+
echo "ERROR: sharp ${version} needs libvips ${need}, but this image builds libvips ${have}. Raise LIBVIPS_VERSION and LIBVIPS_SHA256 in BOTH server/Dockerfile and client/Dockerfile." >&2; \
|
|
139
|
+
exit 1; \
|
|
140
|
+
fi; \
|
|
141
|
+
if [ "$(uname -m)" = "x86_64" ]; then \
|
|
142
|
+
export CFLAGS="-march=x86-64 -mtune=generic" CXXFLAGS="-march=x86-64 -mtune=generic"; \
|
|
143
|
+
fi; \
|
|
144
|
+
SHARP_FORCE_GLOBAL_LIBVIPS=1 npm_config_nodedir=/usr/local \
|
|
145
|
+
NODE_PATH=/tmp/sharp-build/node_modules PATH="/tmp/sharp-build/node_modules/.bin:$PATH" \
|
|
146
|
+
node install/build.js; \
|
|
147
|
+
addon="sharp-linuxmusl-$(node -p "process.arch")-${version}.node"; \
|
|
148
|
+
test -f "src/build/Release/${addon}"; \
|
|
149
|
+
mkdir -p /opt/sharp-addon; \
|
|
150
|
+
cp "src/build/Release/${addon}" /opt/sharp-addon/; \
|
|
151
|
+
rm -rf src/build /tmp/sharp-build; \
|
|
152
|
+
mkdir -p src/build/Release; \
|
|
153
|
+
cp "/opt/sharp-addon/${addon}" src/build/Release/
|
|
154
|
+
|
|
32
155
|
# ===================================================================
|
|
33
|
-
# Stage
|
|
156
|
+
# Stage 3: Build (compile Next.js)
|
|
34
157
|
# ===================================================================
|
|
35
|
-
FROM
|
|
158
|
+
# Built FROM the dependencies stage: node_modules (with the CPU-safe addon)
|
|
159
|
+
# is already in place, and libvips is loadable should the build touch sharp.
|
|
160
|
+
# None of these build tools reach the runtime image.
|
|
161
|
+
FROM dependencies AS builder
|
|
36
162
|
|
|
37
163
|
WORKDIR /app
|
|
38
164
|
|
|
39
|
-
# Copy
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
# Copy source code
|
|
165
|
+
# Copy source code (.dockerignore keeps node_modules out, so the installed
|
|
166
|
+
# modules above are not overwritten)
|
|
43
167
|
COPY . .
|
|
44
168
|
|
|
45
169
|
# Build arguments for env vars needed at build time
|
|
@@ -54,6 +178,19 @@ ENV NEXT_PUBLIC_APP_NAME=$NEXT_PUBLIC_APP_NAME
|
|
|
54
178
|
# Disable Next.js telemetry during build
|
|
55
179
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
56
180
|
|
|
181
|
+
# The API address must be known now. src/lib/env.ts falls back to
|
|
182
|
+
# http://localhost:8000/api/v1 when it is unset (handy for local builds and
|
|
183
|
+
# CI), so without this check an image built without the value builds green
|
|
184
|
+
# and every visitor's browser then calls its own machine: a client that looks
|
|
185
|
+
# deployed but cannot reach its API. A localhost value only warns, because a
|
|
186
|
+
# local docker test may mean it. (Whitespace-only counts as empty.)
|
|
187
|
+
RUN set -eu; \
|
|
188
|
+
url="$(printf '%s' "${NEXT_PUBLIC_API_BASE_URL:-}" | tr -d '[:space:]')"; \
|
|
189
|
+
case "$url" in \
|
|
190
|
+
"") echo "ERROR: NEXT_PUBLIC_API_BASE_URL is empty at build time. NEXT_PUBLIC_* values are baked into the client's JavaScript: in Coolify, set it on the client app and tick 'Available at Buildtime'." >&2; exit 1 ;; \
|
|
191
|
+
*://localhost*|*://127.0.0.1*) echo "WARNING: NEXT_PUBLIC_API_BASE_URL is $url: visitors' browsers would call their own machine. Fine for a local test, wrong for a deployed client." >&2 ;; \
|
|
192
|
+
esac
|
|
193
|
+
|
|
57
194
|
# Build the application (outputs to .next/standalone)
|
|
58
195
|
# NOTE: a `--mount=type=cache,target=/app/.next/cache` was tested here and REMOVED —
|
|
59
196
|
# it measured SLOWER (next build 29s→58s on a real create-tigra client), because Next's
|
|
@@ -62,12 +199,29 @@ ENV NEXT_TELEMETRY_DISABLED=1
|
|
|
62
199
|
RUN npm run build
|
|
63
200
|
|
|
64
201
|
# ===================================================================
|
|
65
|
-
# Stage
|
|
202
|
+
# Stage 4: Production Runtime
|
|
66
203
|
# ===================================================================
|
|
67
|
-
FROM node:
|
|
204
|
+
FROM node:24-alpine@sha256:44fce696f38ba8c987c4db89954051000bbd05ffd8d8659ba15ed4a8602e937f AS production
|
|
205
|
+
|
|
206
|
+
# dumb-init: proper signal handling.
|
|
207
|
+
# curl: Coolify's own HTTP health check runs curl (then BusyBox wget) inside
|
|
208
|
+
# the container. Without curl it falls back to wget, which tries only the
|
|
209
|
+
# first address `localhost` resolves to (often IPv6 ::1, while Next listens
|
|
210
|
+
# on IPv4), so Coolify marks a healthy app unhealthy and rolls the deploy
|
|
211
|
+
# back. curl tries both addresses.
|
|
212
|
+
# The rest are the shared libraries our libvips links against (one per -dev
|
|
213
|
+
# package in stage 1; same list as the server image). Alpine ships WebP's
|
|
214
|
+
# mux/demux libraries separately from libwebp and sharp fails to load without
|
|
215
|
+
# them; the libheif plugins decode HEIC (libde265) and AVIF (aom).
|
|
216
|
+
RUN apk add --no-cache \
|
|
217
|
+
dumb-init curl \
|
|
218
|
+
glib expat libjpeg-turbo libpng libwebp libwebpmux libwebpdemux \
|
|
219
|
+
tiff lcms2 libheif libheif-libde265 libheif-aom libexif
|
|
68
220
|
|
|
69
|
-
#
|
|
70
|
-
|
|
221
|
+
# The libvips built in stage 1. sharp's addon links to it without an rpath,
|
|
222
|
+
# so the dynamic loader is told where it lives.
|
|
223
|
+
COPY --from=libvips /opt/vips/lib /opt/vips/lib
|
|
224
|
+
ENV LD_LIBRARY_PATH=/opt/vips/lib
|
|
71
225
|
|
|
72
226
|
# Create non-root user for security
|
|
73
227
|
RUN addgroup -g 1001 -S nodejs && \
|
|
@@ -84,9 +238,22 @@ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
|
|
84
238
|
# Copy public folder (favicon, images, etc.)
|
|
85
239
|
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
|
|
86
240
|
|
|
241
|
+
# Ship the CPU-safe sharp addon explicitly. Next's standalone file tracing
|
|
242
|
+
# does not reliably carry a source-built addon (or any libvips .so), so it is
|
|
243
|
+
# copied to the exact path sharp's loader tries first.
|
|
244
|
+
COPY --from=dependencies --chown=nextjs:nodejs /opt/sharp-addon/ ./node_modules/sharp/src/build/Release/
|
|
245
|
+
|
|
246
|
+
# Build-time sharp proof (see the script's header).
|
|
247
|
+
COPY --chown=nextjs:nodejs scripts/check-sharp.mjs ./scripts/check-sharp.mjs
|
|
248
|
+
|
|
87
249
|
# Switch to non-root user
|
|
88
250
|
USER nextjs
|
|
89
251
|
|
|
252
|
+
# Prove sharp loads our addon and converts images in THIS image, as the
|
|
253
|
+
# runtime user, before the image can ship. A failure fails the build instead
|
|
254
|
+
# of breaking every /_next/image request in production.
|
|
255
|
+
RUN node scripts/check-sharp.mjs
|
|
256
|
+
|
|
90
257
|
# Disable telemetry at runtime
|
|
91
258
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
92
259
|
|
|
@@ -97,9 +264,14 @@ ENV HOSTNAME="0.0.0.0"
|
|
|
97
264
|
ENV PORT=3000
|
|
98
265
|
EXPOSE 3000
|
|
99
266
|
|
|
100
|
-
# Health check for container orchestration
|
|
267
|
+
# Health check for container orchestration. Coolify uses this one (not its
|
|
268
|
+
# dashboard check) to decide when a rolling deploy may switch over.
|
|
269
|
+
# /healthz (src/app/healthz/route.ts) answers 200 without rendering a page, so
|
|
270
|
+
# it stays healthy when a project makes its home page redirect; `/` did not.
|
|
271
|
+
# 127.0.0.1, never localhost: Next listens on IPv4 (HOSTNAME=0.0.0.0), and
|
|
272
|
+
# localhost can resolve to IPv6 ::1 first, which would refuse the probe.
|
|
101
273
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
|
|
102
|
-
CMD node -e "const p=process.env.PORT||3000;require('http').get('http://
|
|
274
|
+
CMD node -e "const p=process.env.PORT||3000;require('http').get('http://127.0.0.1:'+p+'/healthz',(r)=>{process.exit(r.statusCode===200?0:1)})"
|
|
103
275
|
|
|
104
276
|
# Use dumb-init to handle signals properly
|
|
105
277
|
ENTRYPOINT ["dumb-init", "--"]
|
|
@@ -2,7 +2,7 @@ import type { NextConfig } from "next";
|
|
|
2
2
|
|
|
3
3
|
// SECURITY HEADERS / CSP NOTE:
|
|
4
4
|
// All security headers — including the Content-Security-Policy — are set in
|
|
5
|
-
// src/
|
|
5
|
+
// src/proxy.ts, which is the SINGLE SOURCE OF TRUTH for them. The CSP is
|
|
6
6
|
// per-request and nonce-based ('strict-dynamic'), which a static headers() block
|
|
7
7
|
// here cannot express, and emitting a CSP from both places would produce a
|
|
8
8
|
// conflicting duplicate Content-Security-Policy header. Do NOT add a headers()
|