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.

554 lines
18 KiB

  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 with Japanese path string' do
  90. let(:text) { 'https://ja.wikipedia.org/wiki/日本' }
  91. it 'matches the full URL' do
  92. is_expected.to include 'href="https://ja.wikipedia.org/wiki/日本"'
  93. end
  94. end
  95. context 'given a URL with Korean path string' do
  96. let(:text) { 'https://ko.wikipedia.org/wiki/대한민국' }
  97. it 'matches the full URL' do
  98. is_expected.to include 'href="https://ko.wikipedia.org/wiki/대한민국"'
  99. end
  100. end
  101. context 'given a URL with Simplified Chinese path string' do
  102. let(:text) { 'https://baike.baidu.com/item/中华人民共和国' }
  103. it 'matches the full URL' do
  104. is_expected.to include 'href="https://baike.baidu.com/item/中华人民共和国"'
  105. end
  106. end
  107. context 'given a URL with Traditional Chinese path string' do
  108. let(:text) { 'https://zh.wikipedia.org/wiki/臺灣' }
  109. it 'matches the full URL' do
  110. is_expected.to include 'href="https://zh.wikipedia.org/wiki/臺灣"'
  111. end
  112. end
  113. context 'given a URL containing unsafe code (XSS attack, visible part)' do
  114. let(:text) { %q{http://example.com/b<del>b</del>} }
  115. it 'escapes the HTML in the URL' do
  116. is_expected.to include '&lt;del&gt;b&lt;/del&gt;'
  117. end
  118. end
  119. context 'given a URL containing unsafe code (XSS attack, invisible part)' do
  120. let(:text) { %q{http://example.com/blahblahblahblah/a<script>alert("Hello")</script>} }
  121. it 'escapes the HTML in the URL' do
  122. is_expected.to include '&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;'
  123. end
  124. end
  125. context 'given text containing HTML code (script tag)' do
  126. let(:text) { '<script>alert("Hello")</script>' }
  127. it 'escapes the HTML' do
  128. is_expected.to include '<p>&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;</p>'
  129. end
  130. end
  131. context 'given text containing HTML (XSS attack)' do
  132. let(:text) { %q{<img src="javascript:alert('XSS');">} }
  133. it 'escapes the HTML' do
  134. is_expected.to include '<p>&lt;img src=&quot;javascript:alert(&apos;XSS&apos;);&quot;&gt;</p>'
  135. end
  136. end
  137. context 'given an invalid URL' do
  138. let(:text) { 'http://www\.google\.com' }
  139. it 'outputs the raw URL' do
  140. is_expected.to eq '<p>http://www\.google\.com</p>'
  141. end
  142. end
  143. context 'given text containing a hashtag' do
  144. let(:text) { '#hashtag' }
  145. it 'creates a hashtag link' do
  146. is_expected.to include '/tags/hashtag" class="mention hashtag" rel="tag">#<span>hashtag</span></a>'
  147. end
  148. end
  149. context 'given text containing a hashtag with Unicode chars' do
  150. let(:text) { '#hashtagタグ' }
  151. it 'creates a hashtag link' do
  152. is_expected.to include '/tags/hashtag%E3%82%BF%E3%82%B0" class="mention hashtag" rel="tag">#<span>hashtagタグ</span></a>'
  153. end
  154. end
  155. end
  156. describe '#format_spoiler' do
  157. subject { Formatter.instance.format_spoiler(status) }
  158. context 'given a post containing plain text' do
  159. let(:status) { Fabricate(:status, text: 'text', spoiler_text: 'Secret!', uri: nil) }
  160. it 'Returns the spoiler text' do
  161. is_expected.to eq 'Secret!'
  162. end
  163. end
  164. context 'given a post with an emoji shortcode at the start' do
  165. let!(:emoji) { Fabricate(:custom_emoji) }
  166. let(:status) { Fabricate(:status, text: 'text', spoiler_text: ':coolcat: Secret!', uri: nil) }
  167. let(:text) { ':coolcat: Beep boop' }
  168. it 'converts the shortcode to an image tag' do
  169. is_expected.to match(/<img draggable="false" class="emojione" alt=":coolcat:"/)
  170. end
  171. end
  172. end
  173. describe '#format' do
  174. subject { Formatter.instance.format(status) }
  175. context 'given a post with local status' do
  176. context 'given a reblogged post' do
  177. let(:reblog) { Fabricate(:status, account: local_account, text: 'Hello world', uri: nil) }
  178. let(:status) { Fabricate(:status, reblog: reblog) }
  179. it 'returns original status with credit to its author' do
  180. 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'
  181. end
  182. end
  183. context 'given a post containing plain text' do
  184. let(:status) { Fabricate(:status, text: 'text', uri: nil) }
  185. it 'paragraphizes the text' do
  186. is_expected.to eq '<p>text</p>'
  187. end
  188. end
  189. context 'given a post containing line feeds' do
  190. let(:status) { Fabricate(:status, text: "line\nfeed", uri: nil) }
  191. it 'removes line feeds' do
  192. is_expected.not_to include "\n"
  193. end
  194. end
  195. context 'given a post containing linkable mentions' do
  196. let(:status) { Fabricate(:status, mentions: [ Fabricate(:mention, account: local_account) ], text: '@alice') }
  197. it 'creates a mention link' do
  198. is_expected.to include '<a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span>'
  199. end
  200. end
  201. context 'given a post containing unlinkable mentions' do
  202. let(:status) { Fabricate(:status, text: '@alice', uri: nil) }
  203. it 'does not create a mention link' do
  204. is_expected.to include '@alice'
  205. end
  206. end
  207. context do
  208. subject do
  209. status = Fabricate(:status, text: text, uri: nil)
  210. Formatter.instance.format(status)
  211. end
  212. include_examples 'encode and link URLs'
  213. end
  214. context 'given a post with custom_emojify option' do
  215. let!(:emoji) { Fabricate(:custom_emoji) }
  216. let(:status) { Fabricate(:status, account: local_account, text: text) }
  217. subject { Formatter.instance.format(status, custom_emojify: true) }
  218. context 'given a post with an emoji shortcode at the start' do
  219. let(:text) { ':coolcat: Beep boop' }
  220. it 'converts the shortcode to an image tag' do
  221. is_expected.to match(/<p><img draggable="false" class="emojione" alt=":coolcat:"/)
  222. end
  223. end
  224. context 'given a post with an emoji shortcode in the middle' do
  225. let(:text) { 'Beep :coolcat: boop' }
  226. it 'converts the shortcode to an image tag' do
  227. is_expected.to match(/Beep <img draggable="false" class="emojione" alt=":coolcat:"/)
  228. end
  229. end
  230. context 'given a post with concatenated emoji shortcodes' do
  231. let(:text) { ':coolcat::coolcat:' }
  232. it 'does not touch the shortcodes' do
  233. is_expected.to match(/:coolcat::coolcat:/)
  234. end
  235. end
  236. context 'given a post with an emoji shortcode at the end' do
  237. let(:text) { 'Beep boop :coolcat:' }
  238. it 'converts the shortcode to an image tag' do
  239. is_expected.to match(/boop <img draggable="false" class="emojione" alt=":coolcat:"/)
  240. end
  241. end
  242. end
  243. end
  244. context 'given a post with remote status' do
  245. let(:status) { Fabricate(:status, account: remote_account, text: 'Beep boop') }
  246. it 'reformats the post' do
  247. is_expected.to eq 'Beep boop'
  248. end
  249. context 'given a post with custom_emojify option' do
  250. let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
  251. let(:status) { Fabricate(:status, account: remote_account, text: text) }
  252. subject { Formatter.instance.format(status, custom_emojify: true) }
  253. context 'given a post with an emoji shortcode at the start' do
  254. let(:text) { '<p>:coolcat: Beep boop<br />' }
  255. it 'converts the shortcode to an image tag' do
  256. is_expected.to match(/<p><img draggable="false" class="emojione" alt=":coolcat:"/)
  257. end
  258. end
  259. context 'given a post with an emoji shortcode in the middle' do
  260. let(:text) { '<p>Beep :coolcat: boop</p>' }
  261. it 'converts the shortcode to an image tag' do
  262. is_expected.to match(/Beep <img draggable="false" class="emojione" alt=":coolcat:"/)
  263. end
  264. end
  265. context 'given a post with concatenated emoji' do
  266. let(:text) { '<p>:coolcat::coolcat:</p>' }
  267. it 'does not touch the shortcodes' do
  268. is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
  269. end
  270. end
  271. context 'given a post with an emoji shortcode at the end' do
  272. let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
  273. it 'converts the shortcode to an image tag' do
  274. is_expected.to match(/<br><img draggable="false" class="emojione" alt=":coolcat:"/)
  275. end
  276. end
  277. end
  278. end
  279. end
  280. describe '#reformat' do
  281. subject { Formatter.instance.reformat(text) }
  282. context 'given a post containing plain text' do
  283. let(:text) { 'Beep boop' }
  284. it 'keeps the plain text' do
  285. is_expected.to include 'Beep boop'
  286. end
  287. end
  288. context 'given a post containing script tags' do
  289. let(:text) { '<script>alert("Hello")</script>' }
  290. it 'strips the scripts' do
  291. is_expected.to_not include '<script>alert("Hello")</script>'
  292. end
  293. end
  294. context 'given a post containing malicious classes' do
  295. let(:text) { '<span class="mention status__content__spoiler-link">Show more</span>' }
  296. it 'strips the malicious classes' do
  297. is_expected.to_not include 'status__content__spoiler-link'
  298. end
  299. end
  300. end
  301. describe '#plaintext' do
  302. subject { Formatter.instance.plaintext(status) }
  303. context 'given a post with local status' do
  304. let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) }
  305. it 'returns the raw text' do
  306. is_expected.to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
  307. end
  308. end
  309. context 'given a post with remote status' do
  310. let(:status) { Fabricate(:status, account: remote_account, text: '<script>alert("Hello")</script>') }
  311. it 'returns tag-stripped text' do
  312. is_expected.to eq ''
  313. end
  314. end
  315. end
  316. describe '#simplified_format' do
  317. subject { Formatter.instance.simplified_format(account) }
  318. context 'given a post with local status' do
  319. let(:account) { Fabricate(:account, domain: nil, note: text) }
  320. context 'given a post containing linkable mentions for local accounts' do
  321. let(:text) { '@alice' }
  322. before { local_account }
  323. it 'creates a mention link' do
  324. 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>'
  325. end
  326. end
  327. context 'given a post containing linkable mentions for remote accounts' do
  328. let(:text) { '@bob@remote.test' }
  329. before { remote_account }
  330. it 'creates a mention link' do
  331. is_expected.to eq '<p><span class="h-card"><a href="https://remote.test/" class="u-url mention">@<span>bob</span></a></span></p>'
  332. end
  333. end
  334. context 'given a post containing unlinkable mentions' do
  335. let(:text) { '@alice' }
  336. it 'does not create a mention link' do
  337. is_expected.to eq '<p>@alice</p>'
  338. end
  339. end
  340. context 'given a post with custom_emojify option' do
  341. let!(:emoji) { Fabricate(:custom_emoji) }
  342. before { account.note = text }
  343. subject { Formatter.instance.simplified_format(account, custom_emojify: true) }
  344. context 'given a post with an emoji shortcode at the start' do
  345. let(:text) { ':coolcat: Beep boop' }
  346. it 'converts the shortcode to an image tag' do
  347. is_expected.to match(/<p><img draggable="false" class="emojione" alt=":coolcat:"/)
  348. end
  349. end
  350. context 'given a post with an emoji shortcode in the middle' do
  351. let(:text) { 'Beep :coolcat: boop' }
  352. it 'converts the shortcode to an image tag' do
  353. is_expected.to match(/Beep <img draggable="false" class="emojione" alt=":coolcat:"/)
  354. end
  355. end
  356. context 'given a post with concatenated emoji shortcodes' do
  357. let(:text) { ':coolcat::coolcat:' }
  358. it 'does not touch the shortcodes' do
  359. is_expected.to match(/:coolcat::coolcat:/)
  360. end
  361. end
  362. context 'given a post with an emoji shortcode at the end' do
  363. let(:text) { 'Beep boop :coolcat:' }
  364. it 'converts the shortcode to an image tag' do
  365. is_expected.to match(/boop <img draggable="false" class="emojione" alt=":coolcat:"/)
  366. end
  367. end
  368. end
  369. include_examples 'encode and link URLs'
  370. end
  371. context 'given a post with remote status' do
  372. let(:text) { '<script>alert("Hello")</script>' }
  373. let(:account) { Fabricate(:account, domain: 'remote', note: text) }
  374. it 'reformats' do
  375. is_expected.to_not include '<script>alert("Hello")</script>'
  376. end
  377. context 'with custom_emojify option' do
  378. let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
  379. before { remote_account.note = text }
  380. subject { Formatter.instance.simplified_format(remote_account, custom_emojify: true) }
  381. context 'given a post with an emoji shortcode at the start' do
  382. let(:text) { '<p>:coolcat: Beep boop<br />' }
  383. it 'converts shortcode to image tag' do
  384. is_expected.to match(/<p><img draggable="false" class="emojione" alt=":coolcat:"/)
  385. end
  386. end
  387. context 'given a post with an emoji shortcode in the middle' do
  388. let(:text) { '<p>Beep :coolcat: boop</p>' }
  389. it 'converts shortcode to image tag' do
  390. is_expected.to match(/Beep <img draggable="false" class="emojione" alt=":coolcat:"/)
  391. end
  392. end
  393. context 'given a post with concatenated emoji shortcodes' do
  394. let(:text) { '<p>:coolcat::coolcat:</p>' }
  395. it 'does not touch the shortcodes' do
  396. is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
  397. end
  398. end
  399. context 'given a post with an emoji shortcode at the end' do
  400. let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
  401. it 'converts shortcode to image tag' do
  402. is_expected.to match(/<br><img draggable="false" class="emojione" alt=":coolcat:"/)
  403. end
  404. end
  405. end
  406. end
  407. end
  408. describe '#sanitize' do
  409. let(:html) { '<script>alert("Hello")</script>' }
  410. subject { Formatter.instance.sanitize(html, Sanitize::Config::MASTODON_STRICT) }
  411. it 'sanitizes' do
  412. is_expected.to eq ''
  413. end
  414. end
  415. end