Skip to content

Commit 12e6cce

Browse files
committed
fix(api): authorize deprecated vertex streams
1 parent 6a8de81 commit 12e6cce

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/backend/base/langflow/api/v1/chat.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
VerticesOrderResponse,
4747
)
4848
from langflow.exceptions.component import ComponentBuildError
49-
from langflow.services.auth.utils import get_current_active_user, get_current_user_optional
49+
from langflow.services.auth.utils import get_current_user_optional
5050
from langflow.services.authorization import FlowAction, ensure_flow_permission
5151
from langflow.services.authorization.fetch import deny_to_404
5252
from langflow.services.chat.service import ChatService
@@ -690,12 +690,12 @@ async def _stream_vertex(flow_id: str, vertex_id: str, chat_service: ChatService
690690
"/build/{flow_id}/{vertex_id}/stream",
691691
response_class=StreamingResponse,
692692
deprecated=True,
693-
dependencies=[Depends(get_current_active_user)],
694693
include_in_schema=False,
695694
)
696695
async def build_vertex_stream(
697696
flow_id: uuid.UUID,
698697
vertex_id: str,
698+
current_user: CurrentActiveUser,
699699
):
700700
"""Build a vertex instead of the entire graph.
701701
@@ -722,6 +722,28 @@ async def build_vertex_stream(
722722
Raises:
723723
HTTPException: If an error occurs while building the vertex.
724724
"""
725+
# The cache is keyed only by flow UUID and may contain another user's
726+
# in-memory graph. Authorize before constructing the streaming response so
727+
# an authenticated non-owner cannot read a built result or invoke
728+
# ``vertex.stream()`` on that cached graph.
729+
async with session_scope() as session:
730+
stmt = (
731+
select(Flow)
732+
.where(Flow.id == flow_id)
733+
.where((Flow.user_id == current_user.id) | (Flow.access_type == AccessTypeEnum.PUBLIC))
734+
)
735+
flow = (await session.exec(stmt)).first()
736+
if not flow:
737+
raise HTTPException(status_code=404, detail=f"Flow with id {flow_id} not found")
738+
await ensure_flow_permission(
739+
current_user,
740+
FlowAction.EXECUTE,
741+
flow_id=flow_id,
742+
flow_user_id=flow.user_id,
743+
workspace_id=flow.workspace_id,
744+
folder_id=flow.folder_id,
745+
)
746+
725747
try:
726748
return StreamingResponse(
727749
_stream_vertex(str(flow_id), vertex_id, get_chat_service()),

src/backend/tests/unit/test_endpoints.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,18 @@ async def test_build_vertex_returns_404_for_other_users_private_flow(
426426
assert response.status_code == 404, response.text
427427

428428

429+
async def test_build_vertex_stream_returns_404_for_other_users_private_flow(
430+
client, added_flow_webhook_test, second_user_headers
431+
):
432+
"""The deprecated stream route must authorize before reading the shared graph cache."""
433+
flow_id = added_flow_webhook_test["id"]
434+
response = await client.get(
435+
f"/api/v1/build/{flow_id}/ChatInput-some-id/stream",
436+
headers=second_user_headers,
437+
)
438+
assert response.status_code == 404, response.text
439+
440+
429441
async def test_build_vertex_invalid_vertex_id(client, added_flow_webhook_test, logged_in_headers):
430442
flow_id = added_flow_webhook_test["id"]
431443
response = await client.post(f"/api/v1/build/{flow_id}/vertices/invalid_vertex_id", headers=logged_in_headers)

0 commit comments

Comments
 (0)