فهرست منبع

build: package the function runner as a third deb

smartbotic-vectorapi-runner ships the runner binary, runner.json as its
own conffile, and a systemd unit. Type=notify, which the binary earns:
it sd_notify()s READY after the bind and pings WATCHDOG, so WatchdogSec
restarts a runner wedged by a pathological function.

Its OWN system user, not the server's. Sharing would mean a sandbox
escape inherits group-read on vectorapi.env - the bootstrap admin key and
the OpenAI key - plus write access to the server's data dir. The runner
has no state, no home and no ReadWritePaths at all, so ProtectSystem
leaves it a read-only filesystem with no credentials in reach.

No unit ordering against the server in either direction. A Before= would
make the API's Type=notify startup wait on the runner's database connect
loop at boot, and the API must come up and serve everything else with no
runner present; RunnerPool health-checks, so a late runner needs no
ordering.

Hardening beyond the server's, because this process runs untrusted code:
MemoryDenyWriteExecute (free - quickjs-ng is a bytecode interpreter with
no JIT - and it removes the usual path from an engine heap bug to native
code), IPAddressDeny=link-local as a kernel-level second layer behind
net_guard (the prize for an SSRF here is the droplet's metadata service),
ProtectProc=invisible, and cgroup backstops behind the in-engine limits.

runner.env is created by postinst and never shipped. A conffile would
give dpkg an md5sum of a secrets file, prompt on every upgrade once
edited, and reset the operator's mode. It is created 0640 via
install(1) so the mode is in place before anything is written, and an
existing file only has its mode tightened, never its contents touched.
Its header documents the SVAPI_FN_<project>__<NAME> scheme where the
operator actually types the secrets.

Four fixes the packaging work exposed outside its own files:

Dockerfile.build hardcoded its cmake line and declared no ARG, and an
undeclared --build-arg is silently dropped - so the release build would
have produced no runner at all.

server.postinst set /etc/smartbotic-vectorapi to 0750, denying the runner
user the +x to reach runner.json. The runner's postinst widens it, but a
SERVER upgrade would reset it and break the runner on its next restart -
a bug that only shows up on the second upgrade, weeks later, with no
obvious cause. Both scripts now use 0751 and point at each other.

control.server now Breaks an older runner, closing the half of the ABI
lockstep the runner package cannot express: the two binaries compile
against the same client pin, and every 2.x client shares one SONAME, so a
stale runner only segfaults on restart.

create-debs.sh warns and skips the runner package when the binary is
absent, matching BUILD_RUNNER defaulting OFF, rather than failing a
release that did not ask for a runner.

417/417 tests pass in the Debian 13 container with BUILD_RUNNER=ON. The
packages were built with dpkg-deb from a staged tree and inspected;
nothing was installed and nothing was published.
fszontagh 1 ماه پیش
والد
کامیت
676a13e1a6

+ 5 - 1
packaging/Dockerfile.build

@@ -2,9 +2,13 @@ ARG BASE_IMAGE=smartbotic-vectorapi-build-base:debian13
 FROM ${BASE_IMAGE} AS builder
 ARG BUILD_VERSION=0.1.0
 ARG BUILD_DEB_REVISION=1
+# The runner vendors QuickJS from GitHub at configure time; the build network
+# already reaches GitHub for cpp-httplib, so this adds no new requirement.
+ARG BUILD_RUNNER=ON
 WORKDIR /build
 COPY . /build/
-RUN cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_WEBUI=ON -DBUILD_TESTS=OFF -S . \
+RUN cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_WEBUI=ON -DBUILD_TESTS=OFF \
+        -DBUILD_RUNNER=${BUILD_RUNNER} -S . \
     && cmake --build build -j"$(nproc)"
 RUN OUTPUT_DIR=/packages BUILD_DEB_REVISION=${BUILD_DEB_REVISION} packaging/deb/create-debs.sh
 

+ 40 - 7
packaging/build.sh

@@ -46,6 +46,13 @@ DO_REPO=0
 SUITE=""
 DO_SYNC=0
 MAKE_DEBS=1
+# The function runner is part of a release: ON here, even though the CMake
+# option defaults to OFF (it vendors QuickJS from GitHub at configure time, and
+# the ordinary dev build must not start depending on a network fetch). The
+# release build already fetches cpp-httplib the same way, so the Docker build
+# demonstrably has network. --no-runner is the escape hatch for an offline or
+# runner-less build.
+BUILD_RUNNER=ON
 
 # ---------------------------------------------------------------------------
 # Colored logging
@@ -92,6 +99,9 @@ Build modes:
 
 Options:
   --install            Install .debs locally after build (--local, Debian host only)
+  --no-runner          Do not build the QuickJS function runner (skips the
+                       smartbotic-vectorapi-runner package; needed for a build
+                       with no network access to github.com)
   --skip-tests         Skip building and running the test suite (--local only)
   --deb-revision N     Debian revision suffix (default: 1)
   --rebuild-base       Force rebuild Docker base image
@@ -116,6 +126,7 @@ while [[ $# -gt 0 ]]; do
     case "$1" in
         --local)        LOCAL_MODE=1; shift ;;
         --install)      INSTALL_AFTER=1; shift ;;
+        --no-runner)    BUILD_RUNNER=OFF; shift ;;
         --skip-tests)   SKIP_TESTS=1; shift ;;
         --deb-revision) DEB_REVISION="$2"; shift 2 ;;
         --rebuild-base) REBUILD_BASE=1; shift ;;
@@ -201,11 +212,12 @@ build_local() {
         tests="OFF"
     fi
 
-    log_info "Configuring CMake (Release, BUILD_TESTS=${tests})..."
+    log_info "Configuring CMake (Release, BUILD_TESTS=${tests}, BUILD_RUNNER=${BUILD_RUNNER})..."
     cmake -B "$PROJECT_DIR/build" -G Ninja \
         -DCMAKE_BUILD_TYPE=Release \
         -DBUILD_WEBUI=ON \
         -DBUILD_TESTS="$tests" \
+        -DBUILD_RUNNER="$BUILD_RUNNER" \
         -S "$PROJECT_DIR"
 
     log_info "Building with ${BUILD_JOBS} jobs..."
@@ -225,15 +237,23 @@ build_local() {
     fi
 
     log_info "Creating .deb packages..."
-    OUTPUT_DIR="$OUTPUT_DIR" BUILD_DEB_REVISION="$DEB_REVISION" "$PROJECT_DIR/packaging/deb/create-debs.sh"
+    # SKIP_RUNNER_DEB keeps --no-runner from emitting a warning about a binary
+    # we deliberately did not build.
+    local skip_runner=0
+    [[ "$BUILD_RUNNER" == "ON" ]] || skip_runner=1
+    OUTPUT_DIR="$OUTPUT_DIR" BUILD_DEB_REVISION="$DEB_REVISION" \
+        SKIP_RUNNER_DEB="$skip_runner" "$PROJECT_DIR/packaging/deb/create-debs.sh"
     log_success "Packages created"
 
     if [[ $INSTALL_AFTER -eq 1 ]]; then
         log_info "Installing .deb packages..."
-        sudo dpkg -i \
-            "$OUTPUT_DIR"/smartbotic-vectorapi_*.deb \
-            "$OUTPUT_DIR"/smartbotic-vectorapi-webui_*.deb \
-            || sudo apt-get install -f -y
+        local debs=("$OUTPUT_DIR"/smartbotic-vectorapi_*.deb "$OUTPUT_DIR"/smartbotic-vectorapi-webui_*.deb)
+        # Installed in the same dpkg run as the server when present: the runner
+        # Breaks older server versions, so they have to be configured together.
+        if compgen -G "$OUTPUT_DIR/smartbotic-vectorapi-runner_*.deb" >/dev/null; then
+            debs+=("$OUTPUT_DIR"/smartbotic-vectorapi-runner_*.deb)
+        fi
+        sudo dpkg -i "${debs[@]}" || sudo apt-get install -f -y
         log_success "Packages installed"
     fi
 }
@@ -305,7 +325,19 @@ build_docker() {
     fi
 
     # Build packages
-    log_info "Building packages in Docker..."
+    log_info "Building packages in Docker (BUILD_RUNNER=${BUILD_RUNNER})..."
+
+    # BUILD_RUNNER has to be consumed by Dockerfile.build's cmake invocation -
+    # the flags live in the Dockerfile, not here. If that ARG is not declared
+    # there, buildx silently ignores the build-arg and the runner package is
+    # simply missing from the output, which is the kind of omission nobody
+    # notices until functions stop working in production. So say so, loudly.
+    if [[ "$BUILD_RUNNER" == "ON" ]] \
+        && ! grep -q 'BUILD_RUNNER' "$PROJECT_DIR/packaging/Dockerfile.build"; then
+        log_warn "packaging/Dockerfile.build does not consume BUILD_RUNNER:"
+        log_warn "  it needs 'ARG BUILD_RUNNER=ON' and -DBUILD_RUNNER=\${BUILD_RUNNER} on its"
+        log_warn "  cmake line. Until then this build produces NO smartbotic-vectorapi-runner deb."
+    fi
 
     local build_cache_args=()
     if [[ $NO_CACHE -eq 1 ]]; then
@@ -319,6 +351,7 @@ build_docker() {
         --build-arg "BUILD_DEB_REVISION=${DEB_REVISION}" \
         --build-arg "BUILD_JOBS=${BUILD_JOBS}" \
         --build-arg "BUILD_GIT_COMMIT=${GIT_COMMIT}" \
+        --build-arg "BUILD_RUNNER=${BUILD_RUNNER}" \
         --target packages \
         --output "type=local,dest=$OUTPUT_DIR" \
         "${build_cache_args[@]+"${build_cache_args[@]}"}" \

+ 36 - 1
packaging/deb/create-debs.sh

@@ -1,5 +1,5 @@
 #!/usr/bin/env bash
-# create-debs.sh — build the smartbotic-vectorapi + -webui .deb packages
+# create-debs.sh - build the smartbotic-vectorapi + -webui + -runner .deb packages
 set -euo pipefail
 
 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -12,6 +12,7 @@ TEMPLATE_DIR="$SCRIPT_DIR/templates"
 SCRIPTS_DIR="$SCRIPT_DIR/scripts"
 SYSTEMD_DIR="$SCRIPT_DIR/systemd"
 CONFIG_DIR="$SOURCE_DIR/config"
+RUNNER_CONFIG_DIR="$SOURCE_DIR/runner/config"
 API_DIR="$SOURCE_DIR/api"
 WEBUI_DIST="${WEBUI_DIST:-$BUILD_DIR/webui/dist}"
 
@@ -92,5 +93,39 @@ fi
 cp "$TEMPLATE_DIR/control.webui" "$PKG2/DEBIAN/control"
 build_deb "smartbotic-vectorapi-webui" "$PKG2" "all"
 
+# ---- Package 3: smartbotic-vectorapi-runner (function runner) ----
+# The runner is an opt-in build (-DBUILD_RUNNER=ON; it vendors QuickJS over the
+# network at configure time), so a missing binary is a configuration choice, not
+# a broken build: warn and skip rather than fail the whole package set, the same
+# posture as the OPTIONAL install() of runner.json in the root CMakeLists.
+# SKIP_RUNNER_DEB=1 silences the warning when the omission is deliberate.
+RUNNER_BIN="$BUILD_DIR/runner/smartbotic-vectorapi-runner"
+if [ "${SKIP_RUNNER_DEB:-0}" = "1" ]; then
+    echo "--- smartbotic-vectorapi-runner: SKIPPED (SKIP_RUNNER_DEB=1) ---"
+elif [ ! -f "$RUNNER_BIN" ]; then
+    echo "--- smartbotic-vectorapi-runner: SKIPPED ---"
+    echo "WARNING: $RUNNER_BIN not found; configure with -DBUILD_RUNNER=ON to ship it" >&2
+else
+echo "--- smartbotic-vectorapi-runner ---"
+PKG3="$(mktemp -d)"; trap 'rm -rf "$PKG1" "$PKG2" "$PKG3"' EXIT
+mkdir -p "$PKG3/DEBIAN" "$PKG3/usr/bin" "$PKG3/etc/smartbotic-vectorapi" \
+         "$PKG3/lib/systemd/system"
+
+cp "$RUNNER_BIN" "$PKG3/usr/bin/smartbotic-vectorapi-runner"
+chmod 0755 "$PKG3/usr/bin/smartbotic-vectorapi-runner"
+# runner.json only. config.json belongs to the server package and must not be
+# claimed twice: two packages shipping one conffile is a dpkg overwrite error.
+cp "$RUNNER_CONFIG_DIR/runner.json" "$PKG3/etc/smartbotic-vectorapi/runner.json"
+cp "$SYSTEMD_DIR/smartbotic-vectorapi-runner.service" "$PKG3/lib/systemd/system/"
+cp "$TEMPLATE_DIR/control.runner" "$PKG3/DEBIAN/control"
+cp "$SCRIPTS_DIR/runner.postinst" "$PKG3/DEBIAN/postinst"
+cp "$SCRIPTS_DIR/runner.prerm"    "$PKG3/DEBIAN/prerm"
+cp "$SCRIPTS_DIR/runner.postrm"   "$PKG3/DEBIAN/postrm"
+# runner.env is NOT here on purpose: postinst creates it 0640 if absent, so an
+# operator's function secrets are never a dpkg-tracked conffile.
+printf '/etc/smartbotic-vectorapi/runner.json\n' > "$PKG3/DEBIAN/conffiles"
+build_deb "smartbotic-vectorapi-runner" "$PKG3" "amd64"
+fi
+
 echo "=== Done. Packages in $OUTPUT_DIR ==="
 ls -lh "$OUTPUT_DIR"/*.deb

+ 96 - 0
packaging/deb/scripts/runner.postinst

@@ -0,0 +1,96 @@
+#!/bin/bash
+# postinst for smartbotic-vectorapi-runner
+set -e
+
+CONFIG_DIR="/etc/smartbotic-vectorapi"
+CONFIG_FILE="$CONFIG_DIR/runner.json"
+ENV_FILE="$CONFIG_DIR/runner.env"
+# Its OWN user, not the server's. The runner executes untrusted tenant
+# JavaScript; running it as smartbotic-vectorapi would mean a sandbox escape
+# inherits read access to that user's vectorapi.env - the bootstrap admin API
+# key and the OpenAI key - plus write access to /var/lib/smartbotic-vectorapi.
+# A separate unprivileged account with no state of its own is the whole point.
+RUN_USER="smartbotic-vectorapi-runner"
+
+case "$1" in
+    configure)
+        if ! getent group "$RUN_USER" >/dev/null 2>&1; then
+            addgroup --system "$RUN_USER"
+        fi
+        if ! getent passwd "$RUN_USER" >/dev/null 2>&1; then
+            # --no-create-home/--home /nonexistent: the service keeps no state
+            # on disk (in-memory engine pool and bytecode cache only) and its
+            # unit grants no writable path at all.
+            adduser --system --ingroup "$RUN_USER" --no-create-home --home /nonexistent \
+                --shell /usr/sbin/nologin --gecos "Smartbotic VectorAPI Function Runner" "$RUN_USER"
+        fi
+
+        # The config dir is shared with the server package, which owns it as
+        # root:smartbotic-vectorapi 0750. 0750 denies our user the +x it needs
+        # to reach runner.json at all, so widen it to 0751: traversal for
+        # others, still no listing, and the server's own files stay 0640 and
+        # therefore unreadable to us. Group ownership is left to whichever
+        # package created the dir.
+        CONFIG_GROUP=root
+        if getent group smartbotic-vectorapi >/dev/null 2>&1; then
+            CONFIG_GROUP=smartbotic-vectorapi
+        fi
+        install -d -o root -g "$CONFIG_GROUP" -m 0751 "$CONFIG_DIR"
+
+        if [ -f "$CONFIG_FILE" ]; then
+            chown root:"$RUN_USER" "$CONFIG_FILE"
+            chmod 640 "$CONFIG_FILE"
+        fi
+
+        # Function secrets (ctx.env). Created empty on first install and never
+        # shipped in the package: a conffile here would give dpkg an md5sum of
+        # a secrets file, prompt on every upgrade an operator has edited it,
+        # and reset the mode the operator chose. Ours to create, never ours to
+        # overwrite.
+        if [ ! -e "$ENV_FILE" ]; then
+            install -o root -g "$RUN_USER" -m 0640 /dev/null "$ENV_FILE"
+            cat > "$ENV_FILE" <<'EOF'
+# Environment for smartbotic-vectorapi-runner (systemd EnvironmentFile).
+# One KEY=value per line, no shell quoting. Not a conffile: package upgrades
+# never touch this file. Keep it 0640 root:smartbotic-vectorapi-runner.
+#
+# The shared token vectorapi authenticates with. REQUIRED unless the runner is
+# bound to 127.0.0.1 - it refuses to start without one on any other address,
+# because an open runner executes arbitrary JavaScript against the database.
+# The same value goes in the server's vectorapi.env.
+#SVAPI_RUNNER_TOKEN=
+#
+# Function secrets, reaching a script as ctx.env.<NAME>:
+#
+#   SVAPI_FN_<project>__<NAME>=value
+#
+# The project name is part of the variable, and only that project's functions
+# can read it - one runner serves every project, so a flat namespace would hand
+# every tenant's secret to every script. Exactly two underscores separate the
+# project from the name. Nothing without the SVAPI_FN_ prefix is ever exposed,
+# so the token above stays invisible to functions.
+#
+#   SVAPI_FN_acme__OPENAI_KEY=sk-...        ->  ctx.env.OPENAI_KEY in acme
+#   SVAPI_FN_novitax__WEBHOOK_URL=https://  ->  ctx.env.WEBHOOK_URL in novitax
+EOF
+            chown root:"$RUN_USER" "$ENV_FILE"
+            chmod 640 "$ENV_FILE"
+        else
+            # Never rewrite the contents, but do make sure an earlier mistake
+            # has not left the operator's secrets world-readable.
+            chmod go-w,o-rwx "$ENV_FILE" || true
+        fi
+
+        systemctl daemon-reload || true
+        if [ -z "$2" ]; then
+            systemctl enable smartbotic-vectorapi-runner.service || true
+            systemctl start smartbotic-vectorapi-runner.service || true
+        else
+            systemctl reset-failed smartbotic-vectorapi-runner.service 2>/dev/null || true
+            systemctl restart smartbotic-vectorapi-runner.service || true
+        fi
+        ;;
+    abort-upgrade|abort-remove|abort-deconfigure) ;;
+    *) echo "postinst called with unknown argument '$1'" >&2; exit 1 ;;
+esac
+exit 0

+ 20 - 0
packaging/deb/scripts/runner.postrm

@@ -0,0 +1,20 @@
+#!/bin/bash
+set -e
+case "$1" in
+    purge)
+        # runner.env is created by our postinst, not unpacked by dpkg, so dpkg
+        # will not remove it: purge means purge, secrets included.
+        rm -f /etc/smartbotic-vectorapi/runner.env
+        if getent passwd smartbotic-vectorapi-runner >/dev/null 2>&1; then
+            deluser --system smartbotic-vectorapi-runner 2>/dev/null || true
+        fi
+        if getent group smartbotic-vectorapi-runner >/dev/null 2>&1; then
+            delgroup --system smartbotic-vectorapi-runner 2>/dev/null || true
+        fi
+        # /etc/smartbotic-vectorapi itself is left alone: the server package
+        # shares that directory and still keeps config.json in it.
+        ;;
+    remove|upgrade|disappear|failed-upgrade|abort-install|abort-upgrade) ;;
+esac
+systemctl daemon-reload 2>/dev/null || true
+exit 0

+ 10 - 0
packaging/deb/scripts/runner.prerm

@@ -0,0 +1,10 @@
+#!/bin/bash
+set -e
+case "$1" in
+    remove|deconfigure)
+        systemctl stop smartbotic-vectorapi-runner.service 2>/dev/null || true
+        systemctl disable smartbotic-vectorapi-runner.service 2>/dev/null || true
+        ;;
+    upgrade|failed-upgrade) ;;
+esac
+exit 0

+ 6 - 1
packaging/deb/scripts/server.postinst

@@ -16,7 +16,12 @@ case "$1" in
         fi
 
         install -d -o smartbotic-vectorapi -g smartbotic-vectorapi -m 0750 "$DATA_DIR"
-        install -d -o root -g smartbotic-vectorapi -m 0750 /etc/smartbotic-vectorapi
+        # 0751, not 0750: the runner runs as its own user and has to traverse
+        # this directory to reach runner.json. Traversal only - it still cannot
+        # list the directory, and the server's own 0640 files (vectorapi.env
+        # holds the bootstrap admin key) stay unreadable to it. Keep this in
+        # step with runner.postinst, which widens it for the same reason.
+        install -d -o root -g smartbotic-vectorapi -m 0751 /etc/smartbotic-vectorapi
         if [ -f "$CONFIG_FILE" ]; then
             chown root:smartbotic-vectorapi "$CONFIG_FILE"
             chmod 640 "$CONFIG_FILE"

+ 118 - 0
packaging/deb/systemd/smartbotic-vectorapi-runner.service

@@ -0,0 +1,118 @@
+[Unit]
+Description=Smartbotic VectorAPI Function Runner
+Documentation=https://git.smartbotics.ai/fszontagh/smartbotic-vectorapi
+# Same DB ordering as the server, and for the same reason: After= only, never
+# Requires=. The database may legitimately live on another host, and the binary
+# retries the connection for 30s before giving up, which Restart=on-failure
+# then turns into a slow retry loop instead of a dead service.
+After=network-online.target smartbotic-database.service
+Wants=network-online.target
+# Deliberately NO ordering against smartbotic-vectorapi.service in either
+# direction. The API must come up and serve everything else with no runner
+# present (it only refuses to STORE a function while no runner is healthy), and
+# a Before= here would make the API's Type=notify start wait on our database
+# connect loop at boot. RunnerPool health-checks us and re-checks after a
+# cooldown, so a runner that arrives late or restarts needs no unit ordering.
+
+[Service]
+# The binary calls sd_notify(READY=1) once the HTTP listener thread is up and
+# pings WATCHDOG=1 every 200ms (when built with libsystemd - the Debian build
+# base installs libsystemd-dev, so it is). If ever built without it, sd_notify
+# is a no-op macro and this must become Type=exec.
+Type=notify
+NotifyAccess=main
+# Comfortably above the 200ms ping interval: this exists to catch a runner
+# wedged by a pathological function, not to police scheduling jitter.
+WatchdogSec=30
+User=smartbotic-vectorapi-runner
+Group=smartbotic-vectorapi-runner
+ExecStart=/usr/bin/smartbotic-vectorapi-runner --config /etc/smartbotic-vectorapi/runner.json
+Restart=on-failure
+RestartSec=3
+# No state of its own: bytecode cache and engine pool are in-memory, and it
+# must not be able to write to the API's data dir. Hence "/" and no
+# ReadWritePaths at all - see the hardening block.
+WorkingDirectory=/
+LimitNOFILE=65536
+UMask=0077
+
+# Per-project function secrets (ctx.env) land here. NOT shipped in the .deb and
+# NOT a conffile: postinst creates an empty 0640 file if none exists, so dpkg
+# has no md5sum for it and an upgrade can never prompt about, reset the mode of,
+# or replace an operator's secrets. The leading "-" keeps us starting if an
+# operator deletes it outright.
+EnvironmentFile=-/etc/smartbotic-vectorapi/runner.env
+
+# ---------------------------------------------------------------------------
+# Security hardening
+#
+# Mirrors smartbotic-vectorapi.service, then goes further, because this process
+# executes untrusted tenant-authored JavaScript. Everything below the first
+# block is defense in depth for one scenario only: a bug in QuickJS or in the
+# ctx bindings lets a function reach native execution. In that case the JS
+# sandbox is already lost, and these are what is left.
+# ---------------------------------------------------------------------------
+NoNewPrivileges=true
+ProtectSystem=strict
+ProtectHome=true
+PrivateTmp=true
+PrivateDevices=true
+ProtectKernelTunables=true
+ProtectKernelModules=true
+ProtectControlGroups=true
+RestrictRealtime=true
+RestrictSUIDSGID=true
+RestrictNamespaces=true
+LockPersonality=true
+SystemCallFilter=@system-service
+SystemCallErrorNumber=EPERM
+
+# No ReadWritePaths: unlike the server there is no /var/lib to grant, so
+# ProtectSystem=strict leaves the whole filesystem read-only. An escaped
+# function therefore has nowhere to drop a payload, and in particular no write
+# access to the API's data dir.
+
+# QuickJS (quickjs-ng) is a bytecode interpreter - it never generates native
+# code - so denying writable-executable memory costs nothing and removes the
+# standard way a heap corruption bug in an engine becomes code execution.
+# Drop this ONLY if a future dependency genuinely needs W+X (a JIT would).
+MemoryDenyWriteExecute=true
+
+# The runner needs IP for the database (gRPC) and for ctx.fetch/embeddings; it
+# has no business with netlink, packet sockets or anything exotic. AF_UNIX
+# stays for the systemd notify socket and local resolvers.
+RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
+SystemCallArchitectures=native
+
+# Defense in depth behind functions/net_guard.hpp, which already refuses
+# loopback/RFC1918/link-local destinations in ctx.fetch. The prize for an SSRF
+# here is the cloud metadata service (169.254.169.254 on the production
+# droplet), i.e. the host's credentials, so it is worth blocking a second time
+# at the cgroup. NOTE: a host whose DNS resolver is itself link-local (some
+# clouds use 169.254.169.253) needs that address added to IPAddressAllow.
+IPAddressDeny=link-local multicast
+
+# A function must not be able to read the process table, the kernel log or the
+# host identity even after an escape - those are reconnaissance for the next
+# step, and nothing the runner itself needs.
+ProtectProc=invisible
+ProcSubset=pid
+ProtectClock=true
+ProtectHostname=true
+ProtectKernelLogs=true
+RemoveIPC=true
+
+# Backstops, not the primary limits: per-run memory/stack/wall-time ceilings are
+# enforced inside the engine (runner.json "runtime"). These only catch a bug in
+# that enforcement. MemoryMax must stay well above pool_size *
+# memory_limit_bytes (4 * 128 MiB by default) - raise both together, or a legit
+# pool growth turns into a cgroup OOM kill that takes every tenant's run with
+# it. CPUWeight rather than CPUQuota on purpose: a busy loop is already bounded
+# by the wall-time ceiling, so we only want the API to win the CPU, not to
+# throttle runs into failing.
+MemoryMax=1G
+TasksMax=128
+CPUWeight=50
+
+[Install]
+WantedBy=multi-user.target

+ 18 - 0
packaging/deb/templates/control.runner

@@ -0,0 +1,18 @@
+Package: smartbotic-vectorapi-runner
+Version: {{VERSION}}
+Architecture: amd64
+Maintainer: Ferenc Szontagh <ferenc.szontagh@smartbotics.ai>
+Description: Smartbotic VectorAPI - server-side function runner
+ Executes project-defined JavaScript functions for smartbotic-vectorapi in a
+ separate, sandboxed process: an embedded QuickJS engine with per-run memory,
+ stack and wall-time ceilings, a `ctx` surface granting database access through
+ the caller's own key scopes, and SSRF-guarded outbound fetch. Speaks the
+ /run, /validate and /health protocol on loopback; smartbotic-vectorapi is the
+ only intended client. Installable on its own host.
+Homepage: https://smartbotics.ai
+Section: contrib/utils
+Priority: optional
+Depends: libsmartbotic-db-client (>= 2.11.4), libssl3t64, libsystemd0, libspdlog1.15
+Recommends: smartbotic-vectorapi (= {{VERSION}})
+Breaks: smartbotic-vectorapi (<< {{UPSTREAM_VERSION}})
+Installed-Size: {{INSTALLED_SIZE}}

+ 4 - 0
packaging/deb/templates/control.server

@@ -12,4 +12,8 @@ Section: contrib/utils
 Priority: optional
 Depends: libsmartbotic-db-client (>= 2.3.0), libssl3t64, libsystemd0, libspdlog1.15
 Recommends: smartbotic-vectorapi-webui (= {{VERSION}})
+# The runner links the same libsmartbotic-db-client and is compiled against the
+# same pin, so the two binaries must move together: every 2.x client shares one
+# SONAME, and a stale runner against a new server only segfaults on restart.
+Breaks: smartbotic-vectorapi-runner (<< {{UPSTREAM_VERSION}})
 Installed-Size: {{INSTALLED_SIZE}}