Spec improvements

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-09-27 09:41:36 +02:00
parent cc3106e425
commit 41f086e2c9
21 changed files with 299 additions and 90 deletions

View File

@@ -62,9 +62,9 @@ defmodule Mobilizon.Web.Plugs.HTTPSecurityPlug do
static_url = Mobilizon.Web.Endpoint.static_url()
websocket_url = Mobilizon.Web.Endpoint.websocket_url()
img_src = [@img_src | get_csp_config(:img_src, options)]
img_src = [@img_src] ++ [get_csp_config(:img_src, options)]
media_src = [@media_src | get_csp_config(:media_src, options)]
media_src = [@media_src] ++ [get_csp_config(:media_src, options)]
connect_src = [
@connect_src,
@@ -85,22 +85,22 @@ defmodule Mobilizon.Web.Plugs.HTTPSecurityPlug do
]
end
script_src = [script_src | get_csp_config(:script_src, options)]
script_src = [script_src] ++ [get_csp_config(:script_src, options)]
style_src =
if Config.get(:env) == :dev, do: [@style_src | "'unsafe-inline' "], else: @style_src
style_src = [style_src | get_csp_config(:style_src, options)]
style_src = [style_src] ++ [get_csp_config(:style_src, options)]
font_src = [@font_src | get_csp_config(:font_src, options)]
font_src = [@font_src] ++ [get_csp_config(:font_src, options)]
frame_src = if Config.get(:env) == :dev, do: "frame-src 'self' ", else: "frame-src 'none' "
frame_src = [frame_src | get_csp_config(:frame_src, options)]
frame_src = [frame_src] ++ [get_csp_config(:frame_src, options)]
frame_ancestors =
if Config.get(:env) == :dev, do: "frame-ancestors 'self' ", else: "frame-ancestors 'none' "
frame_ancestors = [frame_ancestors | get_csp_config(:frame_ancestors, options)]
frame_ancestors = [frame_ancestors] ++ [get_csp_config(:frame_ancestors, options)]
insecure = if scheme == "https", do: "upgrade-insecure-requests"

View File

@@ -11,8 +11,10 @@ defmodule Mobilizon.Web.Plugs.SetLocalePlug do
import Plug.Conn, only: [assign: 3]
alias Mobilizon.Web.Gettext, as: GettextBackend
@spec init(any()) :: nil
def init(_), do: nil
@spec call(Plug.Conn.t(), any()) :: Plug.Conn.t()
def call(conn, _) do
locale =
[
@@ -29,17 +31,22 @@ defmodule Mobilizon.Web.Plugs.SetLocalePlug do
assign(conn, :locale, locale)
end
@spec supported_locale?(String.t()) :: boolean()
defp supported_locale?(locale) do
GettextBackend
|> Gettext.known_locales()
|> Enum.member?(locale)
end
@spec default_locale :: String.t()
defp default_locale do
Keyword.get(Mobilizon.Config.instance_config(), :default_language, "en")
end
@spec determine_best_locale(String.t()) :: String.t()
@doc """
Determine the best available locale for a given locale ID
"""
@spec determine_best_locale(String.t()) :: String.t() | nil
def determine_best_locale(locale) when is_binary(locale) do
locale = String.trim(locale)
locales = Gettext.known_locales(GettextBackend)
@@ -58,5 +65,6 @@ defmodule Mobilizon.Web.Plugs.SetLocalePlug do
def determine_best_locale(_), do: nil
# Keep only the first part of the locale
@spec split_locale(String.t()) :: String.t()
defp split_locale(locale), do: locale |> String.split("_", trim: true, parts: 2) |> hd
end