闭社主体 forked from https://github.com/tootsuite/mastodon
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.

618 lines
20 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 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. context 'given a stand-alone xmpp: URI' do
  186. let(:text) { 'xmpp:user@instance.com' }
  187. it 'matches the full URI' do
  188. is_expected.to include 'href="xmpp:user@instance.com"'
  189. end
  190. end
  191. context 'given a an xmpp: URI with a query-string' do
  192. let(:text) { 'please join xmpp:muc@instance.com?join right now' }
  193. it 'matches the full URI' do
  194. is_expected.to include 'href="xmpp:muc@instance.com?join"'
  195. end
  196. end
  197. context 'given text containing a magnet: URI' do
  198. let(:text) { 'wikipedia gives this example of a magnet uri: magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a' }
  199. it 'matches the full URI' do
  200. is_expected.to include 'href="magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a"'
  201. end
  202. end
  203. end
  204. describe '#format_spoiler' do
  205. subject { Formatter.instance.format_spoiler(status) }
  206. context 'given a post containing plain text' do
  207. let(:status) { Fabricate(:status, text: 'text', spoiler_text: 'Secret!', uri: nil) }
  208. it 'Returns the spoiler text' do
  209. is_expected.to eq 'Secret!'
  210. end
  211. end
  212. context 'given a post with an emoji shortcode at the start' do
  213. let!(:emoji) { Fabricate(:custom_emoji) }
  214. let(:status) { Fabricate(:status, text: 'text', spoiler_text: ':coolcat: Secret!', uri: nil) }
  215. let(:text) { ':coolcat: Beep boop' }
  216. it 'converts the shortcode to an image tag' do
  217. is_expected.to match(/<img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  218. end
  219. end
  220. end
  221. describe '#format' do
  222. subject { Formatter.instance.format(status) }
  223. context 'given a post with local status' do
  224. context 'given a reblogged post' do
  225. let(:reblog) { Fabricate(:status, account: local_account, text: 'Hello world', uri: nil) }
  226. let(:status) { Fabricate(:status, reblog: reblog) }
  227. it 'returns original status with credit to its author' do
  228. 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'
  229. end
  230. end
  231. context 'given a post containing plain text' do
  232. let(:status) { Fabricate(:status, text: 'text', uri: nil) }
  233. it 'paragraphizes the text' do
  234. is_expected.to eq '<p>text</p>'
  235. end
  236. end
  237. context 'given a post containing line feeds' do
  238. let(:status) { Fabricate(:status, text: "line\nfeed", uri: nil) }
  239. it 'removes line feeds' do
  240. is_expected.not_to include "\n"
  241. end
  242. end
  243. context 'given a post containing linkable mentions' do
  244. let(:status) { Fabricate(:status, mentions: [ Fabricate(:mention, account: local_account) ], text: '@alice') }
  245. it 'creates a mention link' do
  246. is_expected.to include '<a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span>'
  247. end
  248. end
  249. context 'given a post containing unlinkable mentions' do
  250. let(:status) { Fabricate(:status, text: '@alice', uri: nil) }
  251. it 'does not create a mention link' do
  252. is_expected.to include '@alice'
  253. end
  254. end
  255. context do
  256. subject do
  257. status = Fabricate(:status, text: text, uri: nil)
  258. Formatter.instance.format(status)
  259. end
  260. include_examples 'encode and link URLs'
  261. end
  262. context 'given a post with custom_emojify option' do
  263. let!(:emoji) { Fabricate(:custom_emoji) }
  264. let(:status) { Fabricate(:status, account: local_account, text: text) }
  265. subject { Formatter.instance.format(status, custom_emojify: true) }
  266. context 'given a post with an emoji shortcode at the start' do
  267. let(:text) { ':coolcat: Beep boop' }
  268. it 'converts the shortcode to an image tag' do
  269. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  270. end
  271. end
  272. context 'given a post with an emoji shortcode in the middle' do
  273. let(:text) { 'Beep :coolcat: boop' }
  274. it 'converts the shortcode to an image tag' do
  275. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  276. end
  277. end
  278. context 'given a post with concatenated emoji shortcodes' do
  279. let(:text) { ':coolcat::coolcat:' }
  280. it 'does not touch the shortcodes' do
  281. is_expected.to match(/:coolcat::coolcat:/)
  282. end
  283. end
  284. context 'given a post with an emoji shortcode at the end' do
  285. let(:text) { 'Beep boop :coolcat:' }
  286. it 'converts the shortcode to an image tag' do
  287. is_expected.to match(/boop <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  288. end
  289. end
  290. end
  291. end
  292. context 'given a post with remote status' do
  293. let(:status) { Fabricate(:status, account: remote_account, text: 'Beep boop') }
  294. it 'reformats the post' do
  295. is_expected.to eq 'Beep boop'
  296. end
  297. context 'given a post with custom_emojify option' do
  298. let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
  299. let(:status) { Fabricate(:status, account: remote_account, text: text) }
  300. subject { Formatter.instance.format(status, custom_emojify: true) }
  301. context 'given a post with an emoji shortcode at the start' do
  302. let(:text) { '<p>:coolcat: Beep boop<br />' }
  303. it 'converts the shortcode to an image tag' do
  304. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  305. end
  306. end
  307. context 'given a post with an emoji shortcode in the middle' do
  308. let(:text) { '<p>Beep :coolcat: boop</p>' }
  309. it 'converts the shortcode to an image tag' do
  310. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  311. end
  312. end
  313. context 'given a post with concatenated emoji' do
  314. let(:text) { '<p>:coolcat::coolcat:</p>' }
  315. it 'does not touch the shortcodes' do
  316. is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
  317. end
  318. end
  319. context 'given a post with an emoji shortcode at the end' do
  320. let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
  321. it 'converts the shortcode to an image tag' do
  322. is_expected.to match(/<br><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  323. end
  324. end
  325. end
  326. end
  327. end
  328. describe '#reformat' do
  329. subject { Formatter.instance.reformat(text) }
  330. context 'given a post containing plain text' do
  331. let(:text) { 'Beep boop' }
  332. it 'keeps the plain text' do
  333. is_expected.to include 'Beep boop'
  334. end
  335. end
  336. context 'given a post containing script tags' do
  337. let(:text) { '<script>alert("Hello")</script>' }
  338. it 'strips the scripts' do
  339. is_expected.to_not include '<script>alert("Hello")</script>'
  340. end
  341. end
  342. context 'given a post containing malicious classes' do
  343. let(:text) { '<span class="mention status__content__spoiler-link">Show more</span>' }
  344. it 'strips the malicious classes' do
  345. is_expected.to_not include 'status__content__spoiler-link'
  346. end
  347. end
  348. end
  349. describe '#plaintext' do
  350. subject { Formatter.instance.plaintext(status) }
  351. context 'given a post with local status' do
  352. let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) }
  353. it 'returns the raw text' do
  354. is_expected.to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
  355. end
  356. end
  357. context 'given a post with remote status' do
  358. let(:status) { Fabricate(:status, account: remote_account, text: '<script>alert("Hello")</script>') }
  359. it 'returns tag-stripped text' do
  360. is_expected.to eq ''
  361. end
  362. end
  363. end
  364. describe '#simplified_format' do
  365. subject { Formatter.instance.simplified_format(account) }
  366. context 'given a post with local status' do
  367. let(:account) { Fabricate(:account, domain: nil, note: text) }
  368. context 'given a post containing linkable mentions for local accounts' do
  369. let(:text) { '@alice' }
  370. before { local_account }
  371. it 'creates a mention link' do
  372. 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>'
  373. end
  374. end
  375. context 'given a post containing linkable mentions for remote accounts' do
  376. let(:text) { '@bob@remote.test' }
  377. before { remote_account }
  378. it 'creates a mention link' do
  379. is_expected.to eq '<p><span class="h-card"><a href="https://remote.test/" class="u-url mention">@<span>bob</span></a></span></p>'
  380. end
  381. end
  382. context 'given a post containing unlinkable mentions' do
  383. let(:text) { '@alice' }
  384. it 'does not create a mention link' do
  385. is_expected.to eq '<p>@alice</p>'
  386. end
  387. end
  388. context 'given a post with custom_emojify option' do
  389. let!(:emoji) { Fabricate(:custom_emoji) }
  390. before { account.note = text }
  391. subject { Formatter.instance.simplified_format(account, custom_emojify: true) }
  392. context 'given a post with an emoji shortcode at the start' do
  393. let(:text) { ':coolcat: Beep boop' }
  394. it 'converts the shortcode to an image tag' do
  395. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  396. end
  397. end
  398. context 'given a post with an emoji shortcode in the middle' do
  399. let(:text) { 'Beep :coolcat: boop' }
  400. it 'converts the shortcode to an image tag' do
  401. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  402. end
  403. end
  404. context 'given a post with concatenated emoji shortcodes' do
  405. let(:text) { ':coolcat::coolcat:' }
  406. it 'does not touch the shortcodes' do
  407. is_expected.to match(/:coolcat::coolcat:/)
  408. end
  409. end
  410. context 'given a post with an emoji shortcode at the end' do
  411. let(:text) { 'Beep boop :coolcat:' }
  412. it 'converts the shortcode to an image tag' do
  413. is_expected.to match(/boop <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  414. end
  415. end
  416. end
  417. include_examples 'encode and link URLs'
  418. end
  419. context 'given a post with remote status' do
  420. let(:text) { '<script>alert("Hello")</script>' }
  421. let(:account) { Fabricate(:account, domain: 'remote', note: text) }
  422. it 'reformats' do
  423. is_expected.to_not include '<script>alert("Hello")</script>'
  424. end
  425. context 'with custom_emojify option' do
  426. let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
  427. before { remote_account.note = text }
  428. subject { Formatter.instance.simplified_format(remote_account, custom_emojify: true) }
  429. context 'given a post with an emoji shortcode at the start' do
  430. let(:text) { '<p>:coolcat: Beep boop<br />' }
  431. it 'converts shortcode to image tag' do
  432. is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  433. end
  434. end
  435. context 'given a post with an emoji shortcode in the middle' do
  436. let(:text) { '<p>Beep :coolcat: boop</p>' }
  437. it 'converts shortcode to image tag' do
  438. is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  439. end
  440. end
  441. context 'given a post with concatenated emoji shortcodes' do
  442. let(:text) { '<p>:coolcat::coolcat:</p>' }
  443. it 'does not touch the shortcodes' do
  444. is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
  445. end
  446. end
  447. context 'given a post with an emoji shortcode at the end' do
  448. let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
  449. it 'converts shortcode to image tag' do
  450. is_expected.to match(/<br><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
  451. end
  452. end
  453. end
  454. end
  455. end
  456. describe '#sanitize' do
  457. let(:html) { '<script>alert("Hello")</script>' }
  458. subject { Formatter.instance.sanitize(html, Sanitize::Config::MASTODON_STRICT) }
  459. it 'sanitizes' do
  460. is_expected.to eq ''
  461. end
  462. end
  463. end