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.

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