Security: Add @login_required to all API endpoints and CSRF protection

- Added @login_required decorator to all 38 unprotected API endpoints
- Initialized Flask-WTF CSRFProtect in app factory
- Added CSRF token meta tag to base.html
- Added JavaScript fetch interceptor to attach X-CSRFToken header to all non-GET requests
- Closes critical security vulnerability where any unauthenticated user could perform administrative actions
This commit is contained in:
Reditus committed 2026-05-14 09:03:14 +01:00
1 parent 573fb9858c
commit 9f56c3943e
3 files changed
+59

No files matched your search

+20
View File
@@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{% block title %}Dune Awakening Server Dashboard{% endblock %}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
@@ -100,6 +101,25 @@
}
setInterval(checkForUpdate, 60000);
checkForUpdate();
// CSRF: Attach token to all non-GET fetch requests
(function() {
const csrfToken = document.querySelector('meta[name="csrf-token"]')?.content;
if (!csrfToken) return;
const origFetch = window.fetch;
window.fetch = function() {
let [resource, config] = arguments;
if (!config) config = {};
if (!config.headers) config.headers = {};
const method = (config.method || 'GET').toUpperCase();
if (method !== 'GET' && method !== 'HEAD' && method !== 'OPTIONS') {
if (typeof config.headers === 'object') {
config.headers['X-CSRFToken'] = csrfToken;
}
}
return origFetch(resource, config);
};
})();
</script>
<style>
.update-banner { background: var(--bg-hover); border-bottom: 2px solid var(--accent); padding: 10px 20px; display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--text); }