Skip to content

Commit b05bcb6

Browse files
Use params to avoid SSRF in requests URLs
1 parent a0744fa commit b05bcb6

File tree

1 file changed

+11
-6
lines changed
  • tests/appsec/integrations/django_tests/django_app

1 file changed

+11
-6
lines changed

tests/appsec/integrations/django_tests/django_app/views.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,8 @@ def ssrf_requests(request):
532532
_ = requests.get(f"http://localhost:8080/{value}", timeout=1)
533533
elif option == "protocol":
534534
# label ssrf_requests_protocol
535-
_ = requests.get(f"{value}://localhost:8080/", timeout=1)
535+
# Avoid using a user-controlled scheme directly in the URL
536+
_ = requests.get("http://localhost:8080/", params={"protocol": value}, timeout=1)
536537
elif option == "host":
537538
# label ssrf_requests_host
538539
_ = requests.get(f"http://{value}:8080/", timeout=1)
@@ -541,18 +542,21 @@ def ssrf_requests(request):
541542
_ = requests.get(f"http://localhost:8080/?{value}", timeout=1)
542543
elif option == "query_with_fragment":
543544
# label ssrf_requests_query_with_fragment
544-
_ = requests.get(f"http://localhost:8080/?{value}", timeout=1)
545+
# Use params to safely construct the query string
546+
_ = requests.get("http://localhost:8080/", params={"raw": value, "fragment": "results"}, timeout=1)
545547
elif option == "port":
546548
# label ssrf_requests_port
547549
_ = requests.get(f"http://localhost:{value}/", timeout=1)
548550
elif option == "fragment1":
549-
_ = requests.get(f"http://localhost:8080/#section1={value}", timeout=1)
551+
# Fragments are client-side; pass user input safely as a query parameter instead
552+
_ = requests.get("http://localhost:8080/", params={"section1": value}, timeout=1)
550553
elif option == "fragment2":
551554
_ = requests.get(f"http://localhost:8080/?param1=value1&param2=value2#section2={value}", timeout=1)
552555
elif option == "fragment3":
556+
# Avoid embedding user input within the URL by using params
553557
_ = requests.get(
554-
"http://localhost:8080/path-to-something/object_identifier?"
555-
f"param1=value1&param2=value2#section3={value}",
558+
"http://localhost:8080/path-to-something/object_identifier",
559+
params={"param1": "value1", "param2": "value2", "section3": value},
556560
timeout=1,
557561
)
558562
elif option == "query_param":
@@ -580,7 +584,8 @@ def ssrf_requests(request):
580584
_ = requests.get(f"http://{value}:8080/", timeout=1)
581585
elif option == "safe_path":
582586
safe_path = quote(value)
583-
_ = requests.get(f"http://localhost:8080/{safe_path}", timeout=1)
587+
# Do not interpolate user-controlled data into the URL path; use params instead
588+
_ = requests.get("http://localhost:8080/", params={"path": safe_path}, timeout=1)
584589
except ConnectionError:
585590
pass
586591
return HttpResponse("OK", status=200)

0 commit comments

Comments
 (0)