diff options
author | Haelwenn (lanodan) Monnier <contact@hacktivis.me> | 2022-01-09 20:00:12 +0100 |
---|---|---|
committer | Haelwenn (lanodan) Monnier <contact@hacktivis.me> | 2022-11-27 04:21:31 +0100 |
commit | da710920039ad892c1d988dc756227411f7a7e63 (patch) | |
tree | 739b8f6454e99bceb4291185210a2d8fec625e60 /lib | |
parent | 4504c810802e2253599f06ddf6d58d3389fb23ac (diff) | |
download | pleroma-da710920039ad892c1d988dc756227411f7a7e63.tar.gz pleroma-da710920039ad892c1d988dc756227411f7a7e63.zip |
EctoType: Add MIME validator
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/constants.ex | 6 | ||||
-rw-r--r-- | lib/pleroma/ecto_type/activity_pub/object_validators/mime.ex | 25 |
2 files changed, 31 insertions, 0 deletions
diff --git a/lib/pleroma/constants.ex b/lib/pleroma/constants.ex index bf92f65cb..343ea1acb 100644 --- a/lib/pleroma/constants.ex +++ b/lib/pleroma/constants.ex @@ -27,4 +27,10 @@ defmodule Pleroma.Constants do do: ~w(index.html robots.txt static static-fe finmoji emoji packs sounds images instance sw.js sw-pleroma.js favicon.png schemas doc embed.js embed.css) ) + + # basic regex, just there to weed out potential mistakes + # https://datatracker.ietf.org/doc/html/rfc2045#section-5.1 + const(mime_regex, + do: ~r/^[^[:cntrl:] ()<>@,;:\\"\/\[\]?=]+\/[^[:cntrl:] ()<>@,;:\\"\/\[\]?=]+(; .*)?$/ + ) end diff --git a/lib/pleroma/ecto_type/activity_pub/object_validators/mime.ex b/lib/pleroma/ecto_type/activity_pub/object_validators/mime.ex new file mode 100644 index 000000000..31d51577d --- /dev/null +++ b/lib/pleroma/ecto_type/activity_pub/object_validators/mime.ex @@ -0,0 +1,25 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.MIME do + use Ecto.Type + + require Pleroma.Constants + + def type, do: :string + + def cast(mime) when is_binary(mime) do + if mime =~ Pleroma.Constants.mime_regex() do + {:ok, mime} + else + {:ok, "application/octet-stream"} + end + end + + def cast(_), do: :error + + def dump(data), do: {:ok, data} + + def load(data), do: {:ok, data} +end |