如何允许用户输入html注释

发布于 2024-12-15 01:50:45 字数 644 浏览 4 评论 0原文

使用 MVC、EF 4.2。我正在开发一个有评论部分的应用程序。现在,如果用户输入包含 HTML(例如)的评论

<b>text</b>

并点击“提交”,我会收到消息 “检测到潜在危险的Request.Form值...”

  1. 我如何处理进入数据库的html?我应该去掉html吗?或者编码?我尝试了 server.htmlencode 文本,但仍然有相同的错误消息。

我读过很多关于此事的帖子,包括一些在这里的SO - 这个这个

理想情况下,我希望能够允许有限数量的 html 标签,例如 em Strong、a。 Anti-XSS、HTML Agility、某种 BB 代码或 Markdown 样式编辑器仍然是推荐的方法吗?我知道杰夫有一段白名单代码 - 但它已经有几年了。

Using MVC, EF 4.2. I am working on an application that has a comment section. Right now if a user enters a comment that contains HTML e.g.

<b>text</b>

and hits submit i get the message
"A ptentially dangerous Request.Form value was detected..."

  1. How do i handle html on the way into the db? Should I just strip the html? Or encode it? I tried server.htmlencode the text but i still had the same error message.

I have read a number of posts on the matter including some here at SO - this one and this one

Ideally, i'd like to be able to allow a limited number of html tags such as em strong, a. Would Anti-XSS, HTML Agility, some kind of BB code, or a markdown style editor still be the recommended way? I know Jeff has a whitelist bit of code - however it is few yrs old.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

oО清风挽发oО 2024-12-22 01:50:45

你可以这样做

[ValidateInput(false)]
public ActionResult foo()
{
}

,或者你可以用 AllowHtml 装饰模型属性

   public class Foo
    {
        [AllowHtml]
        public string bar{ get; set; }
    }

you can do

[ValidateInput(false)]
public ActionResult foo()
{
}

or you can decorate the model property with AllowHtml

   public class Foo
    {
        [AllowHtml]
        public string bar{ get; set; }
    }
寒尘 2024-12-22 01:50:45

您可能还需要在 web.config 中设置 requestValidationMode:

</system.web>
  <httpRuntime requestValidationMode="2.0" />
</system.web>

请参阅此链接 了解更多详情。

You may also need to set the requestValidationMode in your web.config:

</system.web>
  <httpRuntime requestValidationMode="2.0" />
</system.web>

See this link for more details.

垂暮老矣 2024-12-22 01:50:45

MVC 有一个属性,允许您指定一个属性应该允许 html,而无需完全禁用验证。它仍然很危险,但可以仅限于单一财产,因此可以减轻风险。这是 MSDN 文章对于AllowHtmlAttribute。该属性的正确用法应该是在模型中装饰适当的属性:

public class MyModel
{
    public MyModel()
    {

    }

    // Some other stuff in here

    [AllowHtml]
    [HttpPost]
    public string MyHtmlString { get; set; }

}

MVC has an attribute that allows you to specify a property should allow html without disabling validation completely. It's still dangerous, but it can be limited to a single property so the risk can be mitigated. Here is the MSDN article for the AllowHtmlAttribute. Proper usage of the attribute should be to decorate the appropriate property in your model:

public class MyModel
{
    public MyModel()
    {

    }

    // Some other stuff in here

    [AllowHtml]
    [HttpPost]
    public string MyHtmlString { get; set; }

}
浅沫记忆 2024-12-22 01:50:45

我的允许 html 注释的解决方案如下:

  1. 我的注释类中 CommentText 属性上的AllowHtml
  2. 允许一小部分标签。使用 Html Sanitizer 类来清理白名单不允许的 Html 和内联脚本
  3. 然后像平常一样将结果保存到数据库
  4. 在输出时,使用 Html.Raw 在注释中显示 Html

My solution for allow html incomments is as follows:

  1. AllowHtml on the CommentText property in my comment class
  2. Allow a narrow subset of tags. Use an Html Sanitizer class to scrub Html and inline script that is not allowed via a whitelist
  3. Then save the result to the db as i normally would
  4. At output time, use Html.Raw to show the Html in the comments
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文