mirror of
https://github.com/immich-app/immich.git
synced 2026-05-27 10:02:31 -04:00
c0898b96ca
* refactor(server)!: sanitize error messages to avoid leaking resource and permission details * fix e2e tests * fix(server): prevent login timing oracle by always running bcrypt Always call compareBcrypt in the login path regardless of whether the email is registered. When no user is found, a dummy hash is used so the bcrypt KDF still runs and response latency is constant, making it impossible to enumerate valid email addresses by measuring response time. * fix(server): collapse OAuth callback messages to prevent email-existence oracle Two distinct error messages in the OAuth callback endpoint revealed whether an email address was already registered in the database. An attacker controlling the OAuth provider's email claim could probe the user table without authentication. Both cases now return the same generic message. * fix(server): replace email-in-use messages to prevent user-existence oracle Error messages on registration and profile-update that named whether an email address was already taken allowed callers to enumerate registered accounts. All three sites now return the same generic message regardless of whether the address is in use. * fix(server): hide slug uniqueness constraint to prevent shared-link probe Surfacing the Postgres unique-constraint name in the error response let any authenticated user brute-force whether a custom slug was already in use by another user's shared link, leaking the existence of other links. * fix(server): unify profile image errors to prevent user-existence oracle via status code GET /users/:id/profile-image returned HTTP 400 for an unknown user ID but HTTP 404 when the user existed without a photo, letting callers distinguish the two cases. Both now return 404 so the response is identical regardless of whether the UUID maps to an account. * fix(server): replace album user-not-found message to prevent UUID-existence oracle Album owners could probe arbitrary UUIDs via the add-user endpoint and determine whether they belonged to registered accounts by receiving 'User not found'. The message is now ambiguous about whether the ID was unrecognised or the user is inactive. * Revert "fix e2e tests" This reverts commit c1bd7a116b3f0fccf3d2530c8e34b13c1d862989. * Revert "refactor(server)!: sanitize error messages to avoid leaking resource and permission details" This reverts commit b96421a08387340fbb77913ca89b0717bcd9945d. * fix(server): use 403 instead of 400 for access-denied errors requireAccess threw BadRequestException which is incorrect HTTP semantics. Access denial is a client authorization problem (403 Forbidden), not a malformed request (400 Bad Request). Keep the descriptive permission name in the message since the full permission set is public API surface. * Revert "fix(server): use 403 instead of 400 for access-denied errors" This reverts commit bb069909571f4e514e7d050ddf588c017ee5a029. * shorten comment * add log messages * format * one more
299 lines
11 KiB
TypeScript
299 lines
11 KiB
TypeScript
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
|
import { Updateable } from 'kysely';
|
|
import { DateTime } from 'luxon';
|
|
import { SALT_ROUNDS } from 'src/constants';
|
|
import { StorageCore } from 'src/cores/storage.core';
|
|
import { OnJob } from 'src/decorators';
|
|
import { AuthDto } from 'src/dtos/auth.dto';
|
|
import { LicenseKeyDto, LicenseResponseDto } from 'src/dtos/license.dto';
|
|
import { OnboardingDto, OnboardingResponseDto } from 'src/dtos/onboarding.dto';
|
|
import { UserPreferencesResponseDto, UserPreferencesUpdateDto, mapPreferences } from 'src/dtos/user-preferences.dto';
|
|
import { CreateProfileImageResponseDto } from 'src/dtos/user-profile.dto';
|
|
import { UserAdminResponseDto, UserResponseDto, UserUpdateMeDto, mapUser, mapUserAdmin } from 'src/dtos/user.dto';
|
|
import { CacheControl, JobName, JobStatus, QueueName, StorageFolder, UserMetadataKey } from 'src/enum';
|
|
import { UserFindOptions } from 'src/repositories/user.repository';
|
|
import { UserTable } from 'src/schema/tables/user.table';
|
|
import { BaseService } from 'src/services/base.service';
|
|
import { JobOf, UserMetadataItem } from 'src/types';
|
|
import { ImmichFileResponse } from 'src/utils/file';
|
|
import { mimeTypes } from 'src/utils/mime-types';
|
|
import { getPreferences, getPreferencesPartial, mergePreferences } from 'src/utils/preferences';
|
|
import { generateProfileImage } from 'src/utils/profile-image';
|
|
|
|
@Injectable()
|
|
export class UserService extends BaseService {
|
|
async search(auth: AuthDto): Promise<UserResponseDto[]> {
|
|
const config = await this.getConfig({ withCache: false });
|
|
|
|
let users;
|
|
if (auth.user.isAdmin || config.server.publicUsers) {
|
|
users = await this.userRepository.getList({ withDeleted: false });
|
|
} else {
|
|
const authUser = await this.userRepository.get(auth.user.id, {});
|
|
users = authUser ? [authUser] : [];
|
|
}
|
|
|
|
return users.map((user) => mapUser(user));
|
|
}
|
|
|
|
async getMe(auth: AuthDto): Promise<UserAdminResponseDto> {
|
|
const user = await this.userRepository.get(auth.user.id, {});
|
|
if (!user) {
|
|
throw new BadRequestException('User not found');
|
|
}
|
|
|
|
return mapUserAdmin(user);
|
|
}
|
|
|
|
async updateMe({ user }: AuthDto, dto: UserUpdateMeDto): Promise<UserAdminResponseDto> {
|
|
if (dto.email) {
|
|
const duplicate = await this.userRepository.getByEmail(dto.email);
|
|
if (duplicate && duplicate.id !== user.id) {
|
|
this.logger.warn('Email already in use by another account');
|
|
throw new BadRequestException('Email is not available');
|
|
}
|
|
}
|
|
|
|
const update: Updateable<UserTable> = {
|
|
email: dto.email,
|
|
name: dto.name,
|
|
avatarColor: dto.avatarColor,
|
|
};
|
|
|
|
if (dto.password) {
|
|
const hashedPassword = await this.cryptoRepository.hashBcrypt(dto.password, SALT_ROUNDS);
|
|
update.password = hashedPassword;
|
|
update.shouldChangePassword = false;
|
|
}
|
|
|
|
const updatedUser = await this.userRepository.update(user.id, update);
|
|
|
|
return mapUserAdmin(updatedUser);
|
|
}
|
|
|
|
async getMyPreferences(auth: AuthDto): Promise<UserPreferencesResponseDto> {
|
|
const metadata = await this.userRepository.getMetadata(auth.user.id);
|
|
return mapPreferences(getPreferences(metadata));
|
|
}
|
|
|
|
async updateMyPreferences(auth: AuthDto, dto: UserPreferencesUpdateDto) {
|
|
const metadata = await this.userRepository.getMetadata(auth.user.id);
|
|
const updated = mergePreferences(getPreferences(metadata), dto);
|
|
|
|
await this.userRepository.upsertMetadata(auth.user.id, {
|
|
key: UserMetadataKey.Preferences,
|
|
value: getPreferencesPartial(updated),
|
|
});
|
|
|
|
return mapPreferences(updated);
|
|
}
|
|
|
|
async get(id: string): Promise<UserResponseDto> {
|
|
const user = await this.findOrFail(id, { withDeleted: false });
|
|
return mapUser(user);
|
|
}
|
|
|
|
async createProfileImage(auth: AuthDto, file: Express.Multer.File): Promise<CreateProfileImageResponseDto> {
|
|
const { profileImagePath: oldPath } = await this.findOrFail(auth.user.id, { withDeleted: false });
|
|
|
|
let profileImagePath: string;
|
|
try {
|
|
const config = await this.getConfig({ withCache: true });
|
|
profileImagePath = await generateProfileImage(
|
|
{ media: this.mediaRepository, crypto: this.cryptoRepository, storageCore: this.storageCore },
|
|
config,
|
|
auth.user.id,
|
|
file.path,
|
|
);
|
|
} catch (error) {
|
|
await this.jobRepository.queue({ name: JobName.FileDelete, data: { files: [file.path] } });
|
|
throw new BadRequestException('Unable to process profile image', { cause: error });
|
|
}
|
|
|
|
const user = await this.userRepository.update(auth.user.id, {
|
|
profileImagePath,
|
|
profileChangedAt: new Date(),
|
|
});
|
|
|
|
const toDelete = [file.path, ...(oldPath ? [oldPath] : [])];
|
|
await this.jobRepository.queue({ name: JobName.FileDelete, data: { files: toDelete } });
|
|
|
|
return {
|
|
userId: user.id,
|
|
profileImagePath: user.profileImagePath,
|
|
profileChangedAt: user.profileChangedAt,
|
|
};
|
|
}
|
|
|
|
async deleteProfileImage(auth: AuthDto): Promise<void> {
|
|
const user = await this.findOrFail(auth.user.id, { withDeleted: false });
|
|
if (user.profileImagePath === '') {
|
|
throw new BadRequestException("Can't delete a missing profile Image");
|
|
}
|
|
await this.userRepository.update(auth.user.id, { profileImagePath: '', profileChangedAt: new Date() });
|
|
await this.jobRepository.queue({ name: JobName.FileDelete, data: { files: [user.profileImagePath] } });
|
|
}
|
|
|
|
async getProfileImage(id: string): Promise<ImmichFileResponse> {
|
|
const user = await this.userRepository.get(id, {});
|
|
if (!user || !user.profileImagePath) {
|
|
this.logger.debug('User or profile image not found');
|
|
throw new NotFoundException();
|
|
}
|
|
|
|
return new ImmichFileResponse({
|
|
path: user.profileImagePath,
|
|
contentType: mimeTypes.lookup(user.profileImagePath),
|
|
cacheControl: CacheControl.None,
|
|
});
|
|
}
|
|
|
|
async getLicense(auth: AuthDto): Promise<LicenseResponseDto> {
|
|
const metadata = await this.userRepository.getMetadata(auth.user.id);
|
|
|
|
const license = metadata.find(
|
|
(item): item is UserMetadataItem<UserMetadataKey.License> => item.key === UserMetadataKey.License,
|
|
);
|
|
if (!license) {
|
|
throw new NotFoundException();
|
|
}
|
|
return { ...license.value, activatedAt: new Date(license.value.activatedAt) };
|
|
}
|
|
|
|
async deleteLicense({ user }: AuthDto): Promise<void> {
|
|
await this.userRepository.deleteMetadata(user.id, UserMetadataKey.License);
|
|
}
|
|
|
|
async setLicense(auth: AuthDto, license: LicenseKeyDto): Promise<LicenseResponseDto> {
|
|
if (!license.licenseKey.startsWith('IMCL-') && !license.licenseKey.startsWith('IMSV-')) {
|
|
throw new BadRequestException('Invalid license key');
|
|
}
|
|
|
|
const { licensePublicKey } = this.configRepository.getEnv();
|
|
|
|
const clientLicenseValid = this.cryptoRepository.verifySha256(
|
|
license.licenseKey,
|
|
license.activationKey,
|
|
licensePublicKey.client,
|
|
);
|
|
|
|
const serverLicenseValid = this.cryptoRepository.verifySha256(
|
|
license.licenseKey,
|
|
license.activationKey,
|
|
licensePublicKey.server,
|
|
);
|
|
|
|
if (!clientLicenseValid && !serverLicenseValid) {
|
|
throw new BadRequestException('Invalid license key');
|
|
}
|
|
|
|
const activatedAt = new Date();
|
|
|
|
await this.userRepository.upsertMetadata(auth.user.id, {
|
|
key: UserMetadataKey.License,
|
|
value: { ...license, activatedAt: activatedAt.toISOString() },
|
|
});
|
|
|
|
return { ...license, activatedAt };
|
|
}
|
|
|
|
async getOnboarding(auth: AuthDto): Promise<OnboardingResponseDto> {
|
|
const metadata = await this.userRepository.getMetadata(auth.user.id);
|
|
|
|
const onboardingData = metadata.find(
|
|
(item): item is UserMetadataItem<UserMetadataKey.Onboarding> => item.key === UserMetadataKey.Onboarding,
|
|
)?.value;
|
|
|
|
if (!onboardingData) {
|
|
return { isOnboarded: false };
|
|
}
|
|
|
|
return {
|
|
isOnboarded: onboardingData.isOnboarded,
|
|
};
|
|
}
|
|
|
|
async deleteOnboarding({ user }: AuthDto): Promise<void> {
|
|
await this.userRepository.deleteMetadata(user.id, UserMetadataKey.Onboarding);
|
|
}
|
|
|
|
async setOnboarding(auth: AuthDto, onboarding: OnboardingDto): Promise<OnboardingResponseDto> {
|
|
await this.userRepository.upsertMetadata(auth.user.id, {
|
|
key: UserMetadataKey.Onboarding,
|
|
value: {
|
|
isOnboarded: onboarding.isOnboarded,
|
|
},
|
|
});
|
|
|
|
return {
|
|
isOnboarded: onboarding.isOnboarded,
|
|
};
|
|
}
|
|
|
|
@OnJob({ name: JobName.UserSyncUsage, queue: QueueName.BackgroundTask })
|
|
async handleUserSyncUsage(): Promise<JobStatus> {
|
|
await this.userRepository.syncUsage();
|
|
return JobStatus.Success;
|
|
}
|
|
|
|
@OnJob({ name: JobName.UserDeleteCheck, queue: QueueName.BackgroundTask })
|
|
async handleUserDeleteCheck(): Promise<JobStatus> {
|
|
const config = await this.getConfig({ withCache: false });
|
|
const users = await this.userRepository.getDeletedAfter(DateTime.now().minus({ days: config.user.deleteDelay }));
|
|
await this.jobRepository.queueAll(users.map((user) => ({ name: JobName.UserDelete, data: { id: user.id } })));
|
|
return JobStatus.Success;
|
|
}
|
|
|
|
@OnJob({ name: JobName.UserDelete, queue: QueueName.BackgroundTask })
|
|
async handleUserDelete({ id, force }: JobOf<JobName.UserDelete>) {
|
|
const config = await this.getConfig({ withCache: false });
|
|
const user = await this.userRepository.get(id, { withDeleted: true });
|
|
if (!user) {
|
|
return;
|
|
}
|
|
|
|
// just for extra protection here
|
|
if (!force && !this.isReadyForDeletion(user, config.user.deleteDelay)) {
|
|
this.logger.warn(`Skipped user that was not ready for deletion: id=${id}`);
|
|
return;
|
|
}
|
|
|
|
this.logger.log(`Deleting user: ${user.id}`);
|
|
|
|
const folders = [
|
|
StorageCore.getLibraryFolder(user),
|
|
StorageCore.getFolderLocation(StorageFolder.Upload, user.id),
|
|
StorageCore.getFolderLocation(StorageFolder.Profile, user.id),
|
|
StorageCore.getFolderLocation(StorageFolder.Thumbnails, user.id),
|
|
StorageCore.getFolderLocation(StorageFolder.EncodedVideo, user.id),
|
|
];
|
|
|
|
for (const folder of folders) {
|
|
this.logger.warn(`Removing user from filesystem: ${folder}`);
|
|
await this.storageRepository.unlinkDir(folder, { recursive: true, force: true });
|
|
}
|
|
|
|
this.logger.warn(`Removing user from database: ${user.id}`);
|
|
await this.albumRepository.deleteAll(user.id);
|
|
await this.userRepository.delete(user, true);
|
|
|
|
await this.eventRepository.emit('UserDelete', user);
|
|
}
|
|
|
|
private isReadyForDeletion(user: { id: string; deletedAt?: Date | null }, deleteDelay: number): boolean {
|
|
if (!user.deletedAt) {
|
|
return false;
|
|
}
|
|
|
|
return DateTime.now().minus({ days: deleteDelay }) > DateTime.fromJSDate(user.deletedAt);
|
|
}
|
|
|
|
private async findOrFail(id: string, options: UserFindOptions) {
|
|
const user = await this.userRepository.get(id, options);
|
|
if (!user) {
|
|
throw new BadRequestException('User not found');
|
|
}
|
|
return user;
|
|
}
|
|
}
|