195 lines
8.5 KiB
Markdown
195 lines
8.5 KiB
Markdown
# security-command.org
|
|
|
|
Source of the [security-command.org](https://security-command.org/) homepage:
|
|
a real Linux terminal in the browser. Every visitor gets their own bash in a
|
|
disposable [gVisor](https://gvisor.dev/) sandbox, with the homepage content
|
|
mounted read only and the network switched off. The same content is also
|
|
served as plain HTML pages for search engines and browsers without
|
|
JavaScript.
|
|
|
|
## How it works
|
|
|
|
```
|
|
browser (xterm.js)
|
|
│ HTTPS / WSS
|
|
nginx on the host (TLS)
|
|
│ 127.0.0.1:BACKEND_PORT
|
|
backend ────────── static site, terminal WebSocket, proof of work, queue
|
|
│ Unix socket
|
|
spawner ────────── the only service with access to the sandbox daemon
|
|
│ /run/docker-sandbox/docker.sock
|
|
sandbox dockerd ── runtime runsc, cgroup sandbox.slice
|
|
│
|
|
one container per visitor
|
|
|
|
jobs ───────────── scripts from config/jobs.toml that keep the content up to date
|
|
```
|
|
|
|
- **Sessions.** A sandbox only starts after the first key press and a proof
|
|
of work of about a second, which gets harder under load. At most 12
|
|
sessions run at once; further visitors wait in a FIFO queue. Sessions end
|
|
after 3 minutes without input or 20 minutes in total, and survive a page
|
|
reload for 45 seconds.
|
|
- **Sandboxes.** Read only root filesystem, no network, no capabilities,
|
|
non root user, 80 MB of memory, half a CPU, 50 processes, and a small
|
|
noexec tmpfs at `/tmp` as the only writable place. The content directory
|
|
is the read only home directory, so `ls` shows it right away. All
|
|
sandboxes together are capped by `sandbox.slice` (1.6 GB, 1.5 CPUs).
|
|
- **Privilege split.** The backend faces the internet and holds no Docker
|
|
socket. It can only ask the spawner to spawn, attach, resize, kill or list
|
|
sandboxes, and the spawner applies fixed flags. The sandbox daemon is a
|
|
second dockerd that runs nothing but sandboxes.
|
|
- **Static site.** The backend indexes the content directory into memory and
|
|
serves `/ls`, a page per file and directory, `/raw/<path>`, the legal
|
|
pages, `sitemap.xml` and `robots.txt`. Symlinks and dotfiles are never
|
|
served. Raw files cannot run scripts in the site's origin: HTML and SVG
|
|
are served as plain text and every raw response carries `CSP: sandbox`.
|
|
|
|
## Repository layout
|
|
|
|
```
|
|
deploy.sh deploys the checked out commit on the server
|
|
apps/backend/ HTTP server, WebSocket gateway, builtin jobs
|
|
apps/frontend/ terminal app, built into the backend image
|
|
apps/spawner/ broker in front of the sandbox daemon
|
|
apps/jobs/ runs script jobs, jobctl
|
|
packages/protocol/ messages between browser, backend and spawner
|
|
packages/indexer/ content index and static site
|
|
packages/jobs/ job file parser and scheduler
|
|
sandbox/ image every visitor gets
|
|
deploy/ compose file, host setup, nginx, env and job examples
|
|
```
|
|
|
|
## Server setup
|
|
|
|
Requirements: Ubuntu 24.04 with cgroup v2, Docker with the compose plugin,
|
|
nginx with TLS for the domain, and a token with `read:package` for the
|
|
registry of the SecurityCommand organization.
|
|
|
|
1. Log in to the registry once as root:
|
|
`docker login git.security-command.org`
|
|
2. Clone the repository to `/srv/security-command.org`.
|
|
3. Create the content directory. It must belong to a regular user, because
|
|
jobs run as its owner.
|
|
4. Run `sudo ./deploy.sh`. The first run asks for the content directory and
|
|
writes `.env` from `deploy/env.example`.
|
|
5. Include `deploy/nginx/security-command.org.conf` in the server block of
|
|
the site.
|
|
|
|
`deploy.sh` pulls the repository, sets up gVisor, the sandbox daemon and
|
|
the slice, pulls the images built for the checked out commit (waiting for
|
|
CI if needed), verifies the sandbox hardening and starts the services. It
|
|
fails if they do not become healthy. Every update is another
|
|
`sudo ./deploy.sh`; `--no-pull` deploys the current checkout as it is.
|
|
|
|
## Configuration
|
|
|
|
`.env` holds the settings; `deploy/env.example` documents each one.
|
|
|
|
| Setting | Default |
|
|
|-------------------|----------------------------------------------------|
|
|
| `CONTENT_DIR` | asked on the first deploy |
|
|
| `JOBS_SRV_DIR` | `/srv`, read only for jobs at `/host/srv` |
|
|
| `JOBS_NGINX_DIR` | `/etc/nginx`, read only for jobs at `/host/nginx` |
|
|
| `SITE_URL` | `https://security-command.org` |
|
|
| `LEGAL_PAGES` | `impressum/imprint:impressum.md,datenschutz/privacy:datenschutz.md` |
|
|
| `REGISTRY` | `git.security-command.org/securitycommand` |
|
|
| `BACKEND_PORT` | `8380`, must match the nginx config |
|
|
| `ALLOWED_ORIGINS` | `https://security-command.org` |
|
|
| `METRICS_PORT` | `9380`, loopback only |
|
|
| `MAX_SESSIONS` | `12` |
|
|
| `MAX_QUEUE` | `20` |
|
|
| `POW_DIFFICULTY` | `20` bits |
|
|
|
|
## Content
|
|
|
|
The content directory is any directory on the host; the application knows
|
|
nothing about its structure. Changes show up in the terminal right away and
|
|
on the static site after two quiet seconds. Update it in place, for example
|
|
with `git pull`, instead of replacing the directory, because running
|
|
containers keep the mount of the old one.
|
|
|
|
- The legal pages from `LEGAL_PAGES` are rendered at `/impressum` and
|
|
`/datenschutz` and linked in the header. The English aliases `/imprint`
|
|
and `/privacy` redirect to them, and browsers that do not prefer German
|
|
show those names in the header.
|
|
- Dotfiles, dot directories and symlinks are skipped.
|
|
- Top level names used by the site itself (`ws`, `raw`, `ls`, `assets`,
|
|
`healthz`, `sitemap.xml`, `robots.txt`, the legal routes and aliases)
|
|
are skipped with a warning in the backend log.
|
|
- Files above 1 MB are listed but not served; the whole index is capped at
|
|
64 MB and 10 000 entries.
|
|
|
|
- Startup files such as `.bashrc` in the content are ignored by the
|
|
sandbox shell, which only reads the configuration in the image.
|
|
- Executables in `.local/bin` of the content are on the `PATH` ahead of the
|
|
built in commands, so the content can carry small scripts. The emblem and
|
|
facts shown at login come from `fastfetch`, which
|
|
`.local/bin/fastfetch` replaces.
|
|
|
|
## Theme
|
|
|
|
All colors of the terminal and the static pages are CSS variables in
|
|
`apps/frontend/public/theme.css`, served at `/theme.css`; the defaults come
|
|
from the Security Command Gitea theme. A `config/theme.css` on the server is
|
|
appended to it on every request, so it overrides colors without a rebuild
|
|
and only needs the variables it changes. Most variables fall back to the
|
|
names Gitea themes use, so the `:root` block of a Gitea theme works too:
|
|
|
|
```css
|
|
:root {
|
|
--accent: #4183c4;
|
|
--ansi-green: #21ba45;
|
|
}
|
|
```
|
|
|
|
## Jobs
|
|
|
|
`config/jobs.toml` schedules jobs. Builtin jobs run in the backend:
|
|
`index` rebuilds the static site, `status` writes the status file shown by
|
|
the `status` command in sandboxes, and `reap-orphans` removes sandboxes no
|
|
session owns. Script jobs live in `jobs/` and run in the jobs service, which
|
|
may write into the content directory. Jobs run on intervals, cron
|
|
schedules, start, or changes of files below `/srv`, and can trigger each
|
|
other. Setting `enabled = false` switches any job off, the system jobs
|
|
included.
|
|
|
|
Neither `config/` nor `jobs/` is part of the repository. The jobs service
|
|
creates `config/jobs.toml` on its first start; `deploy/jobs.example.toml`
|
|
describes every option.
|
|
|
|
## Operations
|
|
|
|
```sh
|
|
sudo ./deploy.sh # update
|
|
docker logs -f security-command-backend-1 # also spawner, jobs
|
|
docker exec security-command-jobs-1 jobctl list # jobs and their last runs
|
|
docker exec security-command-jobs-1 jobctl run <id>
|
|
curl -s 127.0.0.1:9380/metrics # Prometheus metrics
|
|
sudo deploy/host/verify-sandbox.sh # hardening checks
|
|
```
|
|
|
|
`/healthz` answers `ok`, or `degraded` while the spawner is unreachable,
|
|
and reveals nothing else.
|
|
|
|
## Development
|
|
|
|
Node 24 and pnpm through corepack:
|
|
|
|
```sh
|
|
corepack pnpm install
|
|
corepack pnpm typecheck
|
|
corepack pnpm test
|
|
```
|
|
|
|
Node runs the TypeScript sources directly. Gitea Actions checks every push
|
|
to `develop` and `master` and builds the images `homepage-sandbox`,
|
|
`homepage-spawner`, `homepage-backend` and `homepage-jobs`, tagged with the
|
|
commit and the branch. It logs in to the registry with the secrets
|
|
`REGISTRY_USER` and `REGISTRY_TOKEN` (a token with package write access)
|
|
when they exist, and otherwise with the automatic token of the run.
|
|
|
|
## License
|
|
|
|
See [LICENSE](LICENSE).
|