AdminAPI: return 404 when deleting users by an unknown nickname #7999

Open
guysoft wants to merge 2 commits from guysoft/pleroma:admin-delete-unknown-nickname into develop
Member

DELETE /api/pleroma/admin/users maps each requested nickname with User.get_cached_by_nickname/1, which returns nil when the nickname no longer resolves -- the account was already deleted, or the admin frontend sent a stale entry. do_deletes/2 then called user.ap_id, so the request died with KeyError (:ap_id, nil) / UndefinedFunctionError (nil.ap_id/0) and returned 500.

Because the delete loop is not atomic, that 500 could also arrive after earlier nicknames in the list had already been deleted, leaving the instance in a half-deleted state with no error surfaced to the admin.

Fail closed instead: if any requested nickname does not resolve, answer 404 and delete none of them. {:error, :not_found} already renders as 404 through Pleroma.Web.AdminAPI.FallbackController.

Skip-the-unknown-and-delete-the-rest was considered and rejected for a destructive admin action: a silent partial success is worse than an explicit error, and no caller retries after a 200 that deleted only part of what was asked.

Changes

  • user_controller.ex: check that every nickname resolved before deleting.
  • user_operation.ex: declare the 404 response.
  • Regression test asserts the 404, that the resolvable user is untouched, and that no moderation log entry is written. It fails against the pre-fix code with the nil KeyError:
    [error] Internal server error: %KeyError{key: :ap_id, term: nil, ...}
    "key :ap_id not found in: nil"
    Sent 500
    
  • changelog.d/admin-delete-unknown-nickname.fix

Testing

  • mix test test/pleroma/web/admin_api/controllers/user_controller_test.exs -> 56 tests, 0 failures
  • mix format --check-formatted -> clean
  • Run against Elixir 1.15.8 / OTP 26 with PostgreSQL 13.

Checklist

  • Adding a changelog
`DELETE /api/pleroma/admin/users` maps each requested nickname with `User.get_cached_by_nickname/1`, which returns `nil` when the nickname no longer resolves -- the account was already deleted, or the admin frontend sent a stale entry. `do_deletes/2` then called `user.ap_id`, so the request died with `KeyError (:ap_id, nil)` / `UndefinedFunctionError (nil.ap_id/0)` and returned 500. Because the delete loop is not atomic, that 500 could also arrive *after* earlier nicknames in the list had already been deleted, leaving the instance in a half-deleted state with no error surfaced to the admin. Fail closed instead: if any requested nickname does not resolve, answer 404 and delete none of them. `{:error, :not_found}` already renders as 404 through `Pleroma.Web.AdminAPI.FallbackController`. Skip-the-unknown-and-delete-the-rest was considered and rejected for a destructive admin action: a silent partial success is worse than an explicit error, and no caller retries after a 200 that deleted only part of what was asked. ### Changes - `user_controller.ex`: check that every nickname resolved before deleting. - `user_operation.ex`: declare the 404 response. - Regression test asserts the 404, that the resolvable user is untouched, and that no moderation log entry is written. It fails against the pre-fix code with the nil `KeyError`: ``` [error] Internal server error: %KeyError{key: :ap_id, term: nil, ...} "key :ap_id not found in: nil" Sent 500 ``` - `changelog.d/admin-delete-unknown-nickname.fix` ### Testing - `mix test test/pleroma/web/admin_api/controllers/user_controller_test.exs` -> 56 tests, 0 failures - `mix format --check-formatted` -> clean - Run against Elixir 1.15.8 / OTP 26 with PostgreSQL 13. ### Checklist - [x] Adding a changelog
AdminAPI: return 404 when deleting users by an unknown nickname
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ae84752752
DELETE /api/pleroma/admin/users maps each requested nickname with
User.get_cached_by_nickname/1, which returns nil when the nickname no
longer resolves -- the account was already deleted, or the admin
frontend sent a stale entry. do_deletes/2 then called user.ap_id and the
request died with KeyError (:ap_id, nil) / UndefinedFunctionError
(nil.ap_id/0) and returned 500.

Because the delete loop is not atomic, that 500 could also arrive after
earlier nicknames in the list had already been deleted, leaving the
instance in a half-deleted state with no error surfaced to the admin.

Fail closed instead: if any requested nickname does not resolve, answer
404 and delete none of them. {:error, :not_found} already renders as 404
through Pleroma.Web.AdminAPI.FallbackController.

Skip-the-unknown-and-delete-the-rest was considered and rejected for a
destructive admin action: a silent partial success is worse than an
explicit error, and no caller retries after a 200 that deleted only part
of what was asked.

- user_controller.ex: check that every nickname resolved before deleting.
- user_operation.ex: declare the 404 response.
- Regression test asserts the 404, that the resolvable user is untouched
  and that no moderation log entry is written. It fails against the
  pre-fix code with the nil KeyError.
- changelog.d/admin-delete-unknown-nickname.fix

/oc review

/oc review
opencode-fix-bot left a comment

opencode-agent:job=168:generation=1

Verdict

No findings. This change is correct, minimal, and safe to merge. Rating: 5/5

What was reviewed

The PR commit (ae84752) touches four files:

  • lib/pleroma/web/admin_api/controllers/user_controller.ex — do_deletes/2 now resolves every nickname first and returns {:error, :not_found} unless all resolve, so no user is deleted when any nickname is stale.
  • lib/pleroma/web/api_spec/operations/admin/user_operation.ex — declares the new 404 response.
  • test/pleroma/web/admin_api/controllers/user_controller_test.exs — regression test covering the 404, the untouched resolvable user, and the absent moderation-log entry.
  • changelog.d/admin-delete-unknown-nickname.fix — changelog fragment.

Correctness checks

  • User.get_cached_by_nickname/1 returns %User{} or nil (lib/pleroma/user.ex), so the Enum.all?(users, &is_struct(&1, User)) guard rejects exactly the bad case and nothing else.
  • {:error, :not_found} is handled by Pleroma.Web.AdminAPI.FallbackController with status 404 and body %{error: "Not found"}, matching the test assertion.
  • The moderation-log write and the 200 response only happen on the all-resolved path, so the fail-closed behavior holds.
  • The 404 in the OpenAPI spec keeps json_response_and_validate_schema(404) green.

Validation run

  • mix test test/pleroma/web/admin_api/controllers/user_controller_test.exs — 56 tests, 0 failures (Elixir 1.17.3 / OTP 27, local PostgreSQL).
  • mix format --check-formatted on all three changed Elixir files — clean.
  • Worktree left byte-for-byte at the claimed head; no commit, push, or PR changes made (review-only).
opencode-agent:job=168:generation=1 ### Verdict No findings. This change is correct, minimal, and safe to merge. **Rating: 5/5** ### What was reviewed The PR commit (`ae84752`) touches four files: - `lib/pleroma/web/admin_api/controllers/user_controller.ex` — `do_deletes/2` now resolves every nickname first and returns `{:error, :not_found}` unless all resolve, so no user is deleted when any nickname is stale. - `lib/pleroma/web/api_spec/operations/admin/user_operation.ex` — declares the new `404` response. - `test/pleroma/web/admin_api/controllers/user_controller_test.exs` — regression test covering the 404, the untouched resolvable user, and the absent moderation-log entry. - `changelog.d/admin-delete-unknown-nickname.fix` — changelog fragment. ### Correctness checks - `User.get_cached_by_nickname/1` returns `%User{}` or `nil` (`lib/pleroma/user.ex`), so the `Enum.all?(users, &is_struct(&1, User))` guard rejects exactly the bad case and nothing else. - `{:error, :not_found}` is handled by `Pleroma.Web.AdminAPI.FallbackController` with status 404 and body `%{error: "Not found"}`, matching the test assertion. - The moderation-log write and the `200` response only happen on the all-resolved path, so the fail-closed behavior holds. - The `404` in the OpenAPI spec keeps `json_response_and_validate_schema(404)` green. ### Validation run - `mix test test/pleroma/web/admin_api/controllers/user_controller_test.exs` — 56 tests, 0 failures (Elixir 1.17.3 / OTP 27, local PostgreSQL). - `mix format --check-formatted` on all three changed Elixir files — clean. - Worktree left byte-for-byte at the claimed head; no commit, push, or PR changes made (review-only).
refactor: simplify unknown nickname check in admin user delete
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
9ad5229e6b
All checks were successful
ci/woodpecker/pr/changelog Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.15 Pipeline was successful
ci/woodpecker/pr/unit-testing-elixir-1.19 Pipeline was successful
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u admin-delete-unknown-nickname:guysoft-admin-delete-unknown-nickname
git switch guysoft-admin-delete-unknown-nickname

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch develop
git merge --no-ff guysoft-admin-delete-unknown-nickname
git switch guysoft-admin-delete-unknown-nickname
git rebase develop
git switch develop
git merge --ff-only guysoft-admin-delete-unknown-nickname
git switch guysoft-admin-delete-unknown-nickname
git rebase develop
git switch develop
git merge --no-ff guysoft-admin-delete-unknown-nickname
git switch develop
git merge --squash guysoft-admin-delete-unknown-nickname
git switch develop
git merge --ff-only guysoft-admin-delete-unknown-nickname
git switch develop
git merge guysoft-admin-delete-unknown-nickname
git push origin develop
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
pleroma/pleroma!7999
No description provided.