handler_outgoing.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class SecureMailing::Backend::HandlerOutgoing < SecureMailing::Backend::Handler
  3. attr_accessor :mail, :security
  4. def initialize(mail, security)
  5. super()
  6. @mail = mail
  7. @security = security
  8. end
  9. def process
  10. return if !process?
  11. workaround_mail_bit_encoding_issue(mail) if type.eql?('S/MIME')
  12. overwrite_mail(do_process)
  13. end
  14. def process?
  15. return false if security.blank?
  16. return false if security[:type] != type
  17. security[:sign][:success] || security[:encryption][:success]
  18. end
  19. # S/MIME signing fails because of message encoding #3147
  20. # workaround for https://github.com/mikel/mail/issues/1190
  21. def workaround_mail_bit_encoding_issue(mail)
  22. # change 7bit/8bit encoding to binary so that
  23. # base64 will be used to encode the content
  24. if mail.body.encoding.include?('bit')
  25. mail.body.encoding = :binary
  26. end
  27. # go into recursion for nested parts
  28. mail.parts&.each do |part|
  29. workaround_mail_bit_encoding_issue(part)
  30. end
  31. mail
  32. end
  33. def overwrite_mail(processed)
  34. mail.body = nil
  35. mail.body = processed.body.encoded
  36. mail.content_disposition = processed.content_disposition
  37. mail.content_transfer_encoding = processed.content_transfer_encoding
  38. mail.content_type = processed.content_type
  39. end
  40. def log(action, status, error = nil)
  41. recipients = %i[to cc].map { |recipient| mail[recipient] }.join(' ').strip!
  42. HttpLog.create(
  43. direction: 'out',
  44. facility: type,
  45. url: "#{mail[:from]} -> #{recipients}",
  46. status: status,
  47. ip: nil,
  48. request: security,
  49. response: { error: error },
  50. method: action,
  51. created_by_id: 1,
  52. updated_by_id: 1,
  53. )
  54. end
  55. private
  56. def do_process
  57. if security[:sign][:success] && security[:encryption][:success]
  58. perform_sign_and_encrypt
  59. elsif security[:sign][:success]
  60. perform_sign
  61. elsif security[:encryption][:success]
  62. perform_encrypt
  63. end
  64. end
  65. def perform_sign_and_encrypt
  66. new_mail = encrypt(signed.encoded)
  67. log('sign', 'success')
  68. log('encryption', 'success')
  69. new_mail
  70. end
  71. def perform_sign
  72. new_mail = signed
  73. log('sign', 'success')
  74. new_mail
  75. end
  76. def perform_encrypt
  77. new_mail = encrypt(mail.encoded)
  78. log('encryption', 'success')
  79. new_mail
  80. end
  81. end