Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix: extend log sanitizer to strip Unicode format chars + add change-password tests (#2641)

LearningCircuit962fe17b9d21
diff --git a/src/local_deep_research/security/log_sanitizer.py b/src/local_deep_research/security/log_sanitizer.py
index ea0b6fb09b..407665f339 100644
--- a/src/local_deep_research/security/log_sanitizer.py
+++ b/src/local_deep_research/security/log_sanitizer.py
@@ -8,19 +8,30 @@ non-printable characters and truncating to a reasonable length.
import re
-# Strip C0/C1 control characters but preserve valid Unicode (accented, CJK, etc.)
-_CONTROL_CHAR_RE = re.compile(r"[\x00-\x1f\x7f-\x9f]")
+# Strip C0/C1 control characters and dangerous Unicode format characters,
+# but preserve visible Unicode (accented, CJK, emoji, etc.)
+_UNSAFE_CHAR_RE = re.compile(
+ r"[\x00-\x1f\x7f-\x9f" # C0/C1 control chars
+ r"\u061c" # Arabic letter mark
+ r"\u200b-\u200f" # Zero-width chars + LTR/RTL marks
+ r"\u202a-\u202e" # Embedding/override (incl. RLO)
+ r"\u2060-\u2064" # Word joiner + math invisible operators
+ r"\u2066-\u2069" # Isolate chars
+ r"\u206a-\u206f" # Digit shape controls
+ r"\ufeff" # BOM / zero-width no-break space
+ r"]"
+)
def strip_control_chars(value: str) -> str:
- """Remove C0/C1 control characters from *value*, preserving Unicode."""
- return _CONTROL_CHAR_RE.sub("", value)
+ """Remove control and format characters from *value*, preserving visible Unicode."""
+ return _UNSAFE_CHAR_RE.sub("", value)
def sanitize_for_log(value: str, max_length: int = 50) -> str:
"""Return a log-safe version of *value*.
- * Control characters (C0/C1) are stripped; valid Unicode is preserved.
+ * Control and format characters are stripped; valid Unicode is preserved.
* The result is truncated to *max_length* characters.
"""
cleaned = strip_control_chars(value)
diff --git a/tests/auth_tests/test_auth_rate_limiting.py b/tests/auth_tests/test_auth_rate_limiting.py
index 8c27194024..8606b7c4ab 100644
--- a/tests/auth_tests/test_auth_rate_limiting.py
+++ b/tests/auth_tests/test_auth_rate_limiting.py
@@ -175,6 +175,79 @@ class TestAuthRateLimiting:
)
assert response.status_code == 429, "4th attempt should be rate limited"
+ def test_password_change_rate_limit_blocks_6th_attempt(self, client, app):
+ """Test that password change blocks the 6th attempt."""
+ # Disable exception propagation so template rendering errors
+ # (e.g. missing 'research.index' endpoint) return 500 instead
+ # of crashing the test. Rate limiting fires before the handler,
+ # so non-429 responses still count toward the limit.
+ app.config["PROPAGATE_EXCEPTIONS"] = False
+ app.config["TESTING"] = False
+
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+
+ for i in range(5):
+ response = client.post(
+ "/auth/change-password",
+ data={
+ "current_password": "",
+ "new_password": "NewStrongP4ss!",
+ "confirm_password": "NewStrongP4ss!",
+ },
+ )
+ assert response.status_code != 429, (
+ f"Attempt {i + 1} should not be rate limited"
+ )
+
+ # 6th attempt should be rate limited
+ response = client.post(
+ "/auth/change-password",
+ data={
+ "current_password": "",
+ "new_password": "NewStrongP4ss!",
+ "confirm_password": "NewStrongP4ss!",
+ },
+ )
+ assert response.status_code == 429, (
+ "6th password change attempt should be rate limited"
+ )
+
+ def test_password_change_has_separate_limit_from_login(self, client, app):
+ """Test that login and password change have independent rate limits."""
+ app.config["PROPAGATE_EXCEPTIONS"] = False
+ app.config["TESTING"] = False
+
+ # Exhaust login limit (5 attempts)
+ for i in range(5):
+ client.post(
+ "/auth/login",
+ data={"username": f"testuser{i}", "password": "wrongpassword"},
+ )
+
+ # Verify login is rate limited
+ response = client.post(
+ "/auth/login",
+ data={"username": "testuser6", "password": "wrongpassword"},
+ )
+ assert response.status_code == 429, "Login should be rate limited"
+
+ # Password change should still work (separate scope)
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+
+ response = client.post(
+ "/auth/change-password",
+ data={
+ "current_password": "",
+ "new_password": "NewStrongP4ss!",
+ "confirm_password": "NewStrongP4ss!",
+ },
+ )
+ assert response.status_code != 429, (
+ "Password change should have separate rate limit from login"
+ )
+
def test_different_endpoints_have_separate_limits(self, client):
"""Test that login and registration have independent rate limits."""
# Exhaust login limit (5 attempts)
diff --git a/tests/security/test_log_sanitizer.py b/tests/security/test_log_sanitizer.py
index 3d80677c5e..d0375c4e10 100644
--- a/tests/security/test_log_sanitizer.py
+++ b/tests/security/test_log_sanitizer.py
@@ -58,5 +58,26 @@ class TestStripControlChars:
def test_preserves_unicode(self):
assert strip_control_chars("café 你好 émoji") == "café 你好 émoji"
+ def test_strips_rlo_override(self):
+ assert strip_control_chars("hello\u202eworld") == "helloworld"
+
+ def test_strips_zero_width_space(self):
+ assert strip_control_chars("hello\u200bworld") == "helloworld"
+
+ def test_strips_bom(self):
+ assert strip_control_chars("\ufeffhello") == "hello"
+
+ def test_strips_word_joiner(self):
+ assert strip_control_chars("hello\u2060world") == "helloworld"
+
+ def test_strips_digit_shape_controls(self):
+ assert strip_control_chars("hello\u206aworld") == "helloworld"
+
+ def test_strips_mixed_format_chars(self):
+ assert (
+ strip_control_chars("café\u202e\u200b\ufeff 你好\u2060")
+ == "café 你好"
+ )
+
def test_empty_string(self):
assert strip_control_chars("") == ""
diff --git a/tests/web/auth/test_auth_routes.py b/tests/web/auth/test_auth_routes.py
index 2fae1b45d7..e857f37a14 100644
--- a/tests/web/auth/test_auth_routes.py
+++ b/tests/web/auth/test_auth_routes.py
@@ -581,6 +581,159 @@ class TestChangePassword:
)
assert response.status_code == 400
+ def test_successful_password_change(self):
+ """Should clear session and redirect to login on success."""
+ app = Flask(__name__)
+ app.secret_key = "test"
+ app.config["WTF_CSRF_ENABLED"] = False
+
+ with (
+ patch("local_deep_research.web.auth.routes.db_manager") as mock_db,
+ patch(
+ "local_deep_research.web.auth.routes.render_template"
+ ) as mock_render,
+ ):
+ mock_db.change_password.return_value = True
+ mock_render.return_value = "Change Password Page"
+
+ from local_deep_research.web.auth.routes import auth_bp
+
+ app.register_blueprint(auth_bp)
+
+ with app.test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+
+ response = client.post(
+ "/auth/change-password",
+ data={
+ "current_password": "OldPass123",
+ "new_password": "NewStrongP4ss!",
+ "confirm_password": "NewStrongP4ss!",
+ },
+ follow_redirects=False,
+ )
+ assert response.status_code == 302
+ assert "login" in response.location
+
+ with client.session_transaction() as sess:
+ assert "username" not in sess
+
+ def test_returns_401_for_wrong_current_password(self):
+ """Should return 401 when current password is incorrect."""
+ app = Flask(__name__)
+ app.secret_key = "test"
+ app.config["WTF_CSRF_ENABLED"] = False
+
+ with (
+ patch("local_deep_research.web.auth.routes.db_manager") as mock_db,
+ patch(
+ "local_deep_research.web.auth.routes.render_template"
+ ) as mock_render,
+ ):
+ mock_db.change_password.return_value = False
+ mock_render.return_value = "Change Password Page"
+
+ from local_deep_research.web.auth.routes import auth_bp
+
+ app.register_blueprint(auth_bp)
+
+ with app.test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+
+ response = client.post(
+ "/auth/change-password",
+ data={
+ "current_password": "WrongPass123",
+ "new_password": "NewStrongP4ss!",
+ "confirm_password": "NewStrongP4ss!",
+ },
+ )
+ assert response.status_code == 401
+ mock_render.assert_called_with("auth/change_password.html")
+
+ def test_renders_page_when_authenticated(self):
+ """Should render change password page for authenticated user."""
+ app = Flask(__name__)
+ app.secret_key = "test"
+ app.config["WTF_CSRF_ENABLED"] = False
+
+ with patch(
+ "local_deep_research.web.auth.routes.render_template"
+ ) as mock_render:
+ mock_render.return_value = "Change Password Page"
+
+ from local_deep_research.web.auth.routes import auth_bp
+
+ app.register_blueprint(auth_bp)
+
+ with app.test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+
+ response = client.get("/auth/change-password")
+ assert response.status_code == 200
+ mock_render.assert_called_with("auth/change_password.html")
+
+ def test_returns_400_for_weak_new_password(self):
+ """Should return 400 when new password is too weak."""
+ app = Flask(__name__)
+ app.secret_key = "test"
+ app.config["WTF_CSRF_ENABLED"] = False
+
+ with patch(
+ "local_deep_research.web.auth.routes.render_template"
+ ) as mock_render:
+ mock_render.return_value = "Change Password Page"
+
+ from local_deep_research.web.auth.routes import auth_bp
+
+ app.register_blueprint(auth_bp)
+
+ with app.test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+
+ response = client.post(
+ "/auth/change-password",
+ data={
+ "current_password": "OldPass123",
+ "new_password": "Pass1",
+ "confirm_password": "Pass1",
+ },
+ )
+ assert response.status_code == 400
+
+ def test_returns_400_for_mismatched_new_passwords(self):
+ """Should return 400 when new passwords don't match."""
+ app = Flask(__name__)
+ app.secret_key = "test"
+ app.config["WTF_CSRF_ENABLED"] = False
+
+ with patch(
+ "local_deep_research.web.auth.routes.render_template"
+ ) as mock_render:
+ mock_render.return_value = "Change Password Page"
+
+ from local_deep_research.web.auth.routes import auth_bp
+
+ app.register_blueprint(auth_bp)
+
+ with app.test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+
+ response = client.post(
+ "/auth/change-password",
+ data={
+ "current_password": "OldPass123",
+ "new_password": "NewStrongP4ss!",
+ "confirm_password": "DifferentP4ss!",
+ },
+ )
+ assert response.status_code == 400
+
class TestIntegrityCheck:
"""Tests for /integrity-check endpoint."""