Server-side request forgery is usually described in terms of cloud metadata endpoints and internal networks. Apex has neither in the usual sense: callouts leave from Salesforce's infrastructure, not from your VPC. So it is tempting to conclude that SSRF does not apply to Salesforce code. It does, it just looks different.
In Apex, the dangerous part of a forged request is what it carries and where it is trusted. A callout can carry an API token, a session ID or record data, and it arrives from Salesforce's published IP ranges, which many internal systems allowlist. This post covers how user input reaches HttpRequest.setEndpoint, why broad Remote Site Settings widen the damage, and how Named Credentials close most of it.
The vulnerable shape
The classic version takes a URL from the caller and fetches it:
public with sharing class PreviewController {
@AuraEnabled
public static String preview(String url) {
HttpRequest req = new HttpRequest();
req.setEndpoint(url); // the caller picks the host
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + TokenStore.partnerToken());
return new Http().send(req).getBody(); // and reads the answer
}
}Any user who can reach the component can make the org send the partner token to a host of their choosing, read the response from any host your Remote Site Settings allow, and use the org as a relay to systems that trust Salesforce's IP ranges. That matches OWASP API7:2023 Server Side Request Forgery and CWE-918.
Less obvious variants are more common in real code:
- The path, not the host.
req.setEndpoint(BASE_URL + '/' + resource)whereresourcecomes from the caller. With../or a full query string, the caller reaches endpoints on the trusted system that the feature never meant to expose. - A record field. The endpoint comes from a custom setting or a field on a record that a less trusted user, or a Web-to-Lead form, can edit.
PageReference.getContent.new PageReference(userUrl).getContent()makes a server-side fetch too.- The body. Caller input concatenated into a JSON or XML body can rewrite the request the trusted system receives.
Remote Site Settings are an allowlist, and often a wide one
Before any Apex callout can call an external site, the site must be registered in Remote Site Settings, unless the callout uses a Named Credential. That makes Remote Site Settings the egress control for raw callouts, and in most orgs they have grown by accretion. Review them like firewall rules:
| Entry shape | Why it matters |
|---|---|
http:// URL |
Callouts and anything on them travel unencrypted |
| Disable Protocol Security ticked | The platform follows HTTPS to HTTP redirects |
| Shared hosting base or a single tenant on it | If the tenant name is released, someone else can claim it and receive the callouts |
| Paste, file drop or request capture sites | A ready-made exfiltration channel |
| A host nobody can name the owner of | An integration that ended, or a test that never got cleaned up |
Every entry you remove narrows what a forged callout can reach.
Named Credentials fix three problems at once
A Named Credential moves the endpoint and the authentication into Setup:
public with sharing class PartnerOrders {
private static final Set<String> ALLOWED = new Set<String>{ 'orders', 'invoices' };
@AuraEnabled
public static String fetch(String resource, String accountNumber) {
if (!ALLOWED.contains(resource)) {
throw new AuraHandledException('Unknown resource.');
}
if (!Pattern.matches('[A-Z0-9]{6,12}', accountNumber)) {
throw new AuraHandledException('Invalid account number.');
}
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Partner_API/' + resource
+ '?account=' + EncodingUtil.urlEncode(accountNumber, 'UTF-8'));
req.setMethod('GET');
req.setTimeout(10000);
return new Http().send(req).getBody();
}
}- The host is fixed.
callout:Partner_APIresolves to the URL an admin configured. The caller can no longer choose the server. - The secret leaves the code. Salesforce manages authentication for callouts that name a Named Credential, so there is no token in Apex, a custom setting or a header the code builds.
- No Remote Site Setting is needed for that host, so the wildcard-ish entries can go.
A Named Credential does not make the path safe. The platform signs the request with the org's identity, so a caller who controls the path reaches any resource on that system as the org. Keep the allowlist of resources and validate every value placed in the path or query, as above.
Two Named Credential settings deserve a second look. Allowing merge fields in the body or header lets record data flow into the outbound request verbatim, so only enable them when the template needs them and the merged fields are trusted. And check the authentication model of the External Credential: a named principal shared by every user is a single identity that every caller borrows.
When the endpoint really must vary
Some features do need a destination chosen at runtime, such as a multi-region partner or a customer-configured webhook. Make the choice from a list, not from free text:
- Store the allowed destinations as separate Named Credentials, and map a short key from the caller to one of them.
- If a URL must be stored, store it in protected configuration that only admins edit, parse it and compare the host against an allowlist of exact names, and require
https. - Never let a guest or external user set it.
Do not forget responses
SSRF is also about what comes back. Returning a raw response body to the browser can leak internal error pages, tokens in JSON, or another tenant's data from a badly designed partner API. Parse the response into the fields the feature needs and return only those. Set a timeout on every callout so a slow host cannot hold the transaction.
A review checklist
- Find every
setEndpoint,PageReference.getContentandgetContentAsPDFcall, and trace where its argument comes from. - Move hardcoded
https://endpoints and hand-builtAuthorizationheaders to Named Credentials. - Allowlist the path segments and validate the query values of every Named Credential callout.
- Prune Remote Site Settings: no
http://, no protocol downgrade, no shared hosting tenants, no paste sites. - Check which Named Credentials allow merge fields, and why.
- Return parsed fields, not response bodies.
Where Vulkro for Salesforce fits
vulkro-sf scan traces request input from @AuraEnabled, REST and Visualforce entry points to callout sinks. apex-taint-ssrf reports input reaching setEndpoint or setBody on a callout without a Named Credential, apex-taint-ssrf-named-credential reports input that picks the path of a Named Credential callout, and apex-taint-ssrf-pageref reports a request-supplied URL fetched with getContent. Configuration checks cover remotesite-cleartext-url, remotesite-disable-protocol-security, remotesite-wildcard-like-host, remotesite-paste-exfil-host and namedcred-merge-body-true, plus apex-setendpoint-hardcoded-url and apex-authz-header-hardcoded for the migration list. On a live org, vulkro-sf org named-credentials -t prod audits the Named Credentials as configured.
FAQ
Can SSRF from Apex reach my internal network?
Only through what you expose. Callouts leave from Salesforce's infrastructure, so they reach the internet and any internal service that accepts traffic from Salesforce's IP ranges. That second group is the real risk.
Do Named Credentials prevent SSRF?
They fix the host and keep the secret out of code. They do not stop a caller who controls the path or query from reaching other resources on the same system, so validate those parts.
Should every callout use a Named Credential?
Yes for anything authenticated, and preferably for everything. It removes the Remote Site Setting, the stored secret and the host choice from code in one change.
See the Named Credentials guide and attack paths in Vulkro for Salesforce, or start a 14-day trial. Related: Named Credentials and External Credentials and SSRF in Node.js.