【问题标题】:MVC allows an XSS attack when using an API callMVC 在使用 API 调用时允许 XSS 攻击
【发布时间】:2019-06-27 14:44:19
【问题描述】:

我正在制作一个 ASP.NET MVC Web 应用程序,我将使用具有以下内容的 API 调用向它发送一些数据。

{
    "content": "<script>alert('hallo');</script>",
    "title": "<script>alert('hallo');</script>"
}

如何防止服务器接受它?我知道默认情况下 MVC 中会阻止 XSS,但它似乎不起作用。这是我在服务器端的代码:

public class DefaultController : Controller
{
    [HttpPost]
    public JsonResult Create()
    {
        // ...skipped
    }
}

我错过了什么吗?

【问题讨论】:

  • 您的服务器接受它没有任何问题。
  • 从@SLaks 开始,问题是如果您将这些信息逐字包含到您发送到浏览器的内容中。如果数据不受信任(应该绝对如此),那么您需要在使用它之前HtmlEncode它。

标签: c# asp.net-mvc xss


【解决方案1】:

没关系。 你的服务器应该接受它。

通常的模式是:

  • 接收来自客户的东西
  • 对您发送回客户端的所有内容进行 html 实体编码

【讨论】:

    猜你喜欢
    • 2013-06-23
    • 2015-06-04
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 2018-07-16
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多