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.

414 lines
18 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. $this->assertContains('<p>We should allow<br />break<br />tags too</p>', $html, '<br> missing');
  50. }
  51. public function testRemovesUnsafeTags() {
  52. $url = 'http://sanitize.example/entry-with-unsafe-tags';
  53. $response = $this->parse(['url' => $url]);
  54. $body = $response->getContent();
  55. $this->assertEquals(200, $response->getStatusCode());
  56. $data = json_decode($body, true);
  57. $html = $data['data']['content']['html'];
  58. $text = $data['data']['content']['text'];
  59. $this->assertEquals('entry', $data['data']['type']);
  60. $this->assertNotContains('<script>', $html);
  61. $this->assertNotContains('<style>', $html);
  62. $this->assertNotContains('visiblity', $html); // from the CSS
  63. $this->assertNotContains('alert', $html); // from the JS
  64. $this->assertNotContains('visiblity', $text);
  65. $this->assertNotContains('alert', $text);
  66. }
  67. public function testAllowsMF2Classes() {
  68. $url = 'http://sanitize.example/entry-with-mf2-classes';
  69. $response = $this->parse(['url' => $url]);
  70. $body = $response->getContent();
  71. $this->assertEquals(200, $response->getStatusCode());
  72. $data = json_decode($body, true);
  73. $html = $data['data']['content']['html'];
  74. $this->assertEquals('entry', $data['data']['type']);
  75. $this->assertContains('<h2 class="p-name">Hello World</h2>', $html);
  76. $this->assertContains('<h3>Utility Class</h3>', $html);
  77. }
  78. public function testEscapingHTMLTagsInText() {
  79. $url = 'http://sanitize.example/html-escaping-in-text';
  80. $response = $this->parse(['url' => $url]);
  81. $body = $response->getContent();
  82. $this->assertEquals(200, $response->getStatusCode());
  83. $data = json_decode($body, true);
  84. $this->assertEquals('entry', $data['data']['type']);
  85. $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']);
  86. }
  87. public function testEscapingHTMLTagsInHTML() {
  88. $url = 'http://sanitize.example/html-escaping-in-html';
  89. $response = $this->parse(['url' => $url]);
  90. $body = $response->getContent();
  91. $this->assertEquals(200, $response->getStatusCode());
  92. $data = json_decode($body, true);
  93. $this->assertEquals('entry', $data['data']['type']);
  94. $this->assertArrayNotHasKey('name', $data['data']);
  95. $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']);
  96. $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']);
  97. }
  98. public function testAllowIframeVideo() {
  99. $url = 'http://sanitize.example/entry-with-iframe-video';
  100. $response = $this->parse(['url' => $url]);
  101. $body = $response->getContent();
  102. $data = json_decode($body, true);
  103. $html = $data['data']['content']['html'];
  104. $this->assertNotContains('<iframe', $html);
  105. $response = $this->parse(['url' => $url, 'allow-iframe-video' => 'true']);
  106. $body = $response->getContent();
  107. $data = json_decode($body, true);
  108. $html = $data['data']['content']['html'];
  109. $this->assertContains('youtube.com', $html);
  110. $this->assertNotContains('https://attack-domain.com', $html);
  111. $this->assertNotContains('<iframe width="580" height="345"', $html);
  112. }
  113. public function testSanitizeJavascriptURLs() {
  114. $url = 'http://sanitize.example/h-entry-with-javascript-urls';
  115. $response = $this->parse(['url' => $url]);
  116. $body = $response->getContent();
  117. $this->assertEquals(200, $response->getStatusCode());
  118. $data = json_decode($body, true);
  119. $this->assertEquals('entry', $data['data']['type']);
  120. $this->assertEquals('', $data['data']['author']['url']);
  121. $this->assertArrayNotHasKey('url', $data['data']);
  122. $this->assertArrayNotHasKey('photo', $data['data']);
  123. $this->assertArrayNotHasKey('audio', $data['data']);
  124. $this->assertArrayNotHasKey('video', $data['data']);
  125. $this->assertArrayNotHasKey('syndication', $data['data']);
  126. $this->assertArrayNotHasKey('in-reply-to', $data['data']);
  127. $this->assertArrayNotHasKey('like-of', $data['data']);
  128. $this->assertArrayNotHasKey('repost-of', $data['data']);
  129. $this->assertArrayNotHasKey('bookmark-of', $data['data']);
  130. $this->assertEquals('Author', $data['data']['author']['name']);
  131. $this->assertEquals('', $data['data']['author']['photo']);
  132. }
  133. public function testSanitizeEmailAuthorURL() {
  134. $url = 'http://sanitize.example/h-entry-with-email-author';
  135. $response = $this->parse(['url' => $url]);
  136. $body = $response->getContent();
  137. $this->assertEquals(200, $response->getStatusCode());
  138. $data = json_decode($body);
  139. $this->assertEquals('entry', $data->data->type);
  140. $this->assertEquals('', $data->data->author->url);
  141. $this->assertEquals('Author', $data->data->author->name);
  142. $this->assertEquals('http://sanitize.example/photo.jpg', $data->data->author->photo);
  143. }
  144. public function testPhotoInContentNoAlt() {
  145. // https://github.com/aaronpk/XRay/issues/52
  146. $url = 'http://sanitize.example/photo-in-content';
  147. $response = $this->parse(['url' => $url]);
  148. $body = $response->getContent();
  149. $this->assertEquals(200, $response->getStatusCode());
  150. $data = json_decode($body);
  151. $this->assertObjectNotHasAttribute('name', $data->data);
  152. $this->assertEquals('http://target.example.com/photo.jpg', $data->data->photo[0]);
  153. $this->assertEquals('This is a photo post with an img tag inside the content.', $data->data->content->text);
  154. $this->assertEquals('This is a photo post with an <code>img</code> tag inside the content.', $data->data->content->html);
  155. }
  156. /*
  157. // Commented out until #56 is resolved
  158. // https://github.com/aaronpk/XRay/issues/56
  159. public function testPhotoInTextContentNoAlt() {
  160. $url = 'http://sanitize.example/photo-in-text-content';
  161. $response = $this->parse(['url' => $url]);
  162. $body = $response->getContent();
  163. $this->assertEquals(200, $response->getStatusCode());
  164. $data = json_decode($body);
  165. $this->assertObjectNotHasAttribute('name', $data->data);
  166. $this->assertEquals('http://target.example.com/photo.jpg', $data->data->photo[0]);
  167. $this->assertEquals('This is a photo post with an img tag inside the content.', $data->data->content->text);
  168. $this->assertEquals('This is a photo post with an <code>img</code> tag inside the content.', $data->data->content->html);
  169. }
  170. */
  171. public function testRelativePhotoInContent() {
  172. $url = 'http://sanitize.example/photo-in-content-relative';
  173. $response = $this->parse(['url' => $url]);
  174. $body = $response->getContent();
  175. $this->assertEquals(200, $response->getStatusCode());
  176. $data = json_decode($body);
  177. $this->assertContains('http://sanitize.example/photo1.jpg', $data->data->content->html);
  178. }
  179. public function testRelativePhotoProperty() {
  180. $url = 'http://sanitize.example/photo-relative';
  181. $response = $this->parse(['url' => $url]);
  182. $body = $response->getContent();
  183. $this->assertEquals(200, $response->getStatusCode());
  184. $data = json_decode($body);
  185. $this->assertEquals('http://sanitize.example/photo.jpg', $data->data->photo[0]);
  186. }
  187. public function testPhotoInContentEmptyAltAttribute() {
  188. // https://github.com/aaronpk/XRay/issues/52
  189. $url = 'http://sanitize.example/photo-in-content-empty-alt';
  190. $response = $this->parse(['url' => $url]);
  191. $body = $response->getContent();
  192. $this->assertEquals(200, $response->getStatusCode());
  193. $data = json_decode($body);
  194. $this->assertObjectNotHasAttribute('name', $data->data);
  195. $this->assertEquals('http://target.example.com/photo.jpg', $data->data->photo[0]);
  196. $this->assertEquals('This is a photo post with an img tag inside the content.', $data->data->content->text);
  197. $this->assertEquals('This is a photo post with an <code>img</code> tag inside the content.', $data->data->content->html);
  198. }
  199. public function testPhotoInContentWithAlt() {
  200. // https://github.com/aaronpk/XRay/issues/52
  201. $url = 'http://sanitize.example/photo-in-content-with-alt';
  202. $response = $this->parse(['url' => $url]);
  203. $body = $response->getContent();
  204. $this->assertEquals(200, $response->getStatusCode());
  205. $data = json_decode($body);
  206. $this->assertObjectNotHasAttribute('name', $data->data);
  207. $this->assertEquals('http://target.example.com/photo.jpg', $data->data->photo[0]);
  208. $this->assertEquals('This is a photo post with an img tag inside the content.', $data->data->content->text);
  209. $this->assertEquals('This is a photo post with an <code>img</code> tag inside the content.', $data->data->content->html);
  210. }
  211. public function testPhotoInContentWithNameAndNoText() {
  212. $url = 'http://sanitize.example/cleverdevil';
  213. $response = $this->parse(['url' => $url]);
  214. $body = $response->getContent();
  215. $this->assertEquals(200, $response->getStatusCode());
  216. $data = json_decode($body);
  217. $this->assertObjectHasAttribute('name', $data->data);
  218. $this->assertEquals('Oh, how well they know me! 🥃', $data->data->name);
  219. $this->assertObjectNotHasAttribute('content', $data->data);
  220. $this->assertEquals('https://cleverdevil.io/file/5bf2fa91c3d4c592f9978200923cb56e/thumb.jpg', $data->data->photo[0]);
  221. }
  222. public function testPhotoWithDupeNameAndAlt1() {
  223. // https://github.com/aaronpk/XRay/issues/57
  224. $url = 'http://sanitize.example/photo-with-dupe-name-alt';
  225. $response = $this->parse(['url' => $url]);
  226. $body = $response->getContent();
  227. $this->assertEquals(200, $response->getStatusCode());
  228. $data = json_decode($body);
  229. $this->assertObjectHasAttribute('name', $data->data);
  230. $this->assertEquals('Photo caption', $data->data->name);
  231. $this->assertObjectNotHasAttribute('content', $data->data);
  232. $this->assertEquals('http://sanitize.example/photo.jpg', $data->data->photo[0]);
  233. }
  234. public function testPhotoWithDupeNameAndAlt2() {
  235. // This is simliar to adactio's markup
  236. // https://adactio.com/notes/13301
  237. $url = 'http://sanitize.example/photo-with-dupe-name-alt-2';
  238. $response = $this->parse(['url' => $url]);
  239. $body = $response->getContent();
  240. $this->assertEquals(200, $response->getStatusCode());
  241. $data = json_decode($body);
  242. $this->assertObjectHasAttribute('content', $data->data);
  243. $this->assertEquals('Photo caption', $data->data->content->text);
  244. $this->assertObjectNotHasAttribute('name', $data->data);
  245. $this->assertEquals('http://sanitize.example/photo.jpg', $data->data->photo[0]);
  246. }
  247. public function testPhotoInContentWithNoText() {
  248. $url = 'http://sanitize.example/photo-in-content-with-alt-no-text';
  249. $response = $this->parse(['url' => $url]);
  250. $body = $response->getContent();
  251. $this->assertEquals(200, $response->getStatusCode());
  252. $data = json_decode($body, true);
  253. $this->assertEquals('<p><img src="http://sanitize.example/photo.jpg" alt="test" /></p>', $data['data']['content']['html']);
  254. $this->assertEquals('', $data['data']['content']['text']);
  255. }
  256. public function testPhotoInContentWithPNoAlt() {
  257. $url = 'http://sanitize.example/photo-in-content-with-p-no-alt';
  258. $response = $this->parse(['url' => $url]);
  259. $body = $response->getContent();
  260. $this->assertEquals(200, $response->getStatusCode());
  261. $data = json_decode($body, true);
  262. $this->assertEquals('<p><img src="http://sanitize.example/photo.jpg" alt="photo.jpg" /></p>', $data['data']['content']['html']);
  263. $this->assertEquals('', $data['data']['content']['text']);
  264. }
  265. public function testPhotoInContentNoPWithURLPhoto() {
  266. $url = 'http://sanitize.example/photo-in-content-no-p-with-url-photo';
  267. $response = $this->parse(['url' => $url]);
  268. $body = $response->getContent();
  269. $this->assertEquals(200, $response->getStatusCode());
  270. $data = json_decode($body, true);
  271. $this->assertEquals('<img src="http://sanitize.example/photo.jpg" alt="test" />', $data['data']['content']['html']);
  272. $this->assertEquals('', $data['data']['content']['text']);
  273. }
  274. public function testPhotoInContentNoPWithAlt() {
  275. // This h-entry has no u-url so has an implied u-photo. we don't actually care what happens with it because
  276. // this should never happen in the wild
  277. $url = 'http://sanitize.example/photo-in-content-no-p-with-alt';
  278. $response = $this->parse(['url' => $url]);
  279. $body = $response->getContent();
  280. $this->assertEquals(200, $response->getStatusCode());
  281. $data = json_decode($body, true);
  282. }
  283. /*
  284. // TODO: add support for embedded video and audio tags in html content
  285. public function testContentIsOnlyVideo() {
  286. $url = 'http://sanitize.example/content-is-only-video';
  287. $response = $this->parse(['url' => $url]);
  288. $body = $response->getContent();
  289. $this->assertEquals(200, $response->getStatusCode());
  290. $data = json_decode($body, true);
  291. print_r($data);
  292. }
  293. */
  294. public function testPhotosWithAlt() {
  295. // https://github.com/microformats/microformats2-parsing/issues/16
  296. $url = 'http://sanitize.example/photos-with-alt';
  297. $response = $this->parse(['url' => $url]);
  298. $body = $response->getContent();
  299. $this->assertEquals(200, $response->getStatusCode());
  300. $data = json_decode($body);
  301. $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'."\n\n".'#justshowup #darknesstodawn #wakeupthesun #fromwhereirun #NovemberProject #sunrise #latergram #nofilter', $data->data->content->text);
  302. $this->assertObjectNotHasAttribute('name', $data->data);
  303. $this->assertEquals('https://igx.4sqi.net/img/general/original/476_g7yruXflacsGr7PyVmECefyTBMB_R99zmPQxW7pftzA.jpg', $data->data->photo[0]);
  304. $this->assertEquals('https://igx.4sqi.net/img/general/original/476_zM3UgU9JHNhom907Ac_1WCEcUhGOJZaNWGlRmev86YA.jpg', $data->data->photo[1]);
  305. }
  306. // Ignoring this issue for now. This should not happen in the wild.
  307. // https://github.com/aaronpk/XRay/issues/55
  308. // Skipping the implied photo check because in the wild, h-entrys should not exist withou a u-url, which stops implied parsing.
  309. public function testEntryWithImgNoImpliedPhoto() {
  310. // See https://github.com/microformats/microformats2-parsing/issues/6#issuecomment-357286985
  311. // and https://github.com/aaronpk/XRay/issues/52#issuecomment-357269683
  312. // and https://github.com/microformats/microformats2-parsing/issues/16
  313. $url = 'http://sanitize.example/entry-with-img-no-implied-photo';
  314. $response = $this->parse(['url' => $url]);
  315. $body = $response->getContent();
  316. $this->assertEquals(200, $response->getStatusCode());
  317. $data = json_decode($body);
  318. $this->assertObjectNotHasAttribute('photo', $data->data);
  319. $this->assertObjectNotHasAttribute('name', $data->data);
  320. $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);
  321. $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);
  322. }
  323. public function testWhitespaceWithBreakTags() {
  324. $url = 'http://sanitize.example/entry-with-br-tags';
  325. $response = $this->parse(['url' => $url]);
  326. $body = $response->getContent();
  327. $this->assertEquals(200, $response->getStatusCode());
  328. $data = json_decode($body);
  329. $this->assertEquals('This content has two break tags to indicate a paragraph break.<br /><br />This is how tantek\'s autolinker works.', $data->data->content->html);
  330. $this->assertEquals("This content has two break tags to indicate a paragraph break.\n\nThis is how tantek's autolinker works.", $data->data->content->text);
  331. }
  332. }