Skip to content

The hiddenforge CLI

Sam Bent edited this page Dec 3, 2026 · 13 revisions

The hiddenforge CLI

hiddenforge is the operator tool, introduced in 2.1.0; this page describes 3.0.2. It writes a compose fragment for you, prints your .onion, reports whether the service is reachable, audits the running container's hardening, handles key backups, and manages key encryption at rest and client authorization. Most of it is also built into the image, so it runs with docker run too.

Install

The CLI is one file, scripts/hiddenforge in the repository. It needs Python 3.10 or later and uses only the standard library, so there is nothing to pip install. Install it where sudo also finds it, because backup needs root under Docker:

sudo install -m 0755 scripts/hiddenforge /usr/local/bin/hiddenforge

On an older Python it refuses to start and says so, instead of crashing half-way through a command:

hiddenforge needs Python 3.10 or later; this is Python 3.9.

2.0.4 has no CLI. On a 2.0.4 image, docker run <image> version --json just starts Tor.

The 16 subcommands

Every subcommand accepts --json. The list below is the commands array in manifest.json, and a test keeps it equal to what the parser registers.

Command What it does Key flags (defaults in brackets)
setup Writes four files into --out: compose.service.yml (the fragment), deploy.sh, README.txt and hiddenforge.json. Touches none of your files. --backend host:port, --name [myapp], --out / -o [./hiddenforge-deploy], --onion-port [80], --service / --services (multi-service), --image, --keys [./tor-keys], --container [tor], --runtime docker|podman [docker], --no-root, --encrypt-keys, --passphrase-file [./key-passphrase.txt], --force, --non-interactive
info Prints the machine contract (manifest.json). --json
version Prints name, version and contract version. --json
show Prints the .onion address(es). --keys, --name, --container (asks the container, as the tor user, for an address the host cannot read; when ./tor-keys does not exist, it reads the key directory that container mounts)
status Reports container, Tor and hidden-service readiness. --container [tor] (a container name or a compose service), --keys
doctor Checks the hardening of the running container. --container [tor] (a container name or a compose service), --keys, --keys-only (skip docker inspect, check key files only)
vanity Brute-forces a vanity .onion prefix with the generator inside the image. --prefix (required), --name [myapp], --keys, --threads [0], --bench, --seconds [5], --force, --image
backup Encrypted offline backup of the key volume. --keys, --out (required), --tool age|gpg|openssl, --passphrase-file, --require-aead
restore Restores a backup. --keys, --input (required), --tool, --passphrase-file, --force
client-auth Opt-in client authorization (Tor's restricted discovery): add, remove or list the clients allowed to reach a service. Runs a throwaway container from the image. positional add|remove|list, --name [myapp], --client (required for add and remove), --keys, --image
unseal-keys Turns key encryption at rest off: puts the plaintext key back. Stop the service first. --name [myapp], --keys, --passphrase-file (required), --image
rotate Destructive: archives the identity and starts over. --keys, --confirm (must be the literal ROTATE)
encrypt-key Encrypts one service's key directory at rest (offline). --keys, --name (required), --tool age|gpg, --passphrase-file, --delete-plaintext, --force, --require-aead
decrypt-key Restores a key directory from its archive. same flags as encrypt-key
verify-image Verifies the signed image digest. positional image, --sigdir [signatures], --identity [sam@sambent.com], --skip-registry, --allow-classical-only, --allow-pq-only, --allowed-signers, --mldsa-pub
sbom Generates an SPDX SBOM for an image. --image, --out [./hiddenforge.spdx.json], --syft-image

Some flag notes that the table cannot hold:

  • For --tool, age works only from an interactive terminal. gpg is the scriptable choice, through --passphrase-file. openssl is refused for backup because it is unauthenticated.
  • --require-aead makes backup, encrypt-key and decrypt-key refuse an envelope that is CFB with MDC instead of authenticated encryption.
  • restore and decrypt-key accept a gpg file only when gpg's own status lines confirm it was decrypted and its integrity was proven (AEAD or MDC). A file that was never encrypted, or that has no integrity protection, is refused before anything is written. For a gpg file, restore reports the mode it found (aead or cfb).
  • backup and encrypt-key refuse a key volume that contains a symbolic link, naming it, instead of leaving it out of the archive.
  • setup --no-root writes a fragment that starts the container as the tor user (100:101) with no capabilities at all; deploy.sh hands the key directory to 100:101 once. setup --encrypt-keys adds the passphrase file as a compose secret (deploy.sh creates a random one if it is missing) and seals the service key on the volume at the first start. See the README for what that does and does not protect.
  • client-auth add prints the client's private line once and does not store it; the service keeps only the public key. Restart the container after any client-auth change, because Tor reads the authorized clients at start.
  • unseal-keys checks that the restored key reads back as the sealed one before it removes the sealed copy. Afterwards remove HIDDENFORGE_KEY_PASSPHRASE_FILE and the passphrase secret from the compose file, or the key is sealed again at the next start.
  • vanity --bench prints how long a prefix is expected to take on this machine, using --seconds of measurement, and generates nothing. vanity runs with no network.
  • sbom runs on the host. It drives a digest-pinned syft container through the Docker socket, which the HiddenForge image deliberately does not have.
  • Which stack status, show and doctor use. --container takes a container name or a compose service name, and the CLI asks both Docker and Podman for it (HIDDENFORGE_ENGINE=docker or podman pins one, and a note says when the container runs under the other engine). If several containers run that service, the one that belongs to the current directory is used: its project name (from COMPOSE_PROJECT_NAME, the .env file, the name: key of its compose file, or the folder's name) or its working folder, which may be a parent of the folder you are in. If that does not settle it, the command stops with exit 2, lists the candidates with their folders and says to pass --container <name> or to cd into the stack. A script that runs these commands from another folder while two or more stacks run a tor service must pass --container with a container's own name. Without --keys, status and doctor read the host side of the key directory that container mounts and say which directory they read; show does the same when --container is given and ./tor-keys does not exist, and says to cd or pass --container or --keys when ./tor-keys does not exist.
  • Errors for a person. Without --json, a failure prints one error: line with the same exit code; a mistyped option prints one error: line that names hiddenforge <command> --help. With --json, a failure is the one JSON document described below, and so is a mistyped option (exit code 2).
  • unseal-keys and client-auth run the image of the container that mounts the key directory (a running container first, stopped ones count), under the engine that runs it, and say which image they used. --image and HIDDENFORGE_IMAGE win over that, and the published :latest is used only when no container mounts the directory. vanity runs in the engine that holds the key directory.
  • setup warns on stderr when the backend host is localhost, 127.x.x.x or ::1: that is the Tor container itself, not your app. The files are still written.
  • rotate keeps the key directory's mode, so a container started as root can still enter a key volume owned by the host user (0751).
  • verify-image checks against pinned keys, never against keys found beside the signatures. It refuses when either half of the hybrid signature is missing; --allow-classical-only accepts a missing ML-DSA half and --allow-pq-only a missing SSH-FIDO2 half, both reported as complete: false, and an image with neither signature verified is refused even with both. From 3.0.2 it checks every ML-DSA signature in the set (ML-DSA-87 and ML-DSA-65) against its pinned key, requires the ML-DSA-87 one for any release after 3.0.1, and accepts ML-DSA-65 alone only for the releases made before that, which are listed by digest in the CLI. It needs OpenSSL 3.5 or later for the ML-DSA checks; on an older one it prints NOT VERIFIED and exits 1. A flag is offered in the message only when there is a verified half for it to accept. A reference pinned by digest (repo@sha256:...) is compared with the signed digest on every run, with or without --skip-registry. See Verifying the Image.

A typical first run:

hiddenforge setup --backend web:80 --name myapp --out ./deploy
docker compose -f compose.yml -f ./deploy/compose.service.yml up -d
# rootless Podman: run Docker Compose against the Podman socket (see Rootless Podman)
hiddenforge show --keys ./tor-keys --name myapp --json

The --json contract

With --json, stdout carries exactly one JSON document and nothing else. Any narration goes to stderr, so hiddenforge show --json | jq . is always safe.

Rule Detail
One document One JSON document, keys sorted; usually a single line. setup --json indents it, and info --json prints the manifest itself, indented, without ok or exit_code.
Always present ok (true only when the exit code is 0) and exit_code, on every command except info.
Nulls kept A field with no value is null, not omitted. A script polling for an address can tell "no address yet" (onion: null) from "field missing".
Errors A failure puts the reason in an error field and still exits non-zero.
No hanging off a TTY Off a terminal nothing prompts: setup exits 2 if a required value such as --backend is missing, and backup, restore, encrypt-key and decrypt-key exit 2 unless given --passphrase-file, and so does unseal-keys. Exit 130 is only an interactive prompt answered with nothing, end of input, or Ctrl-C.

To poll, call hiddenforge status --json and publish nothing until ready is true. Then read the address with hiddenforge show --json. This is the pattern manifest.json documents.

Exit codes

Code Meaning
0 Success
1 Refused or failed (see stderr, or the error field with --json)
2 Usage, validation or environment error
3 A key appeared at the output path while a vanity search was running
4 Not ready yet (status: the container exists but is not ready, and reason names why; show: no address published yet)
130 Aborted at a prompt

doctor exits 1 when any check fails and 0 otherwise; doctor and status exit 2 if the container is not found or if the name matches several stacks and the current directory does not pick one. Warnings do not change the exit code.

Which subcommands run inside the image

docker run --rm <image> <cmd> runs 13 of the 16. Anything else in that position boots Tor as usual.

docker run --rm sambent.dev/sam/hiddenforge:latest version --json
docker run --rm sambent.dev/sam/hiddenforge:latest info --json
Runs in the image Does not
setup, show, doctor, version, info, vanity, encrypt-key, decrypt-key, backup, restore, rotate, client-auth, unseal-keys status, verify-image, sbom: they run on the host. docker exec <container> hiddenforge status exits 2 and says so; docker run <image> status is not a command and boots Tor

How the dispatch behaves:

  • show and doctor read the 0700 tor-owned key directories. Run with docker run, they switch to the tor user themselves. Run as root with docker exec, they read the directories directly and switch to tor only when root is refused, which is what happens under rootless Podman. The other commands run as root so they can write into a host-owned key mount.
  • The host CLI runs client-auth, unseal-keys and vanity for you in a throwaway container with no network. client-auth and unseal-keys run as the tor user (100:101) with no capabilities.
  • doctor inside the image always adds --keys-only. There is no engine socket in the container, so it checks on-disk key hygiene only. The full hardening audit needs the host CLI.
  • vanity runs the generator wrapper built into the image directly, so inside the image its output flag is --out (default: the key mount), not the host CLI's --keys; --prefix, --name, --threads, --bench, --seconds and --force are the same.
  • The image sets HIDDENFORGE_KEYS_DIR=/var/lib/tor/hidden_service, so the key-volume default points at the mount.
  • age and gpg are not in the image. backup, restore, encrypt-key and decrypt-key need them, so run those on the host. Inside the image they exit 2 with a message naming the missing tool or passphrase file.
  • The image holds only Tor, its libraries, the hiddenforge binary and the vanity generator, so every command in it is hiddenforge <command>. Two exist only in the image: hiddenforge healthcheck [--json], the container's health state, and hiddenforge inspect --json, the torrc Tor runs with, any dropped mappings and the service addresses. Both read as the tor user. The healthcheck that older compose files declare, su-exec tor python3 /usr/local/bin/healthcheck.py, still runs the native one; any other su-exec form is refused with exit 127 and a message pointing to hiddenforge <command>, and docker exec <container> sh fails with the engine's "executable file not found".

Environment variables

Variable Effect Default
HIDDENFORGE_ENGINE Pins the container engine for every inspect, exec and run. Accepts docker or podman. Without it, a container is looked for in Docker and in Podman, and the engine that holds it is used. docker if it is on PATH, otherwise podman; a container found under the other engine switches to that one
HIDDENFORGE_IMAGE Image used by setup, vanity, client-auth and unseal-keys. sambent.dev/sam/hiddenforge:latest
HIDDENFORGE_KEYS_DIR Default for every --keys flag except setup's, which always defaults to ./tor-keys. ./tor-keys

What doctor checks

doctor reads the container's real state (its cgroup and capability set where it can) instead of trusting the compose file. Its checks cover capabilities, no-new-privileges, the read-only root, the seccomp profile (a container that still sets seccomp=unconfined fails), the process cap, swap, published ports, the log driver, DNS, the generated torrc and key-file modes. A container started in no-root mode (user 100 or tor) needs no capabilities at all, so doctor treats any granted capability as extra. It fails a container the engine reports unhealthy (one still starting is a warning) and a key directory the container's tor user cannot enter, and says to run chmod 751 on it. Whether AppArmor applies is asked of Podman directly (podman info) when the engine is Podman, so a stack made by setup --runtime podman passes; if Podman cannot be asked, the check warns. See Security Model for why each setting exists.

Restart policy, including systemd. A container with no engine restart policy normally fails the restart check. If the container carries the PODMAN_SYSTEMD_UNIT label, doctor checks that unit instead. Whether a unit really brings the container back depends on details doctor cannot read with confidence (its ExecStart must use podman run --replace, and RestartPreventExitStatus=, the start limits and RestartSec= must not stop it), so a unit in the right shape is reported as a warning naming what to verify: the unit's main process is the container's conmon (with RemainAfterExit=no and the default ExitType=main) and it has Restart=always. Verify it once by killing the container (podman kill <name>) and watching the unit start it again. A unit in any other shape fails, with the fix: a Type=forking unit with PIDFile= and podman run --replace --conmon-pidfile <same path> -d ..., or a quadlet unit.

Check Pass condition Severity
Unit restarts the container Right shape and Restart=always: warning (verify by killing the container once); anything else: fail warn / fail
Container returns after a reboot The unit's file state is enabled (a generated quadlet unit is reported as such: it starts at boot only if its [Install] section says so) warn
User services run without a login session linger is on (checked when the engine is Podman) warn

The unit is read with systemctl --user show, then without --user. If it cannot be read, the check fails and names the unit.

Host-network containers and DNS. A container on the host network uses the host's resolver and cannot be given dns:. So doctor checks something else: whether any backend in *_TOR_SERVICE_HOSTS is a hostname. A hostname raises a warning that lists the names, and the fix is to give those backends as an IP or localhost, which are never looked up and pass. Outside host networking, the check wants dns: set to a loopback address.

For the Podman-specific checks (capabilities, linger, podman-restart.service), see Rootless Podman.

Clone this wiki locally