| 1 |
-- URL filter tests |
| 2 |
|
| 3 |
context("URL filter functions", function() |
| 4 |
local lua_url_filter = require("lua_url_filter") |
| 5 |
local url = require("rspamd_url") |
| 6 |
local mpool = require("rspamd_mempool") |
| 7 |
local test_helper = require("rspamd_test_helper") |
| 8 |
local logger = require("rspamd_logger") |
| 9 |
local rspamd_text = require("rspamd_text") |
| 10 |
|
| 11 |
test_helper.init_url_parser() |
| 12 |
|
| 13 |
local pool = mpool.create() |
| 14 |
|
| 15 |
local ACCEPT = 0 |
| 16 |
local SUSPICIOUS = 1 |
| 17 |
local REJECT = 2 |
| 18 |
|
| 19 |
-- Test filter_url_string basic validation |
| 20 |
local filter_cases = { |
| 21 |
-- Normal URLs - should accept |
| 22 |
{ "http://example.com", 0, ACCEPT, "normal URL" }, |
| 23 |
{ "https://www.example.com/path", 0, ACCEPT, "normal HTTPS URL" }, |
| 24 |
{ "ftp://ftp.example.com", 0, ACCEPT, "normal FTP URL" }, |
| 25 |
|
| 26 |
-- Long user fields - should be suspicious or rejected |
| 27 |
{ "http://" .. string.rep("a", 100) .. "@example.com", 0, SUSPICIOUS, "100-char user (suspicious)" }, |
| 28 |
{ "http://" .. string.rep("a", 300) .. "@example.com", 0, SUSPICIOUS, "300-char user (suspicious)" }, |
| 29 |
{ "http://" .. string.rep("a", 600) .. "@example.com", 0, REJECT, "600-char user (reject)" }, |
| 30 |
|
| 31 |
-- Multiple @ signs |
| 32 |
{ "http://user@@example.com", 0, SUSPICIOUS, "double @ sign" }, |
| 33 |
{ "http://user@[email protected]", 0, SUSPICIOUS, "multiple @ signs" }, |
| 34 |
{ "http://" .. string.rep("@", 25) .. "example.com", 0, REJECT, ">20 @ signs (reject)" }, |
| 35 |
|
| 36 |
-- Very long URLs |
| 37 |
{ "http://example.com/" .. string.rep("a", 2100), 0, REJECT, ">2048 char URL (reject)" }, |
| 38 |
|
| 39 |
-- Control characters (should reject) |
| 40 |
{ "http://example.com/\x00test", 0, REJECT, "URL with null byte" }, |
| 41 |
{ "http://example.com/\x1ftest", 0, REJECT, "URL with control char" }, |
| 42 |
} |
| 43 |
|
| 44 |
for i, c in ipairs(filter_cases) do |
| 45 |
test("filter_url_string: " .. c[4], function() |
| 46 |
local url_text = rspamd_text.fromstring(c[1]) |
| 47 |
local result = lua_url_filter.filter_url_string(url_text, c[2]) |
| 48 |
assert_equal(c[3], result, |
| 49 |
logger.slog('expected result %s, but got %s for "%s"', |
| 50 |
c[3], result, c[4])) |
| 51 |
end) |
| 52 |
end |
| 53 |
|
| 54 |
-- Test filter_url with URL objects |
| 55 |
local url_object_cases = { |
| 56 |
{ "http://example.com", ACCEPT, "normal URL object" }, |
| 57 |
{ "http://" .. string.rep("a", 150) .. "@example.com", SUSPICIOUS, "long user in URL object" }, |
| 58 |
} |
| 59 |
|
| 60 |
for i, c in ipairs(url_object_cases) do |
| 61 |
test("filter_url: " .. c[3], function() |
| 62 |
local parsed_url = url.create(pool, c[1]) |
| 63 |
assert_not_nil(parsed_url, "failed to parse: " .. c[1]) |
| 64 |
|
| 65 |
local result = lua_url_filter.filter_url(parsed_url) |
| 66 |
assert_equal(c[2], result, |
| 67 |
logger.slog('expected result %s, but got %s for "%s"', |
| 68 |
c[2], result, c[3])) |
| 69 |
end) |
| 70 |
end |
| 71 |
|
| 72 |
-- Test UTF-8 validation |
| 73 |
local utf8_cases = { |
| 74 |
{ "http://example.com/valid", ACCEPT, "valid ASCII" }, |
| 75 |
{ "http://example.com/Тест", ACCEPT, "valid UTF-8 Cyrillic" }, |
| 76 |
{ "http://example.com/日本語", ACCEPT, "valid UTF-8 Japanese" }, |
| 77 |
{ "http://example.com/\xFF\xFE", REJECT, "invalid UTF-8" }, |
| 78 |
} |
| 79 |
|
| 80 |
for i, c in ipairs(utf8_cases) do |
| 81 |
test("UTF-8 validation: " .. c[3], function() |
| 82 |
local url_text = rspamd_text.fromstring(c[1]) |
| 83 |
local result = lua_url_filter.filter_url_string(url_text, 0) |
| 84 |
assert_equal(c[2], result, |
| 85 |
logger.slog('expected result %s, but got %s for "%s"', |
| 86 |
c[2], result, c[3])) |
| 87 |
end) |
| 88 |
end |
| 89 |
|
| 90 |
-- Test custom filter registration |
| 91 |
test("register custom filter", function() |
| 92 |
lua_url_filter.clear_filters() -- Clear any previously registered filters |
| 93 |
|
| 94 |
local called = false |
| 95 |
local custom_filter = function(url_text, flags) |
| 96 |
called = true |
| 97 |
-- Custom filters receive rspamd_text, use :find instead of :match |
| 98 |
if url_text:find("blocked") then |
| 99 |
return REJECT |
| 100 |
end |
| 101 |
return ACCEPT |
| 102 |
end |
| 103 |
|
| 104 |
lua_url_filter.register_filter(custom_filter) |
| 105 |
|
| 106 |
local url_text = rspamd_text.fromstring("http://blocked.example.com") |
| 107 |
local result = lua_url_filter.filter_url_string(url_text, 0) |
| 108 |
assert_true(called, "custom filter was not called") |
| 109 |
assert_equal(REJECT, result, "custom filter did not reject") |
| 110 |
|
| 111 |
lua_url_filter.clear_filters() -- Clean up after test |
| 112 |
end) |
| 113 |
|
| 114 |
-- Test filter chaining |
| 115 |
test("filter chaining stops on REJECT", function() |
| 116 |
lua_url_filter.clear_filters() -- Clear any previously registered filters |
| 117 |
|
| 118 |
local filter1_called = false |
| 119 |
local filter2_called = false |
| 120 |
|
| 121 |
lua_url_filter.register_filter(function(url_str, flags) |
| 122 |
filter1_called = true |
| 123 |
return REJECT |
| 124 |
end) |
| 125 |
|
| 126 |
lua_url_filter.register_filter(function(url_str, flags) |
| 127 |
filter2_called = true |
| 128 |
return ACCEPT |
| 129 |
end) |
| 130 |
|
| 131 |
local url_text = rspamd_text.fromstring("http://example.com") |
| 132 |
lua_url_filter.filter_url_string(url_text, 0) |
| 133 |
|
| 134 |
assert_true(filter1_called, "first filter not called") |
| 135 |
assert_false(filter2_called, "second filter called despite REJECT") |
| 136 |
|
| 137 |
lua_url_filter.clear_filters() -- Clean up after test |
| 138 |
end) |
| 139 |
|
| 140 |
-- Test oversized user field (issue #5731) |
| 141 |
test("issue #5731 - oversized user field parsing", function() |
| 142 |
local long_user = string.rep("a", 80) |
| 143 |
local url_str = "http://" .. long_user .. ":[email protected]/path" |
| 144 |
|
| 145 |
local url_text = rspamd_text.fromstring(url_str) |
| 146 |
local result = lua_url_filter.filter_url_string(url_text, 0) |
| 147 |
|
| 148 |
-- Should be SUSPICIOUS, not REJECT, allowing C parser to continue |
| 149 |
assert_equal(SUSPICIOUS, result, |
| 150 |
"80-char user should be SUSPICIOUS, allowing parsing to continue") |
| 151 |
|
| 152 |
-- Verify URL can still be parsed by C parser |
| 153 |
local parsed_url = url.create(pool, url_str) |
| 154 |
assert_not_nil(parsed_url, "URL with 80-char user should be parseable") |
| 155 |
|
| 156 |
local t = parsed_url:to_table() |
| 157 |
assert_equal("example.com", t.host, "host should be parsed correctly") |
| 158 |
end) |
| 159 |
|
| 160 |
end) |