fix(mrf): match domain rules literally

Escape the domain so `.` only matches a dot, and use an optional
group for the `*.` prefix, which matches the same hosts without
exponential backtracking. Bad entries now match nothing instead of
raising on every check.
This commit is contained in:
Lain Soykaf 2026-10-31 13:17:50 +04:00
commit 7e157d8cc9
No known key found for this signature in database
3 changed files with 27 additions and 20 deletions

View file

@ -148,14 +148,13 @@ defmodule Pleroma.Web.ActivityPub.MRF do
@spec subdomains_regex([String.t()]) :: [Regex.t()]
def subdomains_regex(domains) when is_list(domains) do
for domain <- domains do
try do
target = String.replace(domain, "*.", "(.*\\.)*")
~r<^#{target}$>i
rescue
e ->
Logger.error("MRF: Invalid subdomain Regex: #{domain}")
reraise e, __STACKTRACE__
end
{prefix, rest} =
case domain do
"*." <> rest -> {"(.*\\.)?", rest}
_ -> {"", domain}
end
~r<^#{prefix}#{Regex.escape(rest)}$>i
end
end