BlueGit

timfa filemanager

Merge fix: prevent self admin demotion

timfa Tim Falken · pushed by timfa

Authored 2026-05-17 · 6 months ago

Hash
a2825cb94eb1e337c9cea00222d82317f5b0b793
5 files changed +64 −3
.github/technical-reference.md +2 −1
@@ -50,6 +50,7 @@
50 50
51 51 ## server/routes/admin.js
52 52 - POST /api/admin/users - Creates a new user and enforces case-insensitive username uniqueness.
53 - PATCH /api/admin/users/:id - Updates user password/admin flag/permissions; rejects self-demotion and still blocks removing the last admin account.
53 54 - normalizePermissions(permissions) - Normalizes permission maps and enforces that write access implies read access.
54 55 - ensureUserFolders(userPath) - Ensures configured userPath exists and creates per-user subfolders for all current users.
55 56 - GET /api/admin/config - Returns admin config including userPath and userFolderSizeLimit (bytes, null for unlimited).
@@ -244,7 +245,7 @@
244 245 - openFolderSidebar() - Shows current folder details when nothing is selected; when at root base-level it also shows root access list.
245 246 - openRenameModal(item) - Renames files/folders; for roots this writes display alias via /api/files/root-alias and reloads roots.
246 247 - uploadFiles(fileList) - Uploads files one-by-one with per-file conflict prompt (overwrite/rename/cancel) when names already exist.
247 - openUserModal(user, paths) - Admin user editor; enforces UI permission rule that Write is disabled/unchecked while Read is unchecked.
248 - openUserModal(user, paths) - Admin user editor; enforces UI permission rule that Write is disabled/unchecked while Read is unchecked and disables the Admin checkbox when editing the current signed-in admin account.
248 249
249 250 ## public/drag-move.js
250 251 - makeDraggable(el, item) - Attaches pointerdown to a .file-item; starts drag after threshold movement. Depends on: api(), canWrite(), navigateTo(), STATE, closeSidebar(), toast() from app.js.
@@ -50,6 +50,7 @@
50
50
51## server/routes/admin.js
51## server/routes/admin.js
52- POST /api/admin/users - Creates a new user and enforces case-insensitive username uniqueness.
52- POST /api/admin/users - Creates a new user and enforces case-insensitive username uniqueness.
53- PATCH /api/admin/users/:id - Updates user password/admin flag/permissions; rejects self-demotion and still blocks removing the last admin account.
53- normalizePermissions(permissions) - Normalizes permission maps and enforces that write access implies read access.
54- normalizePermissions(permissions) - Normalizes permission maps and enforces that write access implies read access.
54- ensureUserFolders(userPath) - Ensures configured userPath exists and creates per-user subfolders for all current users.
55- ensureUserFolders(userPath) - Ensures configured userPath exists and creates per-user subfolders for all current users.
55- GET /api/admin/config - Returns admin config including userPath and userFolderSizeLimit (bytes, null for unlimited).
56- GET /api/admin/config - Returns admin config including userPath and userFolderSizeLimit (bytes, null for unlimited).
@@ -244,7 +245,7 @@
244- openFolderSidebar() - Shows current folder details when nothing is selected; when at root base-level it also shows root access list.
245- openFolderSidebar() - Shows current folder details when nothing is selected; when at root base-level it also shows root access list.
245- openRenameModal(item) - Renames files/folders; for roots this writes display alias via /api/files/root-alias and reloads roots.
246- openRenameModal(item) - Renames files/folders; for roots this writes display alias via /api/files/root-alias and reloads roots.
246- uploadFiles(fileList) - Uploads files one-by-one with per-file conflict prompt (overwrite/rename/cancel) when names already exist.
247- uploadFiles(fileList) - Uploads files one-by-one with per-file conflict prompt (overwrite/rename/cancel) when names already exist.
247- openUserModal(user, paths) - Admin user editor; enforces UI permission rule that Write is disabled/unchecked while Read is unchecked.
248- openUserModal(user, paths) - Admin user editor; enforces UI permission rule that Write is disabled/unchecked while Read is unchecked and disables the Admin checkbox when editing the current signed-in admin account.
248
249
249## public/drag-move.js
250## public/drag-move.js
250- makeDraggable(el, item) - Attaches pointerdown to a .file-item; starts drag after threshold movement. Depends on: api(), canWrite(), navigateTo(), STATE, closeSidebar(), toast() from app.js.
251- makeDraggable(el, item) - Attaches pointerdown to a .file-item; starts drag after threshold movement. Depends on: api(), canWrite(), navigateTo(), STATE, closeSidebar(), toast() from app.js.
public/app.js +8 −2
@@ -5219,7 +5219,14 @@ function openUserModal (user, paths)
5219 5219 usernameInput.readOnly = !!user;
5220 5220
5221 5221 document.getElementById('user-password').value = '';
5222 document.getElementById('user-is-admin').checked = user ? user.isAdmin : false;
5222 const adminChk = document.getElementById('user-is-admin');
5223 const editingOwnAccount = !!(user
5224 && String(user.username || '').toLowerCase() === String(STATE.username || '').toLowerCase());
5225 adminChk.checked = user ? user.isAdmin : false;
5226 adminChk.disabled = editingOwnAccount;
5227 adminChk.title = editingOwnAccount
5228 ? 'You cannot remove admin privileges from your own account.'
5229 : '';
5223 5230
5224 5231 // Password placeholder
5225 5232 document.getElementById('user-password').placeholder =
@@ -5236,7 +5243,6 @@ function openUserModal (user, paths)
5236 5243
5237 5244 const isAdminUser = !!(user && user.isAdmin);
5238 5245 // Mirror the is-admin checkbox so toggling it live-updates the perm rows.
5239 const adminChk = document.getElementById('user-is-admin');
5240 5246 const rebuildPermRows = (admin) =>
5241 5247 {
5242 5248 permDiv.querySelectorAll('.perm-row').forEach(r =>
@@ -5219,7 +5219,14 @@ function openUserModal (user, paths)
5219 usernameInput.readOnly = !!user;
5219 usernameInput.readOnly = !!user;
5220
5220
5221 document.getElementById('user-password').value = '';
5221 document.getElementById('user-password').value = '';
5222 document.getElementById('user-is-admin').checked = user ? user.isAdmin : false;
5222 const adminChk = document.getElementById('user-is-admin');
5223 const editingOwnAccount = !!(user
5224 && String(user.username || '').toLowerCase() === String(STATE.username || '').toLowerCase());
5225 adminChk.checked = user ? user.isAdmin : false;
5226 adminChk.disabled = editingOwnAccount;
5227 adminChk.title = editingOwnAccount
5228 ? 'You cannot remove admin privileges from your own account.'
5229 : '';
5223
5230
5224 // Password placeholder
5231 // Password placeholder
5225 document.getElementById('user-password').placeholder =
5232 document.getElementById('user-password').placeholder =
@@ -5236,7 +5243,6 @@ function openUserModal (user, paths)
5236
5243
5237 const isAdminUser = !!(user && user.isAdmin);
5244 const isAdminUser = !!(user && user.isAdmin);
5238 // Mirror the is-admin checkbox so toggling it live-updates the perm rows.
5245 // Mirror the is-admin checkbox so toggling it live-updates the perm rows.
5239 const adminChk = document.getElementById('user-is-admin');
5240 const rebuildPermRows = (admin) =>
5246 const rebuildPermRows = (admin) =>
5241 {
5247 {
5242 permDiv.querySelectorAll('.perm-row').forEach(r =>
5248 permDiv.querySelectorAll('.perm-row').forEach(r =>
server/routes/admin.js +6 −0
@@ -134,6 +134,12 @@ router.patch('/users/:id', (req, res) =>
134 134
135 135 const { password, isAdmin, permissions } = req.body;
136 136
137 // Admins may not remove admin privileges from their own account.
138 if ((isAdmin === false || isAdmin === 0) && String(user.id) === String(req.user.id))
139 {
140 return res.status(400).json({ error: 'Cannot remove your own admin privileges.' });
141 }
142
137 143 // Prevent removing the last admin
138 144 if (isAdmin === false || isAdmin === 0)
139 145 {
@@ -134,6 +134,12 @@ router.patch('/users/:id', (req, res) =>
134
134
135 const { password, isAdmin, permissions } = req.body;
135 const { password, isAdmin, permissions } = req.body;
136
136
137 // Admins may not remove admin privileges from their own account.
138 if ((isAdmin === false || isAdmin === 0) && String(user.id) === String(req.user.id))
139 {
140 return res.status(400).json({ error: 'Cannot remove your own admin privileges.' });
141 }
142
137 // Prevent removing the last admin
143 // Prevent removing the last admin
138 if (isAdmin === false || isAdmin === 0)
144 if (isAdmin === false || isAdmin === 0)
139 {
145 {
tests/admin.routes.test.js +14 −0
@@ -246,6 +246,20 @@ describe('PATCH /api/admin/users/:id', () =>
246 246 const bob = body.users.find(u => u.id === bobId);
247 247 expect(bob.permissions).toMatchObject({ data: { read: true, write: true } });
248 248 });
249
250 test('rejects removing own admin privileges', async () =>
251 {
252 const { status, body } = await req('PATCH', `/api/admin/users/${mockUser.id}`, {
253 isAdmin: false,
254 });
255 expect(status).toBe(400);
256 expect(body.error).toMatch(/own admin privileges/i);
257
258 const usersRes = await req('GET', '/api/admin/users');
259 const me = usersRes.body.users.find(u => String(u.id) === String(mockUser.id));
260 expect(me).toBeTruthy();
261 expect(me.isAdmin).toBe(true);
262 });
249 263 });
250 264
251 265 describe('DELETE /api/admin/users/:id', () =>
@@ -246,6 +246,20 @@ describe('PATCH /api/admin/users/:id', () =>
246 const bob = body.users.find(u => u.id === bobId);
246 const bob = body.users.find(u => u.id === bobId);
247 expect(bob.permissions).toMatchObject({ data: { read: true, write: true } });
247 expect(bob.permissions).toMatchObject({ data: { read: true, write: true } });
248 });
248 });
249
250 test('rejects removing own admin privileges', async () =>
251 {
252 const { status, body } = await req('PATCH', `/api/admin/users/${mockUser.id}`, {
253 isAdmin: false,
254 });
255 expect(status).toBe(400);
256 expect(body.error).toMatch(/own admin privileges/i);
257
258 const usersRes = await req('GET', '/api/admin/users');
259 const me = usersRes.body.users.find(u => String(u.id) === String(mockUser.id));
260 expect(me).toBeTruthy();
261 expect(me.isAdmin).toBe(true);
262 });
249});
263});
250
264
251describe('DELETE /api/admin/users/:id', () =>
265describe('DELETE /api/admin/users/:id', () =>
tests/page.load.test.js +34 −0
@@ -884,6 +884,40 @@ describe('page load smoke test', () =>
884 884 dom.window.close();
885 885 });
886 886
887 test('admin user modal disables admin checkbox when editing own account', async () =>
888 {
889 meFixture = { username: 'admin', isAdmin: true, permissions: {} };
890 adminConfigFixture = { port: 3000, paths: ['/data'], effectivePaths: ['/data'] };
891 adminUsersFixture = {
892 users: [
893 {
894 id: 1,
895 username: 'admin',
896 isAdmin: true,
897 permissions: {},
898 },
899 ],
900 };
901
902 const { dom, runtimeErrors } = await bootClient();
903 expect(runtimeErrors).toEqual([]);
904
905 dom.window.document.getElementById('btn-admin').dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true }));
906 await new Promise(resolve => dom.window.setTimeout(resolve, 80));
907
908 const editBtn = dom.window.document.querySelector('#admin-users-list [data-action="edit"]');
909 expect(editBtn).toBeTruthy();
910 editBtn.dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true }));
911 await new Promise(resolve => dom.window.setTimeout(resolve, 40));
912
913 const adminCb = dom.window.document.getElementById('user-is-admin');
914 expect(adminCb).toBeTruthy();
915 expect(adminCb.checked).toBe(true);
916 expect(adminCb.disabled).toBe(true);
917
918 dom.window.close();
919 });
920
887 921 test('admin topbar shows update badge after login when updates are available', async () =>
888 922 {
889 923 meFixture = { username: 'admin', isAdmin: true, permissions: {} };
@@ -884,6 +884,40 @@ describe('page load smoke test', () =>
884 dom.window.close();
884 dom.window.close();
885 });
885 });
886
886
887 test('admin user modal disables admin checkbox when editing own account', async () =>
888 {
889 meFixture = { username: 'admin', isAdmin: true, permissions: {} };
890 adminConfigFixture = { port: 3000, paths: ['/data'], effectivePaths: ['/data'] };
891 adminUsersFixture = {
892 users: [
893 {
894 id: 1,
895 username: 'admin',
896 isAdmin: true,
897 permissions: {},
898 },
899 ],
900 };
901
902 const { dom, runtimeErrors } = await bootClient();
903 expect(runtimeErrors).toEqual([]);
904
905 dom.window.document.getElementById('btn-admin').dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true }));
906 await new Promise(resolve => dom.window.setTimeout(resolve, 80));
907
908 const editBtn = dom.window.document.querySelector('#admin-users-list [data-action="edit"]');
909 expect(editBtn).toBeTruthy();
910 editBtn.dispatchEvent(new dom.window.MouseEvent('click', { bubbles: true }));
911 await new Promise(resolve => dom.window.setTimeout(resolve, 40));
912
913 const adminCb = dom.window.document.getElementById('user-is-admin');
914 expect(adminCb).toBeTruthy();
915 expect(adminCb.checked).toBe(true);
916 expect(adminCb.disabled).toBe(true);
917
918 dom.window.close();
919 });
920
887 test('admin topbar shows update badge after login when updates are available', async () =>
921 test('admin topbar shows update badge after login when updates are available', async () =>
888 {
922 {
889 meFixture = { username: 'admin', isAdmin: true, permissions: {} };
923 meFixture = { username: 'admin', isAdmin: true, permissions: {} };