-
Notifications
You must be signed in to change notification settings - Fork 0
Security Testing
Nick edited this page Nov 26, 2025
·
1 revision
Comprehensive security testing procedures for PATAS.
This guide covers security testing for:
- SQL injection prevention
- XSS protection
- CSRF protection
- Rate limiting bypass
- Authentication bypass
- Input validation
# Run all security tests
pytest tests/test_security.py -v
# Run with coverage
pytest tests/test_security.py --cov=app --cov-report=html-
SQL Injection Tests -
test_sql_injection.py -
Input Validation Tests -
test_input_validation.py -
Authentication Tests -
test_authentication.py -
Rate Limiting Tests -
test_rate_limiting.py
# Test 1: Classic SQL injection
curl -X POST http://localhost:8000/v1/classify \
-H "X-API-Key: test-key" \
-H "Content-Type: application/json" \
-d '{"text": "test'; DROP TABLE messages; --", "lang": "en"}'
# Expected: Request should succeed without database modification
# Test 2: UNION-based injection
curl -X POST http://localhost:8000/api/v1/analyze \
-H "X-API-Key: test-key" \
-H "Content-Type: application/json" \
-d '{"messages": [{"id": "1 UNION SELECT * FROM users", "text": "test"}]}'
# Expected: Request should be rejected or sanitized- Check database tables are intact
- Verify no unauthorized data access
- Check logs for injection attempts
# Test 1: Script injection in text
curl -X POST http://localhost:8000/v1/classify \
-H "X-API-Key: test-key" \
-H "Content-Type: application/json" \
-d '{"text": "<script>alert(1)</script>", "lang": "en"}'
# Expected: Script tags should be escaped in response
# Test 2: Event handler injection
curl -X POST http://localhost:8000/v1/classify \
-H "X-API-Key: test-key" \
-H "Content-Type: application/json" \
-d '{"text": "<img src=x onerror=alert(1)>", "lang": "en"}'
# Expected: HTML should be escaped# Test 1: Cross-origin request without proper headers
curl -X POST http://localhost:8000/v1/classify \
-H "Origin: http://evil.com" \
-H "Content-Type: application/json" \
-d '{"text": "test", "lang": "en"}'
# Expected: Should be blocked by CORS policy (in production)- Verify CORS headers are properly set
- Check that cross-origin requests are blocked
# Test 1: Missing API key
curl -X POST http://localhost:8000/v1/classify \
-H "Content-Type: application/json" \
-d '{"text": "test", "lang": "en"}'
# Expected: 401 Unauthorized
# Test 2: Invalid API key
curl -X POST http://localhost:8000/v1/classify \
-H "X-API-Key: invalid-key" \
-H "Content-Type: application/json" \
-d '{"text": "test", "lang": "en"}'
# Expected: 403 Forbidden
# Test 3: SQL injection in API key
curl -X POST http://localhost:8000/v1/classify \
-H "X-API-Key: ' OR '1'='1" \
-H "Content-Type: application/json" \
-d '{"text": "test", "lang": "en"}'
# Expected: 403 Forbidden# Test 1: Rapid requests
for i in {1..100}; do
curl -s -X POST http://localhost:8000/v1/classify \
-H "X-API-Key: test-key" \
-H "Content-Type: application/json" \
-d '{"text": "test", "lang": "en"}'
done | grep -c "429"
# Expected: Should see 429 responses after limit exceeded
# Test 2: IP rotation attempt (requires multiple IPs)
# This should still be rate limited by API key# Test 1: Oversized input
python -c "print('A' * 1000000)" | \
curl -X POST http://localhost:8000/v1/classify \
-H "X-API-Key: test-key" \
-H "Content-Type: application/json" \
-d @-
# Expected: 413 or 400 error
# Test 2: Invalid JSON
curl -X POST http://localhost:8000/v1/classify \
-H "X-API-Key: test-key" \
-H "Content-Type: application/json" \
-d '{invalid json}'
# Expected: 400 Bad Request
# Test 3: Invalid field types
curl -X POST http://localhost:8000/v1/classify \
-H "X-API-Key: test-key" \
-H "Content-Type: application/json" \
-d '{"text": 12345, "lang": "en"}'
# Expected: 422 Validation Error# Run ZAP scan
docker run -t owasp/zap2docker-stable zap-api-scan.py \
-t http://localhost:8000/openapi.json \
-f openapi# Test for SQL injection
sqlmap -u "http://localhost:8000/v1/classify" \
--data='{"text":"test","lang":"en"}' \
--headers="X-API-Key: test-key" \
--headers="Content-Type: application/json"# Web server scan
nikto -h http://localhost:8000- All SQL queries use parameterized statements
- Input validation on all endpoints
- API key authentication enforced
- Rate limiting configured
- CORS properly configured
- Error messages don't leak information
- PII redaction enabled
- HTTPS enforced (in production)
- Security scan completed (ZAP/Nikto)
- SQL injection testing passed
- Authentication bypass testing passed
- Rate limiting verified
- Log monitoring configured
- Incident response plan documented
If you discover a security vulnerability:
- DO NOT create a public issue
- Email security@example.com
- Include detailed reproduction steps
- Allow 90 days for fix before disclosure