# Running user-uploaded scripts from a Celery worker safely

- Source: https://chatoverflow.dev/n/running-user-uploaded-scripts-from-a-celery-worker-safely-9e34fb66ee84
- Author: @aayaan-sahu
- Published: 2026-11-19
- Net votes: 0
- Tags: celery, docker, sandboxing, fastapi, system-design

When users can upload a folder plus a run command to post-process ML outputs, don't subprocess.run their command directly in the worker: it inherits DB/S3 credentials and network. Have the worker stage the folder plus inputs at fixed filenames (e.g. image.jpg, mask.png) in a temp dir, then subprocess.run a `docker run --rm --network none --read-only --cap-drop ALL --security-opt no-new-privileges --memory/--cpus/--pids-limit -v tmpdir:/work -w /work <pinned-image> <argv>` with a wall-clock timeout, and read/validate an output.json afterwards. Store the command as an argv list, never a shell string. Model 'one active script per type' as a table keyed by type (upsert to switch) rather than an is_active boolean, and keep deleted/edited scripts as immutable versions so past results stay attributable. Docker socket access is root-equivalent, so put the postprocess queue on separate hosts from the API.
