formatter_spec.rb 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. require 'rails_helper'
  2. RSpec.describe Formatter do
  3. let(:local_account) { Fabricate(:account, domain: nil, username: 'alice') }
  4. let(:remote_account) { Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/') }
  5. shared_examples 'encode and link URLs' do
  6. context 'given a stand-alone medium URL' do
  7. let(:text) { 'https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4' }
  8. it 'matches the full URL' do
  9. is_expected.to include 'href="https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4"'
  10. end
  11. end
  12. context 'given a stand-alone google URL' do
  13. let(:text) { 'http://google.com' }
  14. it 'matches the full URL' do
  15. is_expected.to include 'href="http://google.com"'
  16. end
  17. end
  18. context 'given a stand-alone IDN URL' do
  19. let(:text) { 'https://nic.みんな/' }
  20. it 'matches the full URL' do
  21. is_expected.to include 'href="https://nic.みんな/"'
  22. end
  23. it 'has display URL' do
  24. is_expected.to include '<span class="">nic.みんな/</span>'
  25. end
  26. end
  27. context 'given a URL with a trailing period' do
  28. let(:text) { 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ' }
  29. it 'matches the full URL but not the period' do
  30. is_expected.to include 'href="http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona"'
  31. end
  32. end
  33. context 'given a URL enclosed with parentheses' do
  34. let(:text) { '(http://google.com/)' }
  35. it 'matches the full URL but not the parentheses' do
  36. is_expected.to include 'href="http://google.com/"'
  37. end
  38. end
  39. context 'given a URL with a trailing exclamation point' do
  40. let(:text) { 'http://www.google.com!' }
  41. it 'matches the full URL but not the exclamation point' do
  42. is_expected.to include 'href="http://www.google.com"'
  43. end
  44. end
  45. context 'given a URL with a trailing single quote' do
  46. let(:text) { "http://www.google.com'" }
  47. it 'matches the full URL but not the single quote' do
  48. is_expected.to include 'href="http://www.google.com"'
  49. end
  50. end
  51. context 'given a URL with a trailing angle bracket' do
  52. let(:text) { 'http://www.google.com>' }
  53. it 'matches the full URL but not the angle bracket' do
  54. is_expected.to include 'href="http://www.google.com"'
  55. end
  56. end
  57. context 'given a URL with a query string' do
  58. context 'with escaped unicode character' do
  59. let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
  60. it 'matches the full URL' do
  61. is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&amp;q=autolink"'
  62. end
  63. end
  64. context 'with unicode character' do
  65. let(:text) { 'https://www.ruby-toolbox.com/search?utf8=✓&q=autolink' }
  66. it 'matches the full URL' do
  67. is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=✓&amp;q=autolink"'
  68. end
  69. end
  70. context 'with unicode character at the end' do
  71. let(:text) { 'https://www.ruby-toolbox.com/search?utf8=✓' }
  72. it 'matches the full URL' do
  73. is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=✓"'
  74. end
  75. end
  76. context 'with escaped and not escaped unicode characters' do
  77. let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&utf81=✓&q=autolink' }
  78. it 'preserves escaped unicode characters' do
  79. is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&amp;utf81=✓&amp;q=autolink"'
  80. end
  81. end
  82. end
  83. context 'given a URL with parentheses in it' do
  84. let(:text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
  85. it 'matches the full URL' do
  86. is_expected.to include 'href="https://en.wikipedia.org/wiki/Diaspora_(software)"'
  87. end
  88. end
  89. context 'given a URL in quotation marks' do
  90. let(:text) { '"https://example.com/"' }
  91. it 'does not match the quotation marks' do
  92. is_expected.to include 'href="https://example.com/"'
  93. end
  94. end
  95. context 'given a URL in angle brackets' do
  96. let(:text) { '<https://example.com/>' }
  97. it 'does not match the angle brackets' do
  98. is_expected.to include 'href="https://example.com/"'
  99. end
  100. end
  101. context 'given a URL with Japanese path string' do
  102. let(:text) { 'https://ja.wikipedia.org/wiki/日本' }
  103. it 'matches the full URL' do
  104. is_expected.to include 'href="https://ja.wikipedia.org/wiki/日本"'
  105. end
  106. end
  107. context 'given a URL with Korean path string' do
  108. let(:text) { 'https://ko.wikipedia.org/wiki/대한민국' }
  109. it 'matches the full URL' do
  110. is_expected.to include 'href="https://ko.wikipedia.org/wiki/대한민국"'
  111. end
  112. end
  113. context 'given a URL with a full-width space' do
  114. let(:text) { 'https://example.com/ abc123' }
  115. it 'does not match the full-width space' do
  116. is_expected.to include 'href="https://example.com/"'
  117. end
  118. end
  119. context 'given a URL in Japanese quotation marks' do
  120. let(:text) { '「[https://example.org/」' }
  121. it 'does not match the quotation marks' do
  122. is_expected.to include 'href="https://example.org/"'
  123. end
  124. end
  125. context 'given a URL with Simplified Chinese path string' do
  126. let(:text) { 'https://baike.baidu.com/item/中华人民共和国' }
  127. it 'matches the full URL' do
  128. is_expected.to include 'href="https://baike.baidu.com/item/中华人民共和国"'
  129. end
  130. end
  131. context 'given a URL with Traditional Chinese path string' do
  132. let(:text) { 'https://zh.wikipedia.org/wiki/臺灣' }
  133. it 'matches the full URL' do
  134. is_expected.to include 'href="https://zh.wikipedia.org/wiki/臺灣"'
  135. end
  136. end
  137. context 'given a URL containing unsafe code (XSS attack, visible part)' do
  138. let(:text) { %q{http://example.com/b<del>b</del>} }
  139. it 'does not include the HTML in the URL' do
  140. is_expected.to include '"http://example.com/b"'
  141. end
  142. it 'escapes the HTML' do
  143. is_expected.to include '&lt;del&gt;b&lt;/del&gt;'
  144. end
  145. end
  146. context 'given a URL containing unsafe code (XSS attack, invisible part)' do
  147. let(:text) { %q{http://example.com/blahblahblahblah/a<script>alert("Hello")</script>} }
  148. it 'does not include the HTML in the URL' do
  149. is_expected.to include '"http://example.com/blahblahblahblah/a"'
  150. end
  151. it 'escapes the HTML' do
  152. is_expected.to include '&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;'
  153. end
  154. end
  155. context 'given text containing HTML code (script tag)' do
  156. let(:text) { '<script>alert("Hello")</script>' }
  157. it 'escapes the HTML' do
  158. is_expected.to include '<p>&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;</p>'
  159. end
  160. end
  161. context 'given text containing HTML (XSS attack)' do
  162. let(:text) { %q{<img src="javascript:alert('XSS');">} }
  163. it 'escapes the HTML' do
  164. is_expected.to include '<p>&lt;img src=&quot;javascript:alert(&apos;XSS&apos;);&quot;&gt;</p>'
  165. end
  166. end
  167. context 'given an invalid URL' do
  168. let(:text) { 'http://www\.google\.com' }
  169. it 'outputs the raw URL' do
  170. is_expected.to eq '<p>http://www\.google\.com</p>'
  171. end
  172. end
  173. context 'given text containing a hashtag' do
  174. let(:text) { '#hashtag' }
  175. it 'creates a hashtag link' do
  176. is_expected.to include '/tags/hashtag" class="mention hashtag" rel="tag">#<span>hashtag</span></a>'
  177. end
  178. end
  179. context 'given text containing a hashtag with Unicode chars' do
  180. let(:text) { '#hashtagタグ' }
  181. it 'creates a hashtag link' do
  182. is_expected.to include '/tags/hashtag%E3%82%BF%E3%82%B0" class="mention hashtag" rel="tag">#<span>hashtagタグ</span></a>'
  183. end
  184. end
  185. end
  186. describe '#format_spoiler' do
  187. subject { Formatter.instance.format_spoiler(status) }
  188. context 'given a post containing plain text' do
  189. let(:status) { Fabricate(:status, text: 'text', spoiler_text: 'Secret!', uri: nil) }
  190. it 'Returns the spoiler text' do
  191. is_expected.to eq 'Secret!'
  192. end
  193. end
  194. context 'given a post with an emoji shortcode at the start' do
  195. let!(:emoji) { Fabricate(:custom_emoji) }
  196. let(:status) { Fabricate(:status, text: 'text', spoiler_text: ':coolcat: Secret!', uri: nil) }
  197. let(:text) { ':coolcat: Beep boop' }
  198. it 'converts the shortcode to an image tag' do
  199. is_expected.to match(/<img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  200. end
  201. end
  202. end
  203. describe '#format' do
  204. subject { Formatter.instance.format(status) }
  205. context 'given a post with local status' do
  206. context 'given a reblogged post' do
  207. let(:reblog) { Fabricate(:status, account: local_account, text: 'Hello world', uri: nil) }
  208. let(:status) { Fabricate(:status, reblog: reblog) }
  209. it 'returns original status with credit to its author' do
  210. is_expected.to include 'RT <span class="h-card"><a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span> Hello world'
  211. end
  212. end
  213. context 'given a post containing plain text' do
  214. let(:status) { Fabricate(:status, text: 'text', uri: nil) }
  215. it 'paragraphizes the text' do
  216. is_expected.to eq '<p>text</p>'
  217. end
  218. end
  219. context 'given a post containing line feeds' do
  220. let(:status) { Fabricate(:status, text: "line\nfeed", uri: nil) }
  221. it 'removes line feeds' do
  222. is_expected.not_to include "\n"
  223. end
  224. end
  225. context 'given a post containing linkable mentions' do
  226. let(:status) { Fabricate(:status, mentions: [ Fabricate(:mention, account: local_account) ], text: '@alice') }
  227. it 'creates a mention link' do
  228. is_expected.to include '<a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span>'
  229. end
  230. end
  231. context 'given a post containing unlinkable mentions' do
  232. let(:status) { Fabricate(:status, text: '@alice', uri: nil) }
  233. it 'does not create a mention link' do
  234. is_expected.to include '@alice'
  235. end
  236. end
  237. context do
  238. subject do
  239. status = Fabricate(:status, text: text, uri: nil)
  240. Formatter.instance.format(status)
  241. end
  242. include_examples 'encode and link URLs'
  243. end
  244. context 'given a post with custom_emojify option' do
  245. let!(:emoji) { Fabricate(:custom_emoji) }
  246. let(:status) { Fabricate(:status, account: local_account, text: text) }
  247. subject { Formatter.instance.format(status, custom_emojify: true) }
  248. context 'given a post with an emoji shortcode at the start' do
  249. let(:text) { ':coolcat: Beep boop' }
  250. it 'converts the shortcode to an image tag' do
  251. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  252. end
  253. end
  254. context 'given a post with an emoji shortcode in the middle' do
  255. let(:text) { 'Beep :coolcat: boop' }
  256. it 'converts the shortcode to an image tag' do
  257. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  258. end
  259. end
  260. context 'given a post with concatenated emoji shortcodes' do
  261. let(:text) { ':coolcat::coolcat:' }
  262. it 'does not touch the shortcodes' do
  263. is_expected.to match(/:coolcat::coolcat:/)
  264. end
  265. end
  266. context 'given a post with an emoji shortcode at the end' do
  267. let(:text) { 'Beep boop :coolcat:' }
  268. it 'converts the shortcode to an image tag' do
  269. is_expected.to match(/boop <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  270. end
  271. end
  272. end
  273. end
  274. context 'given a post with remote status' do
  275. let(:status) { Fabricate(:status, account: remote_account, text: 'Beep boop') }
  276. it 'reformats the post' do
  277. is_expected.to eq 'Beep boop'
  278. end
  279. context 'given a post with custom_emojify option' do
  280. let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
  281. let(:status) { Fabricate(:status, account: remote_account, text: text) }
  282. subject { Formatter.instance.format(status, custom_emojify: true) }
  283. context 'given a post with an emoji shortcode at the start' do
  284. let(:text) { '<p>:coolcat: Beep boop<br />' }
  285. it 'converts the shortcode to an image tag' do
  286. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  287. end
  288. end
  289. context 'given a post with an emoji shortcode in the middle' do
  290. let(:text) { '<p>Beep :coolcat: boop</p>' }
  291. it 'converts the shortcode to an image tag' do
  292. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  293. end
  294. end
  295. context 'given a post with concatenated emoji' do
  296. let(:text) { '<p>:coolcat::coolcat:</p>' }
  297. it 'does not touch the shortcodes' do
  298. is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
  299. end
  300. end
  301. context 'given a post with an emoji shortcode at the end' do
  302. let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
  303. it 'converts the shortcode to an image tag' do
  304. is_expected.to match(/<br><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  305. end
  306. end
  307. end
  308. end
  309. end
  310. describe '#reformat' do
  311. subject { Formatter.instance.reformat(text) }
  312. context 'given a post containing plain text' do
  313. let(:text) { 'Beep boop' }
  314. it 'keeps the plain text' do
  315. is_expected.to include 'Beep boop'
  316. end
  317. end
  318. context 'given a post containing script tags' do
  319. let(:text) { '<script>alert("Hello")</script>' }
  320. it 'strips the scripts' do
  321. is_expected.to_not include '<script>alert("Hello")</script>'
  322. end
  323. end
  324. context 'given a post containing malicious classes' do
  325. let(:text) { '<span class="mention status__content__spoiler-link">Show more</span>' }
  326. it 'strips the malicious classes' do
  327. is_expected.to_not include 'status__content__spoiler-link'
  328. end
  329. end
  330. end
  331. describe '#plaintext' do
  332. subject { Formatter.instance.plaintext(status) }
  333. context 'given a post with local status' do
  334. let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) }
  335. it 'returns the raw text' do
  336. is_expected.to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
  337. end
  338. end
  339. context 'given a post with remote status' do
  340. let(:status) { Fabricate(:status, account: remote_account, text: '<script>alert("Hello")</script>') }
  341. it 'returns tag-stripped text' do
  342. is_expected.to eq ''
  343. end
  344. end
  345. end
  346. describe '#simplified_format' do
  347. subject { Formatter.instance.simplified_format(account) }
  348. context 'given a post with local status' do
  349. let(:account) { Fabricate(:account, domain: nil, note: text) }
  350. context 'given a post containing linkable mentions for local accounts' do
  351. let(:text) { '@alice' }
  352. before { local_account }
  353. it 'creates a mention link' do
  354. is_expected.to eq '<p><span class="h-card"><a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span></p>'
  355. end
  356. end
  357. context 'given a post containing linkable mentions for remote accounts' do
  358. let(:text) { '@bob@remote.test' }
  359. before { remote_account }
  360. it 'creates a mention link' do
  361. is_expected.to eq '<p><span class="h-card"><a href="https://remote.test/" class="u-url mention">@<span>bob</span></a></span></p>'
  362. end
  363. end
  364. context 'given a post containing unlinkable mentions' do
  365. let(:text) { '@alice' }
  366. it 'does not create a mention link' do
  367. is_expected.to eq '<p>@alice</p>'
  368. end
  369. end
  370. context 'given a post with custom_emojify option' do
  371. let!(:emoji) { Fabricate(:custom_emoji) }
  372. before { account.note = text }
  373. subject { Formatter.instance.simplified_format(account, custom_emojify: true) }
  374. context 'given a post with an emoji shortcode at the start' do
  375. let(:text) { ':coolcat: Beep boop' }
  376. it 'converts the shortcode to an image tag' do
  377. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  378. end
  379. end
  380. context 'given a post with an emoji shortcode in the middle' do
  381. let(:text) { 'Beep :coolcat: boop' }
  382. it 'converts the shortcode to an image tag' do
  383. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  384. end
  385. end
  386. context 'given a post with concatenated emoji shortcodes' do
  387. let(:text) { ':coolcat::coolcat:' }
  388. it 'does not touch the shortcodes' do
  389. is_expected.to match(/:coolcat::coolcat:/)
  390. end
  391. end
  392. context 'given a post with an emoji shortcode at the end' do
  393. let(:text) { 'Beep boop :coolcat:' }
  394. it 'converts the shortcode to an image tag' do
  395. is_expected.to match(/boop <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  396. end
  397. end
  398. end
  399. include_examples 'encode and link URLs'
  400. end
  401. context 'given a post with remote status' do
  402. let(:text) { '<script>alert("Hello")</script>' }
  403. let(:account) { Fabricate(:account, domain: 'remote', note: text) }
  404. it 'reformats' do
  405. is_expected.to_not include '<script>alert("Hello")</script>'
  406. end
  407. context 'with custom_emojify option' do
  408. let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
  409. before { remote_account.note = text }
  410. subject { Formatter.instance.simplified_format(remote_account, custom_emojify: true) }
  411. context 'given a post with an emoji shortcode at the start' do
  412. let(:text) { '<p>:coolcat: Beep boop<br />' }
  413. it 'converts shortcode to image tag' do
  414. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  415. end
  416. end
  417. context 'given a post with an emoji shortcode in the middle' do
  418. let(:text) { '<p>Beep :coolcat: boop</p>' }
  419. it 'converts shortcode to image tag' do
  420. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  421. end
  422. end
  423. context 'given a post with concatenated emoji shortcodes' do
  424. let(:text) { '<p>:coolcat::coolcat:</p>' }
  425. it 'does not touch the shortcodes' do
  426. is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
  427. end
  428. end
  429. context 'given a post with an emoji shortcode at the end' do
  430. let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
  431. it 'converts shortcode to image tag' do
  432. is_expected.to match(/<br><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  433. end
  434. end
  435. end
  436. end
  437. end
  438. describe '#sanitize' do
  439. let(:html) { '<script>alert("Hello")</script>' }
  440. subject { Formatter.instance.sanitize(html, Sanitize::Config::MASTODON_STRICT) }
  441. it 'sanitizes' do
  442. is_expected.to eq ''
  443. end
  444. end
  445. end