~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(notes): validate API control values (#5396)
Ziyang Guo257e457f43e1
diff --git a/changelog.d/+notes-api-validation.bugfix.md b/changelog.d/+notes-api-validation.bugfix.mdnew file mode 100644index 0000000000..14e3167aac--- /dev/null+++ b/changelog.d/+notes-api-validation.bugfix.md@@ -0,0 +1 @@+Reject invalid Notes API control values and stale similar-passage note IDs instead of executing unintended operations.diff --git a/src/local_deep_research/web/routes/notes_routes.py b/src/local_deep_research/web/routes/notes_routes.pyindex 60b9d04894..cfecc48efe 100644--- a/src/local_deep_research/web/routes/notes_routes.py+++ b/src/local_deep_research/web/routes/notes_routes.py@@ -2,6 +2,8 @@Notes Routes - API endpoints and pages for the notes feature."""+import math+from flask import Blueprint, jsonify, request, sessionfrom loguru import loggerfrom sqlalchemy.exc import IntegrityError@@ -736,11 +738,21 @@ def patch_note_research(note_id, research_id):try:data = request.get_json(silent=True) or {}+ is_collapsed = data.get("is_collapsed")+ if "is_collapsed" in data and not isinstance(+ data["is_collapsed"], bool+ ):+ return jsonify(+ {+ "success": False,+ "error": "is_collapsed must be a boolean",+ }+ ), 400service = NoteService(username)updated = service.update_note_research(note_id=note_id,research_id=research_id,- is_collapsed=data.get("is_collapsed"),+ is_collapsed=is_collapsed,)if not updated:return jsonify(@@ -1418,6 +1430,13 @@ def index_note_to_collection(note_id):collection_id = data["collection_id"]force_reindex = data.get("force_reindex", False)+ if not isinstance(force_reindex, bool):+ return jsonify(+ {+ "success": False,+ "error": "force_reindex must be a boolean",+ }+ ), 400# Verify the id is actually a note before indexing — every sibling# note route guards this, but this one passed the id straight to the@@ -1484,13 +1503,16 @@ def semantic_search_notes():try:limit = _clamp_limit(10)- min_similarity = max(- 0.0, min(float(request.args.get("min_similarity", 0.3)), 1.0)- )+ min_similarity_raw = float(request.args.get("min_similarity", 0.3))+ min_similarity = max(0.0, min(min_similarity_raw, 1.0))except (ValueError, TypeError):return jsonify({"success": False, "error": "Invalid limit or min_similarity"}), 400+ if not math.isfinite(min_similarity_raw):+ return jsonify(+ {"success": False, "error": "Invalid limit or min_similarity"}+ ), 400collection_id = request.args.get("collection_id") or None@@ -1879,6 +1901,9 @@ def similar_passages(note_id):return jsonify({"success": False, "error": "text required"}), 400text = text.strip()[:MAX_PASSAGE_LEN]+ if not NoteService(username).note_exists(note_id):+ return jsonify({"success": False, "error": "Note not found"}), 404+service = NoteAIService(username)passages = service.find_similar_passages(text, exclude_note_id=note_id)return jsonify({"success": True, "passages": passages})@@ -2306,6 +2331,13 @@ def synthesize_notes():}), 400create_note = data.get("create_note", True)+ if not isinstance(create_note, bool):+ return jsonify(+ {+ "success": False,+ "error": "create_note must be a boolean",+ }+ ), 400ai_service = NoteAIService(username)result = ai_service.synthesize_notes(note_ids, synthesis_type)diff --git a/tests/notes/test_notes_api.py b/tests/notes/test_notes_api.pyindex cb8ed05c55..f56d01003f 100644--- a/tests/notes/test_notes_api.py+++ b/tests/notes/test_notes_api.py@@ -925,6 +925,11 @@ class TestNoteRouteFreeTextCaps:ai = MagicMock()ai.find_similar_passages.return_value = []monkeypatch.setattr(notes_routes, "NoteAIService", lambda username: ai)+ note_service = MagicMock()+ note_service.note_exists.return_value = True+ monkeypatch.setattr(+ notes_routes, "NoteService", lambda username: note_service+ )oversize = "y" * (notes_routes.MAX_PASSAGE_LEN + 500)payload, status = _call(diff --git a/tests/notes/test_notes_api_validation_followups.py b/tests/notes/test_notes_api_validation_followups.pynew file mode 100644index 0000000000..d4ec1622e1--- /dev/null+++ b/tests/notes/test_notes_api_validation_followups.py@@ -0,0 +1,158 @@+"""Route-level regressions for Notes API input validation follow-ups."""++from unittest.mock import MagicMock++import pytest++from local_deep_research.web.routes import notes_routes+from tests.notes.test_notes_api import _call, _route_app, _unwrap+++class TestNotesAPIValidationFollowups:+ @pytest.mark.parametrize("value", ["nan", "inf", "-inf"])+ def test_semantic_search_rejects_non_finite_similarity(+ self, monkeypatch, db_session, value+ ):+ app, _ = _route_app(monkeypatch, db_session)+ ai_service = MagicMock()+ monkeypatch.setattr(+ notes_routes, "NoteAIService", lambda username: ai_service+ )++ payload, status = _call(+ app,+ f"/notes/api/notes/semantic-search?q=topic&min_similarity={value}",+ _unwrap(notes_routes.semantic_search_notes),+ )++ assert status == 400+ assert payload["success"] is False+ ai_service.semantic_search.assert_not_called()++ @pytest.mark.parametrize("value", ["false", 0, None, [], {}])+ def test_index_note_rejects_non_boolean_force_reindex(+ self, monkeypatch, db_session, value+ ):+ app, _ = _route_app(monkeypatch, db_session)+ note_service = MagicMock()+ monkeypatch.setattr(+ notes_routes, "NoteService", lambda username: note_service+ )++ payload, status = _call(+ app,+ "/notes/api/notes/note-1/index",+ _unwrap(notes_routes.index_note_to_collection),+ "note-1",+ method="POST",+ json={"collection_id": "collection-1", "force_reindex": value},+ )++ assert status == 400+ assert "force_reindex" in payload["error"]+ note_service.note_exists.assert_not_called()++ @pytest.mark.parametrize("value", ["false", 0, None, [], {}])+ def test_synthesize_rejects_non_boolean_create_note(+ self, monkeypatch, db_session, value+ ):+ app, _ = _route_app(monkeypatch, db_session)+ ai_service = MagicMock()+ monkeypatch.setattr(+ notes_routes, "NoteAIService", lambda username: ai_service+ )++ payload, status = _call(+ app,+ "/notes/api/notes/synthesize",+ _unwrap(notes_routes.synthesize_notes),+ method="POST",+ json={+ "note_ids": ["note-1", "note-2"],+ "synthesis_type": "merge",+ "create_note": value,+ },+ )++ assert status == 400+ assert "create_note" in payload["error"]+ ai_service.synthesize_notes.assert_not_called()++ @pytest.mark.parametrize("value", ["false", 0, [], {}])+ def test_patch_research_rejects_non_boolean_is_collapsed(+ self, monkeypatch, db_session, value+ ):+ app, _ = _route_app(monkeypatch, db_session)+ note_service = MagicMock()+ monkeypatch.setattr(+ notes_routes, "NoteService", lambda username: note_service+ )++ payload, status = _call(+ app,+ "/notes/api/notes/note-1/research/research-1",+ _unwrap(notes_routes.patch_note_research),+ "note-1",+ "research-1",+ method="PATCH",+ json={"is_collapsed": value},+ )++ assert status == 400+ assert "is_collapsed" in payload["error"]+ note_service.update_note_research.assert_not_called()++ def test_patch_research_allows_omitted_is_collapsed(+ self, monkeypatch, db_session+ ):+ app, _ = _route_app(monkeypatch, db_session)+ note_service = MagicMock()+ note_service.update_note_research.return_value = True+ monkeypatch.setattr(+ notes_routes, "NoteService", lambda username: note_service+ )++ payload, status = _call(+ app,+ "/notes/api/notes/note-1/research/research-1",+ _unwrap(notes_routes.patch_note_research),+ "note-1",+ "research-1",+ method="PATCH",+ json={},+ )++ assert status == 200+ assert payload["success"] is True+ note_service.update_note_research.assert_called_once_with(+ note_id="note-1",+ research_id="research-1",+ is_collapsed=None,+ )++ def test_similar_passages_rejects_stale_note_id(+ self, monkeypatch, db_session+ ):+ app, _ = _route_app(monkeypatch, db_session)+ note_service = MagicMock()+ note_service.note_exists.return_value = False+ ai_service = MagicMock()+ monkeypatch.setattr(+ notes_routes, "NoteService", lambda username: note_service+ )+ monkeypatch.setattr(+ notes_routes, "NoteAIService", lambda username: ai_service+ )++ payload, status = _call(+ app,+ "/notes/api/notes/missing/similar-passages",+ _unwrap(notes_routes.similar_passages),+ "missing",+ method="POST",+ json={"text": "selected passage"},+ )++ assert status == 404+ assert "not found" in payload["error"].lower()+ ai_service.find_similar_passages.assert_not_called()