Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要删除onclick的Regex="&引用;ASP.NET C#(服务器端)中HTML元素的属性_C#_Asp.net_Regex_Richtextbox_Xss - Fatal编程技术网

要删除onclick的Regex="&引用;ASP.NET C#(服务器端)中HTML元素的属性

要删除onclick的Regex="&引用;ASP.NET C#(服务器端)中HTML元素的属性,c#,asp.net,regex,richtextbox,xss,C#,Asp.net,Regex,Richtextbox,Xss,我正试图编写一个正则表达式函数来从HTML元素中删除onclick(也包括onload、onmouseover等)属性。我希望在将HTML发送到客户端之前在服务器端执行此操作 我的内容来自一个富文本编辑器,并以div的形式显示在屏幕上,我想防止XSS(跨站点脚本)。显然,我无法使用Server.HtmlEncode()对其进行HTML编码,因为富文本将文本存储为HTML标记,因此我使用黑名单方法,查找某些元素,如和。我现在尝试查找onclick、onmouseover等属性,到目前为止,我有以下

我正试图编写一个正则表达式函数来从HTML元素中删除onclick(也包括onload、onmouseover等)属性。我希望在将HTML发送到客户端之前在服务器端执行此操作

我的内容来自一个富文本编辑器,并以div的形式显示在屏幕上,我想防止XSS(跨站点脚本)。显然,我无法使用Server.HtmlEncode()对其进行HTML编码,因为富文本将文本存储为HTML标记,因此我使用黑名单方法,查找某些元素,如
。我现在尝试查找onclick、onmouseover等属性,到目前为止,我有以下内容:

returnVal = Regex.Replace(returnVal, @"\<(.*?)(\ on[a-z]+\=\""?.*?\""?)*(.*?)\>",
               "<$1 $3>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
returnVal=Regex.Replace(returnVal,@“\”,
“”,RegexOptions.Singleline | RegexOptions.IgnoreCase);
…这不起作用,我尝试了一些变化。基本上我想要它,这样

<p style="font-style: italic" onclick="alert('hacked!!');">Hello World</p>
你好,世界

变成

<p style="font-style: italic">Hello World</p>
你好,世界

有什么想法吗?干杯

试试这个正则表达式:


returnValue = 
    Regex.Replace(
        returnValue,
        @"(<[\s\S]*?) on.*?\=(['""])[\s\S]*?\2([\s\S]*?>)", 
        delegate(Match match)
        {
            return String.Concat(match.Groups[1].Value, match.Groups[3].Value);
        }, RegexOptions.Compiled | RegexOptions.IgnoreCase);

返回值=
Regex.Replace(
返回值,

@“(这是对‘Rubens Farias’答案的回应,我用了一个类似这样的while循环

while (Regex.IsMatch(returnVal, @"(<[\s\S]*?) on.*?\=(['""])[\s\S]*?\2([\s\S]*?>)", RegexOptions.Compiled | RegexOptions.IgnoreCase))
{
    returnVal = Regex.Replace(returnVal, @"(<[\s\S]*?) on.*?\=(['""])[\s\S]*?\2([\s\S]*?>)",
                    delegate(Match match)
                    {
                        return String.Concat(match.Groups[1].Value, match.Groups[3].Value);
                    }, RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
while(Regex.IsMatch(returnVal,@“()”,
代表(比赛)
{
返回字符串.Concat(match.Groups[1].Value,match.Groups[3].Value);
},RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
对于那些感兴趣的人,这里是我用来帮助防止XSS的整个方法

/// <summary>
///     'Helps' protect against XSS (Cross Site Scripting attacks) by stripping out known evil HTML elements
///     such as script and style. Used for outputing text generated by a Rich Text Editor. Doesn't HTML encode!
/// </summary>
/// <param name="input">Input string to strip bad HTML elements from</param>
public static string XSSProtect(string input)
{
    string returnVal = input ?? "";

    returnVal = Regex.Replace(returnVal, @"\<script(.*?)\>(.*?)\<\/script(.*?)\>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
    returnVal = Regex.Replace(returnVal, @"\<style(.*?)\>(.*?)\<\/style(.*?)\>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);

    while (Regex.IsMatch(returnVal, @"(<[\s\S]*?) on.*?\=(['""])[\s\S]*?\2([\s\S]*?>)", RegexOptions.Compiled | RegexOptions.IgnoreCase))
    {
        returnVal = Regex.Replace(returnVal, @"(<[\s\S]*?) on.*?\=(['""])[\s\S]*?\2([\s\S]*?>)",
                        delegate(Match match)
                        {
                            return String.Concat(match.Groups[1].Value, match.Groups[3].Value);
                        }, RegexOptions.Compiled | RegexOptions.IgnoreCase);
    }

    return returnVal;
}
//
///通过剥离已知的有害HTML元素,“帮助”防止XSS(跨站点脚本攻击)
///例如脚本和样式。用于输出富文本编辑器生成的文本。不进行HTML编码!
/// 
///用于从中删除错误HTML元素的输入字符串
公共静态字符串XSSProtect(字符串输入)
{
字符串returnVal=输入;
returnVal=Regex.Replace(returnVal,@“\(.*?\”,”,RegexOptions.Singleline|RegexOptions.IgnoreCase);
returnVal=Regex.Replace(returnVal,@“\(.*?\”,”,RegexOptions.Singleline|RegexOptions.IgnoreCase);
while(Regex.IsMatch)(returnVal,@“()”,
代表(比赛)
{
返回字符串.Concat(match.Groups[1].Value,match.Groups[3].Value);
},RegexOptions.Compiled | RegexOptions.IgnoreCase);
}
返回值;
}

您可以存储旧的返回值,然后在while循环中进行检查,查看是否没有任何更改,如果是,则中断循环

if(oldContent.Equals(newContent)) { break; }
像这样

if(!String.prototype.replaceAll){
(功能(){
String.prototype.replaceAll=函数(目标,替换){
返回此.split(目标).join(替换);
};
})();
};
html=html.replaceAll(/onclick.*?\=(['''')[\s\s]*(['''')/ig,”);

console.log(html);
WOW!几乎完美,除非onclick属性在单个元素中出现两次,否则它只会删除其中一个。我使用Regex.IsMatch将该代码固定在while循环中(使用您的表达式,它似乎有效。我将在这个问题的单独帖子中发布代码,因为这些注释中的代码示例不太好。我必须承认,我对这种方法有点紧张,是否有任何情况会导致无限循环?我也不太舒服;您还应该在后面添加一个额外的\s*。)before=symbol,onclick=“alert()”您在这里看到了吗?