【发布时间】:2020-10-03 03:14:14
【问题描述】:
我在Micro services 架构中使用API Gateway Pattern,其中Front End Angular app 为我的API Gateway 项目创建一个HTTP request,这只是一个ASP.net Core 3.1 Web API 项目。目前我只有 2 个micro services 和一个API Gateway,它们都是ASP.net Core 3.1 Web API 项目类型。 API Gateway 项目拥有我的micro services 的所有控制器。 API Gateway 的目的只是接收来自Front end 的请求并将HTTP Request 发送到相应的Micro service。
现在在我的API Gateway 项目的AccountController.cs 中,我有以下代码
/// <summary>
/// Gets the detail of an account by its id
/// </summary>
/// <param name="organizationId">Id of the Organization of which the account belongs to</param>
/// <param name="accountId">Id of Account of which information is being requested</param>
/// <returns>Account's Details</returns>
[HttpGet("{organizationId}/{accountId}")]
public async Task<IActionResult> GetAccountAsync(Guid organizationId, Guid accountId)
{
_uri = new Uri(uriString: $"{_configurationService.AccountAPI}GetAccount/{organizationId}/{accountId}");
using var result = await _client.GetAsync(_uri);
var content = await result.Content.ReadAsStringAsync();
return Ok(content.AsObject<MessageResponse<AccountDetailVM>>());
}
在 stackoverflow 上搜索 SSRF 问题后,我在 Veracode community 找到了以下建议。
如果可以,Veracode 静态分析将报告 CWE 918 的缺陷 从应用程序外部检测该数据(如 HTTP 请求 来自用户,也可能是用户上传的文件, 数据库数据、网络服务数据等)能够改变 网络请求。
在Stackoverflow 我找到了以下修复
对于 CWE ID 918,除非您有静态 URL,否则很难让 Veracode 识别您的修复。您需要验证成为请求 URL 一部分的所有输入。
这意味着在将输入参数 OrganizationId 和 AccountId 附加到请求 URL 之前,我必须对其进行清理。
另外一个关于veracode community的问题建议
Veracode 静态分析会自动检测到作为该缺陷类别的补救措施的唯一一件事就是将输入更改为硬编码
他们提出了查询字符串的解决方案
给定的示例似乎采用了模型标识符并将其放入 内部请求中使用的 URL。我们建议验证 ID 根据您对此数据类型的规则(通常这应该只 是字母数字且少于 255 个字符)并在之前对其进行 URLencode 将其附加到 URL。
在所有这些东西之后,我对我的代码进行了以下更改
- 确保 OrganizationId 和 AccountId Guid 不为空
- URL 编码的字符串
这里是修改后的代码
/// <summary>
/// Gets the detail of an account by its id
/// </summary>
/// <param name="organizationId">Id of the Organization of which the account belongs to</param>
/// <param name="accountId">Id of Account of which information is being requested</param>
/// <returns>Account's Details</returns>
[HttpGet("{organizationId}/{accountId}")]
public async Task<IActionResult> GetAccountAsync(Guid organizationId, Guid accountId)
{
if (organizationId != Guid.Empty && accountId != Guid.Empty)
{
string url = HttpUtility.UrlEncode($"{_configurationService.AccountAPI}GetAccount/{organizationId}/{accountId}");
using var result = await _client.GetAsync(url);
var content = await result.Content.ReadAsStringAsync();
return Ok(content.AsObject<MessageResponse<AccountDetailVM>>());
}
return BadRequest();
}
我能做的就是清理我的输入参数OrganizationId 和AccountId,但在所有这些更改之后veracode 仍然识别出SSRF 在线缺陷
使用 var 结果 = await _client.GetAsync(url);
【问题讨论】:
标签: microservices asp.net-core-webapi api-gateway veracode ssrf