fix(api_spec): drop every unexpected query param in permissive mode
Ported from Akkoma 7e6efb235. Co-authored-by: Oneric <oneric@oneric.stub>
This commit is contained in:
parent
88a9562a9b
commit
d601a94c69
3 changed files with 66 additions and 23 deletions
1
changelog.d/cast-and-validate-many-params.fix
Normal file
1
changelog.d/cast-and-validate-many-params.fix
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Drop all unexpected query params, not just the first one, when `Pleroma.Web.ApiSpec.CastAndValidate` `strict` is off
|
||||||
|
|
@ -20,6 +20,9 @@ defmodule Pleroma.Web.ApiSpec.CastAndValidate do
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
|
# How often to drop offending query params and retry in permissive mode
|
||||||
|
@max_retries 20
|
||||||
|
|
||||||
@impl Plug
|
@impl Plug
|
||||||
def init(opts) do
|
def init(opts) do
|
||||||
opts
|
opts
|
||||||
|
|
@ -114,34 +117,49 @@ defmodule Pleroma.Web.ApiSpec.CastAndValidate do
|
||||||
false = _strict,
|
false = _strict,
|
||||||
cast_opts
|
cast_opts
|
||||||
) do
|
) do
|
||||||
case OpenApiSpex.cast_and_validate(spec, operation, conn, content_type, cast_opts) do
|
permissive_cast_and_validate(spec, operation, conn, content_type, cast_opts, @max_retries)
|
||||||
{:ok, conn} ->
|
end
|
||||||
{:ok, conn}
|
|
||||||
|
|
||||||
# Remove unexpected query params and cast/validate again
|
# OpenApiSpex reports one offending param at a time, so drop it and cast/validate again
|
||||||
{:error, errors} ->
|
defp permissive_cast_and_validate(
|
||||||
query_params =
|
spec,
|
||||||
Enum.reduce(errors, conn.query_params, fn
|
operation,
|
||||||
%{reason: :unexpected_field, name: name, path: [name]}, params ->
|
%Conn{} = conn,
|
||||||
Map.delete(params, name)
|
content_type,
|
||||||
|
cast_opts,
|
||||||
|
retries
|
||||||
|
) do
|
||||||
|
with {:error, errors} = error <-
|
||||||
|
OpenApiSpex.cast_and_validate(spec, operation, conn, content_type, cast_opts) do
|
||||||
|
query_params = drop_invalid_query_params(errors, conn.query_params)
|
||||||
|
|
||||||
# Filter out empty params
|
if retries > 0 and query_params != conn.query_params do
|
||||||
%{reason: :invalid_type, path: [name_atom], value: ""}, params ->
|
conn = %{conn | query_params: query_params}
|
||||||
Map.delete(params, to_string(name_atom))
|
permissive_cast_and_validate(spec, operation, conn, content_type, cast_opts, retries - 1)
|
||||||
|
else
|
||||||
%{reason: :invalid_enum, name: nil, path: path, value: value}, params ->
|
error
|
||||||
path = path |> Enum.reverse() |> tl() |> Enum.reverse() |> list_items_to_string()
|
end
|
||||||
update_in(params, path, &List.delete(&1, value))
|
|
||||||
|
|
||||||
_, params ->
|
|
||||||
params
|
|
||||||
end)
|
|
||||||
|
|
||||||
conn = %Conn{conn | query_params: query_params}
|
|
||||||
OpenApiSpex.cast_and_validate(spec, operation, conn, content_type, cast_opts)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp drop_invalid_query_params(errors, query_params) do
|
||||||
|
Enum.reduce(errors, query_params, fn
|
||||||
|
%{reason: :unexpected_field, name: name, path: [name]}, params ->
|
||||||
|
Map.delete(params, name)
|
||||||
|
|
||||||
|
# Filter out empty params
|
||||||
|
%{reason: :invalid_type, path: [name_atom], value: ""}, params ->
|
||||||
|
Map.delete(params, to_string(name_atom))
|
||||||
|
|
||||||
|
%{reason: :invalid_enum, name: nil, path: path, value: value}, params ->
|
||||||
|
path = path |> Enum.reverse() |> tl() |> Enum.reverse() |> list_items_to_string()
|
||||||
|
update_in(params, path, &List.delete(&1, value))
|
||||||
|
|
||||||
|
_, params ->
|
||||||
|
params
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
defp list_items_to_string(list) do
|
defp list_items_to_string(list) do
|
||||||
Enum.map(list, fn
|
Enum.map(list, fn
|
||||||
i when is_atom(i) -> to_string(i)
|
i when is_atom(i) -> to_string(i)
|
||||||
|
|
|
||||||
24
test/pleroma/web/api_spec/cast_and_validate_test.exs
Normal file
24
test/pleroma/web/api_spec/cast_and_validate_test.exs
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.ApiSpec.CastAndValidateTest do
|
||||||
|
use Pleroma.Web.ConnCase, async: false
|
||||||
|
|
||||||
|
describe "permissive mode" do
|
||||||
|
setup do: clear_config([Pleroma.Web.ApiSpec.CastAndValidate, :strict], false)
|
||||||
|
|
||||||
|
test "drops several unexpected query params", %{conn: conn} do
|
||||||
|
assert conn
|
||||||
|
|> get("/api/v1/timelines/public?foo=1&bar=2&baz=3&local=true")
|
||||||
|
|> json_response_and_validate_schema(200)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "still rejects an invalid known param", %{conn: conn} do
|
||||||
|
assert %{"error" => _} =
|
||||||
|
conn
|
||||||
|
|> get("/api/v1/timelines/public?foo=1&bar=2&limit=lots")
|
||||||
|
|> json_response(400)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue