更新时间:2024-05-10 GMT+08:00
分享

SafeUtils安全工具类预置API一览表

表1 SafeUtils安全工具类预置API一览表

API

API说明

escapeHtml()

将HTML字符串进行转义。

getSafeUrl()

对外部链接进行xss安全过滤,获取安全的绝对URL地址。

escapeHtml()

对HTML字符串进行转义,获取转义后的字符串供页面显示用,使用方式参考如下:

/*
 * @param {*} text
 */
SafeUtils.escapeHtml(text) // 方法返回转义后的字符串

例如输入字符串如下:

<div data-v-a7e06658="" id="page-view-header" class="page-view-header" style="width: 1920px; height: 0px; overflow: hidden; display: none;"> <div data-v-a7e06658="" class="page-view-header-bottom" style="top: -11px;"></div></div>

返回值经过xss转义后则为:

&lt;div data-v-a7e06658=&quot;&quot; id=&quot;page-view-header&quot; class=&quot;page-view-header&quot; style=&quot;width: 1920px; height: 0px; overflow: hidden; display: none;&quot;&gt; &lt;div data-v-a7e06658=&quot;&quot; class=&quot;page-view-header-bottom&quot; style=&quot;top: -11px;&quot;&gt;&lt;/div&gt;&lt;/div&gt;

getSafeUrl()

对外部链接进行xss安全过滤 ,获取安全的绝对URL地址,使用方式参考如下:

/*
 * @param {*} url
 */
SafeUtils.getSafeUrl(url);

SafeUtils.getSafeUrl('/test.html')  //return:https://appcube.cn-north-4.huaweicloud.com/test.html
SafeUtils.getSafeUrl('javascript:alert(123)') //return:'unsafe:'

只有安全的链接才会被访问,其他形式的URL都会相应一个“unsafe:”,无法访问。

分享:

    相关文档

    相关产品