using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Ufangx.Xss; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; using System.Diagnostics; namespace Framework461Test { [TestClass] public class UnitTest1 { void FilterAttacks(RichText richText, Func fn, [CallerMemberName] string propertyName = null) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append($"\n==== in {propertyName} ==================================================\n原文:\n{richText.Source}\n"); stringBuilder.Append("过滤\n"); string clean = richText.ToString(); stringBuilder.Append(clean); var isTrue = fn(clean); stringBuilder.Append($"\n状态:{isTrue}"); Console.WriteLine(stringBuilder.ToString()); Assert.IsTrue(isTrue); } [TestMethod] public void testScriptAttacks() { FilterAttacks("", str => str.IndexOf("script", StringComparison.OrdinalIgnoreCase) == -1); FilterAttacks("test", str => str.IndexOf("script", StringComparison.OrdinalIgnoreCase) == -1); FilterAttacks("<<<><", str => str.IndexOf("", str => str.IndexOf("onload", StringComparison.OrdinalIgnoreCase) == -1); FilterAttacks("", str => str.IndexOf("alert") == -1); FilterAttacks("", str => str.IndexOf("iframe") == -1); FilterAttacks("", str => str.IndexOf("javascript") == -1); FilterAttacks("", str => str.IndexOf("background") == -1); FilterAttacks("
", str => str.IndexOf("background") == -1); FilterAttacks("
", str => str.IndexOf("javascript") == -1); FilterAttacks("
", str => str.IndexOf("alert") == -1); FilterAttacks("", str => str.IndexOf("alert") == -1); FilterAttacks("", str => str.IndexOf("ript:alert") == -1); FilterAttacks("", str => str.IndexOf("javascript") == -1); FilterAttacks("", str => str.IndexOf("", str => str.IndexOf("", str => str.IndexOf("", str => str.IndexOf("", str => str.IndexOf("\" SRC=\"http://ha.ckers.org/xss.js\">", str => str.IndexOf("\" '' SRC=\"http://ha.ckers.org/xss.js\">", str => str.IndexOf("` SRC=\"http://ha.ckers.org/xss.js\">", str => str.IndexOf("'>\" SRC=\"http://ha.ckers.org/xss.js\">", str => str.IndexOf("document.write(\"PT SRC=\"http://ha.ckers.org/xss.js\">", str => str.IndexOf("script") == -1); FilterAttacks("