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.

325 lines
9.6 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', username: 'bob', url: 'https://remote/') }
  5. shared_examples 'encode and link URLs' do
  6. context 'matches 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 'has valid 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 'matches a stand-alone google URL' do
  13. let(:text) { 'http://google.com' }
  14. it 'has valid URL' do
  15. is_expected.to include 'href="http://google.com/"'
  16. end
  17. end
  18. context 'matches a stand-alone IDN URL' do
  19. let(:text) { 'https://nic.みんな/' }
  20. it 'has valid URL' do
  21. is_expected.to include 'href="https://nic.xn--q9jyb4c/"'
  22. end
  23. it 'has display URL' do
  24. is_expected.to include '<span class="">nic.みんな/</span>'
  25. end
  26. end
  27. context 'matches a URL without trailing period' do
  28. let(:text) { 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ' }
  29. it 'has valid URL' 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 'matches a URL without closing paranthesis' do
  34. let(:text) { '(http://google.com/)' }
  35. it 'has valid URL' do
  36. is_expected.to include 'href="http://google.com/"'
  37. end
  38. end
  39. context 'matches a URL without exclamation point' do
  40. let(:text) { 'http://www.google.com!' }
  41. it 'has valid URL' do
  42. is_expected.to include 'href="http://www.google.com/"'
  43. end
  44. end
  45. context 'matches a URL without single quote' do
  46. let(:text) { "http://www.google.com'" }
  47. it 'has valid URL' do
  48. is_expected.to include 'href="http://www.google.com/"'
  49. end
  50. end
  51. context 'matches a URL without angle brackets' do
  52. let(:text) { 'http://www.google.com>' }
  53. it 'has valid URL' do
  54. is_expected.to include 'href="http://www.google.com/"'
  55. end
  56. end
  57. context 'matches a URL with a query string' do
  58. let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
  59. it 'has valid URL' do
  60. is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&amp;q=autolink"'
  61. end
  62. end
  63. context 'matches a URL with parenthesis in it' do
  64. let(:text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
  65. it 'has valid URL' do
  66. is_expected.to include 'href="https://en.wikipedia.org/wiki/Diaspora_(software)"'
  67. end
  68. end
  69. context 'matches a URL with Japanese path string' do
  70. let(:text) { 'https://ja.wikipedia.org/wiki/日本' }
  71. it 'has valid URL' do
  72. is_expected.to include 'href="https://ja.wikipedia.org/wiki/%E6%97%A5%E6%9C%AC"'
  73. end
  74. end
  75. context 'matches a URL with Korean path string' do
  76. let(:text) { 'https://ko.wikipedia.org/wiki/대한민국' }
  77. it 'has valid URL' do
  78. is_expected.to include 'href="https://ko.wikipedia.org/wiki/%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD"'
  79. end
  80. end
  81. context 'matches a URL with Simplified Chinese path string' do
  82. let(:text) { 'https://baike.baidu.com/item/中华人民共和国' }
  83. it 'has valid URL' do
  84. is_expected.to include 'href="https://baike.baidu.com/item/%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD"'
  85. end
  86. end
  87. context 'matches a URL with Traditional Chinese path string' do
  88. let(:text) { 'https://zh.wikipedia.org/wiki/臺灣' }
  89. it 'has valid URL' do
  90. is_expected.to include 'href="https://zh.wikipedia.org/wiki/%E8%87%BA%E7%81%A3"'
  91. end
  92. end
  93. context 'contains HTML (script tag)' do
  94. let(:text) { '<script>alert("Hello")</script>' }
  95. it 'has escaped HTML' do
  96. is_expected.to include '<p>&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;</p>'
  97. end
  98. end
  99. context 'contains HTML (XSS attack)' do
  100. let(:text) { %q{<img src="javascript:alert('XSS');">} }
  101. it 'has escaped HTML' do
  102. is_expected.to include '<p>&lt;img src=&quot;javascript:alert(&apos;XSS&apos;);&quot;&gt;</p>'
  103. end
  104. end
  105. context 'contains invalid URL' do
  106. let(:text) { 'http://www\.google\.com' }
  107. it 'has raw URL' do
  108. is_expected.to eq '<p>http://www\.google\.com</p>'
  109. end
  110. end
  111. context 'contains a hashtag' do
  112. let(:text) { '#hashtag' }
  113. it 'has a link' do
  114. is_expected.to include '/tags/hashtag" class="mention hashtag" rel="tag">#<span>hashtag</span></a>'
  115. end
  116. end
  117. end
  118. describe '#format' do
  119. subject { Formatter.instance.format(status) }
  120. context 'with local status' do
  121. context 'with reblog' do
  122. let(:reblog) { Fabricate(:status, account: local_account, text: 'Hello world', uri: nil) }
  123. let(:status) { Fabricate(:status, reblog: reblog) }
  124. it 'returns original status with credit to its author' do
  125. 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'
  126. end
  127. end
  128. context 'contains plain text' do
  129. let(:status) { Fabricate(:status, text: 'text', uri: nil) }
  130. it 'paragraphizes' do
  131. is_expected.to eq '<p>text</p>'
  132. end
  133. end
  134. context 'contains line feeds' do
  135. let(:status) { Fabricate(:status, text: "line\nfeed", uri: nil) }
  136. it 'removes line feeds' do
  137. is_expected.not_to include "\n"
  138. end
  139. end
  140. context 'contains linkable mentions' do
  141. let(:status) { Fabricate(:status, mentions: [ Fabricate(:mention, account: local_account) ], text: '@alice') }
  142. it 'links' do
  143. is_expected.to include '<a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span>'
  144. end
  145. end
  146. context 'contains unlinkable mentions' do
  147. let(:status) { Fabricate(:status, text: '@alice', uri: nil) }
  148. it 'does not link' do
  149. is_expected.to include '@alice'
  150. end
  151. end
  152. context do
  153. subject do
  154. status = Fabricate(:status, text: text, uri: nil)
  155. Formatter.instance.format(status)
  156. end
  157. include_examples 'encode and link URLs'
  158. end
  159. end
  160. context 'with remote status' do
  161. let(:status) { Fabricate(:status, account: remote_account, text: 'Beep boop') }
  162. it 'reformats' do
  163. is_expected.to eq 'Beep boop'
  164. end
  165. end
  166. end
  167. describe '#reformat' do
  168. subject { Formatter.instance.reformat(text) }
  169. context 'contains plain text' do
  170. let(:text) { 'Beep boop' }
  171. it 'contains plain text' do
  172. is_expected.to include 'Beep boop'
  173. end
  174. end
  175. context 'contains scripts' do
  176. let(:text) { '<script>alert("Hello")</script>' }
  177. it 'strips scripts' do
  178. is_expected.to_not include '<script>alert("Hello")</script>'
  179. end
  180. end
  181. context 'contains malicious classes' do
  182. let(:text) { '<span class="status__content__spoiler-link">Show more</span>' }
  183. it 'strips malicious classes' do
  184. is_expected.to_not include 'status__content__spoiler-link'
  185. end
  186. end
  187. end
  188. describe '#plaintext' do
  189. subject { Formatter.instance.plaintext(status) }
  190. context 'with local status' do
  191. let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) }
  192. it 'returns raw text' do
  193. is_expected.to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
  194. end
  195. end
  196. context 'with remote status' do
  197. let(:status) { Fabricate(:status, account: remote_account, text: '<script>alert("Hello")</script>') }
  198. it 'returns tag-stripped text' do
  199. is_expected.to eq ''
  200. end
  201. end
  202. end
  203. describe '#simplified_format' do
  204. subject { Formatter.instance.simplified_format(account) }
  205. context 'with local status' do
  206. let(:account) { Fabricate(:account, domain: nil, note: text) }
  207. context 'contains linkable mentions for local accounts' do
  208. let(:text) { '@alice' }
  209. before { local_account }
  210. it 'links' do
  211. 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>'
  212. end
  213. end
  214. context 'contains linkable mentions for remote accounts' do
  215. let(:text) { '@bob@remote' }
  216. before { remote_account }
  217. it 'links' do
  218. is_expected.to eq '<p><span class="h-card"><a href="https://remote/" class="u-url mention">@<span>bob</span></a></span></p>'
  219. end
  220. end
  221. context 'contains unlinkable mentions' do
  222. let(:text) { '@alice' }
  223. it 'returns raw mention texts' do
  224. is_expected.to eq '<p>@alice</p>'
  225. end
  226. end
  227. include_examples 'encode and link URLs'
  228. end
  229. context 'with remote status' do
  230. let(:text) { '<script>alert("Hello")</script>' }
  231. let(:account) { Fabricate(:account, domain: 'remote', note: text) }
  232. it 'reformats' do
  233. is_expected.to_not include '<script>alert("Hello")</script>'
  234. end
  235. end
  236. end
  237. describe '#sanitize' do
  238. let(:html) { '<script>alert("Hello")</script>' }
  239. subject { Formatter.instance.sanitize(html, Sanitize::Config::MASTODON_STRICT) }
  240. it 'sanitizes' do
  241. is_expected.to eq 'alert("Hello")'
  242. end
  243. end
  244. end