fix(memories): save the Immich connection and auto_upload in one transaction

PUT /api/integrations/memories/immich/settings saved the connection through
one service call and then wrote auto_upload through a second one from
the controller, each committed on its own. A failure between them left
a new connection with the old upload choice.

saveImmichSettings takes the auto_upload flag and writes it with the
connection (or the disconnect) inside one transaction, after the SSRF
check that does the DNS lookup. An absent or non-boolean flag still
keeps the stored choice, a refused URL still writes nothing, and the
response is unchanged. The controller's write sequence leaves the
lint:tx baseline, and the MemoriesService facade loses the
immichSetAutoUpload pass-through it no longer needs. No MCP tool writes
these settings.
This commit is contained in:
Maurice
2026-10-08 16:17:57 +02:00
parent f40d92a69e
commit 9cebadf67c
9 changed files with 82 additions and 50 deletions
@@ -37,15 +37,14 @@ export class ImmichMemoriesController {
): Promise<void> {
const { immich_url, immich_api_key, auto_upload, allow_insecure_tls } = body;
// Absent stays undefined and leaves the stored choice alone, so an older
// client cannot clear it by saving.
const result = await this.memories.immichSaveSettings(user.id, immich_url, immich_api_key, getClientIp(req), allow_insecure_tls);
// client cannot clear it by saving. auto_upload is written with the
// connection in one transaction, and only when it is a boolean.
const autoUpload = typeof auto_upload === 'boolean' ? auto_upload : undefined;
const result = await this.memories.immichSaveSettings(user.id, immich_url, immich_api_key, getClientIp(req), allow_insecure_tls, autoUpload);
if (!result.success) {
res.status(400).json({ error: result.error });
return;
}
if (typeof auto_upload === 'boolean') {
await this.memories.immichSetAutoUpload(user.id, auto_upload);
}
if (result.warning) {
res.json({ success: true, warning: result.warning });
return;
+31 -21
View File
@@ -11,6 +11,7 @@ import path from 'node:path';
import { MemoriesAccessService } from './memories-access.service';
import { describeFetchFailure, fail, handleServiceResult, isWithinLocalDayRange, pipeAsset, shiftCalendarDay, sortAssetsByTakenAtDesc, type Selection } from './memories.helpers';
import { Users } from '../../db/entities/Users.entity';
import { UnitOfWork } from '../database/unit-of-work';
import type { UsersRepository } from '../../db/repositories/Users.repository';
const ALBUM_PAGE_SIZE = 1000;
@@ -81,6 +82,7 @@ export class ImmichService {
private readonly access: MemoriesAccessService,
private readonly storage: StorageService,
@InjectRepository(Users) private readonly users: UsersRepository,
private readonly uow: UnitOfWork,
) {}
async getImmichCredentials(userId: number): Promise<ImmichCreds | null> {
@@ -138,7 +140,9 @@ export class ImmichService {
* `allowInsecureTls` left undefined keeps the stored choice while the URL
* stays the same, so a client that does not know the switch cannot clear it
* by saving. The switch trusts one server, so a new URL without it starts
* off, and disconnecting (no URL) always turns it off again.
* off, and disconnecting (no URL) always turns it off again. `autoUpload`
* is written with the connection in one transaction, after the URL check;
* left undefined it keeps the stored choice.
*/
async saveImmichSettings(
userId: number,
@@ -146,32 +150,38 @@ export class ImmichService {
immichApiKey: string | undefined,
clientIp: string | null,
allowInsecureTls?: boolean,
autoUpload?: boolean,
): Promise<{ success: boolean; warning?: string; error?: string }> {
let ssrf: Awaited<ReturnType<typeof checkSsrf>> | null = null;
const url = (immichUrl?.endsWith('/') ? immichUrl.slice(0, -1) : immichUrl ?? '').trim();
if (immichUrl) {
if (immichUrl.endsWith('/')) {
immichUrl = immichUrl.slice(0, -1);
}
const ssrf = await checkSsrf(immichUrl.trim());
ssrf = await checkSsrf(url);
if (!ssrf.allowed) {
return { success: false, error: `Invalid Immich URL: ${ssrf.error}` };
}
const insecure = allowInsecureTls === undefined ? null : Number(allowInsecureTls);
// The stored URL decides whether an undefined switch keeps its value (IM4).
await this.users.setImmichSettings(userId, immichUrl.trim(), maybe_encrypt_api_key(immichApiKey), insecure);
if (ssrf.isPrivate) {
await this.audit.writeAudit({
userId,
action: 'immich.private_ip_configured',
ip: clientIp,
details: { immich_url: immichUrl.trim(), resolved_ip: ssrf.resolvedIp },
});
return {
success: true,
warning: `Immich URL resolves to a private IP address (${ssrf.resolvedIp}). Make sure this is intentional.`,
};
}
// The DNS check above is network I/O, so it runs before the transaction opens.
await this.uow.transactional(async () => {
if (immichUrl) {
const insecure = allowInsecureTls === undefined ? null : Number(allowInsecureTls);
// The stored URL decides whether an undefined switch keeps its value (IM4).
await this.users.setImmichSettings(userId, url, maybe_encrypt_api_key(immichApiKey), insecure);
} else {
await this.users.clearImmichSettings(userId, maybe_encrypt_api_key(immichApiKey));
}
} else {
await this.users.clearImmichSettings(userId, maybe_encrypt_api_key(immichApiKey));
if (typeof autoUpload === 'boolean') await this.setImmichAutoUpload(userId, autoUpload);
});
if (ssrf?.isPrivate) {
await this.audit.writeAudit({
userId,
action: 'immich.private_ip_configured',
ip: clientIp,
details: { immich_url: url, resolved_ip: ssrf.resolvedIp },
});
return {
success: true,
warning: `Immich URL resolves to a private IP address (${ssrf.resolvedIp}). Make sure this is intentional.`,
};
}
return { success: true };
}
+2 -6
View File
@@ -67,12 +67,8 @@ export class MemoriesService {
return this.immich.getConnectionSettings(userId);
}
immichSaveSettings(userId: number, immichUrl: string | undefined, immichApiKey: string | undefined, clientIp: string | null, allowInsecureTls?: boolean) {
return this.immich.saveImmichSettings(userId, immichUrl, immichApiKey, clientIp, allowInsecureTls);
}
immichSetAutoUpload(userId: number, enabled: boolean): Promise<void> {
return this.immich.setImmichAutoUpload(userId, enabled);
immichSaveSettings(userId: number, immichUrl: string | undefined, immichApiKey: string | undefined, clientIp: string | null, allowInsecureTls?: boolean, autoUpload?: boolean) {
return this.immich.saveImmichSettings(userId, immichUrl, immichApiKey, clientIp, allowInsecureTls, autoUpload);
}
immichGetConnectionStatus(userId: number) {