我有一个简单的 ASP.NET Core Razor 页面网站,用户在其中输入房间号,然后输出结果(无论房间是否可用)。
开发完成后,提交给安全团队,我们得到了这样的消息:
问题存在于POST端,当我们操作请求时,我们可以传递特殊字符(原始)或URL编码。
这是 Index.cshtml
的一部分<input class="form-control" asp-for="InputRoomNo" pattern="[a-zA-Z0-9]+" title="Only charcters and letters please" minlength="3" maxlength="5">
这是 Index.cshtml.cs
的大部分内容public class IndexModel : PageModel
{
public readonly IConfiguration config;
[BindProperty(SupportsGet = false)]
public string InputRoomNo { get; set; }
public Room roomInfo = null;
public string conn;
public IndexModel(IConfiguration Configuration)
{
config = Configuration;
}
public void OnPost()
{
conn = config.GetConnectionString("ConnectionString");
string b = $"{InputRoomNo}";
b = Request.Form["inputRoomNo"];
roomInfo = Search($"{InputRoomNo}");
}
public void OnGet()
{
}
Search 是一种打开数据库连接并进行选择的方法。
出了什么问题以及如何解决安全问题?
看起来你的概念很混乱,所以简化一下可能会对你有好处。
InputRoomNo
作为 BindProperty
,这意味着 OnPost
方法可以从那里读取值Request.Form
,因为您可以引用属性中的值。b
是用来做什么的您的代码可以简化如下(除非在您的帖子中未包含的代码中使用了
b
):
public void OnPost()
{
conn = config.GetConnectionString("ConnectionString");
roomInfo = Search(InputRoomNo);
}
通过直接从公共访问该值
BindProperty
,您可以绕过对请求表单数据的潜在操纵,但不要忘记在使用数据之前对其进行适当的清理和必要的验证。