gpt4 book ai didi

asp.net - 无法在 ASP.NET 中纠正 VeraCode CWE ID 918 - (SSRF)

转载 作者:行者123 更新时间:2023-12-02 02:43:04 25 4
gpt4 key购买 nike

长话短说,无论我尝试什么,VeraCode 都会继续将我的 8 行代码标记为 CWE 918 的缺陷。这是旧代码,所以我不确定为什么它会突然被标记。

这是一个示例 [offending] 方法,带有粗体标记的行

  public virtual async Task<HttpResponseMessage> Put(string controller = "", Dictionary<string, object> parameters = null, object body = null)
{
if (string.IsNullOrWhiteSpace(ApiBaseUrl)) return null;
HttpResponseMessage response = null;

using (var client = GetHttpClient())
{
client.BaseAddress = new Uri(ApiBaseUrl);

if (!string.IsNullOrEmpty(Token)) client.DefaultRequestHeaders.Add("Token-Key", Token);
if (!string.IsNullOrEmpty(DeviceId)) client.DefaultRequestHeaders.Add("DeviceId", DeviceId);

var url = GenerateUrl(controller, parameters);

var requestBody = GeneratedHttpContent(body);
if (requestBody == null) requestBody = new StringContent("");

**response = await client.PutAsync(url, requestBody);**

await LogError(response);
return response;
}
}

这是我提议的修复程序,它利用扩展方法来验证 URL
var url = GenerateUrl(controller, parameters);

var requestBody = GeneratedHttpContent(body);
if (requestBody == null) requestBody = new StringContent("");

**if (url.IsValidUrl())
{
response = await client.PutAsync(url, requestBody);
}
else
{
response = new HttpResponseMessage(HttpStatusCode.BadRequest);
}**

await LogError(response);
return response;

这是带有 VeraCode 属性的扩展方法
        [RedirectUrlCleanser]
public static bool IsValidUrl(this string source)
{
return Uri.TryCreate(source, UriKind.RelativeOrAbsolute, out Uri uriResult) && Uri.IsWellFormedUriString(source, UriKind.RelativeOrAbsolute);
}

我可以让 VeraCode 根据属性自动缓解,但我们的客户端将执行自己的扫描,当然不会启用该设置。

关于如何解决这个问题的任何想法将不胜感激。

最佳答案

缺陷的真正根源在于您的 GenerateUrl 方法内部,遗憾的是没有显示,但这里是 Veracode 提示的总体思路。

对于 CWE ID 918,除非您有静态 URL,否则很难让 Veracode 识别您的修复。您需要验证成为请求 URL 一部分的所有输入。
以下是我在 Veracode 网站上找到的内容:
https://community.veracode.com/s/question/0D52T00004i1UiSSAU/how-to-fix-cwe-918-veracode-flaw-on-webrequest-getresponce-method

完整的解决方案仅适用于您有单个或少量可能输入值(白名单)的情况:

public WebResponse ProxyImage(string image_host, string image_path)
{
string validated_image_host = AllowedHosts.Host1;
if (image_host.Equals(AllowedHosts.Host2))
validated_image_host = AllowedHosts.Host2;

string validated_image = AllowedImages.Image1;
if (image_path.Equals(AllowedImages.Image2))
validated_image = AllowedImages.Image2;

string url = $"http://{validated_image_host}.example.com/{validated_image}";

return WebRequest.Create(url).GetResponse();
}

如果可能的有效值集对于这种验证来说太大,那么您需要通过使用正则表达式实现输入的动态验证来修复该缺陷。不幸的是,Veracode 不够聪明,无法识别这种修复,因此仍然需要“通过设计进行缓解”。
public WebResponse ProxyImage(string image_host, string image_path)
{
var image_host_regex = new System.Text.RegularExpressions.Regex("^[a-z]{1,10}$");
if (!image_host_regex.Match(image_host).Success)
throw new ArgumentException("Invalid image_host");

var image_path_regex = new System.Text.RegularExpressions.Regex("^/[a-z]{1,10}/[a-z]{1,255}.png$");
if (!image_path_regex.Match(image_path).Success)
throw new ArgumentException("Invalid image_host");

string url = $"http://{image_host}.example.com/{image_path}";
return WebRequest.Create(url).GetResponse();
}

关于asp.net - 无法在 ASP.NET 中纠正 VeraCode CWE ID 918 - (SSRF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57912436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com