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.pyindex 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_permissionfrom fastapi import APIRouter, HTTPException, Depends, statusfrom typing import Listfrom datetime import datetime+from sqlalchemy import or_from sqlalchemy.orm import Sessionfrom 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 Rolefrom app.api.deps import get_current_userfrom 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 analysisdiff --git a/flowsint-api/app/api/routes/chat.py b/flowsint-api/app/api/routes/chat.pyindex 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, uuid4from fastapi import APIRouter, HTTPException, Depends, statusfrom typing import Dict, List, Optionalfrom datetime import datetime+from sqlalchemy import or_from sqlalchemy.orm import Sessionfrom 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 Rolefrom app.api.deps import get_current_userfrom app.api.schemas.chat import ChatCreate, ChatRead+from app.security.permissions import check_investigation_permissionrouter = APIRouter()diff --git a/flowsint-api/app/api/routes/events.py b/flowsint-api/app/api/routes/events.pyindex 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, Requestfrom sqlalchemy.orm import Sessionfrom flowsint_core.core.postgre_db import get_db-from flowsint_core.core.models import Log, Sketch+from flowsint_core.core.models import Log, Sketch, Scanfrom flowsint_core.core.events import event_emitterfrom sse_starlette.sse import EventSourceResponsefrom 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_ssefrom flowsint_core.core.models import Profile, Sketch+from app.security.permissions import check_investigation_permissionimport jsonimport asynciofrom 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_idawait 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.pyindex 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, FlowBranchfrom sqlalchemy.orm import Sessionfrom 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, Sketchfrom app.api.deps import get_current_userfrom sqlalchemy import funcfrom app.api.schemas.flow import FlowRead, FlowCreate, FlowUpdate+from app.security.permissions import check_investigation_permissionfrom 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 IDsgraph_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.pyindex 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.pyindex 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 UUIDfrom fastapi import APIRouter, HTTPException, Depends, statusfrom typing import List+from sqlalchemy import or_from sqlalchemy.orm import Sessionfrom 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 Rolefrom app.api.deps import get_current_userfrom app.api.schemas.scan import ScanRead+from app.security.permissions import check_investigation_permissionrouter = 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 Nonediff --git a/flowsint-api/app/api/routes/sketches.py b/flowsint-api/app/api/routes/sketches.pyindex 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_REGISTRYfrom pydantic import BaseModel, Field-from typing import Literal, List, Optional, Dict, Any-from datetime import datetime, timezone+from typing import Literal, List, Dict, Anyfrom flowsint_core.utils import flattenfrom sqlalchemy.orm import Sessionfrom app.api.schemas.sketch import SketchCreate, SketchRead, SketchUpdate@@ -24,6 +23,9 @@ from flowsint_core.core.postgre_db import get_dbfrom app.api.deps import get_current_userfrom flowsint_core.imports import FileParseResult, parse_import_filefrom 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}")