diff --git a/core/auth/adminUsers.ts b/core/auth/adminUsers.ts index a374a12..60f2641 100644 --- a/core/auth/adminUsers.ts +++ b/core/auth/adminUsers.ts @@ -443,18 +443,30 @@ export async function verifyAdminCredentials(email: string, password: string) { const result = await pool.query( `SELECT email, password_salt, password_hash FROM admin_users - WHERE email = $1`, - [cleanEmail], + WHERE id = 1`, ); if (result.rowCount && result.rowCount > 0) { - const admin = result.rows[0] as - | { email: string; password_salt: Buffer; password_hash: Buffer } - | undefined; - const salt = admin?.password_salt ?? Buffer.alloc(PASSWORD_SALT_BYTES); - const expectedHash = - admin?.password_hash ?? Buffer.alloc(PASSWORD_HASH_BYTES); - const actualHash = await derivePasswordHash(password, salt); - return Boolean(admin && timingSafeEqual(actualHash, expectedHash)); + const admin = result.rows[0] as { + email: string; + password_salt: Buffer; + password_hash: Buffer; + }; + const emailMatches = matchesSecret( + cleanEmail, + admin.email.trim().toLowerCase(), + ); + const actualHash = await derivePasswordHash( + password, + admin.password_salt, + ); + const passwordMatches = timingSafeEqual( + actualHash, + admin.password_hash, + ); + return emailMatches && passwordMatches; + } + if (!process.env.DATASTACK_ADMIN_PASSWORD) { + return false; } } catch { // Database connection error, fall back to in-memory admin diff --git a/docker/Dockerfile.server b/docker/Dockerfile.server index 44cdbb3..5fec7c8 100644 --- a/docker/Dockerfile.server +++ b/docker/Dockerfile.server @@ -1,7 +1,7 @@ FROM node:22-alpine AS dependencies WORKDIR /app -COPY package.json package-lock.json ./ -RUN npm ci --omit=dev +COPY package.json package-lock.json* ./ +RUN if [ -f package-lock.json ]; then npm ci --omit=dev; else npm install --omit=dev --no-audit --no-fund; fi FROM node:22-alpine AS runner WORKDIR /app diff --git a/docker/Dockerfile.web b/docker/Dockerfile.web index 8daa047..a3d793e 100644 --- a/docker/Dockerfile.web +++ b/docker/Dockerfile.web @@ -1,7 +1,7 @@ FROM node:22-alpine AS dependencies WORKDIR /app -COPY package.json package-lock.json ./ -RUN npm ci +COPY package.json package-lock.json* ./ +RUN if [ -f package-lock.json ]; then npm ci; else npm install --no-audit --no-fund; fi FROM node:22-alpine AS builder WORKDIR /app @@ -16,13 +16,11 @@ ENV NODE_ENV=production ENV NEXT_TELEMETRY_DISABLED=1 ENV PORT=3000 ENV HOSTNAME=0.0.0.0 -RUN addgroup -S -g 1001 nodejs \ - && adduser -S -u 1001 -G nodejs nextjs -COPY --from=builder --chown=nextjs:nodejs /app/public ./public -COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ -COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static +COPY --from=builder --chown=node:node /app/public ./public +COPY --from=builder --chown=node:node /app/.next/standalone ./ +COPY --from=builder --chown=node:node /app/.next/static ./.next/static RUN mkdir -p /app/storage/data \ - && chown -R nextjs:nodejs /app/storage -USER nextjs + && chown -R node:node /app/storage +USER node EXPOSE 3000 CMD ["node", "server.js"] diff --git a/docker/coolify-nginx.conf b/docker/coolify-nginx.conf index 8e694f7..41da820 100644 --- a/docker/coolify-nginx.conf +++ b/docker/coolify-nginx.conf @@ -3,6 +3,16 @@ map $http_upgrade $connection_upgrade { '' close; } +map $http_x_forwarded_proto $forwarded_proto { + default $http_x_forwarded_proto; + '' $scheme; +} + +map $http_x_forwarded_host $forwarded_host { + default $http_x_forwarded_host; + '' $host; +} + upstream nextjs_upstream { server web:3000; } @@ -23,9 +33,10 @@ server { proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_set_header Host $host; + proxy_set_header X-Forwarded-Host $forwarded_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto; + proxy_set_header X-Forwarded-Proto $forwarded_proto; proxy_read_timeout 86400s; proxy_send_timeout 86400s; proxy_buffering off; @@ -35,8 +46,9 @@ server { proxy_pass http://nextjs_upstream; proxy_http_version 1.1; proxy_set_header Host $host; + proxy_set_header X-Forwarded-Host $forwarded_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto; + proxy_set_header X-Forwarded-Proto $forwarded_proto; } } diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 3027450..c82d66a 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -6,14 +6,19 @@ services: dockerfile: docker/Dockerfile.web expose: - "3000" + volumes: + - datastack-data:/app/storage environment: NODE_ENV: production PORT: "3000" DATASTACK_JWT_SECRET: "${SERVICE_REALBASE64_64_JWT:?Coolify must generate SERVICE_REALBASE64_64_JWT}" - DATASTACK_ADMIN_EMAIL: "${DATASTACK_ADMIN_EMAIL:?Set DATASTACK_ADMIN_EMAIL in .env}" + DATASTACK_ADMIN_EMAIL: "${DATASTACK_ADMIN_EMAIL:-admin@datastack.dev}" DATASTACK_ADMIN_SESSION_SECRET: "${SERVICE_REALBASE64_64_ADMINSESSION:?Coolify must generate SERVICE_REALBASE64_64_ADMINSESSION}" DATASTACK_ADMIN_SETUP_KEY: "${SERVICE_REALBASE64_64_ADMININSTALL:?Coolify must generate SERVICE_REALBASE64_64_ADMININSTALL}" DATABASE_URL: "postgresql://datastack:${SERVICE_PASSWORD_64_POSTGRES:?Coolify must generate SERVICE_PASSWORD_64_POSTGRES}@postgres:5432/${SERVICE_HEX_32_DATABASENAME:?Coolify must generate SERVICE_HEX_32_DATABASENAME}" + depends_on: + postgres: + condition: service_healthy restart: unless-stopped healthcheck: test: @@ -85,7 +90,7 @@ services: timeout: 5s retries: 3 - # Reserved for the planned metadata integration; not consumed by the current runtime. + # PostgreSQL stores the bootstrap admin credential and login throttle state in pgdata. postgres: image: postgres:16-alpine environment: