harden authentication and authorization on the user API
This commit is contained in:
5 files changed
+185
-30
No files matched your search
@@ -82,6 +82,7 @@ export const GET: RequestHandler = async ({ cookies }) => {
|
||||
* resp-201-desc: The first user ever created (or every user in Free edition) automatically gets the Admin role
|
||||
* resp-201-example: {"id":1,"username":"jdoe","email":"jdoe@example.com","isAdmin":true}
|
||||
* resp-400: Missing username/password, or password shorter than 8 characters
|
||||
* resp-401: Authentication required (auth is enabled and the caller is not authenticated; the first admin during initial setup is exempt)
|
||||
* resp-403: Permission denied (RBAC 'users:create' missing — only applies once an admin already exists)
|
||||
* resp-409: Username already exists
|
||||
* resp-500: Unexpected error while creating the user
|
||||
@@ -90,9 +91,13 @@ export const POST: RequestHandler = async (event) => {
|
||||
const { request, cookies } = event;
|
||||
const auth = await authorize(cookies);
|
||||
|
||||
// When auth is enabled and user is logged in, check they can manage users
|
||||
// (allow if no user logged in for initial setup when no users exist)
|
||||
if (auth.authEnabled && auth.isAuthenticated && !await auth.canManageUsers()) {
|
||||
// When auth is enabled, a caller must be authenticated and allowed to manage users.
|
||||
// Initial setup runs with auth disabled (the first admin is created then), so that
|
||||
// path is unaffected.
|
||||
if (auth.authEnabled && !auth.isAuthenticated) {
|
||||
return json({ error: 'Authentication required' }, { status: 401 });
|
||||
}
|
||||
if (auth.authEnabled && !await auth.canManageUsers()) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
@@ -111,7 +116,7 @@ export const POST: RequestHandler = async (event) => {
|
||||
// Hash password
|
||||
const passwordHash = await hashPassword(password);
|
||||
|
||||
// Check if this is the first user
|
||||
// The first user (no Admin-role user yet) is auto-assigned the Admin role below.
|
||||
const isFirstUser = !(await hasAdminUser());
|
||||
|
||||
// Create user
|
||||
|
||||
@@ -81,6 +81,7 @@ export const GET: RequestHandler = async ({ params, cookies }) => {
|
||||
* body-example: {"displayName":"Jane Doe","email":"jane@example.com"}
|
||||
* resp-200: {id:integer!, username:string!, email:string, displayName:string, mfaEnabled:boolean!, isAdmin:boolean!, isActive:boolean!, lastLogin:string, createdAt:string!, updatedAt:string!}
|
||||
* resp-400: User id is required, or the new password is shorter than 8 characters
|
||||
* resp-401: Authentication required (auth is enabled and the caller is not authenticated)
|
||||
* resp-403: Permission denied (editing another user without users:edit)
|
||||
* resp-404: User not found
|
||||
* resp-409: This is the last admin user (confirmDisableAuth required), or the username already exists
|
||||
@@ -96,10 +97,20 @@ export const PUT: RequestHandler = async (event) => {
|
||||
|
||||
const userId = parseInt(params.id);
|
||||
|
||||
// Allow users to edit their own profile, otherwise check permission
|
||||
// (free edition allows all, enterprise checks RBAC)
|
||||
if (auth.user && auth.user.id !== userId && !await auth.can('users', 'edit')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
// Require an authenticated caller when auth is enabled.
|
||||
if (auth.authEnabled && !auth.isAuthenticated) {
|
||||
return json({ error: 'Authentication required' }, { status: 401 });
|
||||
}
|
||||
|
||||
// With auth enabled: allow a user to edit their own profile, otherwise require the
|
||||
// users-edit permission. Written fail-closed - editing another account is denied
|
||||
// unless there is a caller who either is that user or holds the permission - so it
|
||||
// does not depend on the 401 above having run first.
|
||||
if (auth.authEnabled) {
|
||||
const isSelfEdit = auth.user?.id === userId;
|
||||
if (!isSelfEdit && !await auth.can('users', 'edit')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -283,6 +294,7 @@ export const PUT: RequestHandler = async (event) => {
|
||||
* query: confirmDisableAuth:boolean Set to true to confirm deleting the last admin (which disables authentication)
|
||||
* resp-200: {success:boolean!, authDisabled:boolean}
|
||||
* resp-400: User id is required
|
||||
* resp-401: Authentication required (auth is enabled and the caller is not authenticated)
|
||||
* resp-403: Permission denied (missing users:remove)
|
||||
* resp-404: User not found
|
||||
* resp-409: This is the last admin user — pass confirmDisableAuth=true to proceed
|
||||
@@ -292,6 +304,11 @@ export const DELETE: RequestHandler = async (event) => {
|
||||
const { params, url, cookies } = event;
|
||||
const auth = await authorize(cookies);
|
||||
|
||||
// Require an authenticated caller when auth is enabled, before the permission check.
|
||||
if (auth.authEnabled && !auth.isAuthenticated) {
|
||||
return json({ error: 'Authentication required' }, { status: 401 });
|
||||
}
|
||||
|
||||
// When auth is enabled, check permission (free edition allows all, enterprise checks RBAC)
|
||||
if (auth.authEnabled && auth.isAuthenticated && !await auth.can('users', 'remove')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
|
||||
Reference in new issue
Block a user