PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/solution/NearForums.Tests/HtmlInputTests.cs

#
C# | 243 lines | 170 code | 41 blank | 32 comment | 2 complexity | 7fe49da6d93be9783dc1c351b2604cea MD5 | raw file
Possible License(s): JSON, LGPL-2.1
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using NearForums.Web.Extensions;
  7. using NearForums.Tests;
  8. using HtmlAgilityPack;
  9. using System.IO;
  10. namespace NearForums.Tests
  11. {
  12. /// <summary>
  13. /// Summary description for SpamPreventionTests
  14. /// </summary>
  15. [TestClass]
  16. public class HtmlInputTests
  17. {
  18. public HtmlInputTests()
  19. {
  20. //
  21. // TODO: Add constructor logic here
  22. //
  23. }
  24. private TestContext testContextInstance;
  25. /// <summary>
  26. ///Gets or sets the test context which provides
  27. ///information about and functionality for the current test run.
  28. ///</summary>
  29. public TestContext TestContext
  30. {
  31. get
  32. {
  33. return testContextInstance;
  34. }
  35. set
  36. {
  37. testContextInstance = value;
  38. }
  39. }
  40. #region Additional test attributes
  41. //
  42. // You can use the following additional attributes as you write your tests:
  43. //
  44. // Use ClassInitialize to run code before running the first test in the class
  45. // [ClassInitialize()]
  46. // public static void MyClassInitialize(TestContext testContext) { }
  47. //
  48. // Use ClassCleanup to run code after all tests in a class have run
  49. // [ClassCleanup()]
  50. // public static void MyClassCleanup() { }
  51. //
  52. // Use TestInitialize to run code before running each test
  53. // [TestInitialize()]
  54. // public void MyTestInitialize() { }
  55. //
  56. // Use TestCleanup to run code after each test has run
  57. // [TestCleanup()]
  58. // public void MyTestCleanup() { }
  59. //
  60. #endregion
  61. [TestMethod]
  62. public void HtmlSanitizer_Basic_Test()
  63. {
  64. var html = "";
  65. html = "<p>Hello World<object></object><script></script><iframe></iframe></p>".SafeHtml();
  66. Assert.IsTrue(html == "<p>Hello World</p>");
  67. html = "<p>Hello World <a href=\"http://argo.com/1.html\">argo</a></p>".SafeHtml();
  68. Assert.IsTrue(html.Contains("<a"));
  69. html = "<p>Hello World <a href=\"onclick:alert('XSS');\">argo</a></p>".SafeHtml();
  70. Assert.IsTrue(!html.Contains("<a"));
  71. html = "<p>Hello World <a href=\"#\" onclick=\"DoXss();\">argo</a></p>".SafeHtml();
  72. Assert.IsTrue(!html.Contains("onclick"));
  73. html = "<p>Hello World <img src=\"http://google.com/logo.gif\" onclick=\"DoXss();\" /></p>".SafeHtml();
  74. Assert.IsTrue(!html.Contains("onclick"));
  75. Assert.IsTrue(html.Contains("<p"));
  76. html = "<p>Accénted</p>".SafeHtml();
  77. Assert.IsTrue(html.Contains("Accénted"));
  78. html = "<p>Acc&eacute;nted</p>".SafeHtml();
  79. Assert.IsTrue(html.Contains("Acc&eacute;nted"));
  80. }
  81. [TestMethod]
  82. public void HtmlSanitizer_Images_Test()
  83. {
  84. var html = "";
  85. #region Image tags
  86. html = "<p>Hello World <img src=\"http://google.com/logo.gif\" height=\"10\" /></p>".SafeHtml();
  87. Assert.IsTrue(html.Contains("<img"));
  88. html = "<p>Hello World <img src=\"http://google.com/logo.gif\" height=\"10\" width=\"10\" /></p>".SafeHtml();
  89. Assert.IsTrue(html.Contains("<img"));
  90. html = "<p>Hello World <img src=\"http://google.com/logo.gif\" height=\"10\" width=\"10\" /></p>".SafeHtml();
  91. Assert.IsTrue(html.Contains("<img"));
  92. html = "<p>Hello World <img src=\"http://google.com/logo.gif\" /></p>".SafeHtml();
  93. Assert.IsTrue(html.Contains("<img"));
  94. #endregion
  95. }
  96. [TestMethod]
  97. public void HtmlSanitizer_Links_Test()
  98. {
  99. var html = "";
  100. #region Link tags
  101. html = "<a href=\"http://google.com\">normal google link</a>".SafeHtml();
  102. Assert.IsTrue(html.Contains("<a"));
  103. html = "<a href=\"#msg123\">[#1]</a>".SafeHtml();
  104. Assert.IsTrue(html.Contains("<a"));
  105. html = "<a href=\"#msg123\" title=\"Message!\">[#1]</a>".SafeHtml();
  106. Assert.IsTrue(html.Contains("<a"));
  107. html = "<a href=\"#msg123\" class=\"fastQuote\">[#1]</a>".SafeHtml();
  108. Assert.IsTrue(html.Contains("<a"));
  109. html = "<a href=\"http://google.com\" class=\"highlight\">google highlighted</a>".SafeHtml();
  110. Assert.IsTrue(html.Contains("<a"));
  111. Assert.IsTrue(html.Contains("rel=\"nofollow\""));
  112. html = "<a href=\"http://google.com\" rel=\"nofollow\">google nofollow</a>".SafeHtml();
  113. Assert.IsTrue(html.Contains("<a"));
  114. #endregion
  115. }
  116. [TestMethod]
  117. public void HtmlSanitizer_WordTexts_Test()
  118. {
  119. var html = "";
  120. #region Word copy-pasting
  121. html = "<p style=\"mso-layout-grid-align: none; text-autospace: none;\"><span style=\"mso-bidi-font-family: Arial; mso-ansi-language: EN-GB;\" lang=\"EN-GB\">Text inside a p</span></p>".SafeHtml();
  122. Assert.IsTrue(html.Contains("<p>Text inside a p"));
  123. html = "<p class=\"MsoNormal\"><span style=\"mso-ansi-language: EN-GB;\" lang=\"EN-GB\">Text inside a p</span></p>".SafeHtml();
  124. Assert.IsTrue(html.Contains("<p"));
  125. Assert.IsTrue(!html.Contains("MsoNormal"));
  126. Assert.IsTrue(html.Contains("Text inside a p"));
  127. html = "<p>Some comments<!-- sdjsdhsdj -- --></p>".SafeHtml();
  128. Assert.IsTrue(!html.Contains("<!--"));
  129. #endregion
  130. }
  131. [TestMethod]
  132. public void HtmlSanitizer_CommonInternet_Test()
  133. {
  134. var html = "";
  135. #region Tests from http://ha.ckers.org/xss.html
  136. html = "<SCRIPT SRC=http://ha.ckers.org/xss.js></SCRIPT>".SafeHtml();
  137. Assert.IsTrue(!html.Contains("script"));
  138. html = "<IMG SRC=javascript:alert('XSS')>".SafeHtml();
  139. Assert.IsTrue(!html.Contains("img"));
  140. html = "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=http://;URL=javascript:alert('XSS');\">".SafeHtml();
  141. Assert.IsTrue(!html.Contains("meta"));
  142. html = "<IFRAME SRC=\"javascript:alert('XSS');\"></IFRAME>".SafeHtml();
  143. Assert.IsTrue(!html.Contains("iframe"));
  144. html = "<TABLE><TD BACKGROUND=\"javascript:alert('XSS')\">".SafeHtml();
  145. Assert.IsTrue(!html.Contains("table"));
  146. html = "<OBJECT TYPE=\"text/x-scriptlet\" DATA=\"http://ha.ckers.org/scriptlet.html\"></OBJECT>".SafeHtml();
  147. Assert.IsTrue(!html.Contains("object"));
  148. html = "<OBJECT TYPE=\"text/x-scriptlet\" DATA=\"http://ha.ckers.org/scriptlet.html\"></OBJECT>".SafeHtml();
  149. Assert.IsTrue(!html.Contains("object"));
  150. html = "".SafeHtml();
  151. Assert.IsTrue(html == "");
  152. #endregion
  153. }
  154. [TestMethod]
  155. public void HtmlSanitizer_Replacement_Test()
  156. {
  157. var html = "";
  158. //Smiley
  159. html = ":)".SafeHtml().ReplaceValues();
  160. Assert.IsTrue(html.Contains("<img"));
  161. //Check interaction with replacements
  162. //Safe + Replacements + SAfe + Replacements
  163. html = "<p>#200: Hey man!</p>".SafeHtml().ReplaceValues().SafeHtml().ReplaceValues().SafeHtml().ReplaceValues();
  164. Assert.IsTrue(html.Contains("[#200]</a>: Hey man!"));
  165. Assert.IsTrue(html.Contains("fastQuote"));
  166. html = "<a href=\"#msg10\" class=\"fastQuote\">Something</a>".SafeHtml();
  167. Assert.IsTrue(html.Contains("class="));
  168. }
  169. [TestMethod]
  170. public void HtmlErrors_Fix_Test()
  171. {
  172. #region Auto close mallformed paragraphs
  173. var outputWriter = new StringWriter();
  174. var doc = new HtmlDocument();
  175. string output;
  176. doc.LoadHtml("<div class=\"wrapper\"><p><strong>This paragraph <br>is not closed</strong></div>");
  177. doc.OptionWriteEmptyNodes = true;
  178. doc.Save(outputWriter);
  179. output = outputWriter.ToString();
  180. Assert.IsTrue(output.Contains("<p />"));
  181. #endregion
  182. #region Include childs in mallformed lists
  183. outputWriter = new StringWriter();
  184. doc = new HtmlDocument();
  185. doc.LoadHtml("<div class=\"wrapper\"><ul><li>Item of the list</li></div>");
  186. doc.OptionWriteEmptyNodes = true;
  187. doc.Save(outputWriter);
  188. output = outputWriter.ToString();
  189. Assert.IsTrue(output.Contains("</ul>"));
  190. #endregion
  191. #region Upper case elements
  192. outputWriter = new StringWriter();
  193. doc = new HtmlDocument();
  194. doc.LoadHtml("<div class=\"wrapper\"><UL><li>Item of the list</li></div>");
  195. doc.OptionWriteEmptyNodes = true;
  196. doc.Save(outputWriter);
  197. output = outputWriter.ToString();
  198. Assert.IsTrue(output.Contains("<ul>"));
  199. #endregion
  200. }
  201. }
  202. }