首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在html onclick中阻止xss,onmouseout,...对于SPA

在html onclick中阻止xss,onmouseout,...对于SPA
EN

Stack Overflow用户
提问于 2018-07-29 23:27:44
回答 1查看 375关注 0票数 0

我在我的客户调查系统中使用AngularJSASP.Net Web API 2

我的网站使用wysiwyg组件来帮助用户输入他/她的数据。这是我正在使用的wysiwyg生成的html:

代码语言:javascript
复制
<h2 style="text-align: center;"><span style="text-decoration: underline;"><strong>Click here</strong></span><strong> to start using the </strong><strong><em>HTML editor online</em></strong></h2>
<p><span style="font-weight: 400;">Please</span><span style="font-weight: 400;"> try out the WYSIWYG HTML editor features found in the kitchen sink above to edit and format your text and images</span></p>
<p><span style="font-weight: 400;">You&rsquo;ll see the content created in the WYSIWYG-HTML editor in source code format on the right.</span><br /><br /></p>
<p>&nbsp;</p>
<p><img onclick="alert('Hello world')" onmouseout="alert('Mouse out')" src="https://icon2.kisspng.com/20180416/ucw/kisspng-tanki-online-video-gaming-clan-world-of-tanks-avat-joker-5ad4b1028d0565.2188790515238883865776.jpg" alt="" width="260" height="260" /></p>

这就是我在API上为上面的html代码编码所做的事情。

代码语言:javascript
复制
/// Encode submited html content
        /// </summary>
        /// <returns></returns>
        [Route("encode")]
        [HttpPost]
        public IHttpActionResult Encode([FromBody] EncodeViewModel httpContent)
        {
            if (httpContent == null)
            {
                httpContent = new EncodeViewModel();
                Validate(httpContent);
            }

            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var encodedHtmlContent= HttpUtility.HtmlEncode(httpContent.HtmlContent);

            // Save content to database here.
            return Ok(encodedHtmlContent);
        }

当我的前端客户端加载编码的html并显示到网站页面时。我希望当我单击图像时,不会显示消息Hello worldMouse out

这意味着,我只允许显示文本、图像和视频,但不允许在内容中使用内联代码。

对此有什么解决方案吗?

谢谢,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-30 04:23:12

要确保某些html是安全的是很难的!有一些库可以进行html XSS过滤,但是它们很大,而且仍然很危险。这就是为什么会有像mobiledoc这样的东西。一般来说,我建议你不要使用HTML作为你的WYSIWYG输出,这样你就不必逃避它了。而是使用其他东西并从它生成HTML。这样你就可以确保你不会生成危险的HTML。如果要进行简单的字符串连接,仍然要小心。

这就是为什么我们有像BB-Code或者wiki markup或者mobiledoc这样的东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51581808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档