File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -67,11 +67,19 @@ RUN apt-get update \
6767 && apt-get clean \
6868 && rm -rf /var/lib/apt/lists/*
6969
70- # Copy published .NET app
70+ # Create unprivileged user for Node.js
71+ RUN groupadd -r nodeuser && useradd -r -g nodeuser -s /bin/bash nodeuser
72+
73+ # Copy published .NET app (owned by root, no access for nodeuser)
7174COPY --from=dotnet-build /src/MyApp/bin/Release/net10.0/publish ./api
75+ RUN chmod -R 700 ./api && chown -R root:root ./api
7276
73- # Copy built Next.js app (including dist, node_modules, public, etc. )
77+ # Copy built Next.js app (owned by nodeuser, read-only )
7478COPY --from=dotnet-build /src/MyApp.Client ./client
79+ RUN chown -R nodeuser:nodeuser ./client && chmod -R 500 ./client
80+
81+ # Create /tmp directory accessible to nodeuser
82+ RUN mkdir -p /tmp && chmod 1777 /tmp
7583
7684ENV ASPNETCORE_URLS=http://0.0.0.0:8080 \
7785 INTERNAL_API_URL=http://127.0.0.1:8080 \
Original file line number Diff line number Diff line change @@ -13,15 +13,25 @@ if [[ "$*" == *"--AppTasks"* ]]; then
1313fi
1414
1515echo " Starting ASP.NET Core on ${ASPNETCORE_URLS} ..."
16- DOTNET_ENV_VARS=(" ASPNETCORE_URLS=${ASPNETCORE_URLS} " )
1716
18- # Start ASP.NET Core application with correct content root so appsettings.json is loaded
17+ # Start ASP.NET Core application as root with full environment
1918ASPNETCORE_CONTENTROOT=" /app/api" ASPNETCORE_URLS=" ${ASPNETCORE_URLS} " dotnet /app/api/MyApp.dll &
2019DOTNET_PID=$!
2120
22- echo " Starting Next.js on port ${NEXT_PORT} ..."
21+ echo " Starting Next.js on port ${NEXT_PORT} as isolated user..."
22+
23+ # Start Node.js with minimal environment and as unprivileged user
24+ # Only pass through safe environment variables
25+ cd /app/client && su nodeuser -s /bin/bash -c "
26+ export HOME=/tmp
27+ export NODE_ENV=production
28+ export NEXT_PORT=${NEXT_PORT}
29+ export INTERNAL_API_URL=${INTERNAL_API_URL:- http:// 127.0.0.1: 8080}
30+ export KAMAL_DEPLOY_HOST=${KAMAL_DEPLOY_HOST}
31+ export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2332cd /app/client
24- npm run start -- --port " ${NEXT_PORT} " &
33+ npm run start -- --port ${NEXT_PORT}
34+ " &
2535NEXT_PID=$!
2636
2737term_handler () {
You can’t perform that action at this time.
0 commit comments