#!/bin/bash # Checks that webgui/api.php rejects state-changing requests without the # Unraid CSRF token. Runs api.php through the PHP CLI, which fills $_SERVER # from the environment. A request that passes the check reaches the daemon # socket test and fails with "Daemon läuft nicht" instead. set -euo pipefail if ! command -v php >/dev/null 2>&1; then echo "api_csrf_test: php not installed, skipped" exit 0 fi ROOT=$(cd "$(dirname "$0")/.." && pwd) WORK=$(mktemp -d) trap 'rm -rf "$WORK"' EXIT printf 'csrf_token="secret-token"\n' > "$WORK/var.ini" call() { env -i PATH="$PATH" URBM_VAR_INI="${VAR_INI:-$WORK/var.ini}" "$@" php "$ROOT/webgui/api.php" 2>/dev/null } fail=0 expect() { local name=$1 want=$2 got=$3 if [[ "$got" != *"$want"* ]]; then echo "FAIL: $name: expected '$want', got: $got" fail=1 fi } expect "GET needs no token" "Daemon" "$(call REQUEST_METHOD=GET)" expect "POST without token" "CSRF" "$(call REQUEST_METHOD=POST)" expect "POST with wrong token" "CSRF" "$(call REQUEST_METHOD=POST HTTP_X_CSRF_TOKEN=wrong)" expect "PUT without token" "CSRF" "$(call REQUEST_METHOD=PUT)" expect "DELETE without token" "CSRF" "$(call REQUEST_METHOD=DELETE)" expect "lowercase post is checked" "CSRF" "$(call REQUEST_METHOD=post)" expect "POST with valid token" "Daemon" "$(call REQUEST_METHOD=POST HTTP_X_CSRF_TOKEN=secret-token)" expect "PUT with valid token" "Daemon" "$(call REQUEST_METHOD=PUT HTTP_X_CSRF_TOKEN=secret-token)" expect "unknown method" "method not allowed" "$(call REQUEST_METHOD=PATCH HTTP_X_CSRF_TOKEN=secret-token)" expect "missing var.ini fails closed" "CSRF" "$(VAR_INI=$WORK/missing.ini call REQUEST_METHOD=POST HTTP_X_CSRF_TOKEN=)" if [[ $fail -ne 0 ]]; then exit 1 fi echo "api_csrf_test: ok"