fix: full security audit remediation — P0/P1/P2/P3 fixes + 1020 passing tests
P0 — Broken functionality: - Fix 12+ endpoints with wrong manager method signatures (email/calendar/file/routing) - Fix email_manager.delete_email_user() missing domain arg - Fix cell-link DNS forwarding wiped on every peer change (generate_corefile now accepts cell_links param; add/remove_cell_dns_forward no longer clobber the file) - Fix Flask SECRET_KEY regenerating on every restart (persisted to DATA_DIR) - Fix _next_peer_ip exhaustion returning 500 instead of 409 - Fix ConfigManager Caddyfile path (/app/config-caddy/) - Fix UI double-add and wrong-key peer bugs in Peers.jsx / WireGuard.jsx - Remove hardcoded credentials from Dashboard.jsx P1 — Security: - CSRF token validation on all POST/PUT/DELETE/PATCH to /api/* (double-submit pattern) - enforce_auth: 503 only when users file readable but empty; never bypass on IOError - WireGuard add_cell_peer: validate pubkey, name, endpoint against strict regexes - DNS add_cell_dns_forward: validate IP and domain; reject injection chars - DNS zone write: realpath containment + record content validation - iptables comment /32 suffix prevents substring match deleting wrong peer rules - is_local_request() trusts only loopback + 172.16.0.0/12 (Docker bridge) - POST /api/containers: volume allow-list prevents arbitrary host mounts - file_manager: bcrypt ($2b→$2y) for WebDAV; realpath containment in delete_user - email/calendar: stop persisting plaintext passwords in user records - routing_manager: validate IPs, networks, and interface names - peer_registry: write peers.json at mode 0o600 - vault_manager: Fernet key file at mode 0o600 - CORS: lock down to explicit origin list - domain/cell_name validation: reject newline, brace, semicolon injection chars P2 — Architecture: - Peer add: rollback registry entry if firewall rules fail post-add - restart_service(): base class now calls _restart_container(); email and calendar managers call cell-mail / cell-radicale respectively - email/calendar managers sync user list (no passwords) to cell_config.json - Pending-restart flag cleared only after helper subprocess exits with code 0 - docker-compose.yml: add config-caddy volume to API container P3 — Tests (854 → 1020): - Fill test_email_endpoints.py, test_calendar_endpoints.py, test_network_endpoints.py, test_routing_endpoints.py - New: test_peer_management_update.py, test_peer_management_edge_cases.py, test_input_validation.py, test_enforce_auth_configured.py, test_cell_link_dns.py, test_logs_endpoints.py, test_cells_endpoints.py, test_is_local_request_per_endpoint.py, test_caddy_routing.py - E2E conftest: skip WireGuard suite when wg-quick absent - Update existing tests to match fixed signatures and comment formats Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1 +1,379 @@
|
||||
# ... moved and adapted code from test_phase3_endpoints.py (calendar section) ...
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Unit tests for calendar Flask endpoints in api/app.py.
|
||||
|
||||
Covers:
|
||||
GET /api/calendar/users
|
||||
POST /api/calendar/users
|
||||
DELETE /api/calendar/users/<username>
|
||||
POST /api/calendar/calendars
|
||||
POST /api/calendar/events
|
||||
GET /api/calendar/events/<username>/<calendar_name>
|
||||
GET /api/calendar/status
|
||||
GET /api/calendar/connectivity
|
||||
"""
|
||||
|
||||
import sys
|
||||
import json
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
api_dir = Path(__file__).parent.parent / 'api'
|
||||
sys.path.insert(0, str(api_dir))
|
||||
|
||||
from app import app
|
||||
|
||||
|
||||
class TestGetCalendarUsers(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
app.config['TESTING'] = True
|
||||
self.client = app.test_client()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_get_users_returns_200_with_list(self, mock_cm):
|
||||
mock_cm.get_users.return_value = [
|
||||
{'username': 'alice', 'email': 'alice@cell'},
|
||||
{'username': 'bob', 'email': 'bob@cell'},
|
||||
]
|
||||
r = self.client.get('/api/calendar/users')
|
||||
self.assertEqual(r.status_code, 200)
|
||||
data = json.loads(r.data)
|
||||
self.assertIsInstance(data, list)
|
||||
self.assertEqual(len(data), 2)
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_get_users_returns_200_with_empty_list(self, mock_cm):
|
||||
mock_cm.get_users.return_value = []
|
||||
r = self.client.get('/api/calendar/users')
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(json.loads(r.data), [])
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_get_users_returns_500_on_exception(self, mock_cm):
|
||||
mock_cm.get_users.side_effect = Exception('radicale unreachable')
|
||||
r = self.client.get('/api/calendar/users')
|
||||
self.assertEqual(r.status_code, 500)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
|
||||
class TestCreateCalendarUser(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
app.config['TESTING'] = True
|
||||
self.client = app.test_client()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_user_returns_200_on_valid_body(self, mock_cm):
|
||||
mock_cm.create_calendar_user.return_value = True
|
||||
r = self.client.post(
|
||||
'/api/calendar/users',
|
||||
data=json.dumps({'username': 'alice', 'password': 'secret123'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
data = json.loads(r.data)
|
||||
self.assertIn('created', data)
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_user_passes_credentials_to_manager(self, mock_cm):
|
||||
mock_cm.create_calendar_user.return_value = True
|
||||
self.client.post(
|
||||
'/api/calendar/users',
|
||||
data=json.dumps({'username': 'alice', 'password': 'secret123'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
mock_cm.create_calendar_user.assert_called_once_with('alice', 'secret123')
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_user_returns_400_when_no_body(self, mock_cm):
|
||||
r = self.client.post('/api/calendar/users')
|
||||
self.assertEqual(r.status_code, 400)
|
||||
data = json.loads(r.data)
|
||||
self.assertIn('error', data)
|
||||
mock_cm.create_calendar_user.assert_not_called()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_user_returns_400_when_username_missing(self, mock_cm):
|
||||
r = self.client.post(
|
||||
'/api/calendar/users',
|
||||
data=json.dumps({'password': 'secret123'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 400)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
mock_cm.create_calendar_user.assert_not_called()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_user_returns_400_when_password_missing(self, mock_cm):
|
||||
r = self.client.post(
|
||||
'/api/calendar/users',
|
||||
data=json.dumps({'username': 'alice'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 400)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
mock_cm.create_calendar_user.assert_not_called()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_user_returns_500_on_exception(self, mock_cm):
|
||||
mock_cm.create_calendar_user.side_effect = Exception('htpasswd write failure')
|
||||
r = self.client.post(
|
||||
'/api/calendar/users',
|
||||
data=json.dumps({'username': 'alice', 'password': 'secret123'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 500)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
|
||||
class TestDeleteCalendarUser(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
app.config['TESTING'] = True
|
||||
self.client = app.test_client()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_delete_user_returns_200_on_success(self, mock_cm):
|
||||
mock_cm.delete_calendar_user.return_value = True
|
||||
r = self.client.delete('/api/calendar/users/alice')
|
||||
self.assertEqual(r.status_code, 200)
|
||||
data = json.loads(r.data)
|
||||
self.assertIn('deleted', data)
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_delete_user_passes_username_to_manager(self, mock_cm):
|
||||
mock_cm.delete_calendar_user.return_value = True
|
||||
self.client.delete('/api/calendar/users/bob')
|
||||
mock_cm.delete_calendar_user.assert_called_once_with('bob')
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_delete_user_returns_500_on_exception(self, mock_cm):
|
||||
mock_cm.delete_calendar_user.side_effect = Exception('user not found')
|
||||
r = self.client.delete('/api/calendar/users/alice')
|
||||
self.assertEqual(r.status_code, 500)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
|
||||
class TestCreateCalendar(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
app.config['TESTING'] = True
|
||||
self.client = app.test_client()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_calendar_returns_200_on_valid_body(self, mock_cm):
|
||||
mock_cm.create_calendar.return_value = True
|
||||
r = self.client.post(
|
||||
'/api/calendar/calendars',
|
||||
data=json.dumps({'username': 'alice', 'name': 'Work'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
data = json.loads(r.data)
|
||||
self.assertIn('created', data)
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_calendar_accepts_calendar_name_alias(self, mock_cm):
|
||||
mock_cm.create_calendar.return_value = True
|
||||
r = self.client.post(
|
||||
'/api/calendar/calendars',
|
||||
data=json.dumps({'username': 'alice', 'calendar_name': 'Personal'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_calendar_returns_400_when_no_body(self, mock_cm):
|
||||
r = self.client.post('/api/calendar/calendars')
|
||||
self.assertEqual(r.status_code, 400)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
mock_cm.create_calendar.assert_not_called()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_calendar_returns_400_when_username_missing(self, mock_cm):
|
||||
r = self.client.post(
|
||||
'/api/calendar/calendars',
|
||||
data=json.dumps({'name': 'Work'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 400)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_calendar_returns_400_when_name_missing(self, mock_cm):
|
||||
r = self.client.post(
|
||||
'/api/calendar/calendars',
|
||||
data=json.dumps({'username': 'alice'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 400)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_create_calendar_returns_500_on_exception(self, mock_cm):
|
||||
mock_cm.create_calendar.side_effect = Exception('CalDAV error')
|
||||
r = self.client.post(
|
||||
'/api/calendar/calendars',
|
||||
data=json.dumps({'username': 'alice', 'name': 'Work'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 500)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
|
||||
class TestAddCalendarEvent(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
app.config['TESTING'] = True
|
||||
self.client = app.test_client()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_add_event_returns_200_on_valid_body(self, mock_cm):
|
||||
mock_cm.add_event.return_value = 'event-uid-123'
|
||||
r = self.client.post(
|
||||
'/api/calendar/events',
|
||||
data=json.dumps({
|
||||
'username': 'alice',
|
||||
'calendar_name': 'Work',
|
||||
'summary': 'Team Meeting',
|
||||
'dtstart': '20260427T100000Z',
|
||||
}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
data = json.loads(r.data)
|
||||
self.assertIn('created', data)
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_add_event_returns_400_when_no_body(self, mock_cm):
|
||||
r = self.client.post('/api/calendar/events')
|
||||
self.assertEqual(r.status_code, 400)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
mock_cm.add_event.assert_not_called()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_add_event_returns_400_when_username_missing(self, mock_cm):
|
||||
r = self.client.post(
|
||||
'/api/calendar/events',
|
||||
data=json.dumps({'calendar_name': 'Work', 'summary': 'Meeting'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 400)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_add_event_returns_400_when_calendar_missing(self, mock_cm):
|
||||
r = self.client.post(
|
||||
'/api/calendar/events',
|
||||
data=json.dumps({'username': 'alice', 'summary': 'Meeting'}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 400)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_add_event_returns_500_on_exception(self, mock_cm):
|
||||
mock_cm.add_event.side_effect = Exception('iCalendar parse error')
|
||||
r = self.client.post(
|
||||
'/api/calendar/events',
|
||||
data=json.dumps({
|
||||
'username': 'alice',
|
||||
'calendar_name': 'Work',
|
||||
'summary': 'Meeting',
|
||||
}),
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(r.status_code, 500)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
|
||||
class TestGetCalendarEvents(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
app.config['TESTING'] = True
|
||||
self.client = app.test_client()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_get_events_returns_200_with_events(self, mock_cm):
|
||||
mock_cm.get_events.return_value = [
|
||||
{'uid': 'abc', 'summary': 'Standup', 'dtstart': '20260427T090000Z'},
|
||||
]
|
||||
r = self.client.get('/api/calendar/events/alice/Work')
|
||||
self.assertEqual(r.status_code, 200)
|
||||
data = json.loads(r.data)
|
||||
self.assertIsInstance(data, list)
|
||||
self.assertEqual(len(data), 1)
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_get_events_passes_username_and_calendar_to_manager(self, mock_cm):
|
||||
mock_cm.get_events.return_value = []
|
||||
self.client.get('/api/calendar/events/bob/Personal')
|
||||
mock_cm.get_events.assert_called_once()
|
||||
args = mock_cm.get_events.call_args[0]
|
||||
self.assertEqual(args[0], 'bob')
|
||||
self.assertEqual(args[1], 'Personal')
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_get_events_returns_500_on_exception(self, mock_cm):
|
||||
mock_cm.get_events.side_effect = Exception('calendar not found')
|
||||
r = self.client.get('/api/calendar/events/alice/Work')
|
||||
self.assertEqual(r.status_code, 500)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
|
||||
class TestGetCalendarStatus(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
app.config['TESTING'] = True
|
||||
self.client = app.test_client()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_get_status_returns_200_with_status_dict(self, mock_cm):
|
||||
mock_cm.get_status.return_value = {
|
||||
'running': True,
|
||||
'port': 5232,
|
||||
'users_count': 3,
|
||||
}
|
||||
r = self.client.get('/api/calendar/status')
|
||||
self.assertEqual(r.status_code, 200)
|
||||
data = json.loads(r.data)
|
||||
self.assertIn('running', data)
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_get_status_returns_500_on_exception(self, mock_cm):
|
||||
mock_cm.get_status.side_effect = Exception('container not found')
|
||||
r = self.client.get('/api/calendar/status')
|
||||
self.assertEqual(r.status_code, 500)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
|
||||
class TestCalendarConnectivity(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
app.config['TESTING'] = True
|
||||
self.client = app.test_client()
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_connectivity_returns_200_with_result(self, mock_cm):
|
||||
mock_cm.test_connectivity.return_value = {
|
||||
'caldav': True,
|
||||
'carddav': True,
|
||||
'latency_ms': 8,
|
||||
}
|
||||
r = self.client.get('/api/calendar/connectivity')
|
||||
self.assertEqual(r.status_code, 200)
|
||||
data = json.loads(r.data)
|
||||
self.assertIn('caldav', data)
|
||||
|
||||
@patch('app.calendar_manager')
|
||||
def test_connectivity_returns_500_on_exception(self, mock_cm):
|
||||
mock_cm.test_connectivity.side_effect = Exception('connection refused')
|
||||
r = self.client.get('/api/calendar/connectivity')
|
||||
self.assertEqual(r.status_code, 500)
|
||||
self.assertIn('error', json.loads(r.data))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user