logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

instance_static.ex (2208B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.Plugs.InstanceStatic do
  5. require Pleroma.Constants
  6. import Plug.Conn, only: [put_resp_header: 3]
  7. @moduledoc """
  8. This is a shim to call `Plug.Static` but with runtime `from` configuration.
  9. Mountpoints are defined directly in the module to avoid calling the configuration for every request including non-static ones.
  10. """
  11. @behaviour Plug
  12. def file_path(path) do
  13. instance_path =
  14. Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path)
  15. frontend_path = Pleroma.Web.Plugs.FrontendStatic.file_path(path, :primary)
  16. (File.exists?(instance_path) && instance_path) ||
  17. (frontend_path && File.exists?(frontend_path) && frontend_path) ||
  18. Path.join(Application.app_dir(:pleroma, "priv/static/"), path)
  19. end
  20. def init(opts) do
  21. opts
  22. |> Keyword.put(:from, "__unconfigured_instance_static_plug")
  23. |> Plug.Static.init()
  24. end
  25. for only <- Pleroma.Constants.static_only_files() do
  26. def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do
  27. call_static(
  28. conn,
  29. opts,
  30. Pleroma.Config.get([:instance, :static_dir], "instance/static")
  31. )
  32. end
  33. end
  34. def call(conn, _) do
  35. conn
  36. end
  37. defp call_static(conn, opts, from) do
  38. # Prevent content-type spoofing by setting content_types: false
  39. opts =
  40. opts
  41. |> Map.put(:from, from)
  42. |> Map.put(:content_types, false)
  43. conn = set_content_type(conn, conn.request_path)
  44. # Call Plug.Static with our sanitized content-type
  45. Plug.Static.call(conn, opts)
  46. end
  47. defp set_content_type(conn, "/emoji/" <> filepath) do
  48. real_mime = MIME.from_path(filepath)
  49. clean_mime =
  50. Pleroma.Web.Plugs.Utils.get_safe_mime_type(%{allowed_mime_types: ["image"]}, real_mime)
  51. put_resp_header(conn, "content-type", clean_mime)
  52. end
  53. defp set_content_type(conn, filepath) do
  54. real_mime = MIME.from_path(filepath)
  55. put_resp_header(conn, "content-type", real_mime)
  56. end
  57. end
  58. # I think this needs to be uncleaned except for emoji.