]> source.dussan.org Git - rspamd.git/commitdiff
Fixed bug with task:inject() function according to the issue #5010 (#5021)
authorIvan Stakhov <50211739+LeftTry@users.noreply.github.com>
Tue, 25 Jun 2024 12:49:56 +0000 (17:49 +0500)
committerGitHub <noreply@github.com>
Tue, 25 Jun 2024 12:49:56 +0000 (13:49 +0100)
* [Fix] Fix issue with task:inject_url

* [Minor] Code clean up

* [Minor] Clean up

* [Minor] Small fix

* [Minor] Fix small typo

* [Test] Debug

* [Test] Debug

* [Test] Debug inject_url

* [Test] Debugging

* [Test] Debugging task:inject test

* [Test] Debugging task:inject test

* [Test] Debugging task:inject test

* [Minor] Debug task:inject_url test

* [Minor] Small fix

* [Minor] Debug task:inject_url test

* [Test] Test base task:inject

* [Test] Test updated task:inject

* [Test] Test updated task:inject

* [Test] Test updated task:inject

* [Test] Test updated task:inject

* [Test] Test updated task:inject

* [Test] Test fixed task:inject

* [Test] Test fixed task:inject

* [Test] Testing task:inject

* [Test] Testing task:inject

* [Test] Fix task:inject

* [Test] Fix task:inject

* [Test] Fix task:inject

* [Test] Fix task:inject

* [Test] Fix task:inject

* [Test] Fix task:inject

* [Test] Fix task:inject

* [Test] Fix task:inject

* [Test] Test base task:inject

* [Test] Test base task:inject

* [Test] Test base task:inject

* [Test] Fix task:inject

* [Test] Fix task:inject

* [Test] Fix task:inject

* [Test] Fix task:inject

* [Minor] Clean up code in task:inject

* [Minor] Updated test for task:inject

* [Test] Debug test for task:inject_url

* [Test] Debug test for task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Fix task:inject_url

* [Test] Check task:inject_url

* [Test] Check task:inject_url

* [Test] Check task:inject_url

* [Test] Check task:inject_url

* [Test] Check task:inject_url

* [Test] Check task:inject_url

* [Minor] Clean up code

* [Minor] Clean up code

* [Minor] Little fix for error messages in url.create function

* [Test] Change way to inject url

* [Test] Fix inject url

* [Test] Fix inject url

* [Test] Fix inject url

* [Test] Fix inject url

* [Test] Fix inject url

* [Minor] Clean up code

* [Test] Without fake mime_part

* [Test] Without fake mime_part

* [Minor] Clean up

* [Test] Mime part of the task

* [Test] Mpart should exist

* [Test] Mpart should exist

* [Test] Mpart should exist

* [Test] Another check for mpart->urls

* [Minor] Clean up

* [Minor] Clean up

* [Fix] Fixing OpenSSL 3.0 compatibility

* [Fix] Reverted

src/lua/lua_task.c
src/lua/lua_url.c
test/functional/cases/001_merged/101_lua.robot
test/functional/configs/merged.conf
test/functional/lua/inject_url.lua [new file with mode: 0644]

index edbe5b89acf60a54427e5d1f8d8c1c94d7894ba3..dc41d4ab78583da09d5cb961d8097693189094cc 100644 (file)
@@ -2634,6 +2634,53 @@ lua_task_has_urls(lua_State *L)
        return 2;
 }
 
+struct rspamd_url_query_to_inject_cbd {
+       struct rspamd_task *task;
+       struct rspamd_url *url;
+       GPtrArray *mpart_urls;
+};
+
+static gboolean
+inject_url_query_callback(struct rspamd_url *url, gsize start_offset,
+                                               gsize end_offset, gpointer ud)
+{
+       struct rspamd_url_query_to_inject_cbd *cbd =
+               (struct rspamd_url_query_to_inject_cbd *) ud;
+       struct rspamd_task *task;
+
+       task = cbd->task;
+
+       url->flags |= RSPAMD_URL_FLAG_QUERY;
+
+       if (rspamd_url_set_add_or_increase(MESSAGE_FIELD(task, urls), url, false) && cbd->mpart_urls) {
+               g_ptr_array_add(cbd->mpart_urls, url);
+       }
+
+       return TRUE;
+}
+
+static void
+inject_url_query(struct rspamd_task *task, struct rspamd_url *url,
+                                          GPtrArray *part_urls)
+{
+       if (url->querylen > 0) {
+               struct rspamd_url_query_to_inject_cbd cbd;
+
+               cbd.task = task;
+               cbd.url = url;
+               cbd.mpart_urls = part_urls;
+
+               rspamd_url_find_multiple(task->task_pool,
+                                                                rspamd_url_query_unsafe(url), url->querylen,
+                                                                RSPAMD_URL_FIND_ALL, NULL,
+                                                                inject_url_query_callback, &cbd);
+       }
+
+       if (part_urls) {
+               g_ptr_array_add(part_urls, url);
+       }
+}
+
 static int
 lua_task_inject_url(lua_State *L)
 {
@@ -2644,15 +2691,13 @@ lua_task_inject_url(lua_State *L)
 
        if (lua_isuserdata(L, 3)) {
                /* We also have a mime part there */
-               mpart = *((struct rspamd_mime_part **) rspamd_lua_check_udata_maybe(L,
-                                                                                                                                                       3, rspamd_mimepart_classname));
+               mpart = *((struct rspamd_mime_part **)
+                                                          rspamd_lua_check_udata_maybe(L, 3, rspamd_mimepart_classname));
        }
-
        if (task && task->message && url && url->url) {
                if (rspamd_url_set_add_or_increase(MESSAGE_FIELD(task, urls), url->url, false)) {
-                       if (mpart && mpart->urls) {
-                               /* Also add url to the mime part */
-                               g_ptr_array_add(mpart->urls, url->url);
+                       if(mpart && mpart->urls) {
+                               inject_url_query(task, url->url, mpart->urls);
                        }
                }
        }
index 8e6680a91cc80bf5f81e55d5c7f094bf88279526..28c42f9f9497c5ca8e6329798dcb2498ce1d2643 100644 (file)
@@ -855,18 +855,20 @@ lua_url_create(lua_State *L)
        if (lua_type(L, 1) == LUA_TUSERDATA) {
                pool = rspamd_lua_check_mempool(L, 1);
                t = lua_check_text_or_string(L, 2);
+               if (t == NULL) {
+                       return luaL_error(L, "invalid arguments: string/text is expected as the second argument");
+               }
        }
        else {
                pool = static_lua_url_pool;
                t = lua_check_text_or_string(L, 1);
+               if (t == NULL) {
+                       return luaL_error(L, "invalid arguments: string/text is expected as the first argument");
+               }
        }
 
        if (pool == NULL) {
-               return luaL_error(L, "invalid arguments: mempool is expected as the second argument");
-       }
-
-       if (t == NULL) {
-               return luaL_error(L, "invalid arguments: string/text is expected as the first argument");
+               return luaL_error(L, "invalid arguments: mempool is expected as the first argument");
        }
 
        rspamd_url_find_single(pool, t->start, t->len, RSPAMD_URL_FIND_ALL,
index d1a771cb3eb651941072a74753739732153cdb7d..2cfc03677ce7e2157dec7028ee13f94c7ebb8c92 100644 (file)
@@ -8,6 +8,7 @@ ${MESSAGE}            ${RSPAMD_TESTDIR}/messages/spam_message.eml
 ${RSPAMD_MAP_MAP}     ${RSPAMD_TESTDIR}/configs/maps/map.list
 ${RSPAMD_RADIX_MAP}   ${RSPAMD_TESTDIR}/configs/maps/ip2.list
 ${RSPAMD_REGEXP_MAP}  ${RSPAMD_TESTDIR}/configs/maps/regexp.list
+${URL_ICS}            ${RSPAMD_TESTDIR}/messages/ics.eml
 
 *** Test Cases ***
 Recipient Parsing Sanity
@@ -47,4 +48,8 @@ Rule conditions
 
 External Maps Simple
   Scan File  ${MESSAGE}  Settings={symbols_enabled = [EXTERNAL_MAP]}
-  Expect Symbol With Exact Options  EXTERNAL_MAP  +hello map
\ No newline at end of file
+  Expect Symbol With Exact Options  EXTERNAL_MAP  +hello map
+
+Task Inject Url
+  Scan File  ${URL_ICS}  Settings={symbols_enabled = [TEST_INJECT_URL]}
+  Expect Symbol  TEST_INJECT_URL
\ No newline at end of file
index 2b3640048280d11c9ff5f57b54f2099e99fb904a..0ee224ceb0373f3f393b11bc36e7ec223aa245d3 100644 (file)
@@ -10,6 +10,7 @@ lua = "{= env.TESTDIR =}/lua/option_order.lua"
 lua = "{= env.TESTDIR =}/lua/recipients.lua"
 lua = "{= env.TESTDIR =}/lua/remove_result.lua"
 lua = "{= env.TESTDIR =}/lua/tlds.lua"
+lua = "{= env.TESTDIR =}/lua/inject_url.lua"
 
 # 104_get_from
 lua = "{= env.TESTDIR =}/lua/get_from.lua"
diff --git a/test/functional/lua/inject_url.lua b/test/functional/lua/inject_url.lua
new file mode 100644 (file)
index 0000000..f8764b2
--- /dev/null
@@ -0,0 +1,17 @@
+local url = require('rspamd_url')
+
+local function task_inject_cb (task)
+    local url_text = 'http://example.com?redir=https://another.com'
+    local url_to_inject = url.create(task:get_mempool(), url_text)
+    task:inject_url(url_to_inject)
+    if #(task:get_urls()) == 2 then
+        return true
+    end
+    return false
+end
+
+rspamd_config:register_symbol({
+    name = 'TEST_INJECT_URL',
+    score = 1.0,
+    callback = task_inject_cb
+})