Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix(security): Containerized Backend application is running as root

Mike Pawlowski87b2c829cbad
diff --git a/Makefile b/Makefile
index dfeea93e5..9feab2a71 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,11 @@ REPO ?= https://github.com/langflow-ai/langflow.git
# Auto-detect container runtime: prefer docker, fall back to podman
CONTAINER_RUNTIME := $(shell command -v docker >/dev/null 2>&1 && echo "docker" || echo "podman")
+
+# Host UID/GID — evaluated once at parse time and used in Docker-assisted chown commands
+# so that Alpine (running as root) can re-own volume directories back to the host user.
+HOST_UID := $(shell id -u)
+HOST_GID := $(shell id -g)
OPENRAG_IMAGE_REPOS := langflowai/openrag-backend langflowai/openrag-frontend langflowai/openrag-langflow langflowai/openrag-opensearch langflowai/openrag-dashboards langflow/langflow opensearchproject/opensearch opensearchproject/opensearch-dashboards
# Only pass --env-file if the file actually exists
ifneq (,$(wildcard $(ENV_FILE)))
@@ -324,9 +329,16 @@ ensure-langflow-data: ## Create the langflow-data directory if it does not exist
@mkdir -p langflow-data
@chmod 777 langflow-data
-ensure-backend-volumes: ## Create and permission backend volume directories for Docker (UID 1000 / appuser)
+ensure-backend-volumes: ## Create and permission backend volume directories, re-owned to the host user
@mkdir -p flows keys config data
- @chmod 775 flows keys config data
+ @# Re-own directories to the host user so local dev (make backend) can always read/write them,
+ @# even after a container run chowned them to UID 1000 (appuser). Runs via Docker Alpine as root
+ @# so it succeeds regardless of current ownership; falls back to native chown if Docker is unavailable.
+ @$(CONTAINER_RUNTIME) run --rm \
+ -v "$$(pwd)/flows:/mnt/flows" -v "$$(pwd)/keys:/mnt/keys" \
+ -v "$$(pwd)/config:/mnt/config" -v "$$(pwd)/data:/mnt/data" \
+ alpine sh -c "chown -R $(HOST_UID):$(HOST_GID) /mnt/flows /mnt/keys /mnt/config /mnt/data && chmod 775 /mnt/flows /mnt/keys /mnt/config /mnt/data" 2>/dev/null \
+ || { chown -R $(HOST_UID):$(HOST_GID) flows keys config data 2>/dev/null || true; chmod 775 flows keys config data 2>/dev/null || true; }
dev: ensure-langflow-data ensure-backend-volumes ## Start full stack with GPU support
@echo "$(YELLOW)Starting OpenRAG with GPU support...$(NC)"
@@ -547,7 +559,8 @@ factory-reset: ## Complete reset (stop, remove volumes, clear data, remove image
fi; \
if [ -f "keys/private_key.pem" ] || [ -f "keys/public_key.pem" ]; then \
echo "Removing JWT keys..."; \
- rm -f keys/private_key.pem keys/public_key.pem; \
+ rm -f keys/private_key.pem keys/public_key.pem 2>/dev/null || \
+ $(CONTAINER_RUNTIME) run --rm -v "$$(pwd)/keys:/keys" alpine rm -f /keys/private_key.pem /keys/public_key.pem 2>/dev/null || true; \
echo "$(PURPLE)JWT keys removed$(NC)"; \
fi; \
echo "$(YELLOW)Removing OpenRAG images...$(NC)"; \
@@ -560,7 +573,7 @@ factory-reset: ## Complete reset (stop, remove volumes, clear data, remove image
# LOCAL DEVELOPMENT
######################
-backend: ## Run backend locally
+backend: ensure-backend-volumes ## Run backend locally
@echo "$(YELLOW)Starting backend locally...$(NC)"
@if [ ! -f $(ENV_FILE) ]; then echo "$(RED)$(ENV_FILE) file not found. Copy .env.example to it first$(NC)"; exit 1; fi
uv run python src/main.py
diff --git a/src/tui/main.py b/src/tui/main.py
index 3e88a06d6..9cd1af5ee 100644
--- a/src/tui/main.py
+++ b/src/tui/main.py
@@ -701,6 +701,55 @@ def migrate_legacy_data_directories():
logger.info("Data migration completed successfully")
+def _reclaim_host_ownership(directories: list[Path]) -> None:
+ """Re-own directories to the current host user via a container running as root.
+
+ On Linux with rootful Docker the backend entrypoint.sh calls
+ ``chown -R appuser:appuser`` on volume-mounted directories, changing their
+ *host-side* ownership to UID 1000. Subsequent TUI startups then cannot
+ chmod or write into those directories (e.g. to regenerate JWT keys after a
+ reset), causing a silent crash.
+
+ This mirrors the ``ensure-backend-volumes`` Makefile target: an Alpine
+ container is launched as root and asked to chown the directories back to the
+ host user. Silently skips directories that are already owned by the current
+ user, or when no container runtime is available.
+ """
+ import shutil as _shutil
+
+ host_uid = os.getuid()
+ host_gid = os.getgid()
+
+ needs_reclaim = [d for d in directories if d.exists() and d.stat().st_uid != host_uid]
+ if not needs_reclaim:
+ return
+
+ runtime = (
+ "docker" if _shutil.which("docker")
+ else "podman" if _shutil.which("podman")
+ else None
+ )
+ if not runtime:
+ logger.error("No container runtime found; cannot reclaim directory ownership")
+ return
+
+ for directory in needs_reclaim:
+ try:
+ subprocess.run(
+ [
+ runtime, "run", "--rm",
+ "-v", f"{directory}:/mnt/target",
+ "alpine", "chown", "-R", f"{host_uid}:{host_gid}", "/mnt/target",
+ ],
+ check=True,
+ capture_output=True,
+ timeout=30,
+ )
+ logger.info(f"Reclaimed ownership of {directory} → {host_uid}:{host_gid}")
+ except Exception as e:
+ logger.error(f"Could not reclaim ownership of {directory}: {e}")
+
+
def generate_jwt_keys(keys_dir: Path):
"""Generate RSA keys for JWT signing if they don't exist.
@@ -784,9 +833,21 @@ def setup_host_directories():
base_dir / "config",
base_dir / "data",
]
+
+ # On Linux + rootful Docker the backend container's entrypoint.sh chowns these
+ # directories to UID 1000 (appuser). Reclaim ownership before chmod so that
+ # subsequent TUI startups can chmod and write into them (e.g. key regeneration
+ # after a factory reset).
+ _reclaim_host_ownership(backend_writable)
+
for directory in backend_writable:
- os.chmod(directory, 0o775)
- logger.debug(f"Set permissions 775 on: {directory}")
+ try:
+ os.chmod(directory, 0o775)
+ logger.debug(f"Set permissions 775 on: {directory}")
+ except PermissionError:
+ # Reclaim may have been skipped (no runtime available); the mode was
+ # already set on first run so this is non-fatal.
+ logger.error(f"Cannot chmod {directory} (not owner), skipping")
# Resolve the configured LANGFLOW_DATA_PATH so we pre-create the exact
# directory that Docker/Podman will mount, regardless of user customisation.
@@ -797,7 +858,10 @@ def setup_host_directories():
# langflow-data must be world-writable so the Langflow container user (uid 1000)
# can write into it on macOS where Podman's :U uid-remapping does not reliably
# update host directory ownership through the VM layer.
- os.chmod(langflow_data_dir, 0o777)
+ try:
+ os.chmod(langflow_data_dir, 0o777)
+ except PermissionError:
+ logger.error(f"Cannot chmod {langflow_data_dir} (not owner), skipping")
# Generate JWT keys on host to avoid container permission issues
generate_jwt_keys(base_dir / "keys")