LiteLLM hat Guardrails als Katalog fertiger Dienste. Für diesen Router wäre das die
falsche Form: welche Inhalte durchgehen, weiß der Betreiber, nicht das Binary. Also
gibt es einen Haken und keine Meinung:
guard request cmd "/usr/local/share/junction/hooks/guard.sh"
Das Skript bekommt JUNCTION_PATH, JUNCTION_MODEL, JUNCTION_VKEY, JUNCTION_BODY_FILE
und JUNCTION_BODY_BYTES. Exit 0 lässt durch, alles andere lehnt mit 403 guard_refused
ab, und stdout wird die Begründung — im Rumpf und im Log. Schweigt das Skript, nennt
die Ablehnung seinen Exit-Code (guard_exit_3), statt eine leere Begründung zu erfinden.
Drei Entscheidungen, die Absicht sind:
- Der Rumpf liegt in einer Datei, nicht auf stdin. Ein Skript, das nur die erste Zeile
liest, hinge sonst beim Schreiben in eine volle Pipe — eine Datei kann nicht deadlocken.
- Der Wächter läuft VOR dem Routing: eine abgelehnte Anfrage belegt keinen Endpunkt,
keinen Warteschlangenplatz und kein Budget.
- Nur die Anfrage wird geprüft. Eine Antwort zu prüfen hieße, jeden Stream zu puffern —
genau das, was dieser Router nicht tut. Das steht so im README, statt es zu verschweigen.
Sichtbar: guard_pass/guard_refused im Log je Anfrage, guard_refused in /status,
junction_guard_refused_total in /metrics.
Tests (tests/guard.sh, Abschnitt 30): ein Skript, das ein Wort ablehnt (Begründung im
Rumpf, Zähler in /metrics, Logzeile), eines, das stumm mit 3 endet (Exit-Code wird zur
Begründung), und eine Anfrage, die durchgeht und als guard_pass geloggt wird.
make test: PASS, 186 Prüfungen. Live: Binary installiert, Dienst neu gestartet, kein
Wächter eingetragen — es gibt keine Regel von Konrad, und eine zu erfinden wäre genau
das, was dieses Feature nicht tut.
78 lines
2.8 KiB
Bash
78 lines
2.8 KiB
Bash
# Wächter vor dem Weiterleiten (guard request) — sourced von tests/run.sh.
|
|
# Erwartet PORT, TMP, PIDS, P_GATED, contains, log_has, ok, bad, wait_port aus run.sh.
|
|
|
|
echo "== 30. Wächter vor dem Weiterleiten =="
|
|
|
|
# junction bringt keine Inhaltsregel mit: was durchgeht, entscheidet das Skript.
|
|
# Der Rumpf liegt in einer Datei (kein stdin — ein Skript, das nur einen Teil liest,
|
|
# würde sonst beim Schreiben in eine volle Pipe hängen).
|
|
cat > "$TMP/guard.sh" <<'EOS'
|
|
#!/bin/sh
|
|
if grep -q VERBOTEN "$JUNCTION_BODY_FILE" 2>/dev/null; then
|
|
echo "verbotenes wort im rumpf"
|
|
exit 1
|
|
fi
|
|
exit 0
|
|
EOS
|
|
chmod +x "$TMP/guard.sh"
|
|
|
|
P_G=$((PORT+45))
|
|
cat > "$TMP/guard.conf" <<CFG
|
|
listen 127.0.0.1:$P_G
|
|
guard request cmd $TMP/guard.sh
|
|
endpoint gated {
|
|
base http://127.0.0.1:$P_GATED/v1
|
|
context 64000
|
|
timeout 10
|
|
idle_timeout 5
|
|
}
|
|
req for "*" check none pass gated
|
|
CFG
|
|
./junction --config "$TMP/guard.conf" > "$TMP/guard.log" 2>&1 &
|
|
PIDS+=($!)
|
|
wait_port $P_G 10
|
|
|
|
R1=$(curl -s -m 15 -X POST -H 'Content-Type: application/json' \
|
|
-d '{"model":"m","messages":[{"role":"user","content":"das ist VERBOTEN"}]}' \
|
|
"http://127.0.0.1:$P_G/v1/chat/completions")
|
|
contains "$R1" "guard_refused" "the guard refuses a request it does not want"
|
|
contains "$R1" "verbotenes wort im rumpf" "the refusal carries the script's reason"
|
|
log_has "$TMP/guard.log" "guard_refused path=/v1/chat/completions" "the refusal is logged"
|
|
|
|
# Ein Skript ohne Ausgabe wird nicht als "abgelehnt, weil leer" gemeldet.
|
|
cat > "$TMP/guard-silent.sh" <<'EOS'
|
|
#!/bin/sh
|
|
exit 3
|
|
EOS
|
|
chmod +x "$TMP/guard-silent.sh"
|
|
P_G2=$((PORT+46))
|
|
cat > "$TMP/guard2.conf" <<CFG
|
|
listen 127.0.0.1:$P_G2
|
|
guard request cmd $TMP/guard-silent.sh
|
|
endpoint gated {
|
|
base http://127.0.0.1:$P_GATED/v1
|
|
context 64000
|
|
timeout 10
|
|
}
|
|
req for "*" check none pass gated
|
|
CFG
|
|
./junction --config "$TMP/guard2.conf" > "$TMP/guard2.log" 2>&1 &
|
|
PIDS+=($!)
|
|
wait_port $P_G2 10
|
|
R2=$(curl -s -m 15 -X POST -H 'Content-Type: application/json' \
|
|
-d '{"model":"m","messages":[{"role":"user","content":"harmlos"}]}' \
|
|
"http://127.0.0.1:$P_G2/v1/chat/completions")
|
|
contains "$R2" "guard_exit_3" "silence is not a reason: the exit code is named"
|
|
|
|
# Und was durchgeht, geht durch — und wird als solches geloggt.
|
|
R3=$(curl -s -m 15 -X POST -H 'Content-Type: application/json' \
|
|
-d '{"model":"m","messages":[{"role":"user","content":"harmlos"}]}' \
|
|
"http://127.0.0.1:$P_G/v1/chat/completions")
|
|
contains "$R3" "served by GATED" "a request the guard allows is served"
|
|
log_has "$TMP/guard.log" "guard_pass path=/v1/chat/completions" "the passed request is logged"
|
|
|
|
M=$(curl -s -m 5 "http://127.0.0.1:$P_G/metrics")
|
|
contains "$M" "junction_guard_refused_total 1" "the refusal is counted in /metrics"
|
|
S=$(curl -s -m 5 "http://127.0.0.1:$P_G/status")
|
|
contains "$S" '"guard_refused":1' "/status counts the refusal"
|