Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(agent): accept registration token from env to keep it out of argv (M5)

Juan Denis089b75526ec1
diff --git a/agent/cmd/agent/main.go b/agent/cmd/agent/main.go
index 238d292b..062465be 100644
--- a/agent/cmd/agent/main.go
+++ b/agent/cmd/agent/main.go
@@ -110,6 +110,21 @@ func registerCmd() *cobra.Command {
Use: "register",
Short: "Register this agent with a ServerKit instance",
RunE: func(cmd *cobra.Command, args []string) error {
+ // Let the secret-bearing inputs come from the environment so the
+ // installer never has to put the registration token in argv —
+ // process arguments are world-readable (`ps`, /proc/<pid>/cmdline),
+ // whereas a process's environment is readable only by its owner and
+ // root. Explicit flags still win when provided.
+ if connStr == "" {
+ connStr = os.Getenv("SERVERKIT_CONNECTION_STRING")
+ }
+ if token == "" {
+ token = os.Getenv("SERVERKIT_REGISTRATION_TOKEN")
+ }
+ if serverURL == "" {
+ serverURL = os.Getenv("SERVERKIT_SERVER_URL")
+ }
+
// --connection-string is the modern entry path: a single
// pasteable blob from the panel that already contains the
// URL and token. When present, it overrides the legacy
@@ -131,7 +146,7 @@ func registerCmd() *cobra.Command {
// no shorthand: "c" belongs to the global --config persistent flag, and
// pflag panics on shorthand redefinition the moment any command runs
cmd.Flags().StringVar(&connStr, "connection-string", "", "panel connection string (single value, replaces --token + --server)")
- cmd.Flags().StringVarP(&token, "token", "t", "", "registration token (use with --server)")
+ cmd.Flags().StringVarP(&token, "token", "t", "", "registration token (use with --server; or set SERVERKIT_REGISTRATION_TOKEN to keep it out of the process arg list)")
cmd.Flags().StringVarP(&serverURL, "server", "s", "", "ServerKit server URL (use with --token)")
cmd.Flags().StringVarP(&name, "name", "n", "", "display name for this server")
diff --git a/agent/scripts/install.ps1 b/agent/scripts/install.ps1
index 9bdba718..f3cf5e10 100644
--- a/agent/scripts/install.ps1
+++ b/agent/scripts/install.ps1
@@ -7,8 +7,8 @@
function Install-ServerKitAgent {
[CmdletBinding()]
param(
- [Parameter(Mandatory=$true)]
- [string]$Token,
+ [Parameter(Mandatory=$false)]
+ [string]$Token = "",
[Parameter(Mandatory=$true)]
[string]$Server,
@@ -22,6 +22,15 @@ function Install-ServerKitAgent {
$ErrorActionPreference = "Stop"
+ # Allow the token to come from the environment so it doesn't have to appear
+ # on the command line (visible to admins via Get-CimInstance Win32_Process).
+ # An explicit -Token wins.
+ if (-not $Token) { $Token = $env:SERVERKIT_REGISTRATION_TOKEN }
+ if (-not $Token) { $Token = $env:SERVERKIT_TOKEN }
+ if (-not $Token) {
+ throw "Registration token required: pass -Token or set SERVERKIT_REGISTRATION_TOKEN"
+ }
+
# Configuration
$InstallDir = "$env:ProgramFiles\ServerKit Agent"
$ConfigDir = "$env:ProgramData\ServerKit\Agent"
@@ -83,12 +92,20 @@ function Install-ServerKitAgent {
# Register with ServerKit
Write-Info "Registering agent with ServerKit..."
- $RegisterArgs = @("register", "--token", $Token, "--server", $Server)
+ $RegisterArgs = @("register", "--server", $Server)
if (-not [string]::IsNullOrEmpty($Name)) {
$RegisterArgs += @("--name", $Name)
}
- $process = Start-Process -FilePath $AgentPath -ArgumentList $RegisterArgs -Wait -PassThru -NoNewWindow
+ # Pass the token via the environment, not -ArgumentList, so it isn't exposed
+ # on the child process command line. Start-Process inherits this session's
+ # environment; clear it again afterward.
+ $env:SERVERKIT_REGISTRATION_TOKEN = $Token
+ try {
+ $process = Start-Process -FilePath $AgentPath -ArgumentList $RegisterArgs -Wait -PassThru -NoNewWindow
+ } finally {
+ Remove-Item Env:\SERVERKIT_REGISTRATION_TOKEN -ErrorAction SilentlyContinue
+ }
if ($process.ExitCode -ne 0) {
Write-Err "Registration failed with exit code $($process.ExitCode)"
throw "Registration failed"
diff --git a/agent/scripts/install.sh b/agent/scripts/install.sh
index 6dd4f97a..f3569d3e 100644
--- a/agent/scripts/install.sh
+++ b/agent/scripts/install.sh
@@ -62,10 +62,16 @@ while [[ $# -gt 0 ]]; do
esac
done
+# Allow the token / server URL to come from the environment. This lets callers
+# avoid putting the token on the `curl ... | sudo bash -s -- --token <token>`
+# line, which would otherwise land in shell history. An explicit --token wins.
+TOKEN="${TOKEN:-${SERVERKIT_REGISTRATION_TOKEN:-$SERVERKIT_TOKEN}}"
+SERVER_URL="${SERVER_URL:-$SERVERKIT_SERVER_URL}"
+
# Validate required arguments
if [ -z "$TOKEN" ]; then
echo -e "${RED}Error: Registration token is required${NC}"
- echo "Usage: $0 --token TOKEN --server URL"
+ echo "Usage: $0 --token TOKEN --server URL (or set SERVERKIT_REGISTRATION_TOKEN)"
exit 1
fi
@@ -202,14 +208,16 @@ install_agent() {
register_agent() {
log_info "Registering agent with ServerKit..."
- REGISTER_CMD="$INSTALL_DIR/serverkit-agent register --token \"$TOKEN\" --server \"$SERVER_URL\""
+ # Pass the token through the environment, not argv: command-line arguments
+ # are world-readable via `ps` / /proc/<pid>/cmdline, while the environment
+ # of this root-run process is readable only by root. The server URL and
+ # name aren't secret, so they stay as flags. (Also drops the old `eval`.)
+ local reg_args=(register --server "$SERVER_URL")
if [ -n "$NAME" ]; then
- REGISTER_CMD="$REGISTER_CMD --name \"$NAME\""
+ reg_args+=(--name "$NAME")
fi
- eval $REGISTER_CMD
-
- if [ $? -ne 0 ]; then
+ if ! SERVERKIT_REGISTRATION_TOKEN="$TOKEN" "$INSTALL_DIR/serverkit-agent" "${reg_args[@]}"; then
log_error "Registration failed"
exit 1
fi
diff --git a/docs/AGENT_AUDIT.md b/docs/AGENT_AUDIT.md
index 0dabc4bb..310c7f60 100644
--- a/docs/AGENT_AUDIT.md
+++ b/docs/AGENT_AUDIT.md
@@ -65,6 +65,16 @@ Severity counts: **3 High, 5 Medium, 7 Low/Informational.**
the synchronous waiter resolves) over the poll transport, which shares the
WebSocket path's auth/registry/routing code. This is the "prove it actually
works" net the subsystem lacked.
+- **M5 — fixed.** The `register` command now reads the token (and
+ connection-string / server URL) from the environment
+ (`SERVERKIT_REGISTRATION_TOKEN`, etc.) when the flags aren't given, so the
+ installers pass the token via env instead of argv — env is owner/root-only
+ (`/proc/<pid>/environ`), whereas argv is world-readable (`ps`,
+ `/proc/<pid>/cmdline`). `install.sh` (env-passed register, `eval` removed) and
+ `install.ps1` (token via env, made optional) updated accordingly, and both
+ accept the token from the environment so it need not appear in the
+ `curl | bash -s -- --token …` line / shell history. Verified: the agent reads
+ the env token at runtime, both scripts pass syntax checks.
- **M3 — fixed.** The DEB/RPM units now run as the dedicated unprivileged
`serverkit-agent` account with the same hardening as `install.sh`
(`NoNewPrivileges=yes`, `ProtectSystem=strict`, `ProtectHome=yes`,