سلام دوستان
من دارم یک سری مقدار از کاربر میگیرم در قالب پیام (با Editor) حالا میخوام سمت خودم یک اعتبارسنجی انجام بدم و تگ هایی مثل script و .... رو پاک کنم از رشته ای که دارم میگیرم
قبلا از این کد استفاده میکردم :

    public static string SanitizeHtml(string html, string whiteListTags = "b(lockquote)?|code|d(d|t|l|el)|em|h(1|2|3|4)|i|kb  d|li|ol|p(re)?|s(ub|up|trong|trike)?|ul|a|img")    {
#region Regex definitions
Regex tagsRegex = new Regex("<[^>]*(>|$)",
RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.Compiled);


Regex cleanupRegex = new Regex("((?<=<\\w+[^>]*)(?!\\shref|\\sclass|\\srel|\\stitle|\\sclass|\\s width|\\sheight|\\salt|\\ssrc)(\\s[\\w-]+)=["']?((?:.(?!["']?\\s+(?:\\S+)=|[>"']))+.)["']?)|((?<=<p[^>]*)\\sclass="MsoNormal")",
RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.Compiled);


Regex whitelistRegex = new Regex("^</?(" + whiteListTags + ")>$|^<(b|h)r\\s?/?>$",
RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);


Regex whitelistAnchorRegex = new Regex(@"
^<a\s
href=""(\#\w+|(https?|ftp)://[-a-z0-9+&@#/%?=~_|!:,.;\(\)]+)""
(
(\sclass=""([\w-]+)"")|(\stitle=""[^""<>]+"")|
(\srel=""nofollow""))*
\s?>$|
^</a>$",
RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);


Regex whitelistImageRegex = new Regex(@"
^<img\s
src=""https?://[-a-z0-9+&@#/%?=~_|!:,.;\(\)]+""
((\swidth=""\d{1,3}"")|
(\sheight=""\d{1,3}"")|
(\salt=""[^""<>]*"")|
(\stitle=""[^""<>]*""))*
\s?/?>$",
RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
#endregion


if (String.IsNullOrEmpty(html))
return html;


//Do a previous cleanup, for not not allowed attributes included comming from word
html = cleanupRegex.Replace(html, "");


string tagname;
Match tag;


// match every HTML tag in the input
MatchCollection tags = tagsRegex.Matches(html);
for (int i = tags.Count - 1; i > -1; i--)
{
tag = tags[i];
tagname = tag.Value.ToLowerInvariant();


if (!(whitelistRegex.IsMatch(tagname) || whitelistAnchorRegex.IsMatch(tagname) || whitelistImageRegex.IsMatch(tagname)))
{
html = html.Remove(tag.Index, tag.Length);
System.Diagnostics.Debug.WriteLine("tag sanitized: " + tagname);
}
}


return html;
}

این کد درست کار میکنه ولی چون امکان داره فونت یا دیگر خصیصه های قالب بندی رو کاربر روی تگهایی مثل span,p و ... وارد کنه، میخوام style هم بیاد داخل استثنا ها و پاکشون نکنه .

اومدم sstyle رو به cleanupRegex اضافه کردم و مثل whitelistAnchorRegex یکی برای استایل هم نوشتم اما کار نمیکنه درست و عنوان تگ رو بر میداره مطمعنم خطام توی regex هست . میشه راهنمایی کنن دوستان ؟ (مثلا table رو اضافه کردم درست کار میکنه t(able|d|h|r) اما style یکم گیجم کرده!!!!)

سوال بعدی اینه که آیا استایل از نظر امنیت باید بررسی بشه وقتی رشته رو دارم از یوزر میگیرم ؟