mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-05 12:24:28 +01:00
engines: the key comes through a config file SGLang merges in memory, on no command line
--api-key "$(cat .../api-key)" put the key in the argv of the docker client and of the server, readable by any local user in /proc. The units, the flash launcher and run.sh pass --config /out/engine-secrets.yaml instead (the parse checked in both serving images), written from the api-key file by engine-secrets.sh before every start (ExecStartPre, the launcher, run.sh) and only when its content changes, so a hand-changed key still reaches the next start and an unchanged one restarts nothing. install.sh installs the helper at step 6; the CI flash gate, SECURITY.md and the uninstall listing follow. Also: a test's fake subprocess.run leaked into every later test of the process; it is put back. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
15ef09017b
commit
780b9bd166
12 files changed
+138
-9
No files matched your search
@@ -516,7 +516,10 @@ jobs:
|
||||
|| { echo "the $tier tier renders --allow-auto-truncate into the launcher"; exit 1; }
|
||||
done
|
||||
grep -q -- '--speculative-draft-model-quantization unquant' /tmp/flash.rendered
|
||||
grep -q -- '--api-key' /tmp/flash.rendered
|
||||
# the key comes through a file SGLang merges in memory, never the command line
|
||||
grep -q -- '--config /out/engine-secrets.yaml' /tmp/flash.rendered
|
||||
! grep -q -- '--api-key' /tmp/flash.rendered || { echo "the flash launcher puts the key on a command line"; exit 1; }
|
||||
grep -q 'engine-secrets.sh' /tmp/flash.rendered
|
||||
grep -q 'chat-template-flashnext.jinja' /tmp/flash.rendered
|
||||
grep -q 'HF_HUB_OFFLINE=1' /tmp/flash.rendered
|
||||
grep -q -- '--revision cafe' /tmp/flash.rendered
|
||||
|
||||
@@ -541,6 +541,17 @@ lane, whose pip dependencies are resolved from PyPI unpinned.
|
||||
being shown acting; a native 27B recipe failed its own validation (no `--context-length`,
|
||||
as a native unit has none). Each is fixed, and two comments that gave wrong numbers are.
|
||||
|
||||
**Keys stay off command lines.** Every lane passed the engine its key as
|
||||
`--api-key "$(cat .../api-key)"`, so the key was in the argv of the docker client and of
|
||||
the server, which any local user reads in `/proc`; the download's `docker run` carried
|
||||
`-e HF_TOKEN=<token>`, and the smoke test's curl the key in a `-H` argument. The units, the
|
||||
flash launcher and `run.sh` now hand the server `--config /out/engine-secrets.yaml`, which
|
||||
SGLang merges into its arguments in memory (checked in both serving images), written from
|
||||
the api-key file by `engine-secrets.sh` before every start, so a key changed by hand still
|
||||
reaches the next start, and only when its content changes, so an update that changes
|
||||
nothing still restarts nothing. The token goes to docker by name, and the smoke key as a
|
||||
header file. The engine restarts once, at the update that brings this.
|
||||
|
||||
**CI gates that could not fail.** A negated `grep` under `bash -e` checked nothing; the syntax
|
||||
and shellcheck lists left out seven scripts, `install-image.sh` among them; the sudoers gate
|
||||
never compared `daemon-reload` and the two `install` calls with the allowlist; and the flash
|
||||
|
||||
+2
-2
@@ -118,8 +118,8 @@ pinned revision, the opencode release by its sha256. The image lane
|
||||
(`--with-image`) is the one part with unpinned bytes: its SGLang wheel and source
|
||||
commit are pinned, but pip resolves the wheel's dependency tree from PyPI at install
|
||||
time, with no hash checked, and its checkpoint is downloaded at the repo's current
|
||||
revision rather than a pinned one. The engine enforces `--api-key` (the file is
|
||||
created 0600) and the chat template and launcher are regenerated from the
|
||||
revision rather than a pinned one. The engine enforces its key, read from a file created
|
||||
0600 and handed to it through SGLang's `--config`, so it is in no process's command line and the chat template and launcher are regenerated from the
|
||||
repo on every install.
|
||||
|
||||
## Assumptions that are design choices, not vulnerabilities
|
||||
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env bash
|
||||
# The engine's key, where SGLang reads it without a command line. This writes
|
||||
# engine-secrets.yaml next to the api-key file, and the units, the flash launcher and
|
||||
# run.sh hand it to the server as `--config /out/engine-secrets.yaml`: SGLang merges
|
||||
# that file into its arguments in memory, so the key is in no process's argv. As
|
||||
# --api-key "$(cat ...)" it was in the docker client's and in the server's, which any
|
||||
# local user reads in /proc (found in review, 2026-09-24). It runs before every start,
|
||||
# so a key changed by hand still reaches the engine at its next start, as it did.
|
||||
# Usage: engine-secrets.sh [config dir] (default: the folder this script sits in)
|
||||
set -euo pipefail
|
||||
d="${1:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
|
||||
key="$(cat "$d/api-key")"
|
||||
case "$key" in
|
||||
"" | *[\"\\]* | *[[:cntrl:]]*)
|
||||
echo "engine-secrets.sh: $d/api-key is empty, or holds a quote, a backslash or a control character" >&2
|
||||
exit 1 ;;
|
||||
esac
|
||||
want="api-key: \"$key\""
|
||||
# Written only when it changes: install.sh keeps an engine whose inputs are older than its
|
||||
# start, and a rewrite of the same bytes would make it restart the engine at every run.
|
||||
if [ -f "$d/engine-secrets.yaml" ] && [ "$(cat "$d/engine-secrets.yaml")" = "$want" ]; then
|
||||
chmod 600 "$d/engine-secrets.yaml"
|
||||
exit 0
|
||||
fi
|
||||
umask 077
|
||||
printf '%s\n' "$want" > "$d/.engine-secrets.yaml.tmp"
|
||||
mv -f "$d/.engine-secrets.yaml.tmp" "$d/engine-secrets.yaml"
|
||||
@@ -1315,6 +1315,12 @@ else
|
||||
echo "API key already present, keeping it"
|
||||
fi
|
||||
KEY="$(cat "$CONFIG_DIR/api-key")" # used by the step-9 smoke test
|
||||
# The engine is handed its key in a file SGLang merges in memory, written by this script
|
||||
# before every start (the units, the flash launcher, run.sh): as --api-key "$(cat ...)" it
|
||||
# was in the argv of the docker client and of the server (found in review, 2026-09-24).
|
||||
cmp -s "$REPO_DIR/engine-secrets.sh" "$CONFIG_DIR/engine-secrets.sh" \
|
||||
|| install -m 755 "$REPO_DIR/engine-secrets.sh" "$CONFIG_DIR/engine-secrets.sh"
|
||||
bash "$CONFIG_DIR/engine-secrets.sh" || die "could not write the engine's key file from $CONFIG_DIR/api-key (see above)"
|
||||
# One patched template per engine file name: the served template always follows
|
||||
# the served model (both fixes: reasoning_effort normalization + mid-conversation
|
||||
# system messages as <system-reminder> blocks; see patch-template.py).
|
||||
|
||||
@@ -69,6 +69,10 @@ __SPEC_TOKEN_MAP_LINE__
|
||||
# refuses anything above the lane's ceiling with a clear 400, so the flag bought
|
||||
# nothing and cost the one failure mode worse than a refusal, which is a wrong
|
||||
# answer from a context that was quietly cut.
|
||||
#
|
||||
# The key goes to the server in a file SGLang merges in memory (see engine-secrets.sh),
|
||||
# written again here before every start, so it is on no command line.
|
||||
/bin/bash __HOME__/.config/qwen38/engine-secrets.sh
|
||||
exec /usr/bin/docker run --rm --name qwen38-flash --gpus all \
|
||||
--memory 110g --memory-swap 110g --shm-size 16g --network host --ipc=host \
|
||||
-v __HF_CACHE__:/root/.cache/huggingface \
|
||||
@@ -94,5 +98,5 @@ exec /usr/bin/docker run --rm --name qwen38-flash --gpus all \
|
||||
"${TIER[@]}" \
|
||||
--reasoning-parser qwen3 --tool-call-parser qwen3_coder \
|
||||
--chat-template /out/chat-template-flashnext.jinja \
|
||||
--api-key "$(cat __HOME__/.config/qwen38/api-key)" \
|
||||
--config /out/engine-secrets.yaml \
|
||||
--host __ENGINE_BIND__ --port __PORT__
|
||||
@@ -15,6 +15,9 @@ Type=simple
|
||||
User=__USER__
|
||||
Group=__GROUP__
|
||||
ExecStartPre=-/usr/bin/docker rm -f qwen38-sglang
|
||||
# The key goes to the server in a file SGLang merges in memory (see engine-secrets.sh),
|
||||
# written again before every start, so it is on no command line.
|
||||
ExecStartPre=/bin/bash __HOME__/.config/qwen38/engine-secrets.sh
|
||||
ExecStart=/bin/bash -c 'exec /usr/bin/docker run --rm --name qwen38-sglang --gpus all \
|
||||
--memory 100g --memory-swap 100g --shm-size 16g --network host --ipc=host \
|
||||
-e TORCHINDUCTOR_CACHE_DIR=/cache/inductor -e SGLANG_ALLOW_OVERWRITE_LONGER_CONTEXT_LEN=1 -e HF_HUB_OFFLINE=1 \
|
||||
@@ -40,7 +43,7 @@ ExecStart=/bin/bash -c 'exec /usr/bin/docker run --rm --name qwen38-sglang --gpu
|
||||
--enable-metrics \
|
||||
--reasoning-parser qwen3 --tool-call-parser qwen3_coder \
|
||||
--chat-template /out/chat-template-sglang.jinja \
|
||||
--api-key "$(cat __HOME__/.config/qwen38/api-key)" \
|
||||
--config /out/engine-secrets.yaml \
|
||||
--host __ENGINE_BIND__ --port __PORT__'
|
||||
ExecStop=-/usr/bin/docker stop -t 20 qwen38-sglang
|
||||
Restart=always
|
||||
|
||||
@@ -15,6 +15,9 @@ Type=simple
|
||||
User=__USER__
|
||||
Group=__GROUP__
|
||||
ExecStartPre=-/usr/bin/docker rm -f qwen38-sglang
|
||||
# The key goes to the server in a file SGLang merges in memory (see engine-secrets.sh),
|
||||
# written again before every start, so it is on no command line.
|
||||
ExecStartPre=/bin/bash __HOME__/.config/qwen38/engine-secrets.sh
|
||||
ExecStart=/bin/bash -c 'exec /usr/bin/docker run --rm --name qwen38-sglang --gpus all \
|
||||
--memory 100g --memory-swap 100g --shm-size 16g --network host --ipc=host \
|
||||
-e TORCHINDUCTOR_CACHE_DIR=/cache/inductor \
|
||||
@@ -41,7 +44,7 @@ ExecStart=/bin/bash -c 'exec /usr/bin/docker run --rm --name qwen38-sglang --gpu
|
||||
--enable-metrics \
|
||||
--reasoning-parser qwen3 --tool-call-parser qwen3_coder \
|
||||
--chat-template /out/chat-template-sglang.jinja \
|
||||
--api-key "$(cat __HOME__/.config/qwen38/api-key)" \
|
||||
--config /out/engine-secrets.yaml \
|
||||
--host __ENGINE_BIND__ --port __PORT__'
|
||||
ExecStop=-/usr/bin/docker stop -t 20 qwen38-sglang
|
||||
Restart=always
|
||||
|
||||
@@ -106,7 +106,8 @@ GPU_APPS="$(nvidia-smi --query-compute-apps=pid,process_name --format=csv,nohead
|
||||
[ -z "$GPU_APPS" ] || die "the GPU is busy. This config needs the machine to itself (unified memory):
|
||||
$GPU_APPS"
|
||||
|
||||
KEY="$(cat "$CONFIG_DIR/api-key")"
|
||||
# the key reaches the server through a file SGLang merges in memory, never the command line
|
||||
bash "$REPO_DIR/engine-secrets.sh" "$CONFIG_DIR" || die "could not write the engine's key file from $CONFIG_DIR/api-key"
|
||||
mkdir -p "$CONFIG_DIR/sglang-cache"
|
||||
echo "Starting in the foreground (first boot ≈ 9 min: torch.compile + CUDA graph capture)."
|
||||
echo " Ready when the log says: The server is fired up and ready to roll!"
|
||||
@@ -146,5 +147,5 @@ exec docker run --rm --name qwen38-sglang-run --gpus all \
|
||||
--chat-template /out/chat-template-sglang.jinja \
|
||||
--sleep-on-idle \
|
||||
--enable-metrics \
|
||||
--api-key "$KEY" \
|
||||
--config /out/engine-secrets.yaml \
|
||||
--host "$ENGINE_BIND" --port "$PORT"
|
||||
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
"""The engine's key is on no command line.
|
||||
|
||||
Every lane passed `--api-key "$(cat .../api-key)"`, so the key was in the argv of the
|
||||
docker client and of the server process, which any local user reads in /proc (found in
|
||||
review, 2026-09-24). The units, the flash launcher and run.sh now hand the server
|
||||
`--config /out/engine-secrets.yaml`, which SGLang merges into its arguments in memory
|
||||
(checked in both serving images), and engine-secrets.sh writes that file from the api-key
|
||||
file before every start, so a key changed by hand still reaches the next start."""
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
|
||||
REPO = pathlib.Path(__file__).resolve().parents[1]
|
||||
HELPER = REPO / "engine-secrets.sh"
|
||||
LAUNCHERS = ("qwen38-sglang.service.template", "qwen38-sglang-1m.service.template",
|
||||
"qwen38-flash-launch.sh.template", "run.sh")
|
||||
|
||||
|
||||
def helper(key):
|
||||
d = pathlib.Path(tempfile.mkdtemp(prefix="engine-key-"))
|
||||
(d / "api-key").write_text(key)
|
||||
r = subprocess.run(["bash", str(HELPER), str(d)], capture_output=True, text=True, timeout=30)
|
||||
return r, d
|
||||
|
||||
|
||||
class TheKeyFile(unittest.TestCase):
|
||||
def test_every_launcher_passes_the_file_and_no_key(self):
|
||||
for name in LAUNCHERS:
|
||||
text = (REPO / name).read_text()
|
||||
self.assertNotIn("--api-key", text.replace("# ", ""), name)
|
||||
self.assertIn("--config /out/engine-secrets.yaml", text, name)
|
||||
self.assertIn("engine-secrets.sh", text, name)
|
||||
|
||||
def test_the_file_is_written_before_the_server_starts(self):
|
||||
for name in ("qwen38-sglang.service.template", "qwen38-sglang-1m.service.template"):
|
||||
text = (REPO / name).read_text()
|
||||
self.assertIn("ExecStartPre=/bin/bash __HOME__/.config/qwen38/engine-secrets.sh\n", text, name)
|
||||
flash = (REPO / "qwen38-flash-launch.sh.template").read_text()
|
||||
self.assertLess(flash.index("/bin/bash __HOME__/.config/qwen38/engine-secrets.sh"),
|
||||
flash.index("exec /usr/bin/docker run"))
|
||||
install = (REPO / "install.sh").read_text()
|
||||
self.assertIn('install -m 755 "$REPO_DIR/engine-secrets.sh" "$CONFIG_DIR/engine-secrets.sh"', install)
|
||||
self.assertIn('bash "$CONFIG_DIR/engine-secrets.sh"', install)
|
||||
|
||||
def test_the_helper_writes_a_private_yaml_once(self):
|
||||
r, d = helper("Abc123xyz\n")
|
||||
self.assertEqual(r.returncode, 0, r.stderr)
|
||||
f = d / "engine-secrets.yaml"
|
||||
self.assertEqual(f.read_text(), 'api-key: "Abc123xyz"\n')
|
||||
self.assertEqual(os.stat(f).st_mode & 0o777, 0o600)
|
||||
before = os.stat(f).st_mtime_ns
|
||||
time.sleep(0.05)
|
||||
subprocess.run(["bash", str(HELPER), str(d)], check=True, timeout=30)
|
||||
self.assertEqual(os.stat(f).st_mtime_ns, before, "the same key rewrote the file: the engine would restart")
|
||||
|
||||
def test_a_key_the_yaml_cannot_carry_is_refused(self):
|
||||
for bad in ("", 'ab"c', "ab\\c", "ab\tc"):
|
||||
r, d = helper(bad)
|
||||
self.assertNotEqual(r.returncode, 0, repr(bad))
|
||||
self.assertFalse((d / "engine-secrets.yaml").exists(), repr(bad))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
@@ -41,6 +41,9 @@ class Base(unittest.TestCase):
|
||||
"served_model_name": "qwen3.8-27b", "model_path": "x"}
|
||||
self.calls = []
|
||||
real = self.m.subprocess.run
|
||||
# self.m.subprocess is the process's own subprocess module: put run() back, or every
|
||||
# test after this one gets these fakes instead of real processes
|
||||
self.addCleanup(setattr, self.m.subprocess, "run", real)
|
||||
|
||||
def fake(argv, **kw):
|
||||
joined = " ".join(map(str, argv))
|
||||
|
||||
+1
-1
@@ -110,7 +110,7 @@ for f in "$CONFIG_DIR"/*.bak-preupdate; do
|
||||
[ -f "$f" ] && echo " backup $f (pre-update unit backup)"
|
||||
done
|
||||
if [ -d "$CONFIG_DIR" ]; then
|
||||
echo " config $CONFIG_DIR ($(dir_size "$CONFIG_DIR")): api-key, patched templates, opencode.json, launch script, compile cache"
|
||||
echo " config $CONFIG_DIR ($(dir_size "$CONFIG_DIR")): api-key and the engine's copy of it, patched templates, opencode.json, launch scripts, compile cache"
|
||||
[ -f "$CONFIG_DIR/opencode-web.env" ] && echo " config $CONFIG_DIR/opencode-web.env (Agent tab: credentials of the opencode web server)"
|
||||
[ -f "$CONFIG_DIR/claude-code.env" ] && echo " legacy $CONFIG_DIR/claude-code.env (pre-v1.3 client config, unmaintained)"
|
||||
[ -f "$CONFIG_DIR/opencode.off" ] && echo " marker $CONFIG_DIR/opencode.off (opencode integration disabled with --no-opencode)"
|
||||
|
||||
Reference in new issue
Block a user