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.

http_headers.lua 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. --[[
  2. Copyright (c) 2015, Vsevolod Stakhov <vsevolod@highsecure.ru>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. ]]--
  22. local logger = require "rspamd_logger"
  23. local ucl = require "ucl"
  24. -- Disable DKIM checks if passed via HTTP headers
  25. rspamd_config:add_condition("R_DKIM_ALLOW", function(task)
  26. local hdr = task:get_request_header('DKIM')
  27. if hdr then
  28. local parser = ucl.parser()
  29. local res, err = parser:parse_string(tostring(hdr))
  30. if not res then
  31. logger.infox(task, "cannot parse DKIM header: %1", err)
  32. return true
  33. end
  34. local obj = parser:get_object()
  35. if obj['result'] then
  36. if obj['result'] == 'pass' or obj['result'] == 'allow' then
  37. task:insert_result('R_DKIM_ALLOW', 1.0, 'http header')
  38. elseif obj['result'] == 'fail' or obj['result'] == 'reject' then
  39. task:insert_result('R_DKIM_REJECT', 1.0, 'http header')
  40. elseif obj['result'] == 'tempfail' or obj['result'] == 'softfail' then
  41. task:insert_result('R_DKIM_TEMPFAIL', 1.0, 'http header')
  42. end
  43. return false
  44. end
  45. end
  46. return true
  47. end)
  48. -- Disable DKIM checks if passed via HTTP headers
  49. rspamd_config:add_condition("R_SPF_ALLOW", function(task)
  50. local hdr = task:get_request_header('SPF')
  51. if hdr then
  52. local parser = ucl.parser()
  53. local res, err = parser:parse_string(tostring(hdr))
  54. if not res then
  55. logger.infox(task, "cannot parse SPF header: %1", err)
  56. return true
  57. end
  58. local obj = parser:get_object()
  59. if obj['result'] then
  60. if obj['result'] == 'pass' or obj['result'] == 'allow' then
  61. task:insert_result('R_SPF_ALLOW', 1.0, 'http header')
  62. elseif obj['result'] == 'fail' or obj['result'] == 'reject' then
  63. task:insert_result('R_SPF_FAIL', 1.0, 'http header')
  64. elseif obj['result'] == 'neutral' then
  65. task:insert_result('R_SPF_NEUTRAL', 1.0, 'http header')
  66. elseif obj['result'] == 'tempfail' or obj['result'] == 'softfail' then
  67. task:insert_result('R_SPF_TEMPFAIL', 1.0, 'http header')
  68. end
  69. return false
  70. end
  71. end
  72. return true
  73. end)
  74. rspamd_config:add_condition("DMARC_POLICY_ALLOW", function(task)
  75. local hdr = task:get_request_header('DMARC')
  76. if hdr then
  77. local parser = ucl.parser()
  78. local res, err = parser:parse_string(tostring(hdr))
  79. if not res then
  80. logger.infox(task, "cannot parse DMARC header: %1", err)
  81. return true
  82. end
  83. local obj = parser:get_object()
  84. if obj['result'] then
  85. if obj['result'] == 'pass' or obj['result'] == 'allow' then
  86. task:insert_result('DMARC_POLICY_ALLOW', 1.0, 'http header')
  87. elseif obj['result'] == 'fail' or obj['result'] == 'reject' then
  88. task:insert_result('DMARC_POLICY_REJECT', 1.0, 'http header')
  89. elseif obj['result'] == 'quarantine' then
  90. task:insert_result('DMARC_POLICY_QUARANTINE', 1.0, 'http header')
  91. elseif obj['result'] == 'tempfail' or obj['result'] == 'softfail' then
  92. task:insert_result('DMARC_POLICY_SOFTFAIL', 1.0, 'http header')
  93. end
  94. return false
  95. end
  96. end
  97. return true
  98. end)