You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
14 KiB

8 years ago
  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class SanitizeTest extends PHPUnit_Framework_TestCase {
  5. private $http;
  6. public function setUp() {
  7. $this->client = new Parse();
  8. $this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
  9. $this->client->mc = null;
  10. }
  11. private function parse($params) {
  12. $request = new Request($params);
  13. $response = new Response();
  14. return $this->client->parse($request, $response);
  15. }
  16. public function testAllowsWhitelistedTags() {
  17. $url = 'http://sanitize.example/entry-with-valid-tags';
  18. $response = $this->parse(['url' => $url]);
  19. $body = $response->getContent();
  20. $this->assertEquals(200, $response->getStatusCode());
  21. $data = json_decode($body, true);
  22. $html = $data['data']['content']['html'];
  23. $this->assertEquals('entry', $data['data']['type']);
  24. $this->assertContains('This content has only valid tags.', $html);
  25. $this->assertContains('<a href="http://sanitize.example/example">links</a>,', $html, '<a> missing');
  26. $this->assertContains('<abbr>abbreviations</abbr>,', $html, '<abbr> missing');
  27. $this->assertContains('<b>bold</b>,', $html, '<b> missing');
  28. $this->assertContains('<code>inline code</code>,', $html, '<code> missing');
  29. $this->assertContains('<del>delete</del>,', $html, '<del> missing');
  30. $this->assertContains('<em>emphasis</em>,', $html, '<em> missing');
  31. $this->assertContains('<i>italics</i>,', $html, '<i> missing');
  32. $this->assertContains('<img src="http://sanitize.example/example.jpg" alt="images are allowed" />', $html, '<img> missing');
  33. $this->assertContains('<q>inline quote</q>,', $html, '<q> missing');
  34. $this->assertContains('<strike>strikethrough</strike>,', $html, '<strike> missing');
  35. $this->assertContains('<strong>strong text</strong>,', $html, '<strong> missing');
  36. $this->assertContains('<time datetime="2016-01-01">time elements</time>', $html, '<time> missing');
  37. $this->assertContains('<blockquote>Blockquote tags are okay</blockquote>', $html);
  38. $this->assertContains('<pre>preformatted text is okay too', $html, '<pre> missing');
  39. $this->assertContains('for code examples and such</pre>', $html, '<pre> missing');
  40. $this->assertContains('<p>Paragraph tags are allowed</p>', $html, '<p> missing');
  41. $this->assertContains('<h1>One</h1>', $html, '<h1> missing');
  42. $this->assertContains('<h2>Two</h2>', $html, '<h2> missing');
  43. $this->assertContains('<h3>Three</h3>', $html, '<h3> missing');
  44. $this->assertContains('<h4>Four</h4>', $html, '<h4> missing');
  45. $this->assertContains('<h5>Five</h5>', $html, '<h5> missing');
  46. $this->assertContains('<h6>Six</h6>', $html, '<h6> missing');
  47. $this->assertContains('<ul>', $html, '<ul> missing');
  48. $this->assertContains('<li>One</li>', $html, '<li> missing');
  49. }
  50. public function testRemovesUnsafeTags() {
  51. $url = 'http://sanitize.example/entry-with-unsafe-tags';
  52. $response = $this->parse(['url' => $url]);
  53. $body = $response->getContent();
  54. $this->assertEquals(200, $response->getStatusCode());
  55. $data = json_decode($body, true);
  56. $html = $data['data']['content']['html'];
  57. $text = $data['data']['content']['text'];
  58. $this->assertEquals('entry', $data['data']['type']);
  59. $this->assertNotContains('<script>', $html);
  60. $this->assertNotContains('<style>', $html);
  61. $this->assertNotContains('visiblity', $html); // from the CSS
  62. $this->assertNotContains('alert', $html); // from the JS
  63. $this->assertNotContains('visiblity', $text);
  64. $this->assertNotContains('alert', $text);
  65. }
  66. public function testAllowsMF2Classes() {
  67. $url = 'http://sanitize.example/entry-with-mf2-classes';
  68. $response = $this->parse(['url' => $url]);
  69. $body = $response->getContent();
  70. $this->assertEquals(200, $response->getStatusCode());
  71. $data = json_decode($body, true);
  72. $html = $data['data']['content']['html'];
  73. $this->assertEquals('entry', $data['data']['type']);
  74. $this->assertContains('<h2 class="p-name">Hello World</h2>', $html);
  75. $this->assertContains('<h3>Utility Class</h3>', $html);
  76. }
  77. public function testEscapingHTMLTagsInText() {
  78. $url = 'http://sanitize.example/html-escaping-in-text';
  79. $response = $this->parse(['url' => $url]);
  80. $body = $response->getContent();
  81. $this->assertEquals(200, $response->getStatusCode());
  82. $data = json_decode($body, true);
  83. $this->assertEquals('entry', $data['data']['type']);
  84. $this->assertEquals('This content has some HTML escaped entities such as & ampersand, " quote, escaped <code> HTML tags, an ümlaut, an @at sign.', $data['data']['content']['text']);
  85. }
  86. public function testEscapingHTMLTagsInHTML() {
  87. $url = 'http://sanitize.example/html-escaping-in-html';
  88. $response = $this->parse(['url' => $url]);
  89. $body = $response->getContent();
  90. $this->assertEquals(200, $response->getStatusCode());
  91. $data = json_decode($body, true);
  92. $this->assertEquals('entry', $data['data']['type']);
  93. $this->assertArrayNotHasKey('name', $data['data']);
  94. $this->assertEquals('This content has some HTML escaped entities such as & ampersand, " quote, escaped <code> HTML tags, an ümlaut, an @at sign.', $data['data']['content']['text']);
  95. $this->assertEquals('This content has some <i>HTML escaped</i> entities such as &amp; ampersand, " quote, escaped &lt;code&gt; HTML tags, an ümlaut, an @at sign.', $data['data']['content']['html']);
  96. }
  97. public function testSanitizeJavascriptURLs() {
  98. $url = 'http://sanitize.example/h-entry-with-javascript-urls';
  99. $response = $this->parse(['url' => $url]);
  100. $body = $response->getContent();
  101. $this->assertEquals(200, $response->getStatusCode());
  102. $data = json_decode($body, true);
  103. $this->assertEquals('entry', $data['data']['type']);
  104. $this->assertEquals('', $data['data']['author']['url']);
  105. $this->assertArrayNotHasKey('url', $data['data']);
  106. $this->assertArrayNotHasKey('photo', $data['data']);
  107. $this->assertArrayNotHasKey('audio', $data['data']);
  108. $this->assertArrayNotHasKey('video', $data['data']);
  109. $this->assertArrayNotHasKey('syndication', $data['data']);
  110. $this->assertArrayNotHasKey('in-reply-to', $data['data']);
  111. $this->assertArrayNotHasKey('like-of', $data['data']);
  112. $this->assertArrayNotHasKey('repost-of', $data['data']);
  113. $this->assertArrayNotHasKey('bookmark-of', $data['data']);
  114. $this->assertEquals('Author', $data['data']['author']['name']);
  115. $this->assertEquals('', $data['data']['author']['photo']);
  116. }
  117. public function testSanitizeEmailAuthorURL() {
  118. $url = 'http://sanitize.example/h-entry-with-email-author';
  119. $response = $this->parse(['url' => $url]);
  120. $body = $response->getContent();
  121. $this->assertEquals(200, $response->getStatusCode());
  122. $data = json_decode($body);
  123. $this->assertEquals('entry', $data->data->type);
  124. $this->assertEquals('', $data->data->author->url);
  125. $this->assertEquals('Author', $data->data->author->name);
  126. $this->assertEquals('http://sanitize.example/photo.jpg', $data->data->author->photo);
  127. }
  128. public function testPhotoInContentNoAlt() {
  129. // https://github.com/aaronpk/XRay/issues/52
  130. $url = 'http://sanitize.example/photo-in-content';
  131. $response = $this->parse(['url' => $url]);
  132. $body = $response->getContent();
  133. $this->assertEquals(200, $response->getStatusCode());
  134. $data = json_decode($body);
  135. $this->assertObjectNotHasAttribute('name', $data->data);
  136. $this->assertEquals('http://target.example.com/photo.jpg', $data->data->photo[0]);
  137. $this->assertEquals('This is a photo post with an img tag inside the content.', $data->data->content->text);
  138. $this->assertEquals('This is a photo post with an <code>img</code> tag inside the content.', $data->data->content->html);
  139. }
  140. /*
  141. // Commented out until #56 is resolved
  142. // https://github.com/aaronpk/XRay/issues/56
  143. public function testPhotoInTextContentNoAlt() {
  144. $url = 'http://sanitize.example/photo-in-text-content';
  145. $response = $this->parse(['url' => $url]);
  146. $body = $response->getContent();
  147. $this->assertEquals(200, $response->getStatusCode());
  148. $data = json_decode($body);
  149. $this->assertObjectNotHasAttribute('name', $data->data);
  150. $this->assertEquals('http://target.example.com/photo.jpg', $data->data->photo[0]);
  151. $this->assertEquals('This is a photo post with an img tag inside the content.', $data->data->content->text);
  152. $this->assertEquals('This is a photo post with an <code>img</code> tag inside the content.', $data->data->content->html);
  153. }
  154. */
  155. public function testPhotoInContentEmptyAltAttribute() {
  156. // https://github.com/aaronpk/XRay/issues/52
  157. $url = 'http://sanitize.example/photo-in-content-empty-alt';
  158. $response = $this->parse(['url' => $url]);
  159. $body = $response->getContent();
  160. $this->assertEquals(200, $response->getStatusCode());
  161. $data = json_decode($body);
  162. $this->assertObjectNotHasAttribute('name', $data->data);
  163. $this->assertEquals('http://target.example.com/photo.jpg', $data->data->photo[0]);
  164. $this->assertEquals('This is a photo post with an img tag inside the content.', $data->data->content->text);
  165. $this->assertEquals('This is a photo post with an <code>img</code> tag inside the content.', $data->data->content->html);
  166. }
  167. public function testPhotoInContentWithAlt() {
  168. // https://github.com/aaronpk/XRay/issues/52
  169. $url = 'http://sanitize.example/photo-in-content-with-alt';
  170. $response = $this->parse(['url' => $url]);
  171. $body = $response->getContent();
  172. $this->assertEquals(200, $response->getStatusCode());
  173. $data = json_decode($body);
  174. $this->assertObjectNotHasAttribute('name', $data->data);
  175. $this->assertEquals('http://target.example.com/photo.jpg', $data->data->photo[0]);
  176. $this->assertEquals('This is a photo post with an img tag inside the content.', $data->data->content->text);
  177. $this->assertEquals('This is a photo post with an <code>img</code> tag inside the content.', $data->data->content->html);
  178. }
  179. public function testPhotoInContentWithNoText() {
  180. $url = 'http://sanitize.example/cleverdevil';
  181. $response = $this->parse(['url' => $url]);
  182. $body = $response->getContent();
  183. $this->assertEquals(200, $response->getStatusCode());
  184. $data = json_decode($body);
  185. $this->assertObjectHasAttribute('name', $data->data);
  186. $this->assertEquals('Oh, how well they know me! 🥃', $data->data->name);
  187. $this->assertObjectNotHasAttribute('content', $data->data);
  188. $this->assertEquals('https://cleverdevil.io/file/5bf2fa91c3d4c592f9978200923cb56e/thumb.jpg', $data->data->photo[0]);
  189. }
  190. public function testPhotoWithDupeNameAndAlt1() {
  191. // https://github.com/aaronpk/XRay/issues/57
  192. $url = 'http://sanitize.example/photo-with-dupe-name-alt';
  193. $response = $this->parse(['url' => $url]);
  194. $body = $response->getContent();
  195. $this->assertEquals(200, $response->getStatusCode());
  196. $data = json_decode($body);
  197. $this->assertObjectHasAttribute('name', $data->data);
  198. $this->assertEquals('Photo caption', $data->data->name);
  199. $this->assertObjectNotHasAttribute('content', $data->data);
  200. $this->assertEquals('http://sanitize.example/photo.jpg', $data->data->photo[0]);
  201. }
  202. public function testPhotoWithDupeNameAndAlt2() {
  203. // This is simliar to adactio's markup
  204. // https://adactio.com/notes/13301
  205. $url = 'http://sanitize.example/photo-with-dupe-name-alt-2';
  206. $response = $this->parse(['url' => $url]);
  207. $body = $response->getContent();
  208. $this->assertEquals(200, $response->getStatusCode());
  209. $data = json_decode($body);
  210. $this->assertObjectHasAttribute('content', $data->data);
  211. $this->assertEquals('Photo caption', $data->data->content->text);
  212. $this->assertObjectNotHasAttribute('name', $data->data);
  213. $this->assertEquals('http://sanitize.example/photo.jpg', $data->data->photo[0]);
  214. }
  215. public function testPhotosWithAlt() {
  216. // https://github.com/microformats/microformats2-parsing/issues/16
  217. $url = 'http://sanitize.example/photos-with-alt';
  218. $response = $this->parse(['url' => $url]);
  219. $body = $response->getContent();
  220. $this->assertEquals(200, $response->getStatusCode());
  221. $data = json_decode($body);
  222. #print_r($data->data);
  223. $this->assertEquals('🌆 Made it to the first #NPSF #earlygang of the year, did in-betweeners abs, and 6:30 workout with a brutal burnout that was really its own workout. But wow pretty sunrise. Plus 50+ deg F? I’ll take it. #100PDPD#justshowup #darknesstodawn #wakeupthesun #fromwhereirun #NovemberProject #sunrise #latergram #nofilter', $data->data->content->text);
  224. $this->assertObjectNotHasAttribute('name', $data->data);
  225. $this->assertEquals('https://igx.4sqi.net/img/general/original/476_g7yruXflacsGr7PyVmECefyTBMB_R99zmPQxW7pftzA.jpg', $data->data->photo[0]);
  226. $this->assertEquals('https://igx.4sqi.net/img/general/original/476_zM3UgU9JHNhom907Ac_1WCEcUhGOJZaNWGlRmev86YA.jpg', $data->data->photo[1]);
  227. }
  228. /*
  229. // Commented out until #55 is resolved
  230. // https://github.com/aaronpk/XRay/issues/55
  231. public function testEntryWithImgNoImpliedPhoto() {
  232. // See https://github.com/microformats/microformats2-parsing/issues/6#issuecomment-357286985
  233. // and https://github.com/aaronpk/XRay/issues/52#issuecomment-357269683
  234. // and https://github.com/microformats/microformats2-parsing/issues/16
  235. $url = 'http://sanitize.example/entry-with-img-no-implied-photo';
  236. $response = $this->parse(['url' => $url]);
  237. $body = $response->getContent();
  238. $this->assertEquals(200, $response->getStatusCode());
  239. $data = json_decode($body);
  240. $this->assertObjectNotHasAttribute('photo', $data->data);
  241. $this->assertObjectNotHasAttribute('name', $data->data);
  242. $this->assertEquals('http://target.example.com/photo.jpg', $data->data->photo[0]);
  243. $this->assertEquals('This is a photo post with an img tag inside the content, which does not have a u-photo class so should not be removed.', $data->data->content->text);
  244. $this->assertEquals('This is a photo post with an <code>img</code> tag inside the content, which does not have a u-photo class so should not be removed. <img src="http://target.example.com/photo.jpg" alt="a photo">', $data->data->content->html);
  245. }
  246. */
  247. }