summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfeld <feld@feld.me>2020-07-09 14:33:24 +0000
committerfeld <feld@feld.me>2020-07-09 14:33:24 +0000
commit6b14f0c514d19fe7c9717dd9d3ada29e442ba706 (patch)
tree7295ba98c7833534622a9c7fb27580691fbc06a5
parent8ca1f3e8c61b906387e9cb6fb8993d2ad496ab3d (diff)
parent49c4e2495357797caa82b4eb0a4d03e8c8dd2730 (diff)
downloadpleroma-6b14f0c514d19fe7c9717dd9d3ada29e442ba706.tar.gz
pleroma-6b14f0c514d19fe7c9717dd9d3ada29e442ba706.zip
Merge branch 'fix/csp-for-captcha' into 'develop'
Add Captcha endpoint to CSP headers when MediaProxy is enabled. See merge request pleroma/pleroma!2718
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/pleroma/plugs/http_security_plug.ex32
2 files changed, 22 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0f3447069..2e914e776 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -81,6 +81,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `blob:` urls not being allowed by connect-src CSP
- Mastodon API: fix `GET /api/v1/notifications` not returning the full result set
- Rich Media Previews for Twitter links
+- Fix CSP policy generation to include remote Captcha services
## [Unreleased (patch)]
diff --git a/lib/pleroma/plugs/http_security_plug.ex b/lib/pleroma/plugs/http_security_plug.ex
index 1420a9611..472a3ff42 100644
--- a/lib/pleroma/plugs/http_security_plug.ex
+++ b/lib/pleroma/plugs/http_security_plug.ex
@@ -69,10 +69,11 @@ defmodule Pleroma.Plugs.HTTPSecurityPlug do
img_src = "img-src 'self' data: blob:"
media_src = "media-src 'self'"
+ # Strict multimedia CSP enforcement only when MediaProxy is enabled
{img_src, media_src} =
if Config.get([:media_proxy, :enabled]) &&
!Config.get([:media_proxy, :proxy_opts, :redirect_on_failure]) do
- sources = get_proxy_and_attachment_sources()
+ sources = build_csp_multimedia_source_list()
{[img_src, sources], [media_src, sources]}
else
{[img_src, " https:"], [media_src, " https:"]}
@@ -107,29 +108,28 @@ defmodule Pleroma.Plugs.HTTPSecurityPlug do
|> :erlang.iolist_to_binary()
end
- defp get_proxy_and_attachment_sources do
+ defp build_csp_multimedia_source_list do
media_proxy_whitelist =
Enum.reduce(Config.get([:media_proxy, :whitelist]), [], fn host, acc ->
add_source(acc, host)
end)
- media_proxy_base_url =
- if Config.get([:media_proxy, :base_url]),
- do: URI.parse(Config.get([:media_proxy, :base_url])).host
+ media_proxy_base_url = build_csp_param(Config.get([:media_proxy, :base_url]))
- upload_base_url =
- if Config.get([Pleroma.Upload, :base_url]),
- do: URI.parse(Config.get([Pleroma.Upload, :base_url])).host
+ upload_base_url = build_csp_param(Config.get([Pleroma.Upload, :base_url]))
- s3_endpoint =
- if Config.get([Pleroma.Upload, :uploader]) == Pleroma.Uploaders.S3,
- do: URI.parse(Config.get([Pleroma.Uploaders.S3, :public_endpoint])).host
+ s3_endpoint = build_csp_param(Config.get([Pleroma.Uploaders.S3, :public_endpoint]))
+
+ captcha_method = Config.get([Pleroma.Captcha, :method])
+
+ captcha_endpoint = build_csp_param(Config.get([captcha_method, :endpoint]))
[]
|> add_source(media_proxy_base_url)
|> add_source(upload_base_url)
|> add_source(s3_endpoint)
|> add_source(media_proxy_whitelist)
+ |> add_source(captcha_endpoint)
end
defp add_source(iodata, nil), do: iodata
@@ -139,6 +139,16 @@ defmodule Pleroma.Plugs.HTTPSecurityPlug do
defp add_csp_param(csp_iodata, param), do: [[param, ?;] | csp_iodata]
+ defp build_csp_param(nil), do: nil
+
+ defp build_csp_param(url) when is_binary(url) do
+ %{host: host, scheme: scheme} = URI.parse(url)
+
+ if scheme do
+ [scheme, "://", host]
+ end
+ end
+
def warn_if_disabled do
unless Config.get([:http_security, :enabled]) do
Logger.warn("