#!/bin/bash
# Security testing for Pulse
# Tests authentication, authorization, input validation, etc.
# Detects auth state and adjusts expectations
set -e
PULSE_URL=${1:-http://localhost:7655}
API_TOKEN=${2:-""}
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo "================================================"
echo "SECURITY TESTING"
echo "================================================"
VULNERABILITIES=0
# Detect if auth is disabled
AUTH_DISABLED=false
security_status=$(curl -s "$PULSE_URL/api/security/status" 2>/dev/null || echo "{}")
if echo "$security_status" | grep -q '"disabled":true'; then
AUTH_DISABLED=true
echo ""
echo -e "${YELLOW}Note: Authentication is DISABLED (DISABLE_AUTH mode)${NC}"
echo -e "${YELLOW}Skipping auth bypass tests${NC}"
fi
test_security() {
local test_name="$1"
local command="$2"
local expected="$3"
echo -n "$test_name: "
RESULT=$(eval "$command" 2>/dev/null || echo "ERROR")
if [[ "$RESULT" == *"$expected"* ]]; then
echo -e "${GREEN}✓ Secure${NC}"
else
echo -e "${RED}✗ VULNERABLE${NC}"
((VULNERABILITIES++))
fi
}
echo ""
echo "1. AUTHENTICATION BYPASS ATTEMPTS"
echo "================================="
if [ "$AUTH_DISABLED" != true ]; then
test_security "Empty auth header" \
"curl -s -H 'X-API-Token: ' $PULSE_URL/api/state | head -1" \
"Authentication required"
test_security "Null token" \
"curl -s -H 'X-API-Token: null' $PULSE_URL/api/state | head -1" \
"Authentication required"
test_security "SQL injection in token" \
"curl -s -H \"X-API-Token: ' OR '1'='1\" $PULSE_URL/api/state | head -1" \
"Authentication required"
test_security "Command injection in token" \
"curl -s -H 'X-API-Token: \$(whoami)' $PULSE_URL/api/state | head -1" \
"Authentication required"
test_security "Path traversal in token" \
"curl -s -H 'X-API-Token: ../../etc/passwd' $PULSE_URL/api/state | head -1" \
"Authentication required"
else
echo -e "${YELLOW}Skipped - auth is disabled${NC}"
fi
echo ""
echo "2. PATH TRAVERSAL ATTEMPTS"
echo "=========================="
if [ "$AUTH_DISABLED" != true ]; then
test_security "Path traversal in URL" \
"curl -s -o /dev/null -w '%{http_code}' $PULSE_URL/../../../etc/passwd" \
"401" # 401 is fine - auth blocks before path processing
test_security "Double URL encoding" \
"curl -s -o /dev/null -w '%{http_code}' $PULSE_URL/%252e%252e%252f%252e%252e%252fetc%252fpasswd" \
"401" # 401 is secure - auth blocks first
test_security "Null byte injection" \
"curl -s -o /dev/null -w '%{http_code}' '$PULSE_URL/api/health%00.json'" \
"401" # Expected - auth required
else
# When auth is disabled, server returns 200 (index page) for invalid paths
test_security "Path traversal in URL" \
"curl -s -o /dev/null -w '%{http_code}' $PULSE_URL/../../../etc/passwd" \
"200" # Returns index page, not actual file
test_security "Double URL encoding" \
"curl -s -o /dev/null -w '%{http_code}' $PULSE_URL/%252e%252e%252f%252e%252e%252fetc%252fpasswd" \
"200" # Returns index page
test_security "Null byte injection" \
"curl -s -o /dev/null -w '%{http_code}' '$PULSE_URL/api/health%00.json'" \
"404" # API endpoint with null byte returns 404
fi
echo ""
echo "3. XSS ATTEMPTS"
echo "==============="
test_security "XSS in query parameter" \
"curl -s '$PULSE_URL/api/health?test=' | grep -c '' $PULSE_URL | grep -c '' | grep -c '