Skip to content

The open alternative to Salesforce, designed for AI.

fix(server): honor X-Forwarded-* via configurable trust proxy (#19824)

Félix Malfaitb292a93376a0
diff --git a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
index 5c153b3eef3..78c7e51053a 100644
--- a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
+++ b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
@@ -1126,6 +1126,23 @@ export class ConfigVariables {
@IsOptional()
SERVER_URL = 'http://localhost:3000';
+ @ConfigVariablesMetadata({
+ group: ConfigVariablesGroup.SERVER_CONFIG,
+ description:
+ 'Express "trust proxy" setting. Controls whether X-Forwarded-* ' +
+ 'headers are honored — required for request.protocol to return ' +
+ '"https" when TLS is terminated upstream (reverse proxy, ingress, ' +
+ 'Cloudflare, etc.). Default trusts loopback + RFC1918/ULA peers, ' +
+ 'which is correct when NestJS runs behind a reverse proxy (our ' +
+ 'recommended self-host setup). Set to "false" when NestJS is ' +
+ 'exposed directly to the internet. Accepts any value Express ' +
+ 'supports — see https://expressjs.com/en/guide/behind-proxies.html.',
+ type: ConfigVariableType.STRING,
+ isEnvOnly: true,
+ })
+ @IsOptional()
+ TRUST_PROXY: string = 'loopback, linklocal, uniquelocal';
+
@ConfigVariablesMetadata({
group: ConfigVariablesGroup.SERVER_CONFIG,
description:
diff --git a/packages/twenty-server/src/main.ts b/packages/twenty-server/src/main.ts
index 0265aca64d8..13b2cd708ed 100644
--- a/packages/twenty-server/src/main.ts
+++ b/packages/twenty-server/src/main.ts
@@ -14,6 +14,7 @@ import { setPgDateTypeParser } from 'src/database/pg/set-pg-date-type-parser';
import { LoggerService } from 'src/engine/core-modules/logger/logger.service';
import { getSessionStorageOptions } from 'src/engine/core-modules/session-storage/session-storage.module-factory';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
+import { configTransformers } from 'src/engine/core-modules/twenty-config/utils/config-transformers.util';
import { UnhandledExceptionFilter } from 'src/filters/unhandled-exception.filter';
import { AppModule } from './app.module';
@@ -43,6 +44,13 @@ const bootstrap = async () => {
const logger = app.get(LoggerService);
const twentyConfigService = app.get(TwentyConfigService);
+ const trustProxyRaw = twentyConfigService.get('TRUST_PROXY');
+ const trustProxy = /^\d+$/.test(trustProxyRaw)
+ ? Number(trustProxyRaw)
+ : (configTransformers.boolean(trustProxyRaw) ?? trustProxyRaw);
+
+ app.set('trust proxy', trustProxy);
+
app.use(session(getSessionStorageOptions(twentyConfigService)));
// Apply class-validator container so that we can use injection in validators