Skip to content

High performance self-hosted photo and video management solution.

4.1 KB
Raw Blame
import z from 'zod';
import {
  AdminConfigDto,
  PublicConfigDto,
  UserConfigDto,
  defaults,
  mapPublicConfig,
  mapUserConfig,
} from 'src/dtos/config.dto.js';
import { getKeysDeep } from 'src/utils/misc.js';

const PUBLIC_PROPERTIES = [
  'ffmpeg.realtime.enabled',
  'map.darkStyle',
  'map.enabled',
  'map.lightStyle',
  'oauth.autoLaunch',
  'oauth.buttonText',
  'oauth.enabled',
  'passkey.enabled',
  'passwordLogin.enabled',
  'server.loginPageMessage',
];

describe('config visibility', () => {
  it('should expose every property to admins', () => {
    const paths = getKeysDeep(defaults);

    expect(paths).toEqual(expect.arrayContaining(PUBLIC_PROPERTIES));
    expect(paths).toContain('oauth.clientSecret');
    expect(paths.length).toBeGreaterThan(100);
  });

  it('should expose the public properties to everyone', () => {
    expect(getKeysDeep(mapPublicConfig(defaults)).sort()).toEqual(PUBLIC_PROPERTIES);
  });

  it('should expose everything public to logged in users as well', () => {
    expect(getKeysDeep(mapUserConfig(defaults))).toEqual(expect.arrayContaining(PUBLIC_PROPERTIES));
  });

  it('should accept the defaults with the admin schema', () => {
    expect(AdminConfigDto.schema.safeParse(defaults)).toEqual(expect.objectContaining({ success: true }));
  });

  it('should map the defaults onto the user and public schemas', () => {
    expect(UserConfigDto.schema.safeParse(mapUserConfig(defaults))).toEqual(expect.objectContaining({ success: true }));
    expect(PublicConfigDto.schema.safeParse(mapPublicConfig(defaults))).toEqual(
      expect.objectContaining({ success: true }),
    );
  });

  it('should not leak admin properties into the public config', () => {
    const config = mapPublicConfig(defaults) as Record<string, any>;

    expect(config.oauth).toEqual({
      autoLaunch: defaults.oauth.autoLaunch,
      buttonText: defaults.oauth.buttonText,
      enabled: defaults.oauth.enabled,
    });
    expect(config.job).toBeUndefined();
    expect(config.image).toBeUndefined();
    expect(config.notifications).toBeUndefined();
  });

  it('should expose only whether smtp is enabled to users', () => {
    const config = mapUserConfig(defaults) as Record<string, any>;

    expect(config.notifications).toEqual({ smtp: { enabled: defaults.notifications.smtp.enabled } });
  });

  it('should keep the visibility metadata out of the schemas', () => {
    for (const schema of [AdminConfigDto.schema, UserConfigDto.schema, PublicConfigDto.schema]) {
      const json = JSON.stringify(z.toJSONSchema(schema, { io: 'input', unrepresentable: 'any' }));
      expect(json).not.toContain('visibility');
    }
  });
});

const parsePasskey = (passkey: Record<string, unknown>) => AdminConfigDto.schema.safeParse({ ...defaults, passkey });
const passkeyMessages = (passkey: Record<string, unknown>) =>
  parsePasskey(passkey).error?.issues.map((issue) => issue.message);

describe('passkey config', () => {
  it('should allow enabling without a domain', () => {
    expect(parsePasskey({ enabled: true, domain: null, additionalDomains: [] }).success).toBe(true);
  });

  it('should reject domains that are not https', () => {
    expect(passkeyMessages({ enabled: true, domain: 'ftp://immich.example', additionalDomains: [] })).toEqual([
      'Invalid URL',
    ]);
  });

  it('should reject domains with a path', () => {
    expect(
      passkeyMessages({ enabled: true, domain: null, additionalDomains: ['https://immich.tailnet.example/photos'] }),
    ).toEqual(['Additional domain must not include a path, query, or fragment']);
  });

  it('should reject duplicate additional domains', () => {
    expect(
      passkeyMessages({
        enabled: true,
        domain: null,
        additionalDomains: ['https://immich.tailnet.example', 'https://immich.tailnet.example/'],
      }),
    ).toEqual(['Duplicate domain']);
  });

  it('should reject the passkey domain as an additional domain', () => {
    expect(
      passkeyMessages({
        enabled: true,
        domain: 'https://immich.example',
        additionalDomains: ['https://immich.example/'],
      }),
    ).toEqual(['The passkey domain is always allowed and must not be listed as an additional domain']);
  });
});