# 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" < "$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" < "$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"