]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Complete lua_State parameter threading through codebase
authorVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 14 Nov 2025 20:21:29 +0000 (20:21 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 14 Nov 2025 20:21:29 +0000 (20:21 +0000)
- Add forward declaration in url.c for rspamd_url_lua_consult
- Add lua_State forward declaration in html_url.hxx
- Add lua_State parameter to html_append_tag_content
- Add lua_State parameter to html_process_img_tag
- Add lua_State parameter to html_process_link_tag
- Add lua_State parameter to html_url_is_phished
- Cast void* to lua_State* when calling HTML functions from task context
- Cast lua_State* to void* when calling C API from C++ functions
- All compilation errors resolved
- Build successful

src/libserver/html/html.cxx
src/libserver/html/html_url.cxx
src/libserver/html/html_url.hxx
src/libserver/url.c

index a55f95533453a4c49584c3e9f44136a199f758fb..eada311197c0e5a7ac67e1f058de594bd41a3ec9 100644 (file)
@@ -1506,7 +1506,8 @@ html_process_img_tag(rspamd_mempool_t *pool,
                                         struct html_tag *tag,
                                         struct html_content *hc,
                                         khash_t(rspamd_url_hash) * url_set,
-                                        GPtrArray *part_urls)
+                                        GPtrArray *part_urls,
+                                        lua_State *L)
 {
        struct html_image *img;
 
@@ -1672,13 +1673,14 @@ static auto
 html_process_link_tag(rspamd_mempool_t *pool, struct html_tag *tag,
                                          struct html_content *hc,
                                          khash_t(rspamd_url_hash) * url_set,
-                                         GPtrArray *part_urls) -> void
+                                         GPtrArray *part_urls,
+                                         lua_State *L) -> void
 {
        auto found_rel_maybe = tag->find_rel();
 
        if (found_rel_maybe) {
                if (found_rel_maybe.value() == "icon") {
-                       html_process_img_tag(pool, tag, hc, url_set, part_urls);
+                       html_process_img_tag(pool, tag, hc, url_set, part_urls, L);
                }
        }
 }
@@ -1875,7 +1877,8 @@ html_append_tag_content(rspamd_mempool_t *pool,
                                                struct html_content *hc,
                                                html_tag *tag,
                                                GList **exceptions,
-                                               khash_t(rspamd_url_hash) * url_set) -> goffset
+                                               khash_t(rspamd_url_hash) * url_set,
+                                               lua_State *L) -> goffset
 {
        auto is_visible = true, is_block = false, is_spaces = false, is_transparent = false;
        goffset next_tag_offset = tag->closing.end,
@@ -2002,7 +2005,7 @@ html_append_tag_content(rspamd_mempool_t *pool,
                }
 
                auto next_offset = html_append_tag_content(pool, start, len,
-                                                                                                  hc, cld, exceptions, url_set);
+                                                                                                  hc, cld, exceptions, url_set, L);
 
                /* Do not allow shifting back */
                if (next_offset > cur_offset) {
@@ -2044,7 +2047,7 @@ html_append_tag_content(rspamd_mempool_t *pool,
                                                                                        {hc->parsed.data() + initial_parsed_offset, std::size_t(written_len)},
                                                                                        tag, exceptions,
                                                                                        url_set, initial_parsed_offset,
-                                                                                       task->cfg ? task->cfg->lua_state : NULL);
+                                                                                       L);
                        /* Count display URL mismatches when URL is present */
                        if (std::holds_alternative<rspamd_url *>(tag->extra)) {
                                auto *u = std::get<rspamd_url *>(tag->extra);
@@ -2223,7 +2226,7 @@ auto html_process_input(struct rspamd_task *task,
                        if (auto href = cur_tag->find_href()) {
                                if (html_is_absolute_url(*href)) {
                                        auto maybe_url = html_process_url(pool, *href,
-                                                                                                         task->cfg ? task->cfg->lua_state : NULL);
+                                                                                                         task->cfg ? (lua_State *) task->cfg->lua_state : NULL);
                                        if (maybe_url) {
                                                struct rspamd_url *u = maybe_url.value();
                                                if (u->hostlen > 0) {
@@ -2274,7 +2277,7 @@ auto html_process_input(struct rspamd_task *task,
                                                                if (!urlv.empty()) {
                                                                        /* validate and count; do not add to urls set */
                                                                        auto maybe_url = html_process_url(pool, urlv,
-                                                                                                                                         task->cfg ? task->cfg->lua_state : NULL);
+                                                                                                                                         task->cfg ? (lua_State *) task->cfg->lua_state : NULL);
                                                                        if (maybe_url) {
                                                                                hc->features.meta_refresh_urls++;
                                                                        }
@@ -2345,7 +2348,7 @@ auto html_process_input(struct rspamd_task *task,
 
                if (cur_tag->flags & FL_HREF && html_document_state == html_document_state::body) {
                        auto maybe_url = html_process_url_tag(pool, cur_tag, hc,
-                                                                                                 task->cfg ? task->cfg->lua_state : NULL);
+                                                                                                 task->cfg ? (lua_State *) task->cfg->lua_state : NULL);
 
                        if (maybe_url.has_value()) {
                                url = maybe_url.value();
@@ -2360,7 +2363,7 @@ auto html_process_input(struct rspamd_task *task,
                                                url->part_order = cur_url_part_order++;
                                                html_process_query_url(pool, url, url_set,
                                                                                           part_urls,
-                                                                                          task->cfg ? task->cfg->lua_state : NULL);
+                                                                                          task->cfg ? (lua_State *) task->cfg->lua_state : NULL);
                                        }
                                        else {
                                                url = maybe_existing;
@@ -2447,7 +2450,7 @@ auto html_process_input(struct rspamd_task *task,
                         * Base is allowed only within head tag but HTML is retarded
                         */
                        auto maybe_url = html_process_url_tag(pool, cur_tag, hc,
-                                                                                                 task->cfg ? task->cfg->lua_state : NULL);
+                                                                                                 task->cfg ? (lua_State *) task->cfg->lua_state : NULL);
 
                        if (maybe_url) {
                                msg_debug_html("got valid base tag");
@@ -2468,11 +2471,13 @@ auto html_process_input(struct rspamd_task *task,
 
                if (cur_tag->id == Tag_IMG) {
                        html_process_img_tag(pool, cur_tag, hc, url_set,
-                                                                part_urls);
+                                                                part_urls,
+                                                                task->cfg ? (lua_State *) task->cfg->lua_state : NULL);
                }
                else if (cur_tag->id == Tag_LINK) {
                        html_process_link_tag(pool, cur_tag, hc, url_set,
-                                                                 part_urls);
+                                                                 part_urls,
+                                                                 task->cfg ? (lua_State *) task->cfg->lua_state : NULL);
                }
 
                /* Track DOM tag count and max depth */
@@ -3106,7 +3111,8 @@ auto html_process_input(struct rspamd_task *task,
 
        if (!hc->all_tags.empty() && hc->root_tag) {
                html_append_tag_content(pool, start, end - start, hc, hc->root_tag,
-                                                               exceptions, url_set);
+                                                               exceptions, url_set,
+                                                               task->cfg ? (lua_State *) task->cfg->lua_state : NULL);
        }
 
        /* Leftover after content */
index 2e4cefdeb657ac4c2c2621bf3f98ac3a3cdf861b..7598a01326dd5404ec11c0ff0146a90f70395e9a 100644 (file)
@@ -132,7 +132,8 @@ is_transfer_proto(struct rspamd_url *u) -> bool
 
 auto html_url_is_phished(rspamd_mempool_t *pool,
                                                 struct rspamd_url *href_url,
-                                                std::string_view text_data) -> std::optional<rspamd_url *>
+                                                std::string_view text_data,
+                                                lua_State *L) -> std::optional<rspamd_url *>
 {
        struct rspamd_url *text_url;
        std::string_view disp_tok, href_tok;
@@ -159,7 +160,7 @@ auto html_url_is_phished(rspamd_mempool_t *pool,
 
                text_url = rspamd_mempool_alloc0_type(pool, struct rspamd_url);
                auto rc = rspamd_url_parse(text_url, url_str, strlen(url_str), pool,
-                                                                  RSPAMD_URL_PARSE_TEXT, L);
+                                                                  RSPAMD_URL_PARSE_TEXT, (void *) L);
 
                if (rc == URI_ERRNO_OK) {
                        text_url->flags |= RSPAMD_URL_FLAG_HTML_DISPLAYED;
@@ -257,7 +258,7 @@ void html_check_displayed_url(rspamd_mempool_t *pool,
                rspamd_string_unicode_trim_inplace(url->ext->visible_part,
                                                                                   &dlen));
        auto maybe_url = html_url_is_phished(pool, url,
-                                                                                {url->ext->visible_part, dlen});
+                                                                                {url->ext->visible_part, dlen}, L);
 
        if (maybe_url) {
                url->flags |= saved_flags;
@@ -456,7 +457,7 @@ auto html_process_url(rspamd_mempool_t *pool, std::string_view &input, lua_State
 
        url = rspamd_mempool_alloc0_type(pool, struct rspamd_url);
        rspamd_url_normalise_propagate_flags(pool, decoded, &dlen, saved_flags);
-       rc = rspamd_url_parse(url, decoded, dlen, pool, RSPAMD_URL_PARSE_HREF, L);
+       rc = rspamd_url_parse(url, decoded, dlen, pool, RSPAMD_URL_PARSE_HREF, (void *) L);
 
        /* Filter some completely damaged urls */
        if (rc == URI_ERRNO_OK && url->hostlen > 0 &&
index 20275b6b3e4012518457c72f96313dcaf3798f93..95a1c7700bf6aa0ae62801c17b04a1f5e7813a57 100644 (file)
@@ -24,6 +24,7 @@
 #include <optional>
 
 struct rspamd_url; /* Forward declaration */
+struct lua_State;  /* Forward declaration */
 
 namespace rspamd::html {
 
index 958e02a12798db2c2b4fe6c7eac7c514ff3ee79b..ccb88c513400e2b5d76b2143f5d07a1b0ab61691 100644 (file)
@@ -1024,6 +1024,14 @@ out:
        return ret;
 }
 
+/* Forward declaration for Lua consultation */
+static enum rspamd_url_lua_filter_result
+rspamd_url_lua_consult(rspamd_mempool_t *pool,
+                                          const char *url_str,
+                                          gsize len,
+                                          unsigned int flags,
+                                          lua_State *L);
+
 static int
 rspamd_web_parse(struct http_parser_url *u, const char *str, gsize len,
                                 char const **end,