From: Vsevolod Stakhov Date: Fri, 14 Nov 2025 20:21:29 +0000 (+0000) Subject: [Fix] Complete lua_State parameter threading through codebase X-Git-Tag: 3.14.1~15^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1214aa434a7e1818fd33895ef4093064aa3fe388;p=thirdparty%2Frspamd.git [Fix] Complete lua_State parameter threading through codebase - 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 --- diff --git a/src/libserver/html/html.cxx b/src/libserver/html/html.cxx index a55f955334..eada311197 100644 --- a/src/libserver/html/html.cxx +++ b/src/libserver/html/html.cxx @@ -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(tag->extra)) { auto *u = std::get(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 */ diff --git a/src/libserver/html/html_url.cxx b/src/libserver/html/html_url.cxx index 2e4cefdeb6..7598a01326 100644 --- a/src/libserver/html/html_url.cxx +++ b/src/libserver/html/html_url.cxx @@ -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 + std::string_view text_data, + lua_State *L) -> std::optional { 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 && diff --git a/src/libserver/html/html_url.hxx b/src/libserver/html/html_url.hxx index 20275b6b3e..95a1c7700b 100644 --- a/src/libserver/html/html_url.hxx +++ b/src/libserver/html/html_url.hxx @@ -24,6 +24,7 @@ #include struct rspamd_url; /* Forward declaration */ +struct lua_State; /* Forward declaration */ namespace rspamd::html { diff --git a/src/libserver/url.c b/src/libserver/url.c index 958e02a127..ccb88c5134 100644 --- a/src/libserver/url.c +++ b/src/libserver/url.c @@ -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,