Skip to content

A modern platform for visual, flexible, and extensible graph-based investigations. For cybersecurity analysts and investigators.

fix(core): stop 500ing on a malformed sketch id in flow launch, dedupe the resolve-and-check

dextmorgnc5ccc373ddbd
diff --git a/flowsint-core/src/flowsint_core/core/services/enricher_service.py b/flowsint-core/src/flowsint_core/core/services/enricher_service.py
index 1613acf0..64378468 100644
--- a/flowsint-core/src/flowsint_core/core/services/enricher_service.py
+++ b/flowsint-core/src/flowsint_core/core/services/enricher_service.py
@@ -17,7 +17,7 @@ from ..repositories import (
SketchRepository,
)
from .base import BaseService
-from .exceptions import NotFoundError
+from .sketch_access import resolve_sketch_for_launch
def template_params_schema(content: Any) -> List[Dict[str, Any]]:
@@ -61,22 +61,11 @@ class EnricherService(BaseService):
def get_sketch_for_launch(self, sketch_id: str, user_id: UUID) -> Sketch:
"""Resolve the target sketch, denying anyone without update rights on
- its investigation. Mirrors FlowService.get_sketch_for_launch: launching
- an enricher writes nodes into the sketch, so it is an update."""
- try:
- # The payload carries sketch_id as a free string. SketchRepository
- # binds it as a UUID, so parse here: a value that is not a UUID
- # names no sketch and has to be denied, not surfaced as a 500.
- parsed_id = UUID(sketch_id)
- except ValueError:
- raise NotFoundError("Sketch not found")
-
- sketch = self._sketch_repo.get_by_id(parsed_id)
- if not sketch:
- raise NotFoundError("Sketch not found")
-
- self._check_permission(user_id, sketch.investigation_id, ["update"])
- return sketch
+ its investigation. Launching an enricher writes nodes into the
+ sketch, so it is an update."""
+ return resolve_sketch_for_launch(
+ self._sketch_repo, self._check_permission, sketch_id, user_id
+ )
# enricher_registry is flowsint_enrichers' EnricherRegistry. That package
# ships no py.typed marker, so naming the class would resolve to Any
diff --git a/flowsint-core/src/flowsint_core/core/services/flow_service.py b/flowsint-core/src/flowsint_core/core/services/flow_service.py
index a997b370..a125013f 100644
--- a/flowsint-core/src/flowsint_core/core/services/flow_service.py
+++ b/flowsint-core/src/flowsint_core/core/services/flow_service.py
@@ -17,6 +17,7 @@ from ..repositories import (
)
from .base import BaseService
from .exceptions import NotFoundError, PermissionDeniedError
+from .sketch_access import resolve_sketch_for_launch
class FlowService(BaseService):
@@ -31,7 +32,7 @@ class FlowService(BaseService):
custom_type_repo: CustomTypeRepository,
sketch_repo: SketchRepository,
investigation_repo: InvestigationRepository,
- **kwargs,
+ **kwargs: Any,
):
super().__init__(db, **kwargs)
self._flow_repo = flow_repo
@@ -39,9 +40,7 @@ class FlowService(BaseService):
self._sketch_repo = sketch_repo
self._investigation_repo = investigation_repo
- def get_all_flows(
- self, category: Optional[str], user_id: UUID
- ) -> List[Dict[str, Any]]:
+ def get_all_flows(self, category: Optional[str], user_id: UUID) -> List[Flow]:
if not category or category.lower() == "undefined":
return self._flow_repo.get_all_with_optional_category(None, user_id)
@@ -51,15 +50,7 @@ class FlowService(BaseService):
)
if custom_type:
- flows = self._flow_repo.get_all_with_optional_category(None, user_id)
return []
- return [
- {
- **(flow.to_dict() if hasattr(flow, "to_dict") else flow.__dict__),
- "wobblyType": True,
- }
- for flow in flows
- ]
return self._flow_repo.get_all_with_optional_category(category, user_id)
@@ -123,12 +114,9 @@ class FlowService(BaseService):
self._commit()
def get_sketch_for_launch(self, sketch_id: str, user_id: UUID) -> Sketch:
- sketch = self._sketch_repo.get_by_id(sketch_id)
- if not sketch:
- raise NotFoundError("Sketch not found")
-
- self._check_permission(user_id, sketch.investigation_id, ["update"])
- return sketch
+ return resolve_sketch_for_launch(
+ self._sketch_repo, self._check_permission, sketch_id, user_id
+ )
def create_flow_service(db: Session) -> FlowService:
diff --git a/flowsint-core/src/flowsint_core/core/services/sketch_access.py b/flowsint-core/src/flowsint_core/core/services/sketch_access.py
new file mode 100644
index 00000000..9f75da28
--- /dev/null
+++ b/flowsint-core/src/flowsint_core/core/services/sketch_access.py
@@ -0,0 +1,35 @@
+"""Shared sketch-resolution logic for services that launch work into a sketch."""
+
+from typing import Callable, List
+from uuid import UUID
+
+from ..models import Sketch
+from ..repositories import SketchRepository
+from .exceptions import NotFoundError
+
+
+def resolve_sketch_for_launch(
+ sketch_repo: SketchRepository,
+ check_permission: Callable[[UUID, UUID, List[str]], bool],
+ sketch_id: str,
+ user_id: UUID,
+) -> Sketch:
+ """Resolve the sketch a launch will write into, denying anyone without
+ update rights on its investigation.
+
+ A sketch_id that isn't a valid UUID names no sketch, so it's denied the
+ same way as a missing one -- never surfaced as a 500 from the repository's
+ UUID bind. Single source of truth: this used to be copy-pasted per
+ service, and the copies drifted (one parsed the id, the other didn't).
+ """
+ try:
+ parsed_id = UUID(sketch_id)
+ except (ValueError, TypeError, AttributeError):
+ raise NotFoundError("Sketch not found")
+
+ sketch = sketch_repo.get_by_id(parsed_id)
+ if not sketch:
+ raise NotFoundError("Sketch not found")
+
+ check_permission(user_id, sketch.investigation_id, ["update"])
+ return sketch
diff --git a/flowsint-core/tests/services/test_flow_service.py b/flowsint-core/tests/services/test_flow_service.py
new file mode 100644
index 00000000..9a4e79ce
--- /dev/null
+++ b/flowsint-core/tests/services/test_flow_service.py
@@ -0,0 +1,77 @@
+"""Tests for FlowService.get_sketch_for_launch.
+
+This used to hand-roll the same resolve-then-check logic as
+EnricherService.get_sketch_for_launch, and the two copies drifted: this one
+never parsed sketch_id to a UUID before handing it to the repository, so a
+malformed id crashed instead of denying. Both now share
+sketch_access.resolve_sketch_for_launch -- these tests pin the behavior on
+this caller.
+"""
+
+from uuid import uuid4
+
+import pytest
+
+from flowsint_core.core.services.exceptions import NotFoundError, PermissionDeniedError
+from flowsint_core.core.services.flow_service import create_flow_service
+from flowsint_core.core.types import Role
+from tests.factories import (
+ InvestigationFactory,
+ InvestigationUserRoleFactory,
+ ProfileFactory,
+ SketchFactory,
+)
+
+
+def _setup(db_session):
+ ProfileFactory._meta.sqlalchemy_session = db_session
+ InvestigationFactory._meta.sqlalchemy_session = db_session
+ InvestigationUserRoleFactory._meta.sqlalchemy_session = db_session
+ SketchFactory._meta.sqlalchemy_session = db_session
+
+
+class TestGetSketchForLaunch:
+ def test_malformed_sketch_id_is_denied_not_crashed(self, db_session):
+ _setup(db_session)
+ service = create_flow_service(db_session)
+
+ with pytest.raises(NotFoundError):
+ service.get_sketch_for_launch("not-a-uuid", uuid4())
+
+ def test_unknown_sketch_id_is_denied(self, db_session):
+ _setup(db_session)
+ service = create_flow_service(db_session)
+
+ with pytest.raises(NotFoundError):
+ service.get_sketch_for_launch(str(uuid4()), uuid4())
+
+ def test_user_without_a_role_is_forbidden(self, db_session):
+ _setup(db_session)
+ sketch = SketchFactory()
+ service = create_flow_service(db_session)
+
+ with pytest.raises(PermissionDeniedError):
+ service.get_sketch_for_launch(str(sketch.id), uuid4())
+
+ def test_a_viewer_is_forbidden(self, db_session):
+ _setup(db_session)
+ sketch = SketchFactory()
+ role = InvestigationUserRoleFactory(
+ investigation=sketch.investigation, roles=[Role.VIEWER]
+ )
+ service = create_flow_service(db_session)
+
+ with pytest.raises(PermissionDeniedError):
+ service.get_sketch_for_launch(str(sketch.id), role.user_id)
+
+ def test_an_editor_can_launch(self, db_session):
+ _setup(db_session)
+ sketch = SketchFactory()
+ role = InvestigationUserRoleFactory(
+ investigation=sketch.investigation, roles=[Role.EDITOR]
+ )
+ service = create_flow_service(db_session)
+
+ resolved = service.get_sketch_for_launch(str(sketch.id), role.user_id)
+
+ assert resolved.id == sketch.id