Skip to content

Commit bd466c6

Browse files
committed
Harden security of the app
The following improvements were made: - Trimmed Dockerfile and added unprivileged user. - Introduced entrypoint.js for mode switching. - Hardened all subprocess.run invocations. - Removed direct exec() usage by sandboxing user code in subprocesses. - Updated README with secure run instructions and hardened container command.
1 parent 0eb9580 commit bd466c6

File tree

6 files changed

+355
-123
lines changed

6 files changed

+355
-123
lines changed

Dockerfile

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,62 @@
1-
FROM ubuntu:latest
1+
# Single-stage Dockerfile using slim Python base
2+
FROM python:3.11-slim
23

3-
COPY . ./app
4-
WORKDIR /app
5-
6-
RUN apt update; apt install -y wget lsb-release software-properties-common gnupg curl ca-certificates
7-
8-
RUN wget -qO- https://apt.llvm.org/llvm.sh | bash -s -- 21
9-
RUN apt install -y libmlir-21-dev mlir-21-tools
4+
# Environment variables
5+
ENV DEBIAN_FRONTEND=noninteractive \
6+
PYTHONUNBUFFERED=1 \
7+
PIP_NO_CACHE_DIR=1 \
8+
PIP_DEFAULT_TIMEOUT=100 \
9+
NEXT_TELEMETRY_DISABLED=1
1010

11-
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
12-
RUN apt install -y nodejs
13-
RUN npm install
11+
ARG APP_ENV=production
12+
ENV NODE_ENV=$APP_ENV
1413

15-
RUN add-apt-repository -y ppa:deadsnakes/ppa; apt install -y python3-pip python3.11-venv
16-
17-
RUN python3.11 -m venv mlir_venv
14+
WORKDIR /app
1815

19-
RUN mlir_venv/bin/pip install --upgrade pip
20-
RUN mlir_venv/bin/pip install --pre torch-mlir torchvision \
21-
--extra-index-url https://download.pytorch.org/whl/nightly/cpu \
22-
-f https://github.com/llvm/torch-mlir-release/releases/expanded_assets/dev-wheels
16+
# Install minimal tooling
17+
RUN apt-get update && \
18+
apt-get install -y --no-install-recommends \
19+
ca-certificates wget curl gnupg lsb-release software-properties-common && \
20+
rm -rf /var/lib/apt/lists/*
2321

24-
RUN mlir_venv/bin/pip install fastapi uvicorn pytest httpx
22+
# Add LLVM 21 repository
23+
RUN wget -qO- https://apt.llvm.org/llvm.sh | bash -s -- 21
2524

25+
# Add Node.js 20 repository and install runtime deps
26+
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
27+
apt-get update && \
28+
apt-get install -y --no-install-recommends \
29+
libmlir-21-dev mlir-21-tools nodejs && \
30+
rm -rf /var/lib/apt/lists/*
31+
32+
# Copy application code
33+
COPY --chown=10001:10001 . /app
34+
35+
# Install JS dependencies, then install 'concurrently' globally
36+
RUN npm install && \
37+
npm install -g concurrently
38+
39+
# Create Python venv and install Python packages
40+
RUN python3 -m venv /opt/venv && \
41+
/opt/venv/bin/pip install --upgrade pip setuptools wheel && \
42+
/opt/venv/bin/pip install --pre torch-mlir torchvision \
43+
--extra-index-url=https://download.pytorch.org/whl/nightly/cpu \
44+
-f https://github.com/llvm/torch-mlir-release/releases/expanded_assets/dev-wheels && \
45+
/opt/venv/bin/pip install fastapi uvicorn pytest httpx
46+
47+
# Create non-root user and fix permissions
48+
RUN useradd -u 10001 -m --shell /usr/sbin/nologin appuser && \
49+
mkdir -p /home/appuser/.cache && \
50+
chown -R appuser:appuser /home/appuser/.cache /app
51+
USER appuser
52+
53+
# Update PATH for venv and LLVM
54+
ENV PATH="/opt/venv/bin:/usr/lib/llvm-21/bin:$PATH"
55+
56+
# Expose ports and add healthcheck
2657
EXPOSE 3000 8000
58+
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s \
59+
CMD curl -f http://localhost:8000/health || exit 1
2760

28-
CMD ["npm", "run", "start:all"]
61+
# Default to interactive shell
62+
CMD ["/bin/sh"]

README.md

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,49 +47,89 @@ Current version of the application is tested on Ubuntu 22.04 windows subsystem u
4747

4848
### Install dependencies
4949

50-
In case of missing prerequisites here are some scripts to help set them up (runs on Debian and its derivatives).
51-
50+
Clone the repository:
5251
```bash
5352
git clone https://github.com/MrSidims/PytorchExplorer.git
5453
cd PytorchExplorer
54+
```
55+
56+
Install frontend dependencies:
57+
```bash
5558
source setup_frontend.sh
5659
```
5760

58-
When you have venv suitable for `torch-mlir` work, install `fastapi`, `uvicorn` etc in venv like this:
61+
Set up backend (Torch, MLIR, etc.):
62+
```bash
63+
source setup_backend.sh
64+
```
5965

66+
If you already have a working venv for Torch-MLIR, you can just install FastAPI and testing dependencies:
6067
```bash
6168
pip install fastapi uvicorn pytest httpx
6269
```
6370

64-
Otherwise here is the script to setup `torch`, `llvm` etc:
71+
To use custom builds of `torch-mlir-opt`, `mlir-opt`, etc. without placing them in your `$PATH`, configure the following environment variables:
72+
- `TORCH_MLIR_OPT_PATH`
73+
- `LLVM_BIN_PATH`
74+
- `TRITON_OPT_PATH`
6575

76+
### Run the application
6677

78+
#### Development mode (local)
6779
```bash
68-
source setup_backend.sh
80+
npm run dev:all
6981
```
82+
Then open http://localhost:3000/
7083

71-
If you want to use your builds of the tools like `torch-mlir-opt`, `mlir-opt` etc without placing them in `PATH` please setup `TORCH_MLIR_OPT_PATH` and `LLVM_BIN_PATH` environment variables.
72-
73-
### Run the application
74-
84+
#### Production mode (local)
7585
```bash
86+
npm run build
7687
npm run start:all
7788
```
7889

7990
Then open http://localhost:3000/ in your browser and enjoy!
8091

81-
### Run in a docker
82-
83-
Build image with:
92+
#### Run in a container (Docker or Podman)
8493

94+
Build the image (change APP_ENV between development/production, default is production):
8595
```bash
86-
docker build -t pytorch_explorer .
96+
docker build -t pytorch_explorer --build-arg APP_ENV=development .
8797
```
8898

89-
Run it:
99+
Run the container in **production mode**:
90100
```bash
91101
docker run -p 3000:3000 -p 8000:8000 pytorch_explorer
92102
```
103+
Then inside the container:
104+
```bash
105+
npm run build
106+
npm run start:all
107+
```
108+
109+
To run in **development mode**:
110+
```bash
111+
docker run -it --rm \
112+
-e NODE_ENV=development \
113+
-p 3000:3000 -p 8000:8000 \
114+
pytorch_explorer
115+
```
116+
Then inside the container:
117+
```bash
118+
npm run dev:all
119+
```
120+
121+
Secure run (in cases, when you don't trust tested samples):
122+
```bash
123+
podman run --rm -it \
124+
--read-only \
125+
--cap-drop=ALL \
126+
--security-opt=no-new-privileges \
127+
--tmpfs /app/.next:rw,size=256m \
128+
-v stored_sessions:/app/StoredSessions:rw \
129+
-p8000:8000 -p3000:3000 \
130+
-e NODE_ENV=production \
131+
pytorch_explorer
132+
```
93133

94134
### Run the tests
95135

0 commit comments

Comments
 (0)