| 1 |
-- Tests for lua_auth_results module (AAR parsing) |
| 2 |
context("Lua auth results - AAR parsing", function() |
| 3 |
local lua_auth_results = require "lua_auth_results" |
| 4 |
|
| 5 |
-- parse_ar_element tests |
| 6 |
context("parse_ar_element", function() |
| 7 |
test("simple dkim=pass", function() |
| 8 |
local r = lua_auth_results.parse_ar_element("dkim=pass header.d=example.com header.s=sel") |
| 9 |
assert_not_nil(r) |
| 10 |
assert_equal("pass", r.dkim) |
| 11 |
assert_equal("example.com", r["header.d"]) |
| 12 |
assert_equal("sel", r["header.s"]) |
| 13 |
end) |
| 14 |
|
| 15 |
test("spf with comment", function() |
| 16 |
local r = lua_auth_results.parse_ar_element( |
| 17 |
'spf=pass (mx.example.com: domain of [email protected] designates 1.2.3.4) [email protected]') |
| 18 |
assert_not_nil(r) |
| 19 |
assert_equal("pass", r.spf) |
| 20 |
assert_equal("[email protected]", r["smtp.mailfrom"]) |
| 21 |
end) |
| 22 |
|
| 23 |
test("dmarc with comment and quoted", function() |
| 24 |
local r = lua_auth_results.parse_ar_element('dmarc=pass (policy=none) header.from=example.com') |
| 25 |
assert_not_nil(r) |
| 26 |
assert_equal("pass", r.dmarc) |
| 27 |
assert_equal("example.com", r["header.from"]) |
| 28 |
end) |
| 29 |
|
| 30 |
test("arc with nested parens containing i=N", function() |
| 31 |
local r = lua_auth_results.parse_ar_element( |
| 32 |
'arc=pass ("mail.example.com:s=selector1:i=2")') |
| 33 |
assert_not_nil(r) |
| 34 |
assert_equal("pass", r.arc) |
| 35 |
end) |
| 36 |
|
| 37 |
test("authserv-id without equals returns nil", function() |
| 38 |
local r = lua_auth_results.parse_ar_element("mail.example.com") |
| 39 |
assert_nil(r) |
| 40 |
end) |
| 41 |
end) |
| 42 |
|
| 43 |
-- parse_aar_header tests |
| 44 |
context("parse_aar_header", function() |
| 45 |
test("basic AAR with dkim and spf", function() |
| 46 |
local r = lua_auth_results.parse_aar_header( |
| 47 |
"i=1; mx.example.com; dkim=pass header.d=example.com; spf=pass [email protected]") |
| 48 |
assert_not_nil(r) |
| 49 |
assert_equal("1", r.i) |
| 50 |
assert_not_nil(r.ar) |
| 51 |
assert_equal(2, #r.ar) |
| 52 |
assert_equal("pass", r.ar[1].dkim) |
| 53 |
assert_equal("example.com", r.ar[1]["header.d"]) |
| 54 |
assert_equal("pass", r.ar[2].spf) |
| 55 |
end) |
| 56 |
|
| 57 |
test("AAR with comment containing i=N (#5963)", function() |
| 58 |
local r = lua_auth_results.parse_aar_header( |
| 59 |
'i=3; mx.example.com; arc=pass ("mail.example.com:s=selector1:i=2"); dkim=pass header.d=example.com') |
| 60 |
assert_not_nil(r) |
| 61 |
assert_equal("3", r.i) |
| 62 |
assert_not_nil(r.ar) |
| 63 |
assert_equal(2, #r.ar) |
| 64 |
assert_equal("pass", r.ar[1].arc) |
| 65 |
assert_equal("pass", r.ar[2].dkim) |
| 66 |
end) |
| 67 |
|
| 68 |
test("AAR with semicolon inside comment does not split incorrectly", function() |
| 69 |
local r = lua_auth_results.parse_aar_header( |
| 70 |
'i=2; mx.example.com; spf=pass (something; tricky) [email protected]') |
| 71 |
assert_not_nil(r) |
| 72 |
assert_equal("2", r.i) |
| 73 |
assert_not_nil(r.ar) |
| 74 |
assert_equal(1, #r.ar) |
| 75 |
assert_equal("pass", r.ar[1].spf) |
| 76 |
assert_equal("[email protected]", r.ar[1]["smtp.mailfrom"]) |
| 77 |
end) |
| 78 |
|
| 79 |
test("AAR with semicolon inside quoted string does not split", function() |
| 80 |
-- The split respects quotes so "foo;bar" stays in one element. |
| 81 |
-- Note: parse_ar_element cannot consume key="quoted" pairs, so |
| 82 |
-- properties after reason="..." are lost. The important thing is |
| 83 |
-- the semicolon inside quotes does not create a spurious element. |
| 84 |
local r = lua_auth_results.parse_aar_header( |
| 85 |
'i=1; mx.example.com; dkim=pass reason="foo;bar" header.d=example.com') |
| 86 |
assert_not_nil(r) |
| 87 |
assert_equal("1", r.i) |
| 88 |
assert_not_nil(r.ar) |
| 89 |
assert_equal(1, #r.ar) |
| 90 |
assert_equal("pass", r.ar[1].dkim) |
| 91 |
end) |
| 92 |
|
| 93 |
test("AAR with nested parentheses", function() |
| 94 |
local r = lua_auth_results.parse_aar_header( |
| 95 |
'i=1; mx.example.com; dmarc=pass (policy=none (nested)) header.from=example.com') |
| 96 |
assert_not_nil(r) |
| 97 |
assert_equal("1", r.i) |
| 98 |
assert_not_nil(r.ar) |
| 99 |
assert_equal(1, #r.ar) |
| 100 |
assert_equal("pass", r.ar[1].dmarc) |
| 101 |
assert_equal("example.com", r.ar[1]["header.from"]) |
| 102 |
end) |
| 103 |
|
| 104 |
test("AAR with only i= and authserv-id", function() |
| 105 |
local r = lua_auth_results.parse_aar_header("i=1; mx.example.com; none") |
| 106 |
assert_not_nil(r) |
| 107 |
assert_equal("1", r.i) |
| 108 |
-- ar may be nil or empty since "none" has no '=' |
| 109 |
end) |
| 110 |
|
| 111 |
test("empty string returns nil", function() |
| 112 |
local r = lua_auth_results.parse_aar_header("") |
| 113 |
assert_nil(r) |
| 114 |
end) |
| 115 |
|
| 116 |
test("multiple dmarc and spf results", function() |
| 117 |
local r = lua_auth_results.parse_aar_header( |
| 118 |
"i=2; mx.example.com; dkim=pass header.d=a.com; spf=fail smtp.mailfrom=b.com; dmarc=pass header.from=a.com") |
| 119 |
assert_not_nil(r) |
| 120 |
assert_equal("2", r.i) |
| 121 |
assert_not_nil(r.ar) |
| 122 |
assert_equal(3, #r.ar) |
| 123 |
assert_equal("pass", r.ar[1].dkim) |
| 124 |
assert_equal("fail", r.ar[2].spf) |
| 125 |
assert_equal("pass", r.ar[3].dmarc) |
| 126 |
end) |
| 127 |
|
| 128 |
test("real-world multi-hop AAR from issue #5963", function() |
| 129 |
-- This is the exact scenario: rspamd generates arc=pass ("domain:s=sel:i=2") |
| 130 |
-- which previously broke the i=N extraction |
| 131 |
local r = lua_auth_results.parse_aar_header( |
| 132 |
'i=4; mx.example.com; arc=pass ("mx.example.com:s=arc-20220101:i=3"); ' |
| 133 |
.. 'dkim=pass header.d=example.com header.s=mail header.b=abc123; ' |
| 134 |
.. 'spf=pass (mx.example.com: domain of [email protected] designates 1.2.3.4 as permitted sender) [email protected]; ' |
| 135 |
.. 'dmarc=pass (policy=reject) header.from=example.com') |
| 136 |
assert_not_nil(r) |
| 137 |
assert_equal("4", r.i) |
| 138 |
assert_not_nil(r.ar) |
| 139 |
assert_equal(4, #r.ar) |
| 140 |
assert_equal("pass", r.ar[1].arc) |
| 141 |
assert_equal("pass", r.ar[2].dkim) |
| 142 |
assert_equal("pass", r.ar[3].spf) |
| 143 |
assert_equal("pass", r.ar[4].dmarc) |
| 144 |
assert_equal("example.com", r.ar[4]["header.from"]) |
| 145 |
end) |
| 146 |
end) |
| 147 |
end) |