> ## Documentation Index
> Fetch the complete documentation index at: https://aeros-c1d071c8.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Security

> How AerosPanel authenticates users, blocks cross-site requests, and guards every systemctl, Git, and filesystem call.

## Threat model

The panel runs as root, because it writes units to `/etc/systemd/system` and calls `systemctl`. Anyone who logs in as the admin can run arbitrary commands on the host through a project's `ExecStart` or build command. Treat the admin password like the root password.

> [!WARNING]
>
> Do not expose port 3000 to the internet over plain HTTP. Reach the panel through Tailscale, a Cloudflare Tunnel, or Caddy with HTTPS. See [Domains and remote access](https://aeros-c1d071c8.mintlify.site/domains-and-remote-access.md).

The guards below limit what a compromised browser session, a forged cross-site request, or a malformed input can do. They do not make the admin account less powerful.

## Passwords

Passwords are hashed with Node's built-in `crypto.scrypt`. No external auth library is used.

| Parameter | Value |
| :- | :- |
| Cost | `N=16384`, `r=8`, `p=1` |
| Salt | 16 random bytes per password |
| Key length | 64 bytes |
| Stored format | `scrypt$<salt-hex>$<hash-hex>` in the `users` table |
| Comparison | `crypto.timingSafeEqual` |

Passwords must be at least 10 characters. Usernames must be 3 to 32 letters, digits, `_`, or `-`.

## Sessions

* A login creates a 32-byte random token. Only its SHA-256 hash is stored in the `sessions` table.
* The token is sent as the cookie `aeros_session` with `HttpOnly`, `SameSite=Lax`, `Path=/`, and `Max-Age` of 7 days. `Secure` is added when the request arrived over HTTPS.
* Expired sessions are deleted when a new session is created. There is no cleanup timer.
* Logout deletes the session row.
* Changing the password deletes every session for that user and issues a new one. `seed.js` also deletes all sessions of the user it updates.

## Login rate limit

`POST /api/auth/login` allows **10 attempts per IP address per 15-minute window**. Every attempt counts as a failure until the password checks out, and it is counted before the database lookup so a concurrent burst cannot slip past the check. A successful login clears the counter. Over the limit, the panel answers `429` with the seconds left in the window.

The counters live in memory and reset when the panel restarts. Only the login endpoint is rate limited.

> [!NOTE]
>
> The client IP comes from `req.ip`. `X-Forwarded-For` is trusted only from loopback, so clients cannot spoof their IP to dodge the limit. Behind a proxy on another machine, every request appears to come from that proxy's IP.

## Initial setup

`POST /api/auth/setup` creates the first admin. It works only while the `users` table is empty, and the count is checked again inside a database transaction. Once a user exists, the endpoint returns `409`.

## Cross-site request protection

`requireSameOriginForMutations` runs before every `/api` route, including unauthenticated ones. For `POST`, `PUT`, `DELETE`, and `PATCH` requests:

1. If `Sec-Fetch-Site` is `cross-site` and the `Origin` is not listed in `CORS_ORIGIN`, the request is rejected with `403 Cross-site request blocked`.
2. If an `Origin` header is present, its host must equal the request's `Host` or the first `X-Forwarded-Host`, or the origin must be listed in `CORS_ORIGIN`. Otherwise: `403 Cross-origin request blocked`.
3. Requests with no `Origin` header (such as `curl` or GitHub webhooks) pass this check and still need a valid session or signature.

This is on top of `SameSite=Lax`, which already keeps the session cookie off cross-site `POST` requests.

### `CORS_ORIGIN`

Unset by default. Set it in `backend/.env` only when the frontend is served from a different origin than the API:

```bash backend/.env theme={null}
CORS_ORIGIN=https://panel.example.com,https://admin.example.com
```

Origins in this list are allowed for CORS with credentials, for mutations, and for WebSocket connections.

## WebSocket

The log stream at `/ws?service=<name>` checks, in order:

| Check | Close code |
| :- | :- |
| `Origin` header present and same-origin (or in `CORS_ORIGIN`). A missing `Origin` is rejected. | `4403 Forbidden origin` |
| Valid `aeros_session` cookie | `4401 Unauthorized` |
| `service` matches `^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$` | `4400 Invalid service name` |

Only then does it spawn `journalctl -u <service> -f -n 100`, with arguments passed as an array. The process is killed when the socket closes.

## Input guards

Every value that reaches `systemctl`, `git`, or the filesystem passes through a validator first. Commands are started with `execFile` or `spawn` and an argument array, never a shell string. Git calls put `--` before user-supplied values.

> [accordion] assertManageableServiceName
>
> Runs before every `start`, `stop`, `restart`, `enable`, `disable`, unit write, unit delete, metrics read, Caddy snippet, and alert drop-in. It requires `^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$` and rejects reserved units: `aeros-panel`, `ssh`, `sshd`, `systemd`, `tailscaled`, `cloudflared`, `docker`, `ufw`, `cron`, `dbus`, and anything starting with `systemd-`. A trailing `.service` and letter case do not get around the list.

> [accordion] validateWorkDir
>
> Requires an absolute path without newlines or null bytes, normalizes it with `path.posix.resolve`, and rejects `/` and anything under `/etc`, `/bin`, `/sbin`, `/usr`, `/lib`, `/lib64`, `/boot`, `/dev`, `/proc`, `/sys`, `/run`, or `/var/lib`. Used for project working directories, `.env` writes, deploys, and backup paths.

> [accordion] validateRepositoryUrl
>
> Accepts `https://`, `http://`, `ssh://`, and `git@host:path`. Rejects values that start with `-` (option injection), contain whitespace or null bytes, use `file://` or `ext::`, have a hostname starting with `-`, or have an SSH path with `..`. It runs at creation and again before every clone.

> [accordion] validateGitBranch and validateGitCommit
>
> Branches must match `^[a-zA-Z0-9][a-zA-Z0-9._/-]{0,119}$` and cannot contain `..` or `@{`, or end with `/` or `.lock`. Rollback targets must be full 40 or 64 character hex SHAs.

> [accordion] Unit field validation
>
> Names and `ExecStart` are flattened to a single line so they cannot inject extra unit directives. `User=`, `MemoryMax=`, and `CPUQuota=` each have a strict pattern. Caddy domains must be a valid FQDN and ports 1 to 65535.

> [!NOTE]
>
> A project's build command is the one place that runs through a shell, because it is meant to hold commands like `npm ci && npm run build`. Only the authenticated admin can set it, and the terminal prints `configured build command` instead of its text.

## Secrets

| Secret | Where it lives | Exposure |
| :- | :- | :- |
| Admin password | `scrypt` hash in `users` | Never returned. |
| Session tokens | SHA-256 hash in `sessions` | Cookie only, `HttpOnly`. |
| GitHub webhook secret | `projects.webhook_secret` | Shown once on creation with `Cache-Control: no-store`. `GET /api/projects` only returns `has_webhook`. Compared with `crypto.timingSafeEqual`. |
| Public status token | `settings` table | Compared with `crypto.timingSafeEqual`. |
| Alert webhook URL | `/etc/aeros-panel/alerts.env`, mode `0600` | Never returned to the browser. |
| Project `.env` | `projects.env_vars` and `<work_dir>/.env` (`0600`) | Responses use `Cache-Control: no-store`. |

The installer sets `aeros.db` and `backend/.env` to `0600`. None of these values are written to logs.

## HTTP headers and limits

Every response carries:

```http theme={null}
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Referrer-Policy: no-referrer
Content-Security-Policy: frame-ancestors 'none'
```

JSON request bodies are limited to 1 MB.

## Reverse proxies

`trust proxy` is set to `loopback`. `X-Forwarded-For` and `X-Forwarded-Proto` are honored only when the direct peer is `127.0.0.1` or `::1`, which covers Caddy and `cloudflared` on the same host. A remote client sending these headers to port 3000 cannot spoof its IP or mark a plain HTTP request as secure.

The same-origin check also accepts the first `X-Forwarded-Host` as a valid host, from any peer. A browser cannot attach that header to a cross-site request without a CORS preflight, which the panel does not grant unless the origin is in `CORS_ORIGIN`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.