Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

34.5 KB
Raw Blame
import express from 'express';
import jwt from 'jsonwebtoken';
import request from 'supertest';
import { Types } from 'mongoose';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { gzipSync } from 'node:zlib';
import { createHash } from 'node:crypto';
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
import type { Express, NextFunction, Request, RequestHandler, Response } from 'express';
import type { ImageAuthorizationDeps } from './authorization';
import { createImageAuthorizationMiddleware } from './authorization';
import staticCache from '~/api/server/utils/staticCache';

const VIEWER_ID = '65cfb246f7ecadb8b1e8036b';
const OWNER_ID = '65cfb246f7ecadb8b1e8036c';
const VICTIM_ID = '65cfb246f7ecadb8b1e8036f';
const AGENT_DB_ID = '65cfb246f7ecadb8b1e8036d';
const AGENT_PATH = `/images/${OWNER_ID}/agent-agent_abc123-avatar-12345.png`;
const USER_AVATAR_PATH = `/images/${OWNER_ID}/avatar-12345.png`;

function createResponse(): Response {
  const response = {
    locals: {},
    status: jest.fn().mockReturnThis(),
    send: jest.fn().mockReturnThis(),
  };
  return response as unknown as Response;
}

function createDeps(): ImageAuthorizationDeps {
  return {
    parseCookies: (header: string) =>
      Object.fromEntries(
        header.split(';').map((part: string) => {
          const [key, ...value] = part.trim().split('=');
          return [key, value.join('=')];
        }),
      ),
    isOpenIdReuseEnabled: jest.fn().mockReturnValue(false),
    getBasePath: jest.fn().mockReturnValue(''),
    findSession: jest.fn().mockResolvedValue({ _id: 'active-session' }),
    getUserById: jest.fn().mockResolvedValue({
      role: 'USER',
      tenantId: 'tenant-a',
      idOnTheSource: null,
    }),
    getAgent: jest.fn().mockResolvedValue(null),
    getAssistant: jest.fn().mockResolvedValue(null),
    getUserPrincipals: jest
      .fn()
      .mockResolvedValue([{ principalType: 'user', principalId: VIEWER_ID }]),
    hasCapabilityForPrincipals: jest.fn().mockResolvedValue(false),
    hasPermission: jest.fn().mockResolvedValue(false),
  } as unknown as ImageAuthorizationDeps;
}

function createRequest(path: string, cookie?: string): Request {
  return {
    originalUrl: path,
    headers: cookie ? { cookie } : {},
  } as unknown as Request;
}

function signUser(userId: string): string {
  return jwt.sign({ id: userId }, process.env.JWT_REFRESH_SECRET as string, { expiresIn: '1h' });
}

function signOpenIdUser(userId: string, refreshToken: string): string {
  return jwt.sign(
    {
      id: userId,
      refreshTokenHash: createHash('sha256').update(refreshToken).digest('base64url'),
    },
    process.env.JWT_REFRESH_SECRET as string,
    { expiresIn: '1h' },
  );
}

describe('createImageAuthorizationMiddleware', () => {
  const originalEnforcement = process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION;
  afterAll(() => {
    if (originalEnforcement === undefined) {
      delete process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION;
    } else {
      process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION = originalEnforcement;
    }
  });
  let deps: ImageAuthorizationDeps;
  let response: Response;
  let next: jest.MockedFunction<NextFunction>;

  beforeEach(() => {
    process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION = 'false';
    process.env.JWT_REFRESH_SECRET = 'image-authorization-secret';
    deps = createDeps();
    response = createResponse();
    next = jest.fn();
  });

  it('authenticates an active session and allows the owner path', async () => {
    const token = signUser(VIEWER_ID);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(
      createRequest(`/images/${VIEWER_ID}/profile.png`, `refreshToken=${token}`),
      response,
      next,
    );

    expect(next).toHaveBeenCalledTimes(1);
    expect(deps.findSession).toHaveBeenCalledWith({ userId: VIEWER_ID, refreshToken: token });
  });

  describe('required enrollment and credential retirement', () => {
    const originalEnforcement = process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION;

    afterEach(() => {
      if (originalEnforcement === undefined) {
        delete process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION;
      } else {
        process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION = originalEnforcement;
      }
    });

    it.each(['local', 'ldap', undefined])(
      'refuses an unenrolled %s viewer without an extra owner read',
      async (provider) => {
        process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION = 'true';
        (deps.getUserById as jest.Mock).mockResolvedValue({ provider, twoFactorEnabled: false });
        const middleware = createImageAuthorizationMiddleware({}, deps);

        await middleware(
          createRequest(`/images/${VIEWER_ID}/private.png`, `refreshToken=${signUser(VIEWER_ID)}`),
          response,
          next,
        );

        expect(next).not.toHaveBeenCalled();
        expect(response.status).toHaveBeenCalledWith(403);
        expect(deps.getUserById).toHaveBeenCalledTimes(1);
      },
    );

    it.each(['local', 'openid', 'legacy-openid'])(
      'dates a %s credential against both account cutoffs',
      async (kind) => {
        const issuedAtMs = Date.now();
        const refreshToken =
          kind === 'local'
            ? jwt.sign({ id: VIEWER_ID, issuedAtMs }, process.env.JWT_REFRESH_SECRET as string, {
                expiresIn: '1h',
              })
            : 'provider-refresh';
        const marker = jwt.sign(
          {
            id: VIEWER_ID,
            issuedAtMs,
            ...(kind === 'openid'
              ? { refreshTokenHash: createHash('sha256').update(refreshToken).digest('base64url') }
              : {}),
          },
          process.env.JWT_REFRESH_SECRET as string,
          { expiresIn: '1h' },
        );
        (deps.isOpenIdReuseEnabled as jest.Mock).mockReturnValue(kind !== 'local');
        const cookie =
          kind === 'local'
            ? `refreshToken=${refreshToken}`
            : `refreshToken=${refreshToken}; token_provider=openid; openid_user_id=${marker}`;
        const req = Object.assign(createRequest(`/images/${VIEWER_ID}/private.png`, cookie), {
          session: { openidTokens: { refreshToken } },
        });
        const middleware = createImageAuthorizationMiddleware({}, deps);

        for (const field of ['twoFactorEnrolledAt', 'credentialsChangedAt']) {
          (deps.getUserById as jest.Mock).mockResolvedValue({
            [field]: new Date(issuedAtMs + 1),
            twoFactorEnabled: true,
          });
          await middleware(req, response, next);
          expect(next).not.toHaveBeenCalled();
          expect(response.status).toHaveBeenCalledWith(403);
        }
        (deps.getUserById as jest.Mock).mockResolvedValue({
          twoFactorEnrolledAt: new Date(issuedAtMs - 1),
          credentialsChangedAt: new Date(issuedAtMs - 1),
          twoFactorEnabled: true,
        });
        await middleware(req, response, next);
        expect(next).toHaveBeenCalledTimes(1);
      },
    );

    it('checks the authenticated viewer rather than the image owner and preserves public access', async () => {
      process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION = 'true';
      (deps.getUserById as jest.Mock).mockImplementation(async (id: string) => ({
        role: 'USER',
        tenantId: 'tenant-a',
        provider: 'local',
        twoFactorEnabled: id === OWNER_ID,
      }));
      (deps.getAgent as jest.Mock).mockResolvedValue({ _id: AGENT_DB_ID });
      (deps.hasPermission as jest.Mock).mockResolvedValue(false);
      const middleware = createImageAuthorizationMiddleware({}, deps);
      const req = createRequest(AGENT_PATH, `refreshToken=${signUser(VIEWER_ID)}`);
      await middleware(req, response, next);
      expect(next).not.toHaveBeenCalled();
      expect(deps.getUserPrincipals).not.toHaveBeenCalled();
      expect(deps.getUserById).toHaveBeenCalledTimes(2);

      (deps.hasPermission as jest.Mock).mockResolvedValue(true);
      await middleware(req, response, next);
      expect(next).toHaveBeenCalledTimes(1);
      expect(deps.hasPermission).toHaveBeenLastCalledWith(
        [{ principalType: 'public' }],
        expect.anything(),
        AGENT_DB_ID,
        expect.anything(),
      );
    });

    it('keeps federated users exempt from the enrollment policy', async () => {
      process.env.ENFORCE_TWO_FACTOR_AUTHENTICATION = 'true';
      (deps.getUserById as jest.Mock).mockResolvedValue({
        provider: 'openid',
        twoFactorEnabled: false,
      });
      const middleware = createImageAuthorizationMiddleware({}, deps);
      await middleware(
        createRequest(`/images/${VIEWER_ID}/private.png`, `refreshToken=${signUser(VIEWER_ID)}`),
        response,
        next,
      );
      expect(next).toHaveBeenCalledTimes(1);
    });
  });

  it('rejects a signed refresh token whose session was revoked', async () => {
    (deps.findSession as jest.Mock).mockResolvedValue(null);
    const token = signUser(VIEWER_ID);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(
      createRequest(`/images/${VIEWER_ID}/profile.png`, `refreshToken=${token}`),
      response,
      next,
    );

    expect(next).not.toHaveBeenCalled();
    expect(response.status).toHaveBeenCalledWith(403);
  });

  it('authenticates a refresh-bound OpenID cookie after the Express session expires', async () => {
    const refreshToken = 'openid-refresh-token';
    const signedUserId = signOpenIdUser(VIEWER_ID, refreshToken);
    (deps.isOpenIdReuseEnabled as jest.Mock).mockReturnValue(true);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(
      createRequest(
        `/images/${VIEWER_ID}/profile.png`,
        `refreshToken=${refreshToken}; token_provider=openid; openid_user_id=${signedUserId}`,
      ),
      response,
      next,
    );

    expect(next).toHaveBeenCalledTimes(1);
    expect(deps.findSession).toHaveBeenCalledWith({
      userId: VIEWER_ID,
      refreshToken,
    });
  });

  it('rejects an OpenID identity cookie paired with a different refresh token', async () => {
    const signedUserId = signOpenIdUser(VIEWER_ID, 'expected-refresh-token');
    (deps.isOpenIdReuseEnabled as jest.Mock).mockReturnValue(true);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(
      createRequest(
        `/images/${VIEWER_ID}/profile.png`,
        `refreshToken=different-refresh-token; token_provider=openid; openid_user_id=${signedUserId}`,
      ),
      response,
      next,
    );

    expect(next).not.toHaveBeenCalled();
    expect(response.status).toHaveBeenCalledWith(403);
  });

  it('rejects a refresh-bound OpenID cookie after its durable session is revoked', async () => {
    const refreshToken = 'revoked-openid-refresh-token';
    const signedUserId = signOpenIdUser(VIEWER_ID, refreshToken);
    (deps.isOpenIdReuseEnabled as jest.Mock).mockReturnValue(true);
    (deps.findSession as jest.Mock).mockResolvedValue(null);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(
      createRequest(
        `/images/${VIEWER_ID}/profile.png`,
        `refreshToken=${refreshToken}; token_provider=openid; openid_user_id=${signedUserId}`,
      ),
      response,
      next,
    );

    expect(next).not.toHaveBeenCalled();
    expect(response.status).toHaveBeenCalledWith(403);
  });

  it('requires an active Express session for a legacy OpenID identity cookie', async () => {
    const refreshToken = 'legacy-refresh-token';
    const signedUserId = signUser(VIEWER_ID);
    (deps.isOpenIdReuseEnabled as jest.Mock).mockReturnValue(true);
    const request = {
      ...createRequest(
        `/images/${VIEWER_ID}/profile.png`,
        `refreshToken=${refreshToken}; token_provider=openid; openid_user_id=${signedUserId}`,
      ),
      session: { openidTokens: { refreshToken } },
    } as unknown as Request;
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(request, response, next);

    expect(next).toHaveBeenCalledTimes(1);
  });

  it('uses the disabled fallback for an image path without an owner layout', async () => {
    deps.getImageConfig = jest.fn();
    const middleware = createImageAuthorizationMiddleware({ secureImageLinks: false }, deps);

    await middleware(createRequest('/images/logo.png'), response, next);

    expect(next).toHaveBeenCalledTimes(1);
    expect(deps.getUserById).not.toHaveBeenCalled();
  });

  it('uses the disabled fallback when the path owner no longer exists', async () => {
    deps.getImageConfig = jest.fn();
    (deps.getUserById as jest.Mock).mockResolvedValue(null);
    const middleware = createImageAuthorizationMiddleware({ secureImageLinks: false }, deps);

    await middleware(createRequest(`/images/${OWNER_ID}/orphaned.png`), response, next);

    expect(next).toHaveBeenCalledTimes(1);
    expect(deps.getImageConfig).not.toHaveBeenCalled();
  });

  it('normalizes repeated separators before applying a disabled fallback', async () => {
    deps.getImageConfig = jest.fn().mockResolvedValue({ secureImageLinks: true });
    const middleware = createImageAuthorizationMiddleware({ secureImageLinks: false }, deps);

    await middleware(createRequest(`/images//${OWNER_ID}/private.png`), response, next);

    expect(next).not.toHaveBeenCalled();
    expect(response.status).toHaveBeenCalledWith(401);
    expect(deps.getImageConfig).toHaveBeenCalledTimes(1);
  });

  it('resolves principals once and applies the tenant to an agent capability check', async () => {
    (deps.getAgent as jest.Mock).mockResolvedValue({ _id: AGENT_DB_ID });
    (deps.hasPermission as jest.Mock).mockResolvedValue(true);
    const token = signUser(VIEWER_ID);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(createRequest(AGENT_PATH, `refreshToken=${token}`), response, next);

    expect(next).toHaveBeenCalledTimes(1);
    expect(deps.getAgent).toHaveBeenCalledWith(
      {
        id: 'agent_abc123',
        'avatar.filepath': {
          $in: [AGENT_PATH, `${AGENT_PATH}?manual=false`, `${AGENT_PATH}?manual=true`],
        },
      },
      { _id: 1 },
    );
    expect(deps.getUserPrincipals).toHaveBeenCalledTimes(1);
    expect(deps.hasCapabilityForPrincipals).toHaveBeenCalledWith(
      expect.objectContaining({ tenantId: 'tenant-a' }),
    );
    expect(deps.hasPermission).toHaveBeenCalledTimes(1);
  });

  it('allows an anonymous request only when the agent has PUBLIC VIEW', async () => {
    (deps.getAgent as jest.Mock).mockResolvedValue({ _id: AGENT_DB_ID });
    (deps.hasPermission as jest.Mock).mockResolvedValue(true);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(createRequest(AGENT_PATH), response, next);

    expect(next).toHaveBeenCalledTimes(1);
    expect(deps.getUserPrincipals).not.toHaveBeenCalled();
    expect(deps.hasPermission).toHaveBeenCalledWith(
      [{ principalType: 'public' }],
      'agent',
      AGENT_DB_ID,
      1,
    );
  });

  it('does not apply a viewer role or group grant across tenant boundaries', async () => {
    (deps.getAgent as jest.Mock).mockResolvedValue({ _id: AGENT_DB_ID });
    (deps.getUserById as jest.Mock)
      .mockResolvedValueOnce({ tenantId: 'tenant-a' })
      .mockResolvedValueOnce({ role: 'USER', tenantId: 'tenant-b', idOnTheSource: null });
    (deps.hasPermission as jest.Mock).mockResolvedValue(true);
    const token = signUser(VIEWER_ID);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(createRequest(AGENT_PATH, `refreshToken=${token}`), response, next);

    expect(next).toHaveBeenCalledTimes(1);
    expect(deps.getUserPrincipals).not.toHaveBeenCalled();
    expect(deps.hasCapabilityForPrincipals).not.toHaveBeenCalled();
    expect(deps.hasPermission).toHaveBeenCalledWith(
      [{ principalType: 'public' }],
      'agent',
      AGENT_DB_ID,
      1,
    );
  });

  it('preserves shared assistant avatars by matching their stored filepath', async () => {
    (deps.getAssistant as jest.Mock).mockResolvedValue({
      _id: '65cfb246f7ecadb8b1e8036e',
      assistant_id: 'asst_shared',
      endpoint: 'assistants',
    });
    const assistantPath = `/images/${OWNER_ID}/assistant-avatar.png`;
    const middleware = createImageAuthorizationMiddleware(
      { assistantEndpoints: [{ endpoint: 'assistants', privateAssistants: false }] },
      deps,
    );
    const token = signUser(VIEWER_ID);

    await middleware(createRequest(assistantPath, `refreshToken=${token}`), response, next);

    expect(next).toHaveBeenCalledTimes(1);
    expect(deps.getAssistant).toHaveBeenCalledWith(
      {
        avatarFilepath: [
          assistantPath,
          `${assistantPath}?manual=false`,
          `${assistantPath}?manual=true`,
        ],
      },
      { _id: 1, assistant_id: 1, endpoint: 1, user: 1 },
    );
  });

  it.each([
    { user: VIEWER_ID, tenantId: 'tenant-a', allowed: true },
    { user: OWNER_ID, tenantId: 'tenant-a', allowed: false },
    { user: VIEWER_ID, tenantId: 'tenant-b', allowed: false },
  ])(
    'checks private assistant ownership and tenant before granting image access: %p',
    async ({ user, tenantId, allowed }) => {
      (deps.getAssistant as jest.Mock).mockResolvedValue({
        _id: '65cfb246f7ecadb8b1e8036e',
        assistant_id: 'asst_private',
        endpoint: 'assistants',
        user,
      });
      (deps.getUserById as jest.Mock)
        .mockResolvedValueOnce({ tenantId: 'tenant-a' })
        .mockResolvedValueOnce({ tenantId });
      const middleware = createImageAuthorizationMiddleware(
        { assistantEndpoints: [{ endpoint: 'assistants', privateAssistants: true }] },
        deps,
      );
      await middleware(
        createRequest(
          `/images/${OWNER_ID}/assistant-avatar.png`,
          `refreshToken=${signUser(VIEWER_ID)}`,
        ),
        response,
        next,
      );
      if (allowed) {
        expect(next).toHaveBeenCalledTimes(1);
        expect(deps.getUserPrincipals).not.toHaveBeenCalled();
      } else {
        expect(next).not.toHaveBeenCalled();
        expect(response.status).toHaveBeenCalledWith(403);
      }
    },
  );

  it('requires authentication for an otherwise shared assistant avatar', async () => {
    (deps.getAssistant as jest.Mock).mockResolvedValue({
      _id: '65cfb246f7ecadb8b1e8036e',
      assistant_id: 'asst_shared',
      endpoint: 'assistants',
    });
    const assistantPath = `/images/${OWNER_ID}/assistant-avatar.png`;
    const middleware = createImageAuthorizationMiddleware(
      { assistantEndpoints: [{ endpoint: 'assistants', privateAssistants: false }] },
      deps,
    );

    await middleware(createRequest(assistantPath), response, next);

    expect(next).not.toHaveBeenCalled();
    expect(response.status).toHaveBeenCalledWith(401);
  });

  it('does not use a shared endpoint policy for a private endpoint assistant', async () => {
    (deps.getAssistant as jest.Mock).mockResolvedValue({
      _id: '65cfb246f7ecadb8b1e8036e',
      assistant_id: 'asst_private',
      endpoint: 'azureAssistants',
    });
    const assistantPath = `/images/${OWNER_ID}/assistant-avatar.png`;
    const middleware = createImageAuthorizationMiddleware(
      {
        assistantEndpoints: [
          { endpoint: 'assistants', privateAssistants: false },
          { endpoint: 'azureAssistants', privateAssistants: true },
        ],
      },
      deps,
    );

    await middleware(createRequest(assistantPath), response, next);

    expect(next).not.toHaveBeenCalled();
    expect(response.status).toHaveBeenCalledWith(401);
  });

  it('allows an authenticated same-tenant viewer to load the stored user avatar', async () => {
    (deps.getUserById as jest.Mock)
      .mockResolvedValueOnce({
        tenantId: 'tenant-a',
        avatar: `${USER_AVATAR_PATH}?manual=true`,
      })
      .mockResolvedValueOnce({ tenantId: 'tenant-a' });
    const token = signUser(VIEWER_ID);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(createRequest(USER_AVATAR_PATH, `refreshToken=${token}`), response, next);

    expect(next).toHaveBeenCalledTimes(1);
    expect(deps.getUserById).toHaveBeenNthCalledWith(
      1,
      OWNER_ID,
      'role tenantId idOnTheSource avatar provider twoFactorEnabled twoFactorEnrolledAt credentialsChangedAt',
    );
    expect(deps.getUserById).toHaveBeenCalledTimes(2);
  });

  it('denies the stored user avatar to a viewer from another tenant', async () => {
    (deps.getUserById as jest.Mock)
      .mockResolvedValueOnce({
        tenantId: 'tenant-a',
        avatar: `${USER_AVATAR_PATH}?manual=true`,
      })
      .mockResolvedValueOnce({ tenantId: 'tenant-b' });
    const token = signUser(VIEWER_ID);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(createRequest(USER_AVATAR_PATH, `refreshToken=${token}`), response, next);

    expect(next).not.toHaveBeenCalled();
    expect(response.status).toHaveBeenCalledWith(403);
  });

  it('normalizes a stored root image URL when the app uses a base path', async () => {
    (deps.getBasePath as jest.Mock).mockReturnValue('/chat');
    (deps.getUserById as jest.Mock)
      .mockResolvedValueOnce({
        tenantId: 'tenant-a',
        avatar: `${USER_AVATAR_PATH}?manual=true`,
      })
      .mockResolvedValueOnce({ tenantId: 'tenant-a' });
    const token = signUser(VIEWER_ID);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(
      createRequest(`/chat${USER_AVATAR_PATH}`, `refreshToken=${token}`),
      response,
      next,
    );

    expect(next).toHaveBeenCalledTimes(1);
  });

  it('uses the owner principal endpoint config instead of the startup fallback', async () => {
    (deps.getAssistant as jest.Mock).mockResolvedValue({
      _id: '65cfb246f7ecadb8b1e8036e',
      assistant_id: 'asst_private',
      endpoint: 'assistants',
    });
    deps.getImageConfig = jest.fn().mockResolvedValue({
      secureImageLinks: true,
      assistantEndpoints: [{ endpoint: 'assistants', privateAssistants: true }],
    });
    const assistantPath = `/images/${OWNER_ID}/assistant-avatar.png`;
    const middleware = createImageAuthorizationMiddleware(
      { assistantEndpoints: [{ endpoint: 'assistants', privateAssistants: false }] },
      deps,
    );

    await middleware(createRequest(assistantPath), response, next);

    expect(next).not.toHaveBeenCalled();
    expect(response.status).toHaveBeenCalledWith(401);
    expect(deps.getImageConfig).toHaveBeenCalledWith({
      userId: OWNER_ID,
      user: expect.objectContaining({ tenantId: 'tenant-a' }),
    });
  });

  it('starts protected image authentication while owner config is resolving', async () => {
    let resolveConfig: ((config: { secureImageLinks: boolean }) => void) | undefined;
    deps.getImageConfig = jest.fn(
      () =>
        new Promise((resolve) => {
          resolveConfig = resolve;
        }),
    );
    const token = signUser(VIEWER_ID);
    const middleware = createImageAuthorizationMiddleware({}, deps);
    const pending = middleware(
      createRequest(`/images/${VIEWER_ID}/profile.png`, `refreshToken=${token}`),
      response,
      next,
    );

    await Promise.resolve();
    await Promise.resolve();

    expect(deps.getImageConfig).toHaveBeenCalledTimes(1);
    expect(deps.findSession).toHaveBeenCalledWith({ userId: VIEWER_ID, refreshToken: token });
    resolveConfig?.({ secureImageLinks: true });
    await pending;
    expect(next).toHaveBeenCalledTimes(1);
  });

  it('honors an owner-scoped setting that disables secure image links', async () => {
    deps.getImageConfig = jest.fn().mockResolvedValue({
      secureImageLinks: false,
      assistantEndpoints: [],
    });
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(createRequest(`/images/${OWNER_ID}/private.png`), response, next);

    expect(next).toHaveBeenCalledTimes(1);
    expect(response.locals.privateImageCache).toBeUndefined();
  });

  it('honors an owner-scoped setting that enables protection over a disabled fallback', async () => {
    deps.getImageConfig = jest.fn().mockResolvedValue({
      secureImageLinks: true,
      assistantEndpoints: [],
    });
    const middleware = createImageAuthorizationMiddleware({ secureImageLinks: false }, deps);

    await middleware(createRequest(`/images/${OWNER_ID}/private.png`), response, next);

    expect(next).not.toHaveBeenCalled();
    expect(response.status).toHaveBeenCalledWith(401);
    expect(response.locals.privateImageCache).toBe(true);
  });

  it('decodes an encoded deployment base path consistently with the request URL', async () => {
    (deps.getBasePath as jest.Mock).mockReturnValue('/libre%20chat');
    const token = signUser(VIEWER_ID);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(
      createRequest(`/libre%20chat/images/${VIEWER_ID}/profile.png`, `refreshToken=${token}`),
      response,
      next,
    );

    expect(next).toHaveBeenCalledTimes(1);
  });

  it('marks protected image responses as private before serving them', async () => {
    const token = signUser(VIEWER_ID);
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(
      createRequest(`/images/${VIEWER_ID}/profile.png`, `refreshToken=${token}`),
      response,
      next,
    );

    expect(response.locals.privateImageCache).toBe(true);
  });

  it.each([
    '..%2F..%2Fetc%2Fpasswd',
    'x%3F%2F..%2F..%2Fsecret.png',
    'x%23%2F..%2F..%2Fsecret.png',
    'x%3f%2f..%2f..%2fsecret.png',
    '%2e%2e',
    'x%5C..%5Csecret.png',
    'x%00.png',
    'x%ZZ.png',
  ])('rejects invalid pathname %s before any resource lookup', async (filename) => {
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(
      createRequest(`/images/${VIEWER_ID}/${filename}`, `refreshToken=${signUser(VIEWER_ID)}`),
      response,
      next,
    );

    expect(next).not.toHaveBeenCalled();
    expect(response.status).toHaveBeenCalledWith(403);
    expect(deps.getUserById).not.toHaveBeenCalled();
    expect(deps.getAgent).not.toHaveBeenCalled();
    expect(deps.getAssistant).not.toHaveBeenCalled();
  });

  it('forwards the authorized pathname with filename delimiters still encoded', async () => {
    const req = createRequest(
      `/images//${VIEWER_ID}/profile%3Fdraft%23one%252F.png?manual=true`,
      `refreshToken=${signUser(VIEWER_ID)}`,
    );
    const middleware = createImageAuthorizationMiddleware({}, deps);

    await middleware(req, response, next);

    expect(next).toHaveBeenCalledTimes(1);
    expect(req.url).toBe(`/${VIEWER_ID}/profile%3Fdraft%23one%252F.png`);
  });

  describe.each([true, false])('HTTP image serving, skipGzipScan=%s', (skipGzipScan) => {
    const privateBytes = Buffer.from('VICTIM_PRIVATE_BYTES');
    const ownerBytes = Buffer.from('OWNER_IMAGE_BYTES');
    const publicBytes = Buffer.from('PUBLIC_AVATAR_BYTES');
    const assistantFilenames = ['company logo.png', '会社.png', 'company+logo.png'];
    let imageRoot: string;

    beforeAll(() => {
      imageRoot = mkdtempSync(join(tmpdir(), 'image-authorization-'));
      for (const id of [VIEWER_ID, OWNER_ID, VICTIM_ID]) {
        mkdirSync(join(imageRoot, id));
      }
      const files = [
        [`${VICTIM_ID}/secret.png`, privateBytes],
        [`${OWNER_ID}/${AGENT_PATH.split('/').pop()}`, publicBytes],
        [`${VIEWER_ID}/profile.png`, ownerBytes],
        [`${VIEWER_ID}/profile?draft.png`, ownerBytes],
        [`${VIEWER_ID}/profile#draft.png`, ownerBytes],
        [`${VIEWER_ID}/profile with spaces.png`, ownerBytes],
        [`${VIEWER_ID}/profile%2F.png`, ownerBytes],
        [`${OWNER_ID}/${AGENT_PATH.split('/').pop()}?private.png`, privateBytes],
        [`${OWNER_ID}/${AGENT_PATH.split('/').pop()}?manual=true`, privateBytes],
        ...assistantFilenames.map((filename) => [`${OWNER_ID}/${filename}`, publicBytes] as const),
        [`${OWNER_ID}/company logo.png?manual=true`, privateBytes],
        [`${OWNER_ID}/company logo.png#private`, privateBytes],
        [`${OWNER_ID}/company%20logo.png`, privateBytes],
      ] as const;
      for (const [filename, bytes] of files) {
        writeFileSync(join(imageRoot, filename), bytes);
        writeFileSync(join(imageRoot, `${filename}.gz`), gzipSync(bytes));
      }
    });

    afterAll(() => {
      rmSync(imageRoot, { recursive: true, force: true });
    });

    describe.each(['', '/chat'])('base path "%s"', (basePath) => {
      let app: Express;

      beforeEach(() => {
        jest.mocked(deps.getBasePath).mockReturnValue(basePath);
        jest.mocked(deps.getUserById).mockImplementation(async (id) => ({
          role: 'USER',
          tenantId: id === VICTIM_ID ? 'tenant-b' : 'tenant-a',
        }));
        jest
          .mocked(deps.getAgent)
          .mockImplementation(async (query) =>
            query.id === 'agent_abc123' &&
            query['avatar.filepath']?.$in?.includes(`${AGENT_PATH}?manual=true`)
              ? { _id: new Types.ObjectId(AGENT_DB_ID) }
              : null,
          );
        jest
          .mocked(deps.hasPermission)
          .mockImplementation(
            async (principals, resourceType, resourceId, permission) =>
              principals.length === 1 &&
              principals[0].principalType === 'public' &&
              resourceType === 'agent' &&
              resourceId.toString() === AGENT_DB_ID &&
              permission === 1,
          );
        app = express();
        const router = express.Router();
        router.use(staticCache(imageRoot, { skipGzipScan }) as RequestHandler);
        app.use(
          `${basePath}/images`,
          createImageAuthorizationMiddleware(
            { assistantEndpoints: [{ endpoint: 'assistants', privateAssistants: false }] },
            deps,
          ),
          router,
        );
      });

      it('denies direct victim requests with and without an active session', async () => {
        const path = `${basePath}/images/${VICTIM_ID}/secret.png`;
        const anonymous = await request(app).get(path);
        const authenticated = await request(app)
          .get(path)
          .set('Cookie', `refreshToken=${signUser(VIEWER_ID)}`);
        expect(anonymous.status).toBe(401);
        expect(authenticated.status).toBe(403);
      });

      it.each(['%3F', '%23'])('rejects an owner-prefix %s traversal', async (delimiter) => {
        const response = await request(app)
          .get(`${basePath}/images//${VIEWER_ID}/x${delimiter}%2F..%2F..%2F${VICTIM_ID}/secret.png`)
          .set('Cookie', `refreshToken=${signUser(VIEWER_ID)}`);
        expect(response.body).not.toEqual(privateBytes);
        expect(response.status).toBe(403);
        expect(deps.getUserById).not.toHaveBeenCalled();
      });

      it.each(['%3F', '%23'])(
        'rejects an anonymous public-avatar %s traversal',
        async (delimiter) => {
          const response = await request(app).get(
            `${basePath}${AGENT_PATH}${delimiter}%2F..%2F..%2F${VICTIM_ID}/secret.png`,
          );
          expect(response.body).not.toEqual(privateBytes);
          expect(response.status).toBe(401);
          expect(deps.getAgent).not.toHaveBeenCalled();
        },
      );

      it.each([
        'profile.png?manual=true&ignored=%ZZ',
        'profile%3Fdraft.png',
        'profile%23draft.png',
        'profile%20with%20spaces.png',
        'profile%252F.png',
      ])('serves only the authorized owner file for %s', async (filename) => {
        const response = await request(app)
          .get(`${basePath}/images//${VIEWER_ID}/${filename}`)
          .set('Cookie', `refreshToken=${signUser(VIEWER_ID)}`)
          .set('Accept-Encoding', 'gzip')
          .expect(200);
        expect(response.body).toEqual(ownerBytes);
        expect(response.headers['cache-control']).toBe('private, no-store');
        expect(response.headers['content-encoding']).toBe(skipGzipScan ? undefined : 'gzip');
      });

      describe.each(['raw', 'encoded'])('stored assistant paths: %s', (representation) => {
        it.each(assistantFilenames)(
          'serves shared assistant avatar %s to a non-owner',
          async (filename) => {
            const storedPath = `/images/${OWNER_ID}/${
              representation === 'raw' ? filename : encodeURIComponent(filename)
            }`;
            for (const suffix of ['', '?manual=false', '?manual=true']) {
              jest.mocked(deps.getAssistant).mockImplementation(async (query) =>
                query.avatarFilepath?.includes(`${storedPath}${suffix}`)
                  ? {
                      _id: new Types.ObjectId(AGENT_DB_ID),
                      assistant_id: 'asst_shared',
                      endpoint: 'assistants',
                    }
                  : null,
              );
              const result = await request(app)
                .get(`${basePath}/images/${OWNER_ID}/${encodeURIComponent(filename)}`)
                .set('Cookie', `refreshToken=${signUser(VIEWER_ID)}`)
                .set('Accept-Encoding', 'gzip');
              expect(result.status).toBe(200);
              expect(result.body).toEqual(publicBytes);
              expect(result.headers['cache-control']).toBe('private, no-store');
            }
          },
        );
      });

      it.each(['%3Fmanual=true', '%23private', '%2520logo.png'])(
        'does not alias an assistant filename suffix %s to a stored URL',
        async (suffix) => {
          const storedPath = `/images/${OWNER_ID}/${
            suffix === '%2520logo.png' ? 'company%20logo.png' : 'company logo.png'
          }?manual=true`;
          jest.mocked(deps.getAssistant).mockImplementation(async (query) =>
            query.avatarFilepath?.includes(storedPath)
              ? {
                  _id: new Types.ObjectId(AGENT_DB_ID),
                  assistant_id: 'asst_shared',
                  endpoint: 'assistants',
                }
              : null,
          );
          const filename =
            suffix === '%2520logo.png' ? `company${suffix}` : `company%20logo.png${suffix}`;
          const result = await request(app)
            .get(`${basePath}/images/${OWNER_ID}/${filename}`)
            .set('Cookie', `refreshToken=${signUser(VIEWER_ID)}`);
          expect(result.status).toBe(403);
          expect(result.body).not.toEqual(privateBytes);
        },
      );

      it('serves the exact PUBLIC avatar but not a filename with an encoded delimiter', async () => {
        const response = await request(app)
          .get(`${basePath}${AGENT_PATH}?manual=false`)
          .set('Accept-Encoding', 'gzip')
          .expect(200);
        expect(response.body).toEqual(publicBytes);
        expect(response.headers['cache-control']).toBe('private, no-store');
        await request(app).get(`${basePath}${AGENT_PATH}%3Fprivate.png`).expect(401);
        await request(app).get(`${basePath}${AGENT_PATH}%3Fmanual=true`).expect(401);
      });
    });
  });
});