Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

fixes

Murtaza Nasir4e09d8c516de
diff --git a/app.py b/app.py
index 6e6cd061..90ef85f4 100644
--- a/app.py
+++ b/app.py
@@ -351,11 +351,11 @@ app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'default-dev-key-change-
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
# --- Secure Session Cookie Configuration ---
-# In a production environment (e.g., when not in debug mode), these should be enabled.
-if not app.debug:
- app.config['SESSION_COOKIE_SECURE'] = True
- app.config['SESSION_COOKIE_HTTPONLY'] = True
- app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
+# For local network usage, disable secure cookies to allow HTTP connections
+# Only enable secure cookies in production when HTTPS is actually being used
+app.config['SESSION_COOKIE_SECURE'] = False # Allow HTTP for local network usage
+app.config['SESSION_COOKIE_HTTPONLY'] = True # Still protect against XSS
+app.config['SESSION_COOKIE_SAMESITE'] = 'Lax' # CSRF protection
db = SQLAlchemy()
db.init_app(app)
@@ -2139,6 +2139,7 @@ def login():
return render_template('login.html', title='Login', form=form)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('login'))
diff --git a/templates/account.html b/templates/account.html
index 9bf50bc0..88950f2d 100644
--- a/templates/account.html
+++ b/templates/account.html
@@ -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>{{ title }} - Speakr</title>
<link rel="icon" href="{{ url_for('static', filename='img/favicon.ico') }}" type="image/svg+xml">
<script src="https://cdn.tailwindcss.com"></script>
@@ -109,6 +110,7 @@
<div>
<form method="POST" action="{{ url_for('account') }}" class="space-y-6">
<div class="mb-6">
+ <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<h3 class="text-lg font-medium text-[var(--text-secondary)] mb-2">Personal Information</h3>
<div class="bg-[var(--bg-tertiary)] p-4 rounded-lg border border-[var(--border-primary)] space-y-4">
<div>
@@ -266,6 +268,7 @@
<button id="closeModalBtn" class="text-[var(--text-light)] hover:text-[var(--text-muted)] text-2xl leading-none">&times;</button>
</div>
<form id="changePasswordForm" method="POST" action="{{ url_for('change_password') }}">
+ <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<div class="mb-4">
<label for="current_password" class="block text-sm font-medium text-[var(--text-secondary)] mb-1">Current Password</label>
<input type="password" id="current_password" name="current_password" required class="w-full px-3 py-2 border border-[var(--border-secondary)] rounded-md focus:outline-none focus:ring-2 focus:ring-[var(--border-focus)] bg-[var(--bg-input)] text-[var(--text-primary)]">
@@ -457,7 +460,18 @@
if (!confirm('Are you sure you want to delete this share? This will revoke access to the public link.')) return;
try {
- const response = await fetch(`/api/share/${shareId}`, { method: 'DELETE' });
+ // Get CSRF token
+ const csrfToken = document.querySelector('meta[name=csrf-token]')?.getAttribute('content');
+
+ const headers = {};
+ if (csrfToken) {
+ headers['X-CSRFToken'] = csrfToken;
+ }
+
+ const response = await fetch(`/api/share/${shareId}`, {
+ method: 'DELETE',
+ headers: headers
+ });
if (!response.ok) throw new Error('Failed to delete');
loadShares();
} catch (error) {
@@ -471,9 +485,17 @@
const value = event.currentTarget.checked;
try {
+ // Get CSRF token
+ const csrfToken = document.querySelector('meta[name=csrf-token]')?.getAttribute('content');
+
+ const headers = { 'Content-Type': 'application/json' };
+ if (csrfToken) {
+ headers['X-CSRFToken'] = csrfToken;
+ }
+
const response = await fetch(`/api/share/${shareId}`, {
method: 'PUT',
- headers: { 'Content-Type': 'application/json' },
+ headers: headers,
body: JSON.stringify({ [prop]: value })
});
if (!response.ok) throw new Error('Failed to update');
@@ -727,8 +749,17 @@
}
try {
+ // Get CSRF token
+ const csrfToken = document.querySelector('meta[name=csrf-token]')?.getAttribute('content');
+
+ const headers = {};
+ if (csrfToken) {
+ headers['X-CSRFToken'] = csrfToken;
+ }
+
const response = await fetch(`/speakers/${speakerId}`, {
- method: 'DELETE'
+ method: 'DELETE',
+ headers: headers
});
if (!response.ok) {
@@ -768,9 +799,20 @@
deleteAllSpeakersModal.classList.add('hidden');
try {
+ // Get CSRF token
+ const csrfToken = document.querySelector('meta[name=csrf-token]')?.getAttribute('content');
+
+ const headers = {};
+ if (csrfToken) {
+ headers['X-CSRFToken'] = csrfToken;
+ }
+
// Delete all speakers one by one
const deletePromises = speakers.map(speaker =>
- fetch(`/speakers/${speaker.id}`, { method: 'DELETE' })
+ fetch(`/speakers/${speaker.id}`, {
+ method: 'DELETE',
+ headers: headers
+ })
);
const results = await Promise.allSettled(deletePromises);
diff --git a/templates/admin.html b/templates/admin.html
index 21162639..d10898ae 100644
--- a/templates/admin.html
+++ b/templates/admin.html
@@ -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>Admin Dashboard - Speakr</title>
<link rel="icon" href="{{ url_for('static', filename='img/favicon.ico') }}" type="image/svg+xml">
<script src="https://cdn.tailwindcss.com"></script>
@@ -549,11 +550,20 @@
const updateSetting = async (key, value, description, settingType) => {
try {
+ // Get CSRF token
+ const csrfToken = document.querySelector('meta[name=csrf-token]')?.getAttribute('content');
+
+ const headers = {
+ 'Content-Type': 'application/json'
+ };
+
+ if (csrfToken) {
+ headers['X-CSRFToken'] = csrfToken;
+ }
+
const response = await fetch('/admin/settings', {
method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
+ headers: headers,
body: JSON.stringify({
key: key,
value: value,
@@ -589,11 +599,20 @@
}
try {
+ // Get CSRF token
+ const csrfToken = document.querySelector('meta[name=csrf-token]')?.getAttribute('content');
+
+ const headers = {
+ 'Content-Type': 'application/json'
+ };
+
+ if (csrfToken) {
+ headers['X-CSRFToken'] = csrfToken;
+ }
+
const response = await fetch('/admin/users', {
method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
+ headers: headers,
body: JSON.stringify({
username: newUser.value.username,
email: newUser.value.email,
@@ -645,11 +664,20 @@
payload.password = editingUser.value.password;
}
+ // Get CSRF token
+ const csrfToken = document.querySelector('meta[name=csrf-token]')?.getAttribute('content');
+
+ const headers = {
+ 'Content-Type': 'application/json'
+ };
+
+ if (csrfToken) {
+ headers['X-CSRFToken'] = csrfToken;
+ }
+
const response = await fetch(`/admin/users/${editingUser.value.id}`, {
method: 'PUT',
- headers: {
- 'Content-Type': 'application/json'
- },
+ headers: headers,
body: JSON.stringify(payload)
});
@@ -675,8 +703,18 @@
const deleteUser = async () => {
try {
+ // Get CSRF token
+ const csrfToken = document.querySelector('meta[name=csrf-token]')?.getAttribute('content');
+
+ const headers = {};
+
+ if (csrfToken) {
+ headers['X-CSRFToken'] = csrfToken;
+ }
+
const response = await fetch(`/admin/users/${userToDelete.value.id}`, {
- method: 'DELETE'
+ method: 'DELETE',
+ headers: headers
});
if (!response.ok) {
@@ -697,8 +735,18 @@
const toggleAdminStatus = async (user) => {
try {
+ // Get CSRF token
+ const csrfToken = document.querySelector('meta[name=csrf-token]')?.getAttribute('content');
+
+ const headers = {};
+
+ if (csrfToken) {
+ headers['X-CSRFToken'] = csrfToken;
+ }
+
const response = await fetch(`/admin/users/${user.id}/toggle-admin`, {
- method: 'POST'
+ method: 'POST',
+ headers: headers
});
if (!response.ok) {