#1848·linkwarden

Admin API Self-Overwrite & Broken Account Management

Author: codeCraft-RitikCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbug

Overview

When a server administrator uses the administrative endpoints (/api/v1/users/[id]) to inspect or modify another user's account, the system validates the administrator's permission but passes the administrator's own user ID (userId) instead of the targeted user ID (queryId) to the backend controllers.

Impact

  • Inability to View User Profiles: When an admin requests GET /api/v1/users/<targetId>, Linkwarden returns the administrator's own profile data instead of the requested user's data.
  • Accidental Self-Overwrites & Lockout: When an admin sends a PUT /api/v1/users/<targetId> to edit a user (e.g., updating their email, username, permissions, or quota), the operation silently overwrites the administrator's own account, leaving the targeted user untouched and potentially locking the admin out of their own system.

Affected Files & Locations

  • File: apps/web/pages/api/v1/users/[id]/index.ts
  • Lines: 37–43 (GET handler) and 78–90 (PUT handler)
typescript
// apps/web/pages/api/v1/users/[id]/index.ts
35: const userId = token.id; // Caller's ID (e.g., Admin ID = 1)
36: 
37: if (req.method === "GET") {
38:   if (userId !== queryId && !isServerAdmin)
39:     return res.status(401).json({ response: "Permission denied." });
40: 
41:   const users = await getUserById(userId); // ❌ BUG: Passed userId instead of queryId
42:   return res.status(users.status).json({ response: users.response });
43: }
...
78: if (req.method === "PUT") {
79:   if (userId !== queryId && !isServerAdmin)
80:     return res.status(401).json({ response: "Permission denied." });
...
88:   const updated = await updateUserById(userId, req.body); // ❌ BUG: Passed userId instead of queryId
89:   return res.status(updated.status).json({ response: updated.response });
90: }

Notice that in contrast, the DELETE handler on line 97 correctly passes queryId:

typescript
97: const updated = await deleteUserById(
98:   userId,
99:   req.body,
100:  isServerAdmin,
101:  queryId, // ✅ Correctly passes queryId
102:  bypassPasswordForDeletion
103: );

Steps to Reproduce

  1. Log in as an administrator (User ID 1).
  2. Have another registered user in the database (e.g., User ID 2, username: johndoe).
  3. As Admin, execute an API call or admin panel action to update User 2:
    bash
    curl -X PUT "https://linkwarden.example.com/api/v1/users/2" \
      -H "Cookie: next-auth.session-token=<ADMIN_TOKEN>" \
      -H "Content-Type: application/json" \
      -d '{"name": "Updated John", "username": "johndoe_updated"}'
  4. Query User 1 (Admin) in the database.
  5. Result: User 1's name has been changed to "Updated John" and their username changed to "johndoe_updated". User 2 remains completely unchanged.

Proposed Fix

Pass queryId into getUserById and updateUserById instead of userId:

diff
--- a/apps/web/pages/api/v1/users/[id]/index.ts
+++ b/apps/web/pages/api/v1/users/[id]/index.ts
@@ -38,7 +38,7 @@ export default async function users(req: NextApiRequest, res: NextApiResponse)
     if (userId !== queryId && !isServerAdmin)
       return res.status(401).json({ response: "Permission denied." });

-    const users = await getUserById(userId);
+    const users = await getUserById(queryId);
     return res.status(users.status).json({ response: users.response });
   }

@@ -85,7 +85,7 @@ export default async function users(req: NextApiRequest, res: NextApiResponse)
           "This action is disabled because this is a read-only demo of Linkwarden.",
       });

-    const updated = await updateUserById(userId, req.body);
+    const updated = await updateUserById(queryId, req.body);
     return res.status(updated.status).json({ response: updated.response });
   } else if (req.method === "DELETE") {