| 1 |
-- URL parser tests |
| 2 |
|
| 3 |
context("URL check functions", function() |
| 4 |
local mpool = require("rspamd_mempool") |
| 5 |
local lua_urls_compose = require "lua_urls_compose" |
| 6 |
local url = require("rspamd_url") |
| 7 |
local lua_util = require("lua_util") |
| 8 |
local logger = require("rspamd_logger") |
| 9 |
local test_helper = require("rspamd_test_helper") |
| 10 |
local ok, ffi = pcall(require, "ffi") |
| 11 |
if not ok then |
| 12 |
ffi = require("cffi") |
| 13 |
end |
| 14 |
|
| 15 |
ffi.cdef [[ |
| 16 |
void rspamd_normalize_path_inplace(char *path, size_t len, size_t *nlen); |
| 17 |
]] |
| 18 |
|
| 19 |
test_helper.init_url_parser() |
| 20 |
|
| 21 |
local pool = mpool.create() |
| 22 |
|
| 23 |
local cases = { |
| 24 |
{ "test.com", { "test.com", nil } }, |
| 25 |
{ " test.com", { "test.com", nil } }, |
| 26 |
{ "<test.com> text", { "test.com", nil } }, |
| 27 |
{ "test.com. text", { "test.com", nil } }, |
| 28 |
{ "mailto:[email protected] text", { "example.com", "A.User" } }, |
| 29 |
{ "http://Тест.Рф:18 text", { "тест.рф", nil } }, |
| 30 |
{ "http://user:password@тест2.РФ:18 text", { "тест2.рф", "user" } }, |
| 31 |
{ "[email protected]", { "example.com", "somebody" } }, |
| 32 |
{ "https://127.0.0.1/abc text", { "127.0.0.1", nil } }, |
| 33 |
{ "https:\\\\127.0.0.1/abc text", { "127.0.0.1", nil } }, |
| 34 |
{ "https:\\\\127.0.0.1", { "127.0.0.1", nil } }, |
| 35 |
{ "https://127.0.0.1 text", { "127.0.0.1", nil } }, |
| 36 |
{ "https://[::1]:1", { "::1", nil } }, |
| 37 |
{ "https://user:password@[::1]:1", { "::1", nil } }, |
| 38 |
{ "https://user:password@[::1]", { "::1", nil } }, |
| 39 |
{ "https://user:password@[::1]/1", { "::1", nil } }, |
| 40 |
} |
| 41 |
|
| 42 |
for i, c in ipairs(cases) do |
| 43 |
local res = url.create(pool, c[1]) |
| 44 |
|
| 45 |
test("Extract urls from text" .. i, function() |
| 46 |
assert_not_nil(res, "cannot parse " .. c[1]) |
| 47 |
local t = res:to_table() |
| 48 |
--local s = logger.slog("%1 -> %2", c[1], t) |
| 49 |
--print(s) |
| 50 |
assert_not_nil(t, "cannot convert to table " .. c[1]) |
| 51 |
assert_equal(c[2][1], t['host'], |
| 52 |
logger.slog('expected host "%s", but got "%s" in url %s => %s', |
| 53 |
c[2][1], t['host'], c[1], t)) |
| 54 |
|
| 55 |
if c[2][2] then |
| 56 |
assert_equal(c[2][1], t['host'], |
| 57 |
logger.slog('expected user "%s", but got "%s" in url %s => %s', |
| 58 |
c[2][1], t['host'], c[1], t)) |
| 59 |
end |
| 60 |
end) |
| 61 |
end |
| 62 |
|
| 63 |
cases = { |
| 64 |
{ [[http://example.net/path/]], true, { |
| 65 |
host = 'example.net', path = 'path/' |
| 66 |
} }, |
| 67 |
{ 'http://example.net/hello%20world.php?arg=x#fragment', true, { |
| 68 |
host = 'example.net', fragment = 'fragment', query = 'arg=x', |
| 69 |
path = 'hello world.php', |
| 70 |
} }, |
| 71 |
{ 'http://example.net/?arg=%23#fragment', true, { |
| 72 |
host = 'example.net', fragment = 'fragment', query = 'arg=#', |
| 73 |
} }, |
| 74 |
{ "http:/\\[::eeee:192.168.0.1]/#test", true, { |
| 75 |
host = '::eeee:c0a8:1', fragment = 'test' |
| 76 |
} }, |
| 77 |
{ "http:/\\[::eeee:192.168.0.1]#test", true, { |
| 78 |
host = '::eeee:c0a8:1', fragment = 'test' |
| 79 |
} }, |
| 80 |
{ "http:/\\[::eeee:192.168.0.1]?test", true, { |
| 81 |
host = '::eeee:c0a8:1', query = 'test' |
| 82 |
} }, |
| 83 |
{ "http:\\\\%30%78%63%30%2e%30%32%35%30.01", true, { --0xc0.0250.01 |
| 84 |
host = '192.168.0.1', |
| 85 |
} }, |
| 86 |
{ "http:/\\www.google.com/foo?bar=baz#", true, { |
| 87 |
host = 'www.google.com', path = 'foo', query = 'bar=baz', tld = 'google.com' |
| 88 |
} }, |
| 89 |
{ "http://[www.google.com]/", true, { |
| 90 |
host = 'www.google.com', |
| 91 |
} }, |
| 92 |
{ "<test.com", true, { |
| 93 |
host = 'test.com', tld = 'test.com', |
| 94 |
} }, |
| 95 |
{ "test.com>", false }, |
| 96 |
{ ",test.com text", false }, |
| 97 |
{ "ht\ttp:@www.google.com:80/;p?#", false }, |
| 98 |
{ "http://user:pass@/", false }, |
| 99 |
{ "http://foo:-80/", false }, |
| 100 |
{ "http:////////user:@google.com:99?foo", true, { |
| 101 |
host = 'google.com', user = 'user', port = 99, query = 'foo' |
| 102 |
} }, |
| 103 |
{ "http://%25DOMAIN:[email protected]/", true, { |
| 104 |
host = 'foodomain.com', user = '%25DOMAIN' |
| 105 |
} }, |
| 106 |
{ "http://0.0xFFFFFF", true, { |
| 107 |
host = '0.255.255.255' |
| 108 |
} }, |
| 109 |
--{"http:/\\030052000001", true, { |
| 110 |
-- host = '192.168.0.1' |
| 111 |
--}}, |
| 112 |
{ "http:\\/0xc0.052000001", true, { |
| 113 |
host = '192.168.0.1' |
| 114 |
} }, |
| 115 |
{ "http://192.168.0.1.?foo", true, { |
| 116 |
host = '192.168.0.1', query = 'foo', |
| 117 |
} }, |
| 118 |
{ "http://twitter.com#test", true, { |
| 119 |
host = 'twitter.com', fragment = 'test' |
| 120 |
} }, |
| 121 |
{ "http:www.twitter.com#test", true, { |
| 122 |
host = 'www.twitter.com', fragment = 'test' |
| 123 |
} }, |
| 124 |
{ "http://example。com#test", true, { |
| 125 |
host = 'example.com', fragment = 'test' |
| 126 |
} }, |
| 127 |
{ "http://hoho.example。com#test", true, { |
| 128 |
host = 'hoho.example.com', fragment = 'test' |
| 129 |
} }, |
| 130 |
{ "http://hoho。example。com#test", true, { |
| 131 |
host = 'hoho.example.com', fragment = 'test' |
| 132 |
} }, |
| 133 |
{ "http://hoho.example。com#test", true, { |
| 134 |
host = 'hoho.example.com', fragment = 'test' |
| 135 |
} }, |
| 136 |
{ "http://hehe。example。com#test", true, { |
| 137 |
host = 'hehe.example.com', fragment = 'test' |
| 138 |
} }, |
| 139 |
{ "http:////$%^&****((@example.org//#f@f", true, { |
| 140 |
user = '$%^&****((', host = 'example.org', fragment = 'f@f' |
| 141 |
} }, |
| 142 |
{ "http://@@example.com", true, { |
| 143 |
user = "@", host = "example.com" |
| 144 |
} }, |
| 145 |
{ "https://example.com\\_Resources\\ClientImages\\UserData?ol\\o#ololo\\", true, { |
| 146 |
host = "example.com", path = "_Resources\\ClientImages\\UserData", |
| 147 |
query = "ol\\o", fragment = "ololo\\", |
| 148 |
} }, |
| 149 |
{ |
| 150 |
"http://0x3f8f29a4/pro/au.html", true, { |
| 151 |
host = "63.143.41.164", |
| 152 |
path = "pro/au.html", |
| 153 |
} }, |
| 154 |
{ |
| 155 |
"http://localhost", true, { |
| 156 |
host = "localhost", |
| 157 |
tld = "localhost", |
| 158 |
} }, |
| 159 |
{ |
| 160 |
"http://localhost.", true, { |
| 161 |
host = "localhost.", |
| 162 |
tld = "localhost", |
| 163 |
} }, |
| 164 |
} |
| 165 |
|
| 166 |
-- Some cases from https://code.google.com/p/google-url/source/browse/trunk/src/url_canon_unittest.cc |
| 167 |
for i, c in ipairs(cases) do |
| 168 |
local res = url.create(pool, c[1]) |
| 169 |
|
| 170 |
test("Parse url: " .. c[1], function() |
| 171 |
if c[2] then |
| 172 |
assert_not_nil(res, "we are able to parse url: " .. c[1]) |
| 173 |
|
| 174 |
local uf = res:to_table() |
| 175 |
|
| 176 |
for k, v in pairs(c[3]) do |
| 177 |
assert_not_nil(uf[k], k .. ' is missing in url, must be ' .. v) |
| 178 |
assert_equal(uf[k], v, logger.slog('expected " %s ", for %s, but got " %s " in url %s => %s', |
| 179 |
v, k, uf[k], c[1], uf)) |
| 180 |
end |
| 181 |
for k, v in pairs(uf) do |
| 182 |
if k ~= 'url' and k ~= 'protocol' and k ~= 'tld' then |
| 183 |
assert_not_nil(c[3][k], k .. ' should be absent but it is ' .. v .. ' in: ' .. c[1]) |
| 184 |
end |
| 185 |
end |
| 186 |
else |
| 187 |
assert_nil(res, "should not parse " .. c[1] .. ' parsed to: ' .. tostring(res)) |
| 188 |
end |
| 189 |
end) |
| 190 |
end |
| 191 |
|
| 192 |
cases = { |
| 193 |
{ "/././foo", "/foo" }, |
| 194 |
{ "/a/b/c/./../../g", "/a/g" }, |
| 195 |
{ "/./.foo", "/.foo" }, |
| 196 |
{ "/foo/.", "/foo/" }, |
| 197 |
{ "/foo/./", "/foo/" }, |
| 198 |
{ "/foo/bar/..", "/foo" }, |
| 199 |
{ "/foo/bar/../", "/foo/" }, |
| 200 |
{ "/foo/..bar", "/foo/..bar" }, |
| 201 |
{ "/foo/bar/../ton", "/foo/ton" }, |
| 202 |
{ "/foo/bar/../ton/../../a", "/a" }, |
| 203 |
{ "/foo/../../..", "/" }, |
| 204 |
{ "/foo/../../../ton", "/ton" }, |
| 205 |
{ "////../..", "/" }, |
| 206 |
{ "./", "" }, |
| 207 |
{ "/./", "/" }, |
| 208 |
{ "/./././././././", "/" }, |
| 209 |
{ "/", "/" }, |
| 210 |
{ "/a/b", "/a/b" }, |
| 211 |
{ "/a/b/", "/a/b/" }, |
| 212 |
{ "..", "/" }, |
| 213 |
{ "/../", "/" }, |
| 214 |
{ "../", "/" }, |
| 215 |
{ "///foo", "/foo" }, |
| 216 |
} |
| 217 |
|
| 218 |
-- Helper to convert cdata numbers to Lua numbers (cffi-lua compatibility) |
| 219 |
local function cdata_to_number(v) |
| 220 |
-- Try tonumber first (works in LuaJIT and for small values) |
| 221 |
local n = tonumber(v) |
| 222 |
if n then return n end |
| 223 |
-- For 64-bit integers in cffi-lua, convert via string |
| 224 |
return tonumber(tostring(v):match("^(%d+)")) |
| 225 |
end |
| 226 |
|
| 227 |
for i, v in ipairs(cases) do |
| 228 |
test(string.format("Normalize paths '%s'", v[1]), function() |
| 229 |
local buf = ffi.new("char[?]", #v[1]) |
| 230 |
local sizbuf = ffi.new("size_t[1]") |
| 231 |
ffi.copy(buf, v[1], #v[1]) |
| 232 |
ffi.C.rspamd_normalize_path_inplace(buf, #v[1], sizbuf) |
| 233 |
local res = ffi.string(buf, cdata_to_number(sizbuf[0])) |
| 234 |
assert_equal(v[2], res, 'expected ' .. v[2] .. ' but got ' .. res .. ' in path ' .. v[1]) |
| 235 |
end) |
| 236 |
end |
| 237 |
|
| 238 |
cases = { |
| 239 |
{ 'example.com', 'example.com' }, |
| 240 |
{ 'baz.example.com', 'baz.example.com' }, |
| 241 |
{ '3.baz.example.com', 'baz.example.com' }, |
| 242 |
{ 'bar.example.com', 'example.com' }, |
| 243 |
{ 'foo.example.com', 'foo.example.com' }, |
| 244 |
{ '3.foo.example.com', '3.foo.example.com' }, |
| 245 |
{ 'foo.com', 'foo.com' }, |
| 246 |
{ 'bar.foo.com', 'foo.com' }, |
| 247 |
} |
| 248 |
|
| 249 |
local excl_rules1 = { |
| 250 |
'example.com', |
| 251 |
'*.foo.example.com', |
| 252 |
'!bar.example.com' |
| 253 |
} |
| 254 |
|
| 255 |
local comp_rules = lua_urls_compose.inject_composition_rules(rspamd_config, excl_rules1) |
| 256 |
|
| 257 |
for _, v in ipairs(cases) do |
| 258 |
test("URL composition " .. v[1], function() |
| 259 |
local u = url.create(pool, v[1]) |
| 260 |
assert_not_nil(u, "we are able to parse url:" .. v[1]) |
| 261 |
local res = comp_rules:process_url(nil, u:get_tld(), u:get_host()) |
| 262 |
assert_equal(v[2], res, 'expected ' .. v[2] .. ' but got ' .. res .. ' in url ' .. v[1]) |
| 263 |
end) |
| 264 |
end |
| 265 |
|
| 266 |
test("URL regexp issue", function() |
| 267 |
local rspamd_regexp = require "rspamd_regexp" |
| 268 |
local u = url.create(pool, |
| 269 |
'https://cls21.bullhornstaffing.com/MailerUnsubscribe.cfm?privateLabelID=3D26028&email=xpto&updKey=3D%3B%28U%2B%2F%200T%3EI%3B%2FQEI%5E%29%25XR%3FZ%40%5B%2EGJY%3CF%23%3F%25%22%29%5D%2D%0A') |
| 270 |
assert_not_nil(u, "we are able to parse url") |
| 271 |
local re = rspamd_regexp.create_cached("^$|^[?].*|^[#].*|[^#?]+") |
| 272 |
assert_not_nil(re, "regexp is valid") |
| 273 |
local res = re:search('/' .. u:get_path() .. '?' .. u:get_query()) |
| 274 |
assert_equal(res[#res], '') |
| 275 |
end) |
| 276 |
end) |