Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Fix open-redirect class in is_safe_url by validating raw next param against local-path allowlist
Murtaza Nasir54302ac012b1
diff --git a/src/api/auth.py b/src/api/auth.pyindex 30c42767..a432ea15 100644--- a/src/api/auth.py+++ b/src/api/auth.py@@ -14,12 +14,11 @@ from flask_wtf import FlaskFormfrom wtforms import StringField, PasswordField, SubmitField, BooleanFieldfrom wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationErrorfrom werkzeug.security import generate_password_hash, check_password_hash-from urllib.parse import urlparse, urljoinimport markdownfrom src.database import dbfrom src.models import User, SystemSetting, GroupMembership-from src.utils import password_check+from src.utils import password_check, is_safe_urlfrom src.auth.sso import (init_sso_client,is_sso_enabled,@@ -110,12 +109,6 @@ class LoginForm(FlaskForm):# --- Helper Functions ----def is_safe_url(target):- ref_url = urlparse(request.host_url)- test_url = urlparse(urljoin(request.host_url, target))- return test_url.scheme in ('http', 'https') and ref_url.netloc == test_url.netloc--def is_registration_domain_allowed(email: str) -> bool:"""Check if email domain is allowed for registration.diff --git a/src/utils/security.py b/src/utils/security.pyindex e7178005..573b05e7 100644--- a/src/utils/security.py+++ b/src/utils/security.py@@ -6,8 +6,7 @@ This module provides security-related utility functions for the application.import refrom wtforms.validators import ValidationError-from urllib.parse import urlparse, urljoin-from flask import request+from urllib.parse import urlparsedef password_check(form, field):@@ -44,7 +43,25 @@ def password_check(form, field):# --- URL Security ---def is_safe_url(target):- ref_url = urlparse(request.host_url)- test_url = urlparse(urljoin(request.host_url, target))- return test_url.scheme in ('http', 'https') and ref_url.netloc == test_url.netloc+ """Return True only for local relative paths.++ Rejects scheme-relative URLs (``//evil.com``), backslash-prefixed URLs+ (``\\evil.com``), absolute URLs, and anything with a scheme or netloc.+ The validator runs against the raw value so the same string can be passed+ to ``redirect()`` without the parser-mismatch open-redirect class.+ """+ if not target or not isinstance(target, str):+ return False+ if not target.startswith('/'):+ return False+ if target.startswith('//') or target.startswith('/\\'):+ return False+ if '\\' in target:+ return False+ if any(ord(ch) < 0x20 for ch in target):+ return False+ parsed = urlparse(target)+ if parsed.scheme or parsed.netloc:+ return False+ return Truediff --git a/tests/test_open_redirect.py b/tests/test_open_redirect.pynew file mode 100644index 00000000..741e6b04--- /dev/null+++ b/tests/test_open_redirect.py@@ -0,0 +1,74 @@+"""Regression tests for the open-redirect class in is_safe_url.++The original implementation validated ``urljoin(host_url, target)`` while+``redirect()`` was called with the raw ``target``. A scheme-relative input+such as ``////evil.com`` looked safe to the validator (urljoin resolves it+to a same-host path) but the browser, given the raw value in the Location+header, treats it as a network-path redirect to ``evil.com``.++The current validator only allows local relative paths, so the same raw+value flows through both the safety check and the redirect call.+"""++import os+import sys++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++from src.utils.security import is_safe_url+++def test_rejects_scheme_relative_redirect():+ assert is_safe_url('//evil.com') is False+ assert is_safe_url('////evil.com') is False+ assert is_safe_url('//evil.com/path') is False+++def test_rejects_absolute_url():+ assert is_safe_url('http://evil.com/login') is False+ assert is_safe_url('https://evil.com/login') is False+ assert is_safe_url('javascript:alert(1)') is False+ assert is_safe_url('data:text/html,<script>') is False+++def test_rejects_backslash_variants():+ assert is_safe_url('/\\evil.com') is False+ assert is_safe_url('\\\\evil.com') is False+ assert is_safe_url('/path\\evil.com') is False+++def test_rejects_empty_or_non_string():+ assert is_safe_url(None) is False+ assert is_safe_url('') is False+ assert is_safe_url(b'/path') is False+ assert is_safe_url(123) is False+++def test_rejects_relative_paths_without_leading_slash():+ assert is_safe_url('recordings/123') is False+ assert is_safe_url('account') is False+++def test_rejects_control_characters():+ assert is_safe_url('/path\nLocation: http://evil.com') is False+ assert is_safe_url('/path\r\nX-Injected: 1') is False+ assert is_safe_url('/path\x00') is False+++def test_accepts_local_paths():+ assert is_safe_url('/') is True+ assert is_safe_url('/recordings') is True+ assert is_safe_url('/recordings/123') is True+ assert is_safe_url('/account?tab=preferences') is True+ assert is_safe_url('/recordings#section') is True+++if __name__ == '__main__':+ test_rejects_scheme_relative_redirect()+ test_rejects_absolute_url()+ test_rejects_backslash_variants()+ test_rejects_empty_or_non_string()+ test_rejects_relative_paths_without_leading_slash()+ test_rejects_control_characters()+ test_accepts_local_paths()+ print('All open-redirect regression tests passed.')