fix(auth): fail closed when auth prerequisites are missing (#1387) (#1397)

Auth mode resolution fell back to "none" (anonymous full admin) whenever
the configured method's prerequisites were missing: no local password
admin for builtin/OIDC, no Calibre-Web database, a blank proxy header,
an
unrecognized AUTH_METHOD (including "OIDC" in uppercase), or any error
while reading the config. Deleting or demoting the last local admin was
allowed on purpose because of that fallback, which exposed OIDC
instances publicly.

- Only an explicit AUTH_METHOD=none disables authentication. A
configured
  method stays active when its prerequisites are missing, so sign-in
  fails instead of opening up.
- An unrecognized or unreadable AUTH_METHOD resolves to "unavailable",
  which still requires a session and accepts no login. Values are
  normalized, so AUTH_METHOD=OIDC works.
- Restore the guard against deleting or demoting the last local password
  admin while builtin/OIDC is active (unless DISABLE_LOCAL_AUTH is set).
- Require a local admin before enabling Local auth, as OIDC already did.
- Log a recovery hint at startup when builtin/OIDC runs without a local
  admin, and document recovery via AUTH_METHOD=none.
- Drop the "will fall back to No Authentication" UI toasts and hints.



Fixes https://github.com/calibrain/shelfmark/issues/1387
This commit is contained in:
CaliBrain authored and GitHub committed 2026-09-25 18:56:16 -04:00
1 parent c7bfb20448
commit ece6b8f341
13 files changed
+290 -237

No files matched your search

@@ -41,9 +41,6 @@ const getPasswordError = (password: string, passwordConfirm: string) => {
return password === passwordConfirm ? null : 'Passwords do not match';
};
const countLocalPasswordAdmins = (users: AdminUser[]): number =>
users.filter((user) => user.auth_source === 'builtin' && user.role === 'admin').length;
const authSourceLabel: Record<AdminUser['auth_source'], string> = {
builtin: 'Local',
oidc: 'OIDC',
@@ -109,7 +106,6 @@ export const useUserMutations = ({
? getPasswordError(editPassword, editPasswordConfirm)
: null;
if (passwordError) return fail(passwordError);
const localAdminsBeforeSave = includeProfile ? countLocalPasswordAdmins(users) : 0;
const caps = editingUser.edit_capabilities;
const settingsPayload = includeSettings
@@ -152,17 +148,7 @@ export const useUserMutations = ({
: 'User updated',
'success',
);
const refreshedUsers = await fetchUsers({ force: true });
if (
includeProfile &&
localAdminsBeforeSave > 0 &&
countLocalPasswordAdmins(refreshedUsers) === 0
) {
onShowToast?.(
"No local admin accounts remain. Authentication will fall back to 'No Authentication' until a local admin is created.",
'info',
);
}
await fetchUsers({ force: true });
return true;
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to update user';
@@ -174,24 +160,17 @@ export const useUserMutations = ({
const deleteUser = async (userId: number) => {
const deletedUser = users.find((user) => user.id === userId) || null;
const localAdminsBeforeDelete = countLocalPasswordAdmins(users);
setDeletingUserId(userId);
try {
await deleteAdminUser(userId);
onShowToast?.('User deleted', 'success');
const refreshedUsers = await fetchUsers({ force: true });
await fetchUsers({ force: true });
if (deletedUser && deletedUser.auth_source !== 'builtin') {
onShowToast?.(
`${authSourceLabel[deletedUser.auth_source]} users may be re-provisioned by your authentication source on a future login or sync.`,
'info',
);
}
if (localAdminsBeforeDelete > 0 && countLocalPasswordAdmins(refreshedUsers) === 0) {
onShowToast?.(
"No local admin accounts remain. Authentication will fall back to 'No Authentication' until a local admin is created.",
'info',
);
}
return true;
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to delete user';