
RK994592 (Community Member) asked a question.
We have scanned our code through Veracode and it gives us ServerSide Request Forgery issue for below line of code. Need help to resolve this issue.
This is my method and i am getting issue At here " response = client.SendAsync(request).Result;" in the below code. Please response as soon as possible.
public string GetServiceResponse(string serviceUrl, string content = null, bool isPostRequest = false)
{
try
{
string baseUrl = Constants.GetHostUrl();
if (!string.IsNullOrWhiteSpace(baseUrl) && !string.IsNullOrWhiteSpace(serviceUrl))
{
HttpClient client = new HttpClient();
HttpResponseMessage response = null;
HttpRequestMessage request = null;
if (!isPostRequest)
{
request = new HttpRequestMessage(HttpMethod.Get, baseUrl + serviceUrl);
request.Headers.Add("UserName", Sitecore.Context.User.Name);
}
else
{
request = new HttpRequestMessage(HttpMethod.Post, baseUrl + serviceUrl);
request.Headers.Add("UserName", Sitecore.Context.User.Name);
request.Content = new StringContent(content, Encoding.UTF8, "application/json");
request.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
}
string key=Convert.ToString(System.Web.HttpContext.Current.Session["Key"]);
string token = Convert.ToString(System.Web.HttpContext.Current.Session["Token"]);
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(token))
{
request.Headers.Add("key", key);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
response = client.SendAsync(request).Result;
if (response != null && response.Content != null && response.IsSuccessStatusCode && response.StatusCode == HttpStatusCode.OK)
{
return (response.Content.ReadAsStringAsync().Result != null && response.Content.ReadAsStringAsync().Result != "null" &&
response.Content.ReadAsStringAsync().Result != string.Empty) ? response.Content.ReadAsStringAsync().Result : string.Empty;
}
return string.Empty;
}
.png)
Hi @RK994592 (Community Member),
Veracode Static Analysis reports flaws of CWE-918 Server-Side Request Forgery (SSRF) when it detects that an HTTP Request sent out from the application contains input from outside of the application. For example from an HTTP Request, or from a file, database result, webservice response, etc.
The concern is that an attacker might be able to abuse this input to change the request being done to access (internal) resources that they should not have access to. For more information on the risk for this flaw please see:
https://www.owasp.org/index.php/Server_Side_Request_Forgery
https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html#available-protections
The only thing that Veracode Static Analysis will automatically detect as a remediation for this flaw category is to change the input to be hardcoded. If this is not possible we recommend that you apply dynamic validation (for example with a regex) but this will not be automatically detected by Veracode Static Analysis and must then be documented in a Mitigation by Design mitigation proposal and reviewed by your security team.
You can find more information on how to do mitigation proposals in our help centre.
Best regards,
Duncan