SSRF in AI-Generated Backend Code: Real Vulnerable Patterns and Fixes
Introduction to SSRF in AI-Generated Backend Code
Server-Side Request Forgery (SSRF) is a vulnerability that allows an attacker to trick a server into making unauthorized requests to internal or external resources. In AI-generated backend code, SSRF vulnerabilities can arise from the use of libraries and frameworks that handle HTTP requests, such as those built with Cursor, Lovable, Bolt, v0, or Replit. The main question is: how can you identify and fix SSRF vulnerabilities in your AI-built app? The answer lies in understanding the vulnerable patterns and implementing proper fixes, which we will cover in this post.
Vulnerable Patterns in AI-Generated Code
AI-generated code can be prone to SSRF vulnerabilities due to the lack of input validation and sanitization. For example, consider the following code snippet in Python:
import requests
def handle_request(url):
response = requests.get(url)
return response.text
This code takes a URL as input and makes a GET request to it without any validation or sanitization. An attacker can exploit this vulnerability by providing a malicious URL that points to an internal resource, such as http://localhost:8080/admin.
Fixing SSRF Vulnerabilities
To fix SSRF vulnerabilities, you need to implement proper input validation and sanitization. One way to do this is to use a whitelist of allowed URLs or domains. Here's an example of how you can modify the previous code snippet to use a whitelist:
import requests
allowed_domains = ["example.com", "api.example.com"]
def handle_request(url):
if not any(domain in url for domain in allowed_domains):
raise ValueError("Invalid URL")
response = requests.get(url)
return response.text
This code checks if the provided URL contains any of the allowed domains before making the request. If the URL is not valid, it raises a ValueError.
Using SecuriSky to Detect SSRF Vulnerabilities
SecuriSky is a security scanner that can help you detect SSRF vulnerabilities in your AI-generated backend code. By integrating SecuriSky into your development workflow, you can identify and fix SSRF vulnerabilities before they become a problem.
Example of a More Complex SSRF Vulnerability
Consider a scenario where your AI-built app uses a library that handles HTTP requests, such as the requests library in Python. The library may have a vulnerability that allows an attacker to bypass the whitelist check. Here's an example of how an attacker could exploit this vulnerability:
import requests
allowed_domains = ["example.com", "api.example.com"]
def handle_request(url):
if not any(domain in url for domain in allowed_domains):
raise ValueError("Invalid URL")
response = requests.get(url, proxies={"http": "http://localhost:8080"})
return response.text
In this example, the attacker can bypass the whitelist check by providing a proxy URL that points to an internal resource.
Fixing the More Complex SSRF Vulnerability
To fix this vulnerability, you need to remove the proxy configuration and ensure that the library is properly configured to prevent SSRF attacks. Here's an example of how you can modify the previous code snippet to fix the vulnerability:
import requests
allowed_domains = ["example.com", "api.example.com"]
def handle_request(url):
if not any(domain in url for domain in allowed_domains):
raise ValueError("Invalid URL")
session = requests.Session()
session.trust_env = False
response = session.get(url)
return response.text
This code removes the proxy configuration and sets the trust_env parameter to False to prevent the library from trusting environment variables that may contain malicious proxy settings.
Quick Fix Checklist
Try it free
Scan your app for these issues now
Paste your URL and get a full security, performance, and SEO report in under 2 minutes — no signup required.
Run a free scan