ticket_xss_test.rb 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. require 'test_helper'
  2. class TicketXssTest < ActiveSupport::TestCase
  3. test 'xss via model' do
  4. ticket = Ticket.create(
  5. title: 'test 123 <script type="text/javascript">alert("XSS!");</script>',
  6. group: Group.lookup(name: 'Users'),
  7. customer_id: 2,
  8. state: Ticket::State.lookup(name: 'new'),
  9. priority: Ticket::Priority.lookup(name: '2 normal'),
  10. updated_by_id: 1,
  11. created_by_id: 1,
  12. )
  13. assert(ticket, 'ticket created')
  14. assert_equal('test 123 <script type="text/javascript">alert("XSS!");</script>', ticket.title, 'ticket.title verify')
  15. assert_equal('Users', ticket.group.name, 'ticket.group verify')
  16. assert_equal('new', ticket.state.name, 'ticket.state verify')
  17. article1 = Ticket::Article.create(
  18. ticket_id: ticket.id,
  19. from: 'some_sender@example.com',
  20. to: 'some_recipient@example.com',
  21. subject: 'some subject <script type="text/javascript">alert("XSS!");</script>',
  22. message_id: 'some@id',
  23. content_type: 'text/html',
  24. body: '<script type="text/javascript">alert("XSS!");</script>',
  25. internal: false,
  26. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  27. type: Ticket::Article::Type.find_by(name: 'email'),
  28. updated_by_id: 1,
  29. created_by_id: 1,
  30. )
  31. assert_equal('alert("XSS!");', article1.body, 'article1.body verify - inbound')
  32. article2 = Ticket::Article.create(
  33. ticket_id: ticket.id,
  34. from: 'some_sender@example.com',
  35. to: 'some_recipient@example.com',
  36. subject: 'some subject <script type="text/javascript">alert("XSS!");</script>',
  37. message_id: 'some@id',
  38. content_type: 'text/html',
  39. body: 'please tell me this doesn\'t work: <script type="text/javascript">alert("XSS!");</script>',
  40. internal: false,
  41. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  42. type: Ticket::Article::Type.find_by(name: 'email'),
  43. updated_by_id: 1,
  44. created_by_id: 1,
  45. )
  46. assert_equal('please tell me this doesn\'t work: alert("XSS!");', article2.body, 'article2.body verify - inbound')
  47. article3 = Ticket::Article.create(
  48. ticket_id: ticket.id,
  49. from: 'some_sender@example.com',
  50. to: 'some_recipient@example.com',
  51. subject: 'some subject <script type="text/javascript">alert("XSS!");</script>',
  52. message_id: 'some@id',
  53. content_type: 'text/html',
  54. body: 'please tell me this doesn\'t work: <table>ada<tr></tr></table><div class="adasd" id="123" data-abc="123"></div><div><a href="javascript:someFunction()">LINK</a><a href="http://lalal.de">aa</a><some_not_existing>ABC</some_not_existing>',
  55. internal: false,
  56. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  57. type: Ticket::Article::Type.find_by(name: 'email'),
  58. updated_by_id: 1,
  59. created_by_id: 1,
  60. )
  61. assert_equal("please tell me this doesn't work: <table>ada<tr></tr>
  62. </table><div></div><div>
  63. LINK<a href=\"http://lalal.de\" rel=\"nofollow noreferrer noopener\" target=\"_blank\" title=\"http://lalal.de\">aa</a>ABC</div>", article3.body, 'article3.body verify - inbound')
  64. article4 = Ticket::Article.create(
  65. ticket_id: ticket.id,
  66. from: 'some_sender@example.com',
  67. to: 'some_recipient@example.com',
  68. subject: 'some subject <script type="text/javascript">alert("XSS!");</script>',
  69. message_id: 'some@id',
  70. content_type: 'text/html',
  71. body: 'please tell me this doesn\'t work: <video>some video</video><foo>alal</foo>',
  72. internal: false,
  73. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  74. type: Ticket::Article::Type.find_by(name: 'email'),
  75. updated_by_id: 1,
  76. created_by_id: 1,
  77. )
  78. assert_equal("please tell me this doesn't work: <video>some video</video>alal", article4.body, 'article4.body verify - inbound')
  79. article5 = Ticket::Article.create(
  80. ticket_id: ticket.id,
  81. from: 'some_sender@example.com',
  82. to: 'some_recipient@example.com',
  83. subject: 'some subject <script type="text/javascript">alert("XSS!");</script>',
  84. message_id: 'some@id',
  85. content_type: 'text/plain',
  86. body: 'please tell me this doesn\'t work: <table>ada<tr></tr></table><div class="adasd" id="123" data-signature-id="123"></div><div><a href="javascript:someFunction()">LINK</a><a href="http://lalal.de">aa</a><some_not_existing>ABC</some_not_existing>',
  87. internal: false,
  88. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  89. type: Ticket::Article::Type.find_by(name: 'email'),
  90. updated_by_id: 1,
  91. created_by_id: 1,
  92. )
  93. assert_equal('please tell me this doesn\'t work: <table>ada<tr></tr></table><div class="adasd" id="123" data-signature-id="123"></div><div><a href="javascript:someFunction()">LINK</a><a href="http://lalal.de">aa</a><some_not_existing>ABC</some_not_existing>', article5.body, 'article5.body verify - inbound')
  94. article6 = Ticket::Article.create(
  95. ticket_id: ticket.id,
  96. from: 'some_sender@example.com',
  97. to: 'some_recipient@example.com',
  98. subject: 'some subject <script type="text/javascript">alert("XSS!");</script>',
  99. message_id: 'some@id',
  100. content_type: 'text/html',
  101. body: 'some message article helper test1 <div><img style="width: 85.5px; height: 49.5px" src="cid:15.274327094.140938@zammad.example.com">asdasd<img src="cid:15.274327094.140939@zammad.example.com"><br>',
  102. internal: false,
  103. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  104. type: Ticket::Article::Type.find_by(name: 'email'),
  105. updated_by_id: 1,
  106. created_by_id: 1,
  107. )
  108. assert_equal('some message article helper test1 <div>
  109. <img style="width: 85.5px; height: 49.5px;" src="cid:15.274327094.140938@zammad.example.com">asdasd<img src="cid:15.274327094.140939@zammad.example.com"><br>
  110. </div>', article6.body, 'article6.body verify - inbound')
  111. article7 = Ticket::Article.create(
  112. ticket_id: ticket.id,
  113. from: 'some_sender@example.com',
  114. to: 'some_recipient@example.com',
  115. subject: 'some subject <script type="text/javascript">alert("XSS!");</script>',
  116. message_id: 'some@id',
  117. content_type: 'text/html',
  118. body: 'some message article helper test1 <div><img style="width: 85.5px; height: 49.5px" src="api/v1/ticket_attachment/123/123/123">asdasd<img src="api/v1/ticket_attachment/123/123/123"><br>',
  119. internal: false,
  120. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  121. type: Ticket::Article::Type.find_by(name: 'email'),
  122. updated_by_id: 1,
  123. created_by_id: 1,
  124. )
  125. assert_equal('some message article helper test1 <div>
  126. <img style="width: 85.5px; height: 49.5px;" src="api/v1/ticket_attachment/123/123/123">asdasd<img src="api/v1/ticket_attachment/123/123/123"><br>
  127. </div>', article7.body, 'article7.body verify - inbound')
  128. article8 = Ticket::Article.create(
  129. ticket_id: ticket.id,
  130. from: 'some_sender@example.com',
  131. to: 'some_recipient@example.com',
  132. subject: 'some subject <script type="text/javascript">alert("XSS!");</script>',
  133. message_id: 'some@id',
  134. content_type: 'text/html',
  135. body: 'some message article helper test1 <a href="#" onclick="some_function();">abc</a> <a href="https://example.com" oNclIck="some_function();">123</a><body>123</body>',
  136. internal: false,
  137. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  138. type: Ticket::Article::Type.find_by(name: 'email'),
  139. updated_by_id: 1,
  140. created_by_id: 1,
  141. )
  142. assert_equal('some message article helper test1 <a href="#">abc</a> <a href="https://example.com" rel="nofollow noreferrer noopener" target="_blank" title="https://example.com">123</a>123', article8.body, 'article8.body verify - inbound')
  143. end
  144. test 'xss via mail' do
  145. data = 'From: ME Bob <me@example.com>
  146. To: customer@example.com
  147. Subject: some subject
  148. Content-Type: text/html
  149. MIME-Version: 1.0
  150. no HTML <script type="text/javascript">alert(\'XSS\')</script>'
  151. parser = Channel::EmailParser.new
  152. ticket, article, user = parser.process({}, data)
  153. assert_equal('text/html', ticket.articles.first.content_type)
  154. assert_equal('no HTML alert(\'XSS\')', ticket.articles.first.body)
  155. data = 'From: ME Bob <me@example.com>
  156. To: customer@example.com
  157. Subject: some subject
  158. Content-Type: text/plain
  159. MIME-Version: 1.0
  160. no HTML <script type="text/javascript">alert(\'XSS\')</script>'
  161. parser = Channel::EmailParser.new
  162. ticket, article, user = parser.process({}, data)
  163. assert_equal('text/plain', ticket.articles.first.content_type)
  164. assert_equal('no HTML <script type="text/javascript">alert(\'XSS\')</script>', ticket.articles.first.body)
  165. end
  166. end