ticket_xss_test.rb 8.4 KB

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