How to prevent SSRF Attacks in Node.js

Server-Side Request Forgery (SSRF) is a security vulnerability that allows an attacker to manipulate and send requests from a server. This type of attack exploits the functionality of a server to make requests to unintended destinations, potentially leading to unauthorized access to internal resources, sensitive data exposure, and further attacks on the server's infrastructure.


Youtuber Snyk has created a very useful video that talks about this and teaches us a little bit about how to prevent this problem from happening.

How SSRF Works

Initial Request: The attacker sends a crafted request to a vulnerable server, usually via a web application that takes a URL or other network resource identifier as input.


Server Makes a Request: The server processes this input and makes a network request based on the provided URL or identifier. The vulnerability arises when the server does not properly validate or sanitize the input, allowing the attacker to control the destination of this request.


Exploitation: The attacker manipulates the input to target internal services, sensitive endpoints, or external resources. For example, the attacker might use URLs like:


http://localhost/admin

http://169.254.169.254/latest/meta-data/ (for cloud metadata services)

http://internal-service.local/

Consequences of SSRF

Access to Internal Systems: Attackers can reach internal systems that are not exposed to the public, potentially accessing administrative interfaces, databases, and other sensitive services.

Data Exfiltration: Attackers might retrieve sensitive information, such as cloud metadata containing credentials or configuration data.


Bypass Security Controls: SSRF can be used to bypass firewalls, network access controls, and other security mechanisms that rely on network isolation.


Pivot to Further Attacks: Once internal access is gained, attackers can use it as a foothold to perform lateral movements within the network, escalating privileges, and deploying further exploits.

Prevention and Mitigation

Input Validation and Sanitization: Rigorously validate and sanitize all user inputs that are used to construct requests. Ensure URLs are safe and restrict the input to only allowed destinations. In Typescript and Nodejs we can use a library like zod


Allowlisting: Implement strict allowlists for outgoing requests to ensure the server only communicates with trusted and necessary endpoints.


Network Segmentation: Isolate internal services and ensure they are not accessible from the server handling external requests.


Metadata API Protection: In cloud environments, block access to metadata endpoints from non-essential services or use IAM roles to control access.


Logging and Monitoring: Log all outgoing requests and monitor for suspicious activities that might indicate SSRF attempts.