template_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. require 'rails_helper'
  2. RSpec.describe NotificationFactory::Template do
  3. subject(:template) do
  4. described_class.new(template_string, escape, trusted)
  5. end
  6. let(:trusted) { false }
  7. describe '#to_s' do
  8. context 'for empty input template (incl. whitespace-only)' do
  9. let(:template_string) { "\#{ }" }
  10. context 'with escape = true' do
  11. let(:escape) { true }
  12. it 'returns an ERB template with the #d helper, and passes escape arg as string' do
  13. expect(template.to_s).to eq('<%= d "", true %>')
  14. end
  15. end
  16. context 'with escape = false' do
  17. let(:escape) { false }
  18. it 'returns an ERB template with the #d helper, and passes escape arg as string' do
  19. expect(template.to_s).to eq('<%= d "", false %>')
  20. end
  21. end
  22. end
  23. context 'for sanitizing the template string' do
  24. let(:escape) { false }
  25. context 'for strings containing ERB' do
  26. let(:template_string) { '<%% <% "<%" %> <%# comment %> <%= "<%" %> <%- "" %> %%>' }
  27. context 'for untrusted templates' do
  28. it 'mutes all pre-existing ERB tags' do
  29. expect(template.to_s).to eq('<%% <%% "<%%" %> <%%# comment %> <%%= "<%%" %> <%%- "" %> %%>')
  30. end
  31. end
  32. context 'for trusted templates' do
  33. let(:trusted) { true }
  34. it 'keeps all pre-existing ERB tags' do
  35. expect(template.to_s).to eq(template_string)
  36. end
  37. end
  38. end
  39. end
  40. context 'for input template using #t helper' do
  41. let(:template_string) { "\#{t('some text')}" }
  42. let(:escape) { false }
  43. it 'returns an ERB template with the #t helper, and passes escape arg as string' do
  44. expect(template.to_s).to eq('<%= t "some text", false %>')
  45. end
  46. context 'with double-quotes in argument' do
  47. let(:template_string) { "\#{t('some \"text\"')}" }
  48. it 'adds backslash-escaping' do
  49. expect(template.to_s).to eq('<%= t "some \"text\"", false %>')
  50. end
  51. end
  52. end
  53. # Regression test for https://github.com/zammad/zammad/issues/385
  54. context 'with HTML auto-injected by browser' do
  55. let(:escape) { true }
  56. context 'for <a> tags wrapped around "ticket.id"' do
  57. let(:template_string) { <<~'TEMPLATE'.chomp }
  58. #{<a href="http://ticket.id" title="http://ticket.id" target="_blank">ticket.id</a>}
  59. TEMPLATE
  60. it 'strips tag from resulting ERB template' do
  61. expect(template.to_s).to eq('<%= d "ticket.id", true %>')
  62. end
  63. end
  64. context 'for <a> tags wrapped around "config.fqdn"' do
  65. let(:template_string) { <<~'TEMPLATE'.chomp }
  66. #{<a href="http://config.fqdn" title="http://config.fqdn" target="_blank">config.fqdn</a>}
  67. TEMPLATE
  68. it 'strips tag from resulting ERB template' do
  69. expect(template.to_s).to eq('<%= c "fqdn", true %>')
  70. end
  71. end
  72. context 'for <a> tags surrounded by whitespace' do
  73. let(:template_string) { <<~'TEMPLATE'.chomp }
  74. #{ <a href="http://ticket.id" title="http://ticket.id" target="_blank">ticket.id </a> }
  75. TEMPLATE
  76. it 'strips tag and spaces from template' do
  77. expect(template.to_s).to eq('<%= d "ticket.id", true %>')
  78. end
  79. end
  80. context 'for unpaired <a> tag and trailing whitespace' do
  81. let(:template_string) { <<~'TEMPLATE'.chomp }
  82. #{<a href="http://ticket.id" title="http://ticket.id" target="_blank">ticket.id }
  83. TEMPLATE
  84. it 'strips tag and spaces from template' do
  85. expect(template.to_s).to eq('<%= d "ticket.id", true %>')
  86. end
  87. end
  88. end
  89. end
  90. end