From 5de9272281e66291a28d2392ea9604e5339613c6 Mon Sep 17 00:00:00 2001 From: Andrii Arsenin Date: Wed, 30 Sep 2026 21:31:48 +0300 Subject: [PATCH] feat(server): choose cors origins by NODE_ENV and run docker in production - allow the vite dev server origins in development and the compose port origins in production; CORS_ORIGINS overrides both - set NODE_ENV=production in the runtime docker image - document both settings in .env.example and the development docs - bump version to 1.19.3 --- .env.example | 6 ++++++ CHANGELOG.md | 14 ++++++++++++++ Dockerfile | 3 +++ compose.yaml | 4 +++- docs/development.md | 3 ++- package-lock.json | 4 ++-- package.json | 2 +- server/src/common/cors.ts | 23 +++++++++++++++++++++++ server/src/config/app.config.ts | 5 +++++ server/src/main.ts | 3 ++- server/test/cors.spec.ts | 29 +++++++++++++++++++++++++++++ 11 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 server/src/common/cors.ts create mode 100644 server/test/cors.spec.ts diff --git a/.env.example b/.env.example index c278fd2..226c447 100644 --- a/.env.example +++ b/.env.example @@ -4,9 +4,15 @@ ## Lines starting with "##" are descriptions. ## Lines starting with "# " are settings: remove the "# " to enable one. +## production in the Docker image; also selects the default CORS origins # NODE_ENV=development # PORT=3000 +## Origins allowed to call the API cross-origin (comma separated). Default: the +## Vite dev server (http://localhost:5173) in development, the compose port +## (http://localhost:13000) in production +# CORS_ORIGINS=http://localhost:5173,http://127.0.0.1:5173 + ## SQLite database path # DB_PATH=data/liqa.db diff --git a/CHANGELOG.md b/CHANGELOG.md index 255d65a..c630b38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,20 @@ All notable changes to this project will be documented in this file. +## 1.19.3 - 2026-09-30 + +Changes since 1.19.2: + +### Changed + +- The Docker image runs with `NODE_ENV=production`. +- The origins allowed to call the API cross-origin depend on `NODE_ENV`: the Vite dev server (`http://localhost:5173`, `http://127.0.0.1:5173`) in development, the compose port (`http://localhost:13000`, `http://127.0.0.1:13000`) in production. The new `CORS_ORIGINS` setting (comma separated) overrides both. A `NODE_ENV=development` in `.env` overrides the image's production mode. +- `.env` is optional: compose loads it only when it exists, and every setting in `.env.example` is commented out and shows its default. + +### Removed + +- The unused `KEYS_DIR` setting and the `./keys` mount in `compose.yaml`. + ## 1.19.2 - 2026-09-30 Changes since 1.19.1: diff --git a/Dockerfile b/Dockerfile index f564f66..df685b2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,6 +31,9 @@ RUN npm run -w server build # ── Runtime ─────────────────────────────────────────────────────────────────── FROM node:24-slim AS runtime +# Runtime only: in the build stages it would make npm ci skip devDependencies +ENV NODE_ENV=production + # Shared libraries chrome-headless-shell links against (from `ldd`), plus # fontconfig and basic fonts for text rendering. Playwright's `--with-deps` # would also pull in xvfb (with Mesa/LLVM) and extra font packages, which diff --git a/compose.yaml b/compose.yaml index a62a881..17fb21e 100644 --- a/compose.yaml +++ b/compose.yaml @@ -3,7 +3,9 @@ services: build: context: . ports: - # UI, REST API (/api/v1), MCP (/mcp) and Swagger (/docs) + # UI, REST API (/api/v1), MCP (/mcp) and Swagger (/docs). + # The production CORS origins (server/src/common/cors.ts) use this port; + # set CORS_ORIGINS in .env when publishing it elsewhere. - "13000:3000" # Optional overrides (see .env.example); every setting has a default env_file: diff --git a/docs/development.md b/docs/development.md index 19646be..42e89d4 100644 --- a/docs/development.md +++ b/docs/development.md @@ -27,7 +27,8 @@ All settings are validated at startup by `server/src/config/app.config.ts`. | Variable | Default | Description | |---|---|---| -| `NODE_ENV` | `development` | Shown in the startup log | +| `NODE_ENV` | `development` | `production` in the Docker image. Shown in the startup log, and selects the default CORS origins | +| `CORS_ORIGINS` | *(empty)* | Comma-separated origins allowed to call the API cross-origin. When empty: the Vite dev server (`http://localhost:5173`, `http://127.0.0.1:5173`) unless `NODE_ENV=production`, then the compose port (`http://localhost:13000`, `http://127.0.0.1:13000`) | | `PORT` | `3000` | HTTP port | | `DB_PATH` | `data/liqa.db` | SQLite database file | | `FILES_DIR` | `data/files` | Where scenario files and run artifacts are stored | diff --git a/package-lock.json b/package-lock.json index 119522d..7c7d30d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "liqa", - "version": "1.19.2", + "version": "1.19.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "liqa", - "version": "1.19.2", + "version": "1.19.3", "workspaces": [ "server", "client" diff --git a/package.json b/package.json index 332fa70..8562958 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "liqa", - "version": "1.19.2", + "version": "1.19.3", "private": true, "engines": { "node": ">=24" diff --git a/server/src/common/cors.ts b/server/src/common/cors.ts new file mode 100644 index 0000000..57a3ce3 --- /dev/null +++ b/server/src/common/cors.ts @@ -0,0 +1,23 @@ +/** Vite dev server (`npm -w client run dev`), calling the API cross-origin. */ +const DEV_ORIGINS = ["http://localhost:5173", "http://127.0.0.1:5173"]; + +/** + * The port compose.yaml publishes. The UI is served from the same origin in + * production, so these only matter for other tools calling the API from a + * page on that origin; keep in sync with compose.yaml. + */ +const PRODUCTION_ORIGINS = ["http://localhost:13000", "http://127.0.0.1:13000"]; + +/** + * Origins allowed to call the API cross-origin. `CORS_ORIGINS` (comma + * separated) overrides the defaults, which depend on NODE_ENV: the compose + * port in production, the Vite dev server otherwise. + */ +export function corsOrigins(nodeEnv: string, override: string): string[] { + const configured = override + .split(",") + .map((origin) => origin.trim()) + .filter(Boolean); + if (configured.length > 0) return configured; + return nodeEnv === "production" ? PRODUCTION_ORIGINS : DEV_ORIGINS; +} diff --git a/server/src/config/app.config.ts b/server/src/config/app.config.ts index a34ee81..cd25e37 100644 --- a/server/src/config/app.config.ts +++ b/server/src/config/app.config.ts @@ -17,9 +17,14 @@ export enum TraceMode { } export class AppConfig { + /** `production` in the Docker image; also selects the default CORS origins. */ @IsString() NODE_ENV: string = "development"; + /** Comma-separated origins allowed to call the API cross-origin; empty = default for NODE_ENV. */ + @IsString() + CORS_ORIGINS: string = ""; + @IsInt() @Min(1) @Max(65535) diff --git a/server/src/main.ts b/server/src/main.ts index 593c7fd..8f7a1ea 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -6,6 +6,7 @@ import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger"; import { AppModule } from "./app.module"; import { appPackage } from "./common/app-package"; import { configureBodyParsers } from "./common/body-parsers"; +import { corsOrigins } from "./common/cors"; import { TraceLogger } from "./common/trace-logger"; import { AppConfig } from "./config/app.config"; import { HttpExceptionFilter } from "./filters/http-exception.filter"; @@ -53,7 +54,7 @@ async function bootstrap() { const nodeEnv = config.get("NODE_ENV"); app.enableCors({ - origin: ["http://localhost:5173", "http://127.0.0.1:5173"], + origin: corsOrigins(nodeEnv, config.get("CORS_ORIGINS")), methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE", "OPTIONS"], credentials: true, }); diff --git a/server/test/cors.spec.ts b/server/test/cors.spec.ts new file mode 100644 index 0000000..dfceda1 --- /dev/null +++ b/server/test/cors.spec.ts @@ -0,0 +1,29 @@ +import { corsOrigins } from "../src/common/cors"; + +describe("corsOrigins", () => { + it("allows the Vite dev server outside production", () => { + for (const env of ["development", "test", ""]) { + expect(corsOrigins(env, "")).toEqual([ + "http://localhost:5173", + "http://127.0.0.1:5173", + ]); + } + }); + + it("allows the compose port in production", () => { + expect(corsOrigins("production", "")).toEqual([ + "http://localhost:13000", + "http://127.0.0.1:13000", + ]); + }); + + it("uses CORS_ORIGINS when set, in any environment", () => { + const override = " https://liqa.example.com , http://localhost:8080,"; + for (const env of ["development", "production"]) { + expect(corsOrigins(env, override)).toEqual([ + "https://liqa.example.com", + "http://localhost:8080", + ]); + } + }); +});