logo

pleroma

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

cast_and_validate.ex (4423B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2019-2020 Moxley Stratton, Mike Buhot <https://github.com/open-api-spex/open_api_spex>, MPL-2.0
  3. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  4. # SPDX-License-Identifier: AGPL-3.0-only
  5. defmodule Pleroma.Web.ApiSpec.CastAndValidate do
  6. @moduledoc """
  7. This plug is based on [`OpenApiSpex.Plug.CastAndValidate`]
  8. (https://github.com/open-api-spex/open_api_spex/blob/master/lib/open_api_spex/plug/cast_and_validate.ex).
  9. The main difference is ignoring unexpected query params instead of throwing
  10. an error and a config option (`[Pleroma.Web.ApiSpec.CastAndValidate, :strict]`)
  11. to disable this behavior. Also, the default rendering error module
  12. is `Pleroma.Web.ApiSpec.RenderError`.
  13. """
  14. @behaviour Plug
  15. alias OpenApiSpex.Plug.PutApiSpec
  16. alias Plug.Conn
  17. @impl Plug
  18. def init(opts) do
  19. opts
  20. |> Map.new()
  21. |> Map.put_new(:render_error, Pleroma.Web.ApiSpec.RenderError)
  22. end
  23. @impl Plug
  24. def call(conn, %{operation_id: operation_id, render_error: render_error} = opts) do
  25. {spec, operation_lookup} = PutApiSpec.get_spec_and_operation_lookup(conn)
  26. operation = operation_lookup[operation_id]
  27. cast_opts = opts |> Map.take([:replace_params]) |> Map.to_list()
  28. content_type =
  29. case Conn.get_req_header(conn, "content-type") do
  30. [header_value | _] ->
  31. header_value
  32. |> String.split(";")
  33. |> List.first()
  34. _ ->
  35. "application/json"
  36. end
  37. conn = Conn.put_private(conn, :operation_id, operation_id)
  38. case cast_and_validate(spec, operation, conn, content_type, strict?(), cast_opts) do
  39. {:ok, conn} ->
  40. conn
  41. {:error, reason} ->
  42. opts = render_error.init(reason)
  43. conn
  44. |> render_error.call(opts)
  45. |> Plug.Conn.halt()
  46. end
  47. end
  48. def call(
  49. %{
  50. private: %{
  51. phoenix_controller: controller,
  52. phoenix_action: action,
  53. open_api_spex: %{spec_module: spec_module}
  54. }
  55. } = conn,
  56. opts
  57. ) do
  58. {spec, operation_lookup} = PutApiSpec.get_spec_and_operation_lookup(conn)
  59. operation =
  60. case operation_lookup[{controller, action}] do
  61. nil ->
  62. operation_id = controller.open_api_operation(action).operationId
  63. operation = operation_lookup[operation_id]
  64. operation_lookup = Map.put(operation_lookup, {controller, action}, operation)
  65. OpenApiSpex.Plug.Cache.adapter().put(spec_module, {spec, operation_lookup})
  66. operation
  67. operation ->
  68. operation
  69. end
  70. if operation.operationId do
  71. call(conn, Map.put(opts, :operation_id, operation.operationId))
  72. else
  73. raise "operationId was not found in action API spec"
  74. end
  75. end
  76. def call(conn, opts), do: OpenApiSpex.Plug.CastAndValidate.call(conn, opts)
  77. defp cast_and_validate(spec, operation, conn, content_type, true = _strict, cast_opts) do
  78. OpenApiSpex.cast_and_validate(spec, operation, conn, content_type, cast_opts)
  79. end
  80. defp cast_and_validate(spec, operation, conn, content_type, false = _strict, cast_opts) do
  81. case OpenApiSpex.cast_and_validate(spec, operation, conn, content_type) do
  82. {:ok, conn} ->
  83. {:ok, conn}
  84. # Remove unexpected query params and cast/validate again
  85. {:error, errors} ->
  86. query_params =
  87. Enum.reduce(errors, conn.query_params, fn
  88. %{reason: :unexpected_field, name: name, path: [name]}, params ->
  89. Map.delete(params, name)
  90. # Filter out empty params
  91. %{reason: :invalid_type, path: [name_atom], value: ""}, params ->
  92. Map.delete(params, to_string(name_atom))
  93. %{reason: :invalid_enum, name: nil, path: path, value: value}, params ->
  94. path = path |> Enum.reverse() |> tl() |> Enum.reverse() |> list_items_to_string()
  95. update_in(params, path, &List.delete(&1, value))
  96. _, params ->
  97. params
  98. end)
  99. conn = %Conn{conn | query_params: query_params}
  100. OpenApiSpex.cast_and_validate(spec, operation, conn, content_type, cast_opts)
  101. end
  102. end
  103. defp list_items_to_string(list) do
  104. Enum.map(list, fn
  105. i when is_atom(i) -> to_string(i)
  106. i -> i
  107. end)
  108. end
  109. defp strict?, do: Pleroma.Config.get([__MODULE__, :strict], false)
  110. end