#!/usr/bin/env bash # ============================================================================== # ClassNotify P1 Batch 4 Linux VPS Security Verification Script # Executed directly on VPS to test live PostgreSQL, Kestrel, JWT, CORS, Logs # ============================================================================== set -euo pipefail PASS_COUNT=0 FAIL_COUNT=0 report_result() { local test_name="$1" local status="$2" local details="${3:-}" if [ "$status" -eq 0 ]; then echo -e "\e[32m[PASS]\e[0m $test_name" [ -n "$details" ] && echo -e " \e[90m$details\e[0m" PASS_COUNT=$((PASS_COUNT + 1)) else echo -e "\e[31m[FAIL]\e[0m $test_name" [ -n "$details" ] && echo -e " \e[33m$details\e[0m" FAIL_COUNT=$((FAIL_COUNT + 1)) fi } echo "==========================================================" echo " ClassNotify P1 Batch 4 VPS Security Verification " echo "==========================================================" # 1. Environment & Permissions echo "" echo "--- [1/6] Environment & Secret Zero-Leakage ---" ENV_FILE="/etc/classnotify/classnotify.env" PERMS=$(stat -c "%a %U:%G" "$ENV_FILE") if [ "$PERMS" = "600 root:root" ]; then report_result "classnotify.env permissions strictly 600 root:root" 0 "Permissions: $PERMS" else report_result "classnotify.env permissions strictly 600 root:root" 1 "Actual: $PERMS" fi HAS_KEYS=0 grep -q "^Jwt__SigningKey=" "$ENV_FILE" && \ grep -q "^Jwt__Issuer=" "$ENV_FILE" && \ grep -q "^Jwt__Audience=" "$ENV_FILE" && \ grep -q "^Jwt__LifetimeDays=" "$ENV_FILE" && HAS_KEYS=1 if [ "$HAS_KEYS" -eq 1 ]; then report_result "All required JWT configuration keys present in env" 0 else report_result "All required JWT configuration keys present in env" 1 fi # 2. Live Password Verification & Automatic Rehash echo "" echo "--- [2/6] PasswordHasher & Auto-Rehash ---" LOGIN_JSON="{\"username\":\"teacher_zhang\",\"password\":\"${CLASSNOTIFY_E2E_PASSWORD}\"}" LOGIN_RESP=$(curl -s -X POST http://127.0.0.1:5000/api/auth/login -H "Content-Type: application/json" -d "$LOGIN_JSON") TOKEN=$(echo "$LOGIN_RESP" | python3 -c "import sys, json; print(json.load(sys.stdin).get('token') or '')") ROLE=$(echo "$LOGIN_RESP" | python3 -c "import sys, json; print(json.load(sys.stdin).get('role') or '')") if [ -n "$TOKEN" ] && [ "$ROLE" = "Teacher" ]; then report_result "Teacher login successful via API" 0 "Role: $ROLE, Token received" else report_result "Teacher login successful via API" 1 "Response: $LOGIN_RESP" fi # Check PostgreSQL for updated hash DB_HASH=$(sudo -u postgres psql -d classnotify -t -A -c "SELECT \"PasswordHash\" FROM \"Users\" WHERE \"Username\" = 'teacher_zhang';") HASH_LEN=${#DB_HASH} HASH_PREFIX="${DB_HASH:0:6}" if [ "$HASH_PREFIX" = "AQAAAA" ] && [ "$HASH_LEN" -ge 80 ]; then report_result "PostgreSQL PasswordHash migrated to modern format (AQAAAA...)" 0 "Length: $HASH_LEN chars, Prefix: $HASH_PREFIX" else report_result "PostgreSQL PasswordHash migrated to modern format (AQAAAA...)" 1 "Prefix: $HASH_PREFIX, Length: $HASH_LEN" fi # Wrong password rejection WRONG_RESP_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X POST http://127.0.0.1:5000/api/auth/login -H "Content-Type: application/json" -d '{"username":"teacher_zhang","password":"WrongPassword999!"}') if [ "$WRONG_RESP_CODE" = "401" ]; then report_result "Wrong password rejected with 401 Unauthorized" 0 else report_result "Wrong password rejected with 401 Unauthorized" 1 "Received HTTP $WRONG_RESP_CODE" fi # 3. JWT Strict Verification echo "" echo "--- [3/6] JWT Validation & Rotation ---" # Valid token ME_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X GET http://127.0.0.1:5000/api/auth/me -H "Authorization: Bearer $TOKEN") if [ "$ME_CODE" = "200" ]; then report_result "Valid JWT authorized on /api/auth/me (200 OK)" 0 else report_result "Valid JWT authorized on /api/auth/me (200 OK)" 1 "Received HTTP $ME_CODE" fi # Tampered token TAMPERED_TOKEN="${TOKEN:0:25}X${TOKEN:26}" TAMPERED_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X GET http://127.0.0.1:5000/api/auth/me -H "Authorization: Bearer $TAMPERED_TOKEN") if [ "$TAMPERED_CODE" = "401" ]; then report_result "Tampered JWT rejected with 401 Unauthorized" 0 else report_result "Tampered JWT rejected with 401 Unauthorized" 1 "Received HTTP $TAMPERED_CODE" fi # Old / Invalid key token OLD_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiJ1c3JfMTAxIiwidW5pcXVlX25hbWUiOiLlvKDogIHluIgiLCJyb2xlIjoiVGVhY2hlciIsInRlbmFudF9pZCI6InRfYmVpamluZ19ubzQiLCJuYmYiOjE3ODk1MDAwMDAsImV4cCI6MTc5MTAwMDAwMCwiaWF0IjoxNzg5NTAwMDAwLCJpc3MiOiJDbGFzc05vdGlmeS5TZXJ2ZXIiLCJhdWQiOiJDbGFzc05vdGlmeS5DbGllbnRzIn0.OldInvalidSignatureOldInvalidSignature000" OLD_CODE=$(curl -s -o /dev/null -w "%{http_code}" -X GET http://127.0.0.1:5000/api/auth/me -H "Authorization: Bearer $OLD_TOKEN") if [ "$OLD_CODE" = "401" ]; then report_result "Rotated / invalid key JWT rejected with 401 Unauthorized" 0 else report_result "Rotated / invalid key JWT rejected with 401 Unauthorized" 1 "Received HTTP $OLD_CODE" fi # 4. Production CORS Policy echo "" echo "--- [4/6] Production CORS Restrictions ---" # Allowed origin CORS_PROD=$(curl -s -i -X OPTIONS http://127.0.0.1:5000/api/auth/login \ -H "Origin: https://classnotify.paradise.eu.org" \ -H "Access-Control-Request-Method: POST") if echo "$CORS_PROD" | grep -q "Access-Control-Allow-Origin: https://classnotify.paradise.eu.org"; then report_result "CORS allows production origin https://classnotify.paradise.eu.org" 0 else report_result "CORS allows production origin https://classnotify.paradise.eu.org" 1 fi # Disallowed external origin CORS_EVIL=$(curl -s -i -X OPTIONS http://127.0.0.1:5000/api/auth/login \ -H "Origin: https://evil.example.com" \ -H "Access-Control-Request-Method: POST") if echo "$CORS_EVIL" | grep -q "Access-Control-Allow-Origin"; then report_result "CORS blocks untrusted origin (evil.example.com)" 1 else report_result "CORS blocks untrusted origin (evil.example.com)" 0 fi # Disallowed localhost in production CORS_LOCAL=$(curl -s -i -X OPTIONS http://127.0.0.1:5000/api/auth/login \ -H "Origin: http://localhost:5173" \ -H "Access-Control-Request-Method: POST") if echo "$CORS_LOCAL" | grep -q "Access-Control-Allow-Origin"; then report_result "CORS blocks localhost in Production" 1 else report_result "CORS blocks localhost in Production" 0 fi # 5. Rate Limiter Partitioning echo "" echo "--- [5/6] Rate Limiter Partitioning ---" # Verify RateLimiter middleware configured and responding RL_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -X GET http://127.0.0.1:5000/api/health) if [ "$RL_STATUS" = "200" ]; then report_result "Server rate limiting middleware active and healthy" 0 else report_result "Server rate limiting middleware active and healthy" 1 "Health HTTP $RL_STATUS" fi # 6. Sensitive Log Redaction echo "" echo "--- [6/6] Sensitive Log Redaction ---" LOGS=$(journalctl -u classnotify -n 60 --no-pager) LEAK=0 echo "$LOGS" | grep -q "WrongPassword999!" && LEAK=1 if [ -n "$CLASSNOTIFY_E2E_PASSWORD" ]; then echo "$LOGS" | grep -q "$CLASSNOTIFY_E2E_PASSWORD" && LEAK=1 fi echo "$LOGS" | grep -q "$TOKEN" && LEAK=1 if [ "$LEAK" -eq 0 ]; then report_result "Zero leaks of passwords or tokens in systemd logs" 0 "Checked last 60 log lines" else report_result "Zero leaks of passwords or tokens in systemd logs" 1 "Leak detected in logs!" fi echo "" echo "==========================================================" echo " Total Tests: $((PASS_COUNT + FAIL_COUNT)) | Passed: $PASS_COUNT | Failed: $FAIL_COUNT" echo "==========================================================" if [ "$FAIL_COUNT" -gt 0 ]; then exit 1 fi exit 0