Skip to content

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

fix(api): patch missing permissions on api routes

dextmorgna15ce33df4ec
diff --git a/flowsint-api/app/api/routes/analysis.py b/flowsint-api/app/api/routes/analysis.py
index ce839060..d66bf651 100644
--- a/flowsint-api/app/api/routes/analysis.py
+++ b/flowsint-api/app/api/routes/analysis.py
@@ -3,9 +3,11 @@ from app.security.permissions import check_investigation_permission
from fastapi import APIRouter, HTTPException, Depends, status
from typing import List
from datetime import datetime
+from sqlalchemy import or_
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import Analysis, Profile
+from flowsint_core.core.models import Analysis, Profile, InvestigationUserRole
+from flowsint_core.core.types import Role
from app.api.deps import get_current_user
from app.api.schemas.analysis import AnalysisRead, AnalysisCreate, AnalysisUpdate
@@ -17,8 +19,21 @@ router = APIRouter()
def get_analyses(
db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)
):
- analyses = db.query(Analysis).filter(Analysis.owner_id == current_user.id).all()
- return analyses
+ # Get all analyses from investigations where user has at least VIEWER role
+ allowed_roles_for_read = [Role.OWNER, Role.EDITOR, Role.VIEWER]
+
+ query = db.query(Analysis).join(
+ InvestigationUserRole,
+ InvestigationUserRole.investigation_id == Analysis.investigation_id,
+ )
+
+ query = query.filter(InvestigationUserRole.user_id == current_user.id)
+
+ # Filter by allowed roles
+ conditions = [InvestigationUserRole.roles.any(role) for role in allowed_roles_for_read]
+ query = query.filter(or_(*conditions))
+
+ return query.distinct().all()
# Create a New analysis
diff --git a/flowsint-api/app/api/routes/chat.py b/flowsint-api/app/api/routes/chat.py
index 95a28a6b..1878c670 100644
--- a/flowsint-api/app/api/routes/chat.py
+++ b/flowsint-api/app/api/routes/chat.py
@@ -9,11 +9,14 @@ from uuid import UUID, uuid4
from fastapi import APIRouter, HTTPException, Depends, status
from typing import Dict, List, Optional
from datetime import datetime
+from sqlalchemy import or_
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import Chat, ChatMessage, Profile
+from flowsint_core.core.models import Chat, ChatMessage, Profile, InvestigationUserRole
+from flowsint_core.core.types import Role
from app.api.deps import get_current_user
from app.api.schemas.chat import ChatCreate, ChatRead
+from app.security.permissions import check_investigation_permission
router = APIRouter()
diff --git a/flowsint-api/app/api/routes/events.py b/flowsint-api/app/api/routes/events.py
index bde6a138..d189e4d3 100644
--- a/flowsint-api/app/api/routes/events.py
+++ b/flowsint-api/app/api/routes/events.py
@@ -1,12 +1,13 @@
from fastapi import APIRouter, Depends, HTTPException, Request
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import Log, Sketch
+from flowsint_core.core.models import Log, Sketch, Scan
from flowsint_core.core.events import event_emitter
from sse_starlette.sse import EventSourceResponse
from flowsint_core.core.types import Event
-from app.api.deps import get_current_user
+from app.api.deps import get_current_user, get_current_user_sse
from flowsint_core.core.models import Profile, Sketch
+from app.security.permissions import check_investigation_permission
import json
import asyncio
from datetime import datetime, timedelta
@@ -20,7 +21,7 @@ def get_logs_by_sketch(
limit: int = 100,
since: datetime | None = None,
db: Session = Depends(get_db),
- # current_user: Profile = Depends(get_current_user)
+ current_user: Profile = Depends(get_current_user)
):
"""Get historical logs for a specific sketch with optional filtering"""
# Check if sketch exists
@@ -30,6 +31,10 @@ def get_logs_by_sketch(
status_code=404, detail=f"Sketch with id {sketch_id} not found"
)
+ check_investigation_permission(
+ current_user.id, sketch.investigation_id, actions=["read"], db=db
+ )
+
print(
f"[EventEmitter] Fetching logs for sketch {sketch_id} (limit: {limit}, since: {since})"
)
@@ -79,7 +84,7 @@ async def stream_events(
request: Request,
sketch_id: str,
db: Session = Depends(get_db),
- # current_user: Profile = Depends(get_current_user)
+ current_user: Profile = Depends(get_current_user_sse)
):
"""Stream events for a specific scan in real-time"""
@@ -90,6 +95,10 @@ async def stream_events(
status_code=404, detail=f"Sketch with id {sketch_id} not found"
)
+ check_investigation_permission(
+ current_user.id, sketch.investigation_id, actions=["read"], db=db
+ )
+
async def event_generator():
channel = sketch_id
await event_emitter.subscribe(channel)
@@ -139,6 +148,17 @@ def delete_scan_logs(
current_user: Profile = Depends(get_current_user),
):
"""Delete all logs for a specific scan"""
+ # Check if sketch exists and user has permission
+ sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
+ if not sketch:
+ raise HTTPException(
+ status_code=404, detail=f"Sketch with id {sketch_id} not found"
+ )
+
+ check_investigation_permission(
+ current_user.id, sketch.investigation_id, actions=["delete"], db=db
+ )
+
try:
db.query(Log).filter(Log.sketch_id == sketch_id).delete()
db.commit()
@@ -153,6 +173,7 @@ async def stream_sketch_status(
request: Request,
sketch_id: str,
db: Session = Depends(get_db),
+ current_user: Profile = Depends(get_current_user_sse)
):
"""Stream COMPLETED events for a specific sketch (for graph refresh)"""
@@ -163,6 +184,10 @@ async def stream_sketch_status(
status_code=404, detail=f"Sketch with id {sketch_id} not found"
)
+ check_investigation_permission(
+ current_user.id, sketch.investigation_id, actions=["read"], db=db
+ )
+
async def status_generator():
channel = f"{sketch_id}_status"
await event_emitter.subscribe(channel)
@@ -202,9 +227,28 @@ async def stream_sketch_status(
@router.get("/status/scan/{scan_id}/stream")
-async def stream_status(request: Request, scan_id: str, db: Session = Depends(get_db)):
+async def stream_status(
+ request: Request,
+ scan_id: str,
+ db: Session = Depends(get_db),
+ current_user: Profile = Depends(get_current_user_sse)
+):
"""Stream status updates for a specific scan in real-time"""
+ # Check if scan exists and user has permission
+ scan = db.query(Scan).filter(Scan.id == scan_id).first()
+ if not scan:
+ raise HTTPException(
+ status_code=404, detail=f"Scan with id {scan_id} not found"
+ )
+
+ # Check investigation permission via sketch
+ sketch = db.query(Sketch).filter(Sketch.id == scan.sketch_id).first()
+ if sketch:
+ check_investigation_permission(
+ current_user.id, sketch.investigation_id, actions=["read"], db=db
+ )
+
async def status_generator():
print("[EventEmitter] Start status generator")
await event_emitter.subscribe(f"scan_{scan_id}_status")
diff --git a/flowsint-api/app/api/routes/flows.py b/flowsint-api/app/api/routes/flows.py
index 37a1b615..ebed538a 100644
--- a/flowsint-api/app/api/routes/flows.py
+++ b/flowsint-api/app/api/routes/flows.py
@@ -24,10 +24,11 @@ from flowsint_types import (
from flowsint_core.core.types import Node, Edge, FlowStep, FlowBranch
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import Flow, Profile, CustomType
+from flowsint_core.core.models import Flow, Profile, CustomType, Sketch
from app.api.deps import get_current_user
from sqlalchemy import func
from app.api.schemas.flow import FlowRead, FlowCreate, FlowUpdate
+from app.security.permissions import check_investigation_permission
from flowsint_types import (
ASN,
CIDR,
@@ -250,6 +251,15 @@ async def launch_flow(
if flow is None:
raise HTTPException(status_code=404, detail="flow not found")
+ # Check investigation permission via sketch
+ sketch = db.query(Sketch).filter(Sketch.id == payload.sketch_id).first()
+ if not sketch:
+ raise HTTPException(status_code=404, detail="Sketch not found")
+
+ check_investigation_permission(
+ current_user.id, sketch.investigation_id, actions=["update"], db=db
+ )
+
# Retrieve nodes from Neo4J by their element IDs
graph_repo = GraphRepository()
nodes_data = graph_repo.get_nodes_by_ids(payload.node_ids, payload.sketch_id)
diff --git a/flowsint-api/app/api/routes/investigations.py b/flowsint-api/app/api/routes/investigations.py
index b77728bf..043b6e0d 100644
--- a/flowsint-api/app/api/routes/investigations.py
+++ b/flowsint-api/app/api/routes/investigations.py
@@ -140,6 +140,7 @@ def get_sketches_by_investigation(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
+ check_investigation_permission(current_user.id, investigation_id, actions=["read"], db=db)
sketches = (
db.query(Sketch).filter(Sketch.investigation_id == investigation_id).all()
)
@@ -158,6 +159,7 @@ def update_investigation(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
+ check_investigation_permission(current_user.id, investigation_id, actions=["write"], db=db)
investigation = (
db.query(Investigation).filter(Investigation.id == investigation_id).first()
)
@@ -181,6 +183,7 @@ def delete_investigation(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
+ check_investigation_permission(current_user.id, investigation_id, actions=["delete"], db=db)
investigation = (
db.query(Investigation)
.filter(
diff --git a/flowsint-api/app/api/routes/scan.py b/flowsint-api/app/api/routes/scan.py
index bb4cf9db..ee331ecb 100644
--- a/flowsint-api/app/api/routes/scan.py
+++ b/flowsint-api/app/api/routes/scan.py
@@ -1,11 +1,14 @@
from uuid import UUID
from fastapi import APIRouter, HTTPException, Depends, status
from typing import List
+from sqlalchemy import or_
from sqlalchemy.orm import Session
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import Scan, Profile
+from flowsint_core.core.models import Scan, Profile, Sketch, InvestigationUserRole
+from flowsint_core.core.types import Role
from app.api.deps import get_current_user
from app.api.schemas.scan import ScanRead
+from app.security.permissions import check_investigation_permission
router = APIRouter()
@@ -18,8 +21,23 @@ router = APIRouter()
def get_scans(
db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)
):
- scans = db.query(Scan).all()
- return scans
+ # Get all scans from sketches in investigations where user has at least VIEWER role
+ allowed_roles_for_read = [Role.OWNER, Role.EDITOR, Role.VIEWER]
+
+ query = db.query(Scan).join(
+ Sketch, Sketch.id == Scan.sketch_id
+ ).join(
+ InvestigationUserRole,
+ InvestigationUserRole.investigation_id == Sketch.investigation_id,
+ )
+
+ query = query.filter(InvestigationUserRole.user_id == current_user.id)
+
+ # Filter by allowed roles
+ conditions = [InvestigationUserRole.roles.any(role) for role in allowed_roles_for_read]
+ query = query.filter(or_(*conditions))
+
+ return query.distinct().all()
# Get a scan by ID
@@ -31,33 +49,36 @@ def get_scan_by_id(
):
scan = db.query(Scan).filter(Scan.id == id).first()
if not scan:
- raise HTTPException(status_code=404, detail="Enricher not found")
- return scan
+ raise HTTPException(status_code=404, detail="Scan not found")
+ # Check investigation permission via sketch
+ sketch = db.query(Sketch).filter(Sketch.id == scan.sketch_id).first()
+ if sketch:
+ check_investigation_permission(
+ current_user.id, sketch.investigation_id, actions=["read"], db=db
+ )
-# Delete a scan by ID
[email protected]("", status_code=status.HTTP_204_NO_CONTENT)
-def delete_scan(db: Session = Depends(get_db)):
- db.query(Scan).delete()
- db.commit()
- return None
+ return scan
# Delete a scan by ID
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
-def delete_scan(
+def delete_scan_by_id(
id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- scan = (
- db.query(Scan).filter(Scan.id == id, Scan.owner_id == current_user.id).first()
- )
+ scan = db.query(Scan).filter(Scan.id == id).first()
if not scan:
raise HTTPException(status_code=404, detail="Scan not found")
- scan = db.query(Scan).filter(Scan.id == id).all()
- # Finally delete the scan
+ # Check investigation permission via sketch
+ sketch = db.query(Sketch).filter(Sketch.id == scan.sketch_id).first()
+ if sketch:
+ check_investigation_permission(
+ current_user.id, sketch.investigation_id, actions=["delete"], db=db
+ )
+
db.delete(scan)
db.commit()
return None
diff --git a/flowsint-api/app/api/routes/sketches.py b/flowsint-api/app/api/routes/sketches.py
index dae084d3..c225c7e2 100644
--- a/flowsint-api/app/api/routes/sketches.py
+++ b/flowsint-api/app/api/routes/sketches.py
@@ -11,8 +11,7 @@ from fastapi import (
)
from flowsint_types import TYPE_REGISTRY
from pydantic import BaseModel, Field
-from typing import Literal, List, Optional, Dict, Any
-from datetime import datetime, timezone
+from typing import Literal, List, Dict, Any
from flowsint_core.utils import flatten
from sqlalchemy.orm import Session
from app.api.schemas.sketch import SketchCreate, SketchRead, SketchUpdate
@@ -24,6 +23,9 @@ from flowsint_core.core.postgre_db import get_db
from app.api.deps import get_current_user
from flowsint_core.imports import FileParseResult, parse_import_file
from app.api.sketch_utils import update_sketch_timestamp
+from flowsint_core.core.types import Role
+from sqlalchemy import or_
+
router = APIRouter()
@@ -90,7 +92,23 @@ def create_sketch(
def list_sketches(
db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)
):
- return db.query(Sketch).filter(Sketch.owner_id == current_user.id).all()
+ from flowsint_core.core.models import InvestigationUserRole
+
+ # Get all investigations where user has at least VIEWER role
+ allowed_roles_for_read = [Role.OWNER, Role.EDITOR, Role.VIEWER]
+
+ query = db.query(Sketch).join(
+ InvestigationUserRole,
+ InvestigationUserRole.investigation_id == Sketch.investigation_id,
+ )
+
+ query = query.filter(InvestigationUserRole.user_id == current_user.id)
+
+ # Filter by allowed roles
+ conditions = [InvestigationUserRole.roles.any(role) for role in allowed_roles_for_read]
+ query = query.filter(or_(*conditions))
+
+ return query.distinct().all()
@router.get("/{sketch_id}")