From fd14c05f3ae0110de209d88323a7e52db1543087 Mon Sep 17 00:00:00 2001 From: devops Date: Sun, 25 Oct 2026 08:19:30 +0000 Subject: [PATCH] fix(build): enforce secure TLS registry and binary verification --- Dockerfile | 16 +++---- backend/Dockerfile | 1 - scripts/compose.stage.yml | 4 +- scripts/production-release.py | 2 +- scripts/test_production_release.py | 3 +- scripts/tests/test_secure_build_install.py | 52 ++++++++++++++++++++++ 6 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 scripts/tests/test_secure_build_install.py diff --git a/Dockerfile b/Dockerfile index e4d5d0cd..3256e482 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:20-alpine AS app-builder +FROM node:22-alpine AS app-builder USER root WORKDIR /app ENV NEXT_TELEMETRY_DISABLED=1 @@ -7,18 +7,17 @@ ENV NEXT_PUBLIC_API_URL=https://api.canina.ir/api # Optional: Use Nexus as npm registry ARG NPM_REGISTRY= RUN if [ -n "$NPM_REGISTRY" ]; then \ - npm config set registry "$NPM_REGISTRY" && \ - npm config set strict-ssl false; \ + npm config set registry "$NPM_REGISTRY"; \ fi COPY frontend/application/package*.json ./ -RUN npm ci --include=dev --prefer-offline --no-audit +RUN npm ci --include=dev --prefer-offline --no-audit && test -x node_modules/.bin/next COPY frontend/application ./ ARG NEXT_PUBLIC_API_URL ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL RUN npm run build -FROM node:20-alpine AS admin-builder +FROM node:22-alpine AS admin-builder USER root WORKDIR /app ENV VITE_API_URL=https://api.canina.ir/api @@ -26,18 +25,17 @@ ENV VITE_API_URL=https://api.canina.ir/api # Optional: Use Nexus as npm registry (inherits build-arg) ARG NPM_REGISTRY= RUN if [ -n "$NPM_REGISTRY" ]; then \ - npm config set registry "$NPM_REGISTRY" && \ - npm config set strict-ssl false; \ + npm config set registry "$NPM_REGISTRY"; \ fi COPY frontend/admin-panel/package*.json ./ -RUN npm ci --include=dev --prefer-offline --no-audit +RUN npm ci --include=dev --prefer-offline --no-audit && test -x node_modules/.bin/vite COPY frontend/admin-panel ./ ARG VITE_API_URL ENV VITE_API_URL=$VITE_API_URL RUN npm run build -FROM node:20-alpine +FROM node:22-alpine USER root RUN addgroup -S nginx 2>/dev/null || true && adduser -S nginx -G nginx 2>/dev/null || true RUN mkdir -p /var/log/nginx /var/cache/nginx /run/nginx /etc/nginx/conf.d /etc/nginx/http.d diff --git a/backend/Dockerfile b/backend/Dockerfile index 960a62a6..d169ddce 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -12,7 +12,6 @@ ENV npm_config_cache=/tmp/npm-cache ARG NPM_REGISTRY= RUN if [ -n "$NPM_REGISTRY" ]; then \ npm config set registry "$NPM_REGISTRY" && \ - npm config set strict-ssl false && \ echo "Using npm registry: $NPM_REGISTRY"; \ fi diff --git a/scripts/compose.stage.yml b/scripts/compose.stage.yml index 45e852ad..a72e0dc5 100644 --- a/scripts/compose.stage.yml +++ b/scripts/compose.stage.yml @@ -24,7 +24,7 @@ services: context: ./backend dockerfile: Dockerfile args: - NPM_REGISTRY: ${NPM_REGISTRY:-} + NPM_REGISTRY: ${NPM_REGISTRY:-https://registry.parsaaghayi.ir/repository/npm/} container_name: canino_backend_stage restart: always env_file: @@ -66,7 +66,7 @@ services: context: . dockerfile: Dockerfile args: - NPM_REGISTRY: ${NPM_REGISTRY:-} + NPM_REGISTRY: ${NPM_REGISTRY:-https://registry.parsaaghayi.ir/repository/npm/} NEXT_PUBLIC_API_URL: "https://stageapi.canina.ir/api" VITE_API_URL: "https://stageapi.canina.ir" container_name: canino_frontend_stage diff --git a/scripts/production-release.py b/scripts/production-release.py index 0799ae53..5c7aa4f9 100644 --- a/scripts/production-release.py +++ b/scripts/production-release.py @@ -14,7 +14,7 @@ ALLOWED = {'nginx.conf', 'Dockerfile', 'backend/Dockerfile', '.dockerignore', 'scripts/compose.stage.yml', 'scripts/rollout-stage.sh', 'scripts/revalidation-runtime.py', 'scripts/provision-revalidation-env.py', 'scripts/tests/test_revalidation_runtime.py', 'scripts/tests/test_ci_host_boundary.py', 'scripts/tests/test_stage_provision_wiring.py', - 'scripts/tests/test_stage_rollout.py', + 'scripts/tests/test_stage_rollout.py', 'scripts/tests/test_secure_build_install.py', # CI1285 verified-payload upgrade repair; no model, cron or payload wildcard. 'infra/hermes-editorial/installer.py', 'infra/hermes-editorial/tests/test_installer.py', diff --git a/scripts/test_production_release.py b/scripts/test_production_release.py index 87a4cbb8..4603c1cb 100644 --- a/scripts/test_production_release.py +++ b/scripts/test_production_release.py @@ -28,7 +28,8 @@ class ProductionGuards(unittest.TestCase): def test_exact_editorial_installer_repair_paths_allowed_only(self): paths = ['infra/hermes-editorial/installer.py', - 'infra/hermes-editorial/tests/test_installer.py'] + 'infra/hermes-editorial/tests/test_installer.py', + 'scripts/tests/test_secure_build_install.py'] r = self.receipt(); r['paths'] = paths m.validate_receipt(r, 'a'*40, 'b'*40, paths) for path in ['infra/hermes-editorial/model-policy.json', diff --git a/scripts/tests/test_secure_build_install.py b/scripts/tests/test_secure_build_install.py new file mode 100644 index 00000000..88b80112 --- /dev/null +++ b/scripts/tests/test_secure_build_install.py @@ -0,0 +1,52 @@ +"""Regression gates for CI1286: trusted routing and fail-closed frontend installs.""" +from pathlib import Path +import os +import re +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[2] +REGISTRY = 'https://registry.parsaaghayi.ir/repository/npm/' + + +class SecureBuildInstallTests(unittest.TestCase): + def test_stage_supplies_operational_tls_registry(self): + compose = (ROOT / 'scripts/compose.stage.yml').read_text() + self.assertEqual(compose.count('NPM_REGISTRY: ${NPM_REGISTRY:-' + REGISTRY + '}'), 2) + + def test_frontend_uses_supported_node_lts_in_all_stages(self): + images = re.findall(r'^FROM node:(\S+)', (ROOT / 'Dockerfile').read_text(), re.M) + self.assertEqual(images, ['22-alpine'] * 3) + + def test_registry_configuration_never_disables_tls(self): + for name in ['Dockerfile', 'backend/Dockerfile']: + self.assertNotIn('strict-ssl false', (ROOT / name).read_text()) + + def test_false_successful_incomplete_install_aborts_layer(self): + dockerfile = (ROOT / 'Dockerfile').read_text().replace('\\\n', '') + commands = re.findall(r'^RUN (npm ci[^\n]+)', dockerfile, re.M) + self.assertEqual(len(commands), 2) + for command, binary in zip(commands, ['next', 'vite']): + with self.subTest(binary=binary), tempfile.TemporaryDirectory() as d: + root = Path(d) + npm = root / 'npm' + # Reproduce npm's observed zero exit with no complete node_modules. + npm.write_text('#!/bin/sh\nexit 0\n') + npm.chmod(0o755) + result = subprocess.run(['sh', '-c', command], cwd=d, + env={**os.environ, 'PATH': d + ':' + os.environ['PATH']}, + capture_output=True) + self.assertNotEqual(result.returncode, 0, 'incomplete install was accepted') + target = root / 'node_modules/.bin' / binary + target.parent.mkdir(parents=True) + target.write_text('#!/bin/sh\nexit 0\n') + target.chmod(0o755) + result = subprocess.run(['sh', '-c', command], cwd=d, + env={**os.environ, 'PATH': d + ':' + os.environ['PATH']}, + capture_output=True) + self.assertEqual(result.returncode, 0, result.stderr) + + +if __name__ == '__main__': + unittest.main()