-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Expand file tree
/
Copy pathtest_chat_endpoint.py
More file actions
1456 lines (1154 loc) · 58.1 KB
/
Copy pathtest_chat_endpoint.py
File metadata and controls
1456 lines (1154 loc) · 58.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import contextlib
import json
import uuid
from uuid import UUID
import pytest
from httpx import codes
from langflow.services.database.models.flow import FlowUpdate
from langflow.services.job_queue.service import JobQueueService
from lfx.log.logger import logger
from lfx.memory import aget_messages
from tests.unit.build_utils import build_flow, consume_and_assert_stream, create_flow, get_build_events
@pytest.fixture(autouse=True)
def allow_custom_components_by_default(monkeypatch):
monkeypatch.setenv("LANGFLOW_ALLOW_CUSTOM_COMPONENTS", "true")
@pytest.mark.benchmark
async def test_build_flow(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Test the build flow endpoint with the new two-step process."""
# First create the flow
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Start the build and get job_id
build_response = await build_flow(client, flow_id, logged_in_headers)
job_id = build_response["job_id"]
assert job_id is not None
# Get the events stream
events_response = await get_build_events(client, job_id, logged_in_headers)
assert events_response.status_code == codes.OK
# Consume and verify the events
await consume_and_assert_stream(events_response, job_id)
@pytest.mark.benchmark
async def test_build_flow_from_request_data(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Test building a flow from request data."""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
response = await client.get(f"api/v1/flows/{flow_id}", headers=logged_in_headers)
flow_data = response.json()
# Start the build and get job_id
build_response = await build_flow(client, flow_id, logged_in_headers, json={"data": flow_data["data"]})
job_id = build_response["job_id"]
# Get the events stream
events_response = await get_build_events(client, job_id, logged_in_headers)
assert events_response.status_code == codes.OK
# Consume and verify the events
await consume_and_assert_stream(events_response, job_id)
await check_messages(flow_id)
async def test_build_flow_validates_request_data_instead_of_stale_db_flow(
client, json_memory_chatbot_no_llm, logged_in_headers, monkeypatch
):
"""When request data is provided, preflight validation should use it instead of the saved flow."""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
response = await client.get(f"api/v1/flows/{flow_id}", headers=logged_in_headers)
flow_data = response.json()
request_data = json.loads(json.dumps(flow_data["data"]))
request_data["nodes"][0]["data"]["node"]["display_name"] = "Updated Request Flow"
saved_flow_validation_message = "saved flow should not be validated when request data is provided"
def fail_if_saved_flow_is_validated(target):
if target == flow_data["data"]:
raise ValueError(saved_flow_validation_message)
monkeypatch.setattr(
"langflow.api.v1.chat.validate_flow_for_current_settings",
fail_if_saved_flow_is_validated,
)
response = await client.post(
f"api/v1/build/{flow_id}/flow",
json={"data": request_data},
headers=logged_in_headers,
)
assert response.status_code == codes.OK
assert "job_id" in response.json()
async def test_build_flow_with_frozen_path(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Test building a flow with a frozen path."""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
response = await client.get(f"api/v1/flows/{flow_id}", headers=logged_in_headers)
flow_data = response.json()
flow_data["data"]["nodes"][0]["data"]["node"]["frozen"] = True
# Update the flow with frozen path
response = await client.patch(
f"api/v1/flows/{flow_id}",
json=FlowUpdate(name="Flow", description="description", data=flow_data["data"]).model_dump(),
headers=logged_in_headers,
)
response.raise_for_status()
# Start the build and get job_id
build_response = await build_flow(client, flow_id, logged_in_headers)
job_id = build_response["job_id"]
# Get the events stream
events_response = await get_build_events(client, job_id, logged_in_headers)
assert events_response.status_code == codes.OK
# Consume and verify the events
await consume_and_assert_stream(events_response, job_id)
await check_messages(flow_id)
async def check_messages(flow_id):
if isinstance(flow_id, str):
flow_id = UUID(flow_id)
messages = await aget_messages(flow_id=flow_id, order="ASC")
flow_id_str = str(flow_id)
assert len(messages) == 2
assert messages[0].session_id == flow_id_str
assert messages[0].sender == "User"
assert messages[0].sender_name == "User"
assert messages[0].text == ""
assert messages[1].session_id == flow_id_str
assert messages[1].sender == "Machine"
assert messages[1].sender_name == "AI"
@pytest.mark.benchmark
async def test_build_flow_invalid_job_id(client, logged_in_headers):
"""Test getting events for an invalid job ID."""
invalid_job_id = str(uuid.uuid4())
response = await get_build_events(client, invalid_job_id, logged_in_headers)
assert response.status_code == codes.NOT_FOUND
assert "Job not found" in response.json()["detail"]
@pytest.mark.benchmark
async def test_build_flow_invalid_flow_id(client, logged_in_headers):
"""Test starting a build with an invalid flow ID."""
invalid_flow_id = uuid.uuid4()
response = await client.post(f"api/v1/build/{invalid_flow_id}/flow", json={}, headers=logged_in_headers)
assert response.status_code == codes.NOT_FOUND
@pytest.mark.benchmark
async def test_build_flow_start_only(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Test only the build flow start endpoint."""
# First create the flow
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Start the build and get job_id
build_response = await build_flow(client, flow_id, logged_in_headers)
# Assert response structure
assert "job_id" in build_response
assert isinstance(build_response["job_id"], str)
# Verify it's a valid UUID
assert uuid.UUID(build_response["job_id"])
@pytest.mark.benchmark
async def test_build_flow_start_with_inputs(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Test the build flow start endpoint with input data."""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Start build with some input data
test_inputs = {"inputs": {"session": "test_session", "input_value": "test message"}}
build_response = await build_flow(client, flow_id, logged_in_headers, json=test_inputs)
assert "job_id" in build_response
assert isinstance(build_response["job_id"], str)
assert uuid.UUID(build_response["job_id"])
@pytest.mark.benchmark
async def test_build_flow_polling(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Test the build flow endpoint with polling (non-streaming)."""
# First create the flow
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Start the build and get job_id
build_response = await build_flow(client, flow_id, logged_in_headers)
assert "job_id" in build_response, f"Expected job_id in build_response, got {build_response}"
job_id = build_response["job_id"]
assert job_id is not None
# Create a response object that mimics a streaming response but uses polling
class PollingResponse:
def __init__(self, client, job_id, headers):
self.client = client
self.job_id = job_id
self.headers = headers
self.status_code = codes.OK
self.max_total_events = 50 # Limit to prevent infinite loops
self.max_empty_polls = 10 # Maximum number of empty polls before giving up
self.poll_timeout = 3.0 # Timeout for each polling request
self._closed = False
async def aiter_lines(self):
if self._closed:
return
try:
empty_polls = 0
total_events = 0
end_event_found = False
while (
empty_polls < self.max_empty_polls
and total_events < self.max_total_events
and not end_event_found
and not self._closed
):
# Add Accept header for NDJSON
headers = {**self.headers, "Accept": "application/x-ndjson"}
try:
# Set a timeout for the request
response = await asyncio.wait_for(
self.client.get(
f"api/v1/build/{self.job_id}/events?event_delivery=polling",
headers=headers,
),
timeout=self.poll_timeout,
)
if response.status_code != codes.OK:
break
# Get the NDJSON response as text
text = response.text
# Skip if response is empty
if not text.strip():
empty_polls += 1
await asyncio.sleep(0.1)
continue
# Reset empty polls counter since we got data
empty_polls = 0
# Process each line as an individual JSON object
line_count = 0
for line in text.splitlines():
if not line.strip():
continue
line_count += 1
total_events += 1
# Check for end event with multiple possible formats
if '"event":"end"' in line or '"event": "end"' in line:
end_event_found = True
# Validate it's proper JSON before yielding
try:
json.loads(line) # Test parse to ensure it's valid JSON
yield line
except json.JSONDecodeError as e:
logger.debug(f"WARNING: Skipping invalid JSON: {line}")
logger.debug(f"Error: {e}")
# Don't yield invalid JSON, but continue processing other lines
# If we had no events in this batch, count as empty poll
if line_count == 0:
empty_polls += 1
# Add a small delay to prevent tight polling
await asyncio.sleep(0.1)
except asyncio.TimeoutError:
logger.debug(f"WARNING: Polling request timed out after {self.poll_timeout}s")
empty_polls += 1
continue
# If we hit the limit without finding the end event, log a warning
if total_events >= self.max_total_events:
logger.debug(
f"WARNING: Reached maximum event limit ({self.max_total_events}) without finding end event"
)
if empty_polls >= self.max_empty_polls and not end_event_found:
logger.debug(
f"WARNING: Reached maximum empty polls ({self.max_empty_polls}) without finding end event"
)
except Exception as e:
logger.debug(f"ERROR: Unexpected error during polling: {e!s}")
raise
finally:
self._closed = True
def close(self):
self._closed = True
polling_response = PollingResponse(client, job_id, logged_in_headers)
# Use the same consume_and_assert_stream function to verify the events
await consume_and_assert_stream(polling_response, job_id)
@pytest.mark.benchmark
async def test_cancel_build_unexpected_error(client, json_memory_chatbot_no_llm, logged_in_headers, monkeypatch):
"""Test handling of unexpected exceptions during flow build cancellation."""
# First create the flow
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Start the build and get job_id
build_response = await build_flow(client, flow_id, logged_in_headers)
job_id = build_response["job_id"]
assert job_id is not None
# Mock the cancel_flow_build function to raise an unexpected exception
import langflow.api.v1.chat
original_cancel_flow_build = langflow.api.v1.chat.cancel_flow_build
async def mock_cancel_flow_build_with_error(*_args, **_kwargs):
msg = "Unexpected error during cancellation"
raise RuntimeError(msg)
monkeypatch.setattr(langflow.api.v1.chat, "cancel_flow_build", mock_cancel_flow_build_with_error)
try:
# Try to cancel the build - should return 500 Internal Server Error
cancel_response = await client.post(f"api/v1/build/{job_id}/cancel", headers=logged_in_headers)
assert cancel_response.status_code == codes.INTERNAL_SERVER_ERROR
# Verify the error message
response_data = cancel_response.json()
assert "detail" in response_data
assert "Unexpected error during cancellation" in response_data["detail"]
finally:
# Restore the original function to avoid affecting other tests
monkeypatch.setattr(langflow.api.v1.chat, "cancel_flow_build", original_cancel_flow_build)
@pytest.mark.benchmark
async def test_cancel_build_success(client, json_memory_chatbot_no_llm, logged_in_headers, monkeypatch):
"""Test successful cancellation of a flow build."""
# First create the flow
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Start the build and get job_id
build_response = await build_flow(client, flow_id, logged_in_headers)
job_id = build_response["job_id"]
assert job_id is not None
# Mock the cancel_flow_build function to simulate a successful cancellation
import langflow.api.v1.chat
original_cancel_flow_build = langflow.api.v1.chat.cancel_flow_build
async def mock_successful_cancel_flow_build(*_args, **_kwargs):
return True # Return True to indicate successful cancellation
monkeypatch.setattr(langflow.api.v1.chat, "cancel_flow_build", mock_successful_cancel_flow_build)
try:
# Try to cancel the build (should return success)
cancel_response = await client.post(f"api/v1/build/{job_id}/cancel", headers=logged_in_headers)
assert cancel_response.status_code == codes.OK
# Verify the response structure indicates success
response_data = cancel_response.json()
assert "success" in response_data
assert "message" in response_data
assert response_data["success"] is True
assert "cancelled successfully" in response_data["message"].lower()
finally:
# Restore the original function to avoid affecting other tests
monkeypatch.setattr(langflow.api.v1.chat, "cancel_flow_build", original_cancel_flow_build)
@pytest.mark.benchmark
async def test_cancel_nonexistent_build(client, logged_in_headers):
"""Test cancelling a non-existent flow build."""
# Generate a random job_id that doesn't exist
invalid_job_id = str(uuid.uuid4())
# Try to cancel a non-existent build
response = await client.post(f"api/v1/build/{invalid_job_id}/cancel", headers=logged_in_headers)
assert response.status_code == codes.NOT_FOUND
assert "Job not found" in response.json()["detail"]
@pytest.mark.benchmark
async def test_cancel_build_failure(client, json_memory_chatbot_no_llm, logged_in_headers, monkeypatch):
"""Test handling of cancellation failure."""
# First create the flow
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Start the build and get job_id
build_response = await build_flow(client, flow_id, logged_in_headers)
job_id = build_response["job_id"]
assert job_id is not None
# Mock the cancel_flow_build function to simulate a failure
# The import path in monkeypatch should match exactly how it's imported in the application
import langflow.api.v1.chat
original_cancel_flow_build = langflow.api.v1.chat.cancel_flow_build
async def mock_cancel_flow_build(*_args, **_kwargs):
return False # Return False to indicate cancellation failure
monkeypatch.setattr(langflow.api.v1.chat, "cancel_flow_build", mock_cancel_flow_build)
try:
# Try to cancel the build (should return failure but success=False)
cancel_response = await client.post(f"api/v1/build/{job_id}/cancel", headers=logged_in_headers)
assert cancel_response.status_code == codes.OK
# Verify the response structure indicates failure
response_data = cancel_response.json()
assert "success" in response_data
assert "message" in response_data
assert response_data["success"] is False
assert "Failed to cancel" in response_data["message"]
finally:
# Restore the original function to avoid affecting other tests
monkeypatch.setattr(langflow.api.v1.chat, "cancel_flow_build", original_cancel_flow_build)
@pytest.mark.benchmark
async def test_cancel_build_with_cancelled_error(client, json_memory_chatbot_no_llm, logged_in_headers, monkeypatch):
"""Test handling of CancelledError during cancellation (should be treated as failure)."""
# First create the flow
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Start the build and get job_id
build_response = await build_flow(client, flow_id, logged_in_headers)
job_id = build_response["job_id"]
assert job_id is not None
# Mock the cancel_flow_build function to raise CancelledError
import asyncio
import langflow.api.v1.chat
original_cancel_flow_build = langflow.api.v1.chat.cancel_flow_build
async def mock_cancel_flow_build_with_cancelled_error(*_args, **_kwargs):
msg = "Task cancellation failed"
raise asyncio.CancelledError(msg)
monkeypatch.setattr(langflow.api.v1.chat, "cancel_flow_build", mock_cancel_flow_build_with_cancelled_error)
try:
# Try to cancel the build - should return failure when CancelledError is raised
# since our implementation treats CancelledError as a failed cancellation
cancel_response = await client.post(f"api/v1/build/{job_id}/cancel", headers=logged_in_headers)
assert cancel_response.status_code == codes.OK
# Verify the response structure indicates failure
response_data = cancel_response.json()
assert "success" in response_data
assert "message" in response_data
assert response_data["success"] is False
assert "failed to cancel" in response_data["message"].lower()
finally:
# Restore the original function to avoid affecting other tests
monkeypatch.setattr(langflow.api.v1.chat, "cancel_flow_build", original_cancel_flow_build)
@pytest.mark.benchmark
@pytest.mark.usefixtures("logged_in_headers")
async def test_should_have_public_events_endpoint_accessible_without_auth(client):
"""Test that public events endpoint exists and is accessible without authentication.
Bug: After sending a message in the Shareable Playground, the chat input resets
but no response is rendered. The root cause is that the events endpoint
(/build/{job_id}/events) requires authentication, which the unauthenticated
shareable playground user does not have.
This test proves:
1. The PUBLIC events endpoint exists and responds without auth (404 = route exists, job not found)
2. The AUTHENTICATED events endpoint rejects unauthenticated requests (403)
"""
fake_job_id = str(uuid.uuid4())
# Assert 1 — the PUBLIC events endpoint is accessible without auth
# Returns 404 "Job not found" (route exists, but job doesn't) — NOT 401/403
events_response = await client.get(
f"api/v1/build_public_tmp/{fake_job_id}/events?event_delivery=polling",
headers={"Accept": "application/x-ndjson"},
)
assert events_response.status_code == codes.NOT_FOUND
# The key proof: the public endpoint responded with 404 (route exists, job not found)
# rather than 401/403 (authentication required). Before the fix, this endpoint
# didn't exist at all and would return 404 for the route, not the job.
assert "Job not found" in events_response.json()["detail"]
@pytest.mark.benchmark
@pytest.mark.usefixtures("logged_in_headers")
async def test_should_have_public_cancel_endpoint_accessible_without_auth(client):
"""Test that public cancel endpoint exists and is accessible without authentication.
Same root cause as the events bug: the cancel endpoint requires auth
but the shareable playground user is unauthenticated.
"""
fake_job_id = str(uuid.uuid4())
# The PUBLIC cancel endpoint is accessible without auth
# Returns 404 "Job not found" (route exists, but job doesn't) — NOT 401/403
cancel_response = await client.post(
f"api/v1/build_public_tmp/{fake_job_id}/cancel",
headers={"Content-Type": "application/json"},
)
assert cancel_response.status_code == codes.NOT_FOUND
assert "Job not found" in cancel_response.json()["detail"]
@pytest.mark.benchmark
async def test_build_public_tmp_ignores_data_parameter(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Test that build_public_tmp endpoint silently ignores data parameter for security.
Security Test: Verifies that when a user attempts to provide custom flow data
to the public flow endpoint, FastAPI silently ignores the extra parameter and
the endpoint functions normally using the stored flow data from the database.
"""
# Create a flow
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Make the flow public
response = await client.patch(
f"api/v1/flows/{flow_id}",
json={"access_type": "PUBLIC"},
headers=logged_in_headers,
)
assert response.status_code == codes.OK
# Create malicious flow data with different structure
malicious_data = {"nodes": [{"id": "malicious", "data": {"type": "CustomComponent"}}], "edges": []}
# Set a client_id cookie
client.cookies.set("client_id", "test-security-client-123")
# Attempt to build with malicious data - FastAPI will silently ignore it
response = await client.post(
f"api/v1/build_public_tmp/{flow_id}/flow",
json={
"inputs": {"session": "test_session"},
"data": malicious_data, # This will be silently ignored by FastAPI
},
headers={"Content-Type": "application/json"},
)
# Verify the request succeeded - the data parameter is simply ignored
assert response.status_code == codes.OK
response_data = response.json()
assert "job_id" in response_data
@pytest.mark.benchmark
@pytest.mark.security
@pytest.mark.parametrize(
"malicious_files",
[
["/etc/hosts"],
["/etc/passwd"],
["../../etc/passwd"],
["..\\..\\windows\\system32\\drivers\\etc\\hosts"],
["s3://other-bucket/secret.txt"],
["just_a_filename.txt"],
# foreign flow_id segment — looks well-formed but isn't this flow's namespace
["00000000-0000-0000-0000-000000000000/file.png"],
# null byte smuggling
["abc\x00/file.png"],
],
)
async def test_build_public_tmp_rejects_malicious_files(
client, json_memory_chatbot_no_llm, logged_in_headers, malicious_files
):
"""Regression for GHSA-rcjh-r59h-gq37 — unauth public build must not accept arbitrary file paths."""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
response = await client.patch(
f"api/v1/flows/{flow_id}",
json={"access_type": "PUBLIC"},
headers=logged_in_headers,
)
assert response.status_code == codes.OK
client.cookies.set("client_id", "test-files-validation-client")
response = await client.post(
f"api/v1/build_public_tmp/{flow_id}/flow",
json={
"inputs": {"session": "test_session"},
"files": malicious_files,
},
headers={"Content-Type": "application/json"},
)
assert response.status_code == codes.BAD_REQUEST
assert "file" in response.json()["detail"].lower()
@pytest.mark.benchmark
async def test_build_public_tmp_accepts_files_in_own_namespace(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Files namespaced under the public flow's own UUID must still be accepted."""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
response = await client.patch(
f"api/v1/flows/{flow_id}",
json={"access_type": "PUBLIC"},
headers=logged_in_headers,
)
assert response.status_code == codes.OK
client.cookies.set("client_id", "test-files-allowed-client")
response = await client.post(
f"api/v1/build_public_tmp/{flow_id}/flow",
json={
"inputs": {"session": "test_session"},
"files": [f"{flow_id}/example_attachment.png"],
},
headers={"Content-Type": "application/json"},
)
assert response.status_code == codes.OK
@pytest.mark.benchmark
async def test_build_public_tmp_checks_public_access_before_validation(
client, json_memory_chatbot_no_llm, logged_in_headers, monkeypatch
):
"""Private flows should fail at the public-access gate before any policy validation runs."""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
client.cookies.set("client_id", "test-private-flow-client")
public_access_validation_message = "validation should not run before public access checks"
def fail_if_validation_runs(_target):
raise ValueError(public_access_validation_message)
monkeypatch.setattr(
"langflow.api.v1.chat.validate_flow_for_current_settings",
fail_if_validation_runs,
)
response = await client.post(
f"api/v1/build_public_tmp/{flow_id}/flow",
json={"inputs": {"session": "test_session"}},
headers={"Content-Type": "application/json"},
)
assert response.status_code == codes.FORBIDDEN
assert response.json()["detail"] == "Flow is not public"
@pytest.mark.benchmark
@pytest.mark.security
async def test_build_public_tmp_rejects_code_execution_components(
client, json_memory_chatbot_no_llm, logged_in_headers
):
"""Report H1-3754930: unauthenticated public builds must reject code-execution components.
A public flow containing a Python interpreter/REPL (or the legacy Python Code
Structured tool) would otherwise let any anonymous visitor trigger
server-side code execution through /build_public_tmp.
"""
flow_dict = json.loads(json_memory_chatbot_no_llm)
flow_dict["data"]["nodes"].append(
{
"id": "PythonREPLComponent-pub1",
"type": "genericNode",
"position": {"x": 0, "y": 0},
"data": {
"id": "PythonREPLComponent-pub1",
"type": "PythonREPLComponent",
"display_name": "Python Interpreter",
"node": {"display_name": "Python Interpreter", "template": {}},
},
}
)
flow_id = await create_flow(client, json.dumps(flow_dict), logged_in_headers)
response = await client.patch(
f"api/v1/flows/{flow_id}",
json={"access_type": "PUBLIC"},
headers=logged_in_headers,
)
assert response.status_code == codes.OK
client.cookies.set("client_id", "test-code-exec-client")
response = await client.post(
f"api/v1/build_public_tmp/{flow_id}/flow",
json={"inputs": {"session": "test_session"}},
headers={"Content-Type": "application/json"},
)
assert response.status_code == codes.BAD_REQUEST
assert response.json()["detail"] == "This flow cannot be executed."
@pytest.mark.benchmark
@pytest.mark.security
async def test_build_public_tmp_rejects_flow_invoking_components(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Report H1-3754930 (transitive case): public builds must reject flow-invoking components.
A public wrapper flow with no directly-blocked nodes could otherwise embed a
Run Flow / Sub Flow / Flow as Tool node that loads and executes another saved
owner flow by id/name at runtime — a private flow which may itself contain a
code-execution component that is never re-validated on the run path. Blocking
the flow-invoking node type on the public path closes that indirection.
"""
flow_dict = json.loads(json_memory_chatbot_no_llm)
flow_dict["data"]["nodes"].append(
{
"id": "RunFlow-pub1",
"type": "genericNode",
"position": {"x": 0, "y": 0},
"data": {
"id": "RunFlow-pub1",
"type": "RunFlow",
"display_name": "Run Flow",
"node": {
"display_name": "Run Flow",
"template": {"flow_id_selected": {"value": str(uuid.uuid4())}},
},
},
}
)
flow_id = await create_flow(client, json.dumps(flow_dict), logged_in_headers)
response = await client.patch(
f"api/v1/flows/{flow_id}",
json={"access_type": "PUBLIC"},
headers=logged_in_headers,
)
assert response.status_code == codes.OK
client.cookies.set("client_id", "test-flow-invoke-client")
response = await client.post(
f"api/v1/build_public_tmp/{flow_id}/flow",
json={"inputs": {"session": "test_session"}},
headers={"Content-Type": "application/json"},
)
assert response.status_code == codes.BAD_REQUEST
assert response.json()["detail"] == "This flow cannot be executed."
@pytest.mark.benchmark
@pytest.mark.security
async def test_build_flow_cross_user_blocked(client, json_memory_chatbot_no_llm, logged_in_headers, user_two):
"""Security (GHSA-qj98-rhf8-v93f): authenticated user cannot build another user's private flow.
Regression guard: verifies that the ownership check added to build_flow rejects
requests where flow.user_id != current_user.id and the flow is not PUBLIC.
"""
victim_flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
login_data = {"username": user_two.username, "password": "hashed_password"} # pragma: allowlist secret
response = await client.post("api/v1/login", data=login_data)
assert response.status_code == 200
attacker_headers = {"Authorization": f"Bearer {response.json()['access_token']}"}
response = await client.post(f"api/v1/build/{victim_flow_id}/flow", json={}, headers=attacker_headers)
assert response.status_code == 404
@pytest.mark.benchmark
@pytest.mark.security
async def test_build_flow_unauthenticated_blocked(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Unauthenticated request to build_flow must be rejected (4xx — no valid credentials)."""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Clear any cookies retained from previous tests to ensure a truly unauthenticated request.
client.cookies.clear()
response = await client.post(f"api/v1/build/{flow_id}/flow", json={})
assert response.status_code == 403
@pytest.mark.benchmark
@pytest.mark.security
async def test_build_flow_nonexistent_flow_returns_404(client, logged_in_headers):
"""Non-existent flow UUID must return 404."""
nonexistent_id = uuid.uuid4()
response = await client.post(f"api/v1/build/{nonexistent_id}/flow", json={}, headers=logged_in_headers)
assert response.status_code == 404
@pytest.mark.benchmark
@pytest.mark.security
async def test_build_events_cross_user_blocked(client, json_memory_chatbot_no_llm, logged_in_headers, user_two):
"""Security (GHSA-qj98-rhf8-v93f): user cannot poll build events owned by another user.
Even if an attacker somehow obtains a valid job_id, the events endpoint independently
enforces ownership via the _job_owners registry in JobQueueService.
"""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
build_response = await build_flow(client, flow_id, logged_in_headers)
job_id = build_response["job_id"]
login_data = {"username": user_two.username, "password": "hashed_password"} # pragma: allowlist secret
response = await client.post("api/v1/login", data=login_data)
assert response.status_code == 200
attacker_headers = {"Authorization": f"Bearer {response.json()['access_token']}"}
response = await get_build_events(client, job_id, attacker_headers)
assert response.status_code == 404
@pytest.mark.benchmark
@pytest.mark.security
async def test_build_flow_public_flow_accessible_by_other_user(
client, json_memory_chatbot_no_llm, logged_in_headers, user_two
):
"""A PUBLIC flow can be built by any authenticated user, not only the owner.
Verifies that the ownership check correctly allows access_type == PUBLIC flows
and does not over-restrict the multi-tenant sharing use case.
"""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
patch_response = await client.patch(
f"api/v1/flows/{flow_id}",
json={"access_type": "PUBLIC"},
headers=logged_in_headers,
)
assert patch_response.status_code == 200
login_data = {"username": user_two.username, "password": "hashed_password"} # pragma: allowlist secret
response = await client.post("api/v1/login", data=login_data)
assert response.status_code == 200
other_headers = {"Authorization": f"Bearer {response.json()['access_token']}"}
response = await client.post(f"api/v1/build/{flow_id}/flow", json={}, headers=other_headers)
assert response.status_code == 200
@pytest.mark.benchmark
@pytest.mark.security
async def test_cancel_build_cross_user_blocked(client, json_memory_chatbot_no_llm, logged_in_headers, user_two):
"""Security: authenticated user cannot cancel a build job owned by another user.
cancel_build carries the same DoS risk as get_build_events — an attacker who
obtains a job_id should not be able to abort the victim's running build.
"""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
build_response = await build_flow(client, flow_id, logged_in_headers)
job_id = build_response["job_id"]
login_data = {"username": user_two.username, "password": "hashed_password"} # pragma: allowlist secret
response = await client.post("api/v1/login", data=login_data)
assert response.status_code == 200
attacker_headers = {"Authorization": f"Bearer {response.json()['access_token']}"}
response = await client.post(f"api/v1/build/{job_id}/cancel", headers=attacker_headers)
assert response.status_code == 404
@pytest.mark.benchmark
async def test_build_public_tmp_without_data_parameter(client, json_memory_chatbot_no_llm, logged_in_headers):
"""Test that build_public_tmp endpoint works without data parameter.
Security Test: Verifies that when no data parameter is provided, the endpoint
works normally and returns a job_id. This proves the data parameter is optional
and the stored flow definition is always used.
"""
# Create a flow
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
# Make the flow public
response = await client.patch(
f"api/v1/flows/{flow_id}",
json={"access_type": "PUBLIC"},
headers=logged_in_headers,
)
assert response.status_code == codes.OK
# Set a client_id cookie
client.cookies.set("client_id", "test-no-data-client")
# Build without providing data parameter
response = await client.post(
f"api/v1/build_public_tmp/{flow_id}/flow",
json={"inputs": {"session": "test_session"}},
headers={"Content-Type": "application/json"},
)
# Verify the request succeeded
assert response.status_code == codes.OK
response_data = response.json()
assert "job_id" in response_data
@pytest.mark.benchmark
@pytest.mark.security
async def test_get_build_events_public_tmp_job_accessible_by_any_auth_user(
client, json_memory_chatbot_no_llm, logged_in_headers, user_two, monkeypatch
):
"""A job started via build_public_tmp has no registered owner and is accessible to any authenticated user.
Verifies that get_build_events skips the ownership check when get_job_owner returns None.
"""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
patch_response = await client.patch(
f"api/v1/flows/{flow_id}",
json={"access_type": "PUBLIC"},
headers=logged_in_headers,
)
assert patch_response.status_code == codes.OK
client.cookies.set("client_id", "test-public-tmp-events-client")
start_response = await client.post(
f"api/v1/build_public_tmp/{flow_id}/flow",
json={},
headers={"Content-Type": "application/json"},
)
assert start_response.status_code == codes.OK
job_id = start_response.json()["job_id"]
login_data = {"username": user_two.username, "password": "hashed_password"} # pragma: allowlist secret
login_response = await client.post("api/v1/login", data=login_data)
assert login_response.status_code == codes.OK
other_headers = {"Authorization": f"Bearer {login_response.json()['access_token']}"}
import langflow.api.v1.chat
from fastapi import Response
async def mock_get_flow_events_response(**_kwargs):
return Response(content="", media_type="application/x-ndjson")
monkeypatch.setattr(langflow.api.v1.chat, "get_flow_events_response", mock_get_flow_events_response)
events_response = await get_build_events(client, job_id, other_headers)
assert events_response.status_code == codes.OK
@pytest.mark.benchmark
@pytest.mark.security
async def test_cancel_build_public_tmp_job_accessible_by_any_auth_user(
client, json_memory_chatbot_no_llm, logged_in_headers, user_two, monkeypatch
):
"""A job started via build_public_tmp has no registered owner and can be cancelled by any authenticated user.
Verifies that cancel_build skips the ownership check when get_job_owner returns None.
"""
flow_id = await create_flow(client, json_memory_chatbot_no_llm, logged_in_headers)
patch_response = await client.patch(
f"api/v1/flows/{flow_id}",
json={"access_type": "PUBLIC"},
headers=logged_in_headers,
)
assert patch_response.status_code == codes.OK
client.cookies.set("client_id", "test-public-tmp-cancel-client")
start_response = await client.post(
f"api/v1/build_public_tmp/{flow_id}/flow",
json={},
headers={"Content-Type": "application/json"},
)
assert start_response.status_code == codes.OK
job_id = start_response.json()["job_id"]
login_data = {"username": user_two.username, "password": "hashed_password"} # pragma: allowlist secret
login_response = await client.post("api/v1/login", data=login_data)
assert login_response.status_code == codes.OK
other_headers = {"Authorization": f"Bearer {login_response.json()['access_token']}"}
import langflow.api.v1.chat
async def mock_cancel_flow_build(*_args, **_kwargs):
return True
monkeypatch.setattr(langflow.api.v1.chat, "cancel_flow_build", mock_cancel_flow_build)
cancel_response = await client.post(f"api/v1/build/{job_id}/cancel", headers=other_headers)
assert cancel_response.status_code == codes.OK
assert cancel_response.json()["success"] is True
@pytest.mark.asyncio
@pytest.mark.security
async def test_job_owner_cleaned_up_after_cleanup_job():
"""JobQueueService.cleanup_job removes the _job_owners entry for the job."""
service = JobQueueService()
service.start()
try:
job_id = str(uuid.uuid4())
user_id = uuid.uuid4()
service.create_queue(job_id)
async def _noop():
await asyncio.sleep(0)
service.start_job(job_id, _noop())
await asyncio.sleep(0.05)
await service.register_job_owner(job_id, user_id)