diff --git a/src/server/config.test.ts b/src/server/config.test.ts index ecaf6f1..53ea86c 100644 --- a/src/server/config.test.ts +++ b/src/server/config.test.ts @@ -85,4 +85,25 @@ describe('buildConfig', () => { expect(response.json()).toEqual({ textLength: largeText.length }); await app.close(); }); + + it('trusts forwarded client IP headers for reverse-proxy deployments', async () => { + const app = Fastify(buildFastifyOptions(buildConfig({}))); + + app.get('/ip', async (request) => ({ + ip: request.ip, + })); + + const response = await app.inject({ + method: 'GET', + url: '/ip', + remoteAddress: '10.0.0.8', + headers: { + 'x-forwarded-for': '203.0.113.5, 10.0.0.8', + }, + }); + + expect(response.statusCode).toBe(200); + expect(response.json()).toEqual({ ip: '203.0.113.5' }); + await app.close(); + }); }); diff --git a/src/server/config.ts b/src/server/config.ts index 2b85533..2aeeae8 100644 --- a/src/server/config.ts +++ b/src/server/config.ts @@ -137,6 +137,7 @@ export function buildFastifyOptions( ): FastifyServerOptions { return { logger: true, + trustProxy: true, bodyLimit: appConfig.requestBodyLimit, }; } diff --git a/src/server/db/proxyFileSchemaCompatibility.test.ts b/src/server/db/proxyFileSchemaCompatibility.test.ts index 7bbaf68..ea08615 100644 --- a/src/server/db/proxyFileSchemaCompatibility.test.ts +++ b/src/server/db/proxyFileSchemaCompatibility.test.ts @@ -62,6 +62,19 @@ describe('ensureProxyFileSchemaCompatibility', () => { expect(executedSql.some((sqlText) => ownerIndexPattern.test(sqlText))).toBe(true); }); + it('emits MySQL indexes compatible with generated bootstrap SQL', async () => { + const { inspector, executedSql } = createInspector('mysql'); + + await ensureProxyFileSchemaCompatibility(inspector); + + expect(executedSql).toContain( + 'CREATE UNIQUE INDEX `proxy_files_public_id_unique` ON `proxy_files` (`public_id`(191))', + ); + expect(executedSql).toContain( + 'CREATE INDEX `proxy_files_owner_lookup_idx` ON `proxy_files` (`owner_type`(191), `owner_id`(191), `deleted_at`)', + ); + }); + it('adds missing columns on existing table before ensuring indexes', async () => { const { inspector, executedSql } = createInspector('postgres', { hasTable: true, diff --git a/src/server/db/proxyFileSchemaCompatibility.ts b/src/server/db/proxyFileSchemaCompatibility.ts index 94c0f3c..54cd832 100644 --- a/src/server/db/proxyFileSchemaCompatibility.ts +++ b/src/server/db/proxyFileSchemaCompatibility.ts @@ -127,8 +127,8 @@ const CREATE_INDEX_SQL: Record = { 'CREATE INDEX IF NOT EXISTS "proxy_files_owner_lookup_idx" ON "proxy_files" ("owner_type", "owner_id", "deleted_at")', ], mysql: [ - 'CREATE UNIQUE INDEX `proxy_files_public_id_unique` ON `proxy_files` (`public_id`)', - 'CREATE INDEX `proxy_files_owner_lookup_idx` ON `proxy_files` (`owner_type`, `owner_id`)', + 'CREATE UNIQUE INDEX `proxy_files_public_id_unique` ON `proxy_files` (`public_id`(191))', + 'CREATE INDEX `proxy_files_owner_lookup_idx` ON `proxy_files` (`owner_type`(191), `owner_id`(191), `deleted_at`)', ], };