Module rspamd_url
Module rspamd_url
This module provides routines to handle URL's and extract URL's from the text.
Objects of this class are returned, for example, by task:get_urls() or task:get_emails().
You can also create rspamd_url from any text.
Example:
local url = require "rspamd_url"
local mpool = require "rspamd_mempool"
url.init("/usr/share/rspamd/effective_tld_names.dat")
local pool = mpool.create()
local res = url.create(pool, 'Look at: http://user@test.example.com/test?query")
local t = res:to_table()
-- Content of t:
-- url = ['http://test.example.com/test?query']
-- host = ['test.example.com']
-- user = ['user']
-- path = ['test']
-- tld = ['example.com']
pool:destroy() -- res is destroyed here, so you should not use it afterwards
local mistake = res:to_table() -- INVALID! as pool is destroyed
Brief content:
Functions:
| Function | Description |
|---|---|
url.create([mempool,] str, [{flags_table}]) | No description |
url.parse([mempool,] str[, options]) | Parse a complete URL directly, without extracting it from text. |
url.init(tld_file) | Initialize url library if not initialized yet by Rspamd. |
Methods:
| Method | Description |
|---|---|
url:get_length() | Get length of the url. |
url:get_host() | Get domain part of the url. |
url:get_port() | Get port of the url. |
url:get_user() | Get user part of the url (e.g. |
url:get_path() | Get path of the url. |
url:get_query() | Get query of the url. |
url:get_fragment() | Get fragment of the url. |
url:get_text([as_text]) | Get full content of the url. |
url:tostring() | Get full content of the url or user@domain in case of email. |
url:to_http() | Get URL suitable for HTTP request (e.g. |
url:get_raw() | Get full content of the url as it was parsed (e.g. |
url:is_phished() | Check whether URL has a displayed-domain mismatch or an obscured display. |
url:is_redirected() | Check whether URL was redirected. |
url:is_obscured() | Check whether URL is treated as obscured or obfuscated (e.g. |
url:is_html_displayed() | Check whether URL is just displayed in HTML (e.g. |
url:is_subject() | Check whether URL is found in subject. |
url:get_phished() | Get another URL that pretends to be this URL (e.g. |
url:get_redirected() | Get the URL that this URL redirects to. |
url:set_redirected(url, pool) | Set url as redirected to another url. |
url:get_tld() | Get effective second level domain (eSLD) of the URL host. |
url:get_public_suffix() | Get the public suffix of the URL host using the suffix list loaded by. |
url:get_protocol() | Get protocol name. |
url:get_count() | Return number of occurrences for this particular URL. |
url:get_query_embedded_urls() | Get the number of URLs found embedded in this URL's query string. |
url:get_query_target(pool) | Follow a single embedded HTTP/HTTPS/FTP URL per query level to its leaf. |
url:get_visible() | Get visible part of the url with html tags stripped. |
url:get_hash() | Get fast hash of the url for deduplication purposes. |
url:to_table() | Return url as a table with the following fields. |
url:get_flags() | Return flags for a specified URL as map 'flag'->true for all flags set,. |
Functions
The module rspamd_url defines the following functions.
Function url.create([mempool,] str, [{flags_table}])
Parameters:
memory {rspamd_mempool}: pool for URL, e.g.task:get_mempool()text {string}: that contains URL (can also contain other stuff)
Returns:
{url}: new url object that exists as long as the corresponding mempool exists
Back to module description.
Function url.parse([mempool,] str[, options])
Parse a complete URL directly, without extracting it from text.
WARNING: Omitting mempool uses a process-wide static pool. Allocations, including those made by failed parses, are not reclaimed by Lua garbage collection and remain until process exit. Repeated calls without a pool therefore cause unbounded memory growth. Never omit the pool in per-message, per-request, or other unbounded code paths.
Use task:get_mempool() for message processing, or rspamd_config:get_mempool() for configured endpoints. The returned URL must not be used after its pool is destroyed. The pool-less form is only suitable for bounded, process-lifetime allocations. without a public suffix and disables text boundary heuristics
Parameters:
mempool {rspamd_mempool}: pool owning the result; strongly recommendedstr {string}: URL to parseoptions {table}: optional table; upstream=true permits config endpoints
Returns:
{url}: URL object, or nil and an error string
Example:
local url, err = require('rspamd_url').parse(
rspamd_config:get_mempool(), 'http://user:password@llm.internal.lan:8080/api',
{upstream = true})
Back to module description.
Function url.init(tld_file)
Initialize url library if not initialized yet by Rspamd
Parameters:
tld_file {string}: path to effective_tld_names.dat file (public suffix list)
Returns:
- nothing
Back to module description.
Methods
The module rspamd_url defines the following methods.
Method url:get_length()
Get length of the url
Parameters:
No parameters
Returns:
{number}: length of url in bytes
Back to module description.
Method url:get_host()
Get domain part of the url
Parameters:
No parameters
Returns:
{string}: domain part of URL
Back to module description.
Method url:get_port()
Get port of the url
Parameters:
No parameters
Returns:
{number}: url port
Back to module description.
Method url:get_user()
Get user part of the url (e.g. username in email)
Parameters:
No parameters
Returns:
{string}: user part of URL
Back to module description.
Method url:get_path()
Get path of the url
Parameters:
No parameters
Returns:
{string}: path part of URL
Back to module description.
Method url:get_query()
Get query of the url
Parameters:
No parameters
Returns:
{string}: query part of URL
Back to module description.
Method url:get_fragment()
Get fragment of the url
Parameters:
No parameters
Returns:
{string}: fragment part of URL
Back to module description.
Method url:get_text([as_text])
Get full content of the url
Parameters:
as_text {boolean}: if true, return as rspamd_text, otherwise as string
Returns:
{string|rspamd_text}: url string or text object
Back to module description.
Method url:tostring()
Get full content of the url or user@domain in case of email
Parameters:
No parameters
Returns:
{string}: url as a string
Back to module description.
Method url:to_http()
Get URL suitable for HTTP request (e.g. by trimming fragment and user parts)
Parameters:
No parameters
Returns:
{string}: url as a string
Back to module description.
Method url:get_raw()
Get full content of the url as it was parsed (e.g. with urldecode)
Parameters:
No parameters
Returns:
{string}: url string
Back to module description.
Method url:is_phished()
Check whether URL has a displayed-domain mismatch or an obscured display. This is a parser signal; the phishing module applies scoring and exceptions.
Parameters:
No parameters
Returns:
{boolean}:trueif URL has a phishing candidate signal
Back to module description.
Method url:is_redirected()
Check whether URL was redirected
Parameters:
No parameters
Returns:
{boolean}:trueif URL is redirected
Back to module description.
Method url:is_obscured()
Check whether URL is treated as obscured or obfuscated (e.g. numbers in IP address or other hacks)
Parameters:
No parameters
Returns:
{boolean}:trueif URL is obscured
Back to module description.
Method url:is_html_displayed()
Check whether URL is just displayed in HTML (e.g. NOT a real href)
Parameters:
No parameters
Returns:
{boolean}:trueif URL is displayed only
Back to module description.
Method url:is_subject()
Check whether URL is found in subject
Parameters:
No parameters
Returns:
{boolean}:trueif URL is found in subject
Back to module description.
Method url:get_phished()
Get another URL that pretends to be this URL (e.g. used in phishing)
Parameters:
No parameters
Returns:
{url}: phished URL, or nil if the url is not phished or is redirected
Back to module description.
Method url:get_redirected()
Get the URL that this URL redirects to
Parameters:
No parameters
Returns:
{url}: target of the redirect, or nil if the url is not redirected
Back to module description.
Method url:set_redirected(url, pool)
Set url as redirected to another url
Parameters:
url {string|url}: new url that is redirecting an old onepool {pool}: memory pool to allocate memory if needed
Returns:
{url}: parsed redirected url (if needed)
Back to module description.
Method url:get_tld()
Get effective second level domain (eSLD) of the URL host. This method uses the Public Suffix List (PSL) to determine boundaries and compute the eSLD, not the top-level domain.
Parameters:
No parameters
Returns:
{string}: effective second level domain part (eSLD) of the url host
Back to module description.
Method url:get_public_suffix()
Get the public suffix of the URL host using the suffix list loaded by
rspamd. Unlike url:get_tld(), which returns the registrable domain
(eSLD), this returns the suffix itself (e.g. co.uk).
flags table of the rspamd_tld_lookup module)
Parameters:
No parameters
Returns:
{string}: public suffix (or nil) and match flags (see the
Back to module description.
Method url:get_protocol()
Get protocol name
Parameters:
No parameters
Returns:
{string}: protocol as a string
Back to module description.
Method url:get_count()
Return number of occurrences for this particular URL
Parameters:
No parameters
Returns:
{number}: number of occurrences
Back to module description.
Method url:get_query_embedded_urls()
Get the number of URLs found embedded in this URL's query string
Parameters:
No parameters
Returns:
{number}: count of query-embedded URLs (0 if none)
Back to module description.
Method url:get_query_target(pool)
Follow a single embedded HTTP/HTTPS/FTP URL per query level to its leaf. Uses the URL parser's parameter boundaries, decoding and nesting limit. An embedded target is untrusted input, not evidence of an HTTP redirect.
Parameters:
pool {mempool}: memory pool for the extracted URLs
Returns:
{url|nil}: leaf URL, or nil for no target, ambiguity or depth exhaustion
Back to module description.
Method url:get_visible()
Get visible part of the url with html tags stripped
Parameters:
No parameters
Returns:
{string}: url string
Back to module description.
Method url:get_hash()
Get fast hash of the url for deduplication purposes. Uses the same hash function as internal URL storage (rspamd_cryptobox_fast_hash). This is much more efficient than converting URLs to strings for deduplication, especially with large numbers of URLs.
Parameters:
No parameters
Returns:
{number}: 64-bit hash as a Lua number
Back to module description.
Method url:to_table()
Return url as a table with the following fields:
url: full contenthost: hostname partuser: user partpath: path parttld: top level domainprotocol: url protocol
Parameters:
No parameters
Returns:
{table}: URL as a table
Back to module description.
Method url:get_flags()
Return flags for a specified URL as map 'flag'->true for all flags set, possible flags are:
phished: URL is likely phishednumeric: URL is numeric (e.g. IP address)obscured: URL was obscuredredirected: URL comes from redirectorhtml_displayed: URL is used just for displaying purposestext: URL comes from the textsubject: URL comes from the subjecthost_encoded: URL host part is encodedschema_encoded: URL schema part is encodedquery_encoded: URL query part is encodedmissing_slashes: URL has some slashes missingidn: URL has international charactershas_port: URL has porthas_user: URL has user partschemaless: URL has no schemaunnormalised: URL has some unicode unnormalitieszw_spaces: URL has some zero width spacesurl_displayed: URL has some other url-like string in visible partimage: URL is from src attribute of img HTML tag
Parameters:
No parameters
Returns:
{table}: URL flags
Back to module description.
Back to top.