Skip to content

The open alternative to Salesforce, designed for AI.

Bound outbound HTTP requests by default (#27608)

Paul Rastoincd467d81d083
diff --git a/packages/twenty-server/src/engine/constants/max-request-body-size.constant.ts b/packages/twenty-server/src/engine/constants/max-request-body-size.constant.ts
new file mode 100644
index 00000000000..06a422bf393
--- /dev/null
+++ b/packages/twenty-server/src/engine/constants/max-request-body-size.constant.ts
@@ -0,0 +1 @@
+export const MAX_REQUEST_BODY_SIZE = '100MB';
diff --git a/packages/twenty-server/src/engine/constants/settings/index.ts b/packages/twenty-server/src/engine/constants/settings/index.ts
index 3e52080997f..8a949e578ee 100644
--- a/packages/twenty-server/src/engine/constants/settings/index.ts
+++ b/packages/twenty-server/src/engine/constants/settings/index.ts
@@ -1,3 +1,5 @@
+import { MAX_REQUEST_BODY_SIZE } from 'src/engine/constants/max-request-body-size.constant';
+
import { type Settings } from './interfaces/settings.interface';
export const settings: Settings = {
@@ -7,7 +9,7 @@ export const settings: Settings = {
maxDirectUploadFileSize: '1GB',
maxCorePictureFileSize: '10MB',
},
- maxRequestBodySize: '100MB',
+ maxRequestBodySize: MAX_REQUEST_BODY_SIZE,
minLengthOfStringForDuplicateCheck: 3,
maxVisibleViewFields: 30,
};
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-package/application-package-fetcher.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-package/application-package-fetcher.service.ts
index 501c1584de4..759473a5fe0 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-package/application-package-fetcher.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-package/application-package-fetcher.service.ts
@@ -6,6 +6,7 @@ import { tmpdir } from 'os';
import { join } from 'path';
import { isAxiosError } from 'axios';
+import bytes from 'bytes';
import { type Manifest } from 'twenty-shared/application';
import { FileFolder } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
@@ -29,6 +30,7 @@ import {
import { FileStorageService } from 'src/engine/core-modules/file-storage/services/file-storage.service';
import { FileEntity } from 'src/engine/core-modules/file/entities/file.entity';
import { removeFileFolderFromFileEntityPath } from 'src/engine/core-modules/file/utils/remove-file-folder-from-file-entity-path.utils';
+import { MAX_REQUEST_BODY_SIZE } from 'src/engine/constants/max-request-body-size.constant';
import { SecureHttpClientService } from 'src/engine/core-modules/secure-http-client/secure-http-client.service';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { streamToBuffer } from 'src/utils/stream-to-buffer';
@@ -371,6 +373,7 @@ export class ApplicationPackageFetcherService implements OnModuleInit {
const httpClient = this.secureHttpClientService.getHttpClient({
timeout: RESOLUTION_TIMEOUT_MS,
+ maxContentLength: bytes(MAX_REQUEST_BODY_SIZE) ?? undefined,
});
try {
diff --git a/packages/twenty-server/src/engine/core-modules/secure-http-client/constants/outbound-http-default-max-payload-size-bytes.constant.ts b/packages/twenty-server/src/engine/core-modules/secure-http-client/constants/outbound-http-default-max-payload-size-bytes.constant.ts
new file mode 100644
index 00000000000..dff88aa19aa
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/secure-http-client/constants/outbound-http-default-max-payload-size-bytes.constant.ts
@@ -0,0 +1 @@
+export const OUTBOUND_HTTP_DEFAULT_MAX_PAYLOAD_SIZE_BYTES = 50 * 1024 * 1024;
diff --git a/packages/twenty-server/src/engine/core-modules/secure-http-client/constants/outbound-http-default-timeout-ms.constant.ts b/packages/twenty-server/src/engine/core-modules/secure-http-client/constants/outbound-http-default-timeout-ms.constant.ts
new file mode 100644
index 00000000000..31fe451f9ae
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/secure-http-client/constants/outbound-http-default-timeout-ms.constant.ts
@@ -0,0 +1 @@
+export const OUTBOUND_HTTP_DEFAULT_TIMEOUT_MS = 60_000;
diff --git a/packages/twenty-server/src/engine/core-modules/secure-http-client/secure-http-client.service.ts b/packages/twenty-server/src/engine/core-modules/secure-http-client/secure-http-client.service.ts
index d173aef9b62..9684f6bb381 100644
--- a/packages/twenty-server/src/engine/core-modules/secure-http-client/secure-http-client.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/secure-http-client/secure-http-client.service.ts
@@ -11,6 +11,8 @@ import { buildAxiosFetch } from '@lifeomic/axios-fetch';
import { createSsrfSafeAgent } from 'src/engine/core-modules/secure-http-client/utils/create-ssrf-safe-agent.util';
import { ALLOW_ALL_INTERNAL_HOSTS } from 'src/engine/core-modules/secure-http-client/constants/allow-all-internal-hosts.constant';
+import { OUTBOUND_HTTP_DEFAULT_MAX_PAYLOAD_SIZE_BYTES } from 'src/engine/core-modules/secure-http-client/constants/outbound-http-default-max-payload-size-bytes.constant';
+import { OUTBOUND_HTTP_DEFAULT_TIMEOUT_MS } from 'src/engine/core-modules/secure-http-client/constants/outbound-http-default-timeout-ms.constant';
import { normalizeAllowedInternalHost } from 'src/engine/core-modules/secure-http-client/utils/normalize-allowed-internal-host.util';
import { resolveAndValidateHostname } from 'src/engine/core-modules/secure-http-client/utils/resolve-and-validate-hostname.util';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
@@ -43,9 +45,19 @@ export class SecureHttpClientService {
const allowedInternalHosts = this.getAllowedInternalHosts();
const isSafeModeEnabled = this.isSafeModeEnabled(allowedInternalHosts);
+ const boundedAxiosConfig: CreateAxiosDefaults = {
+ ...axiosConfig,
+ maxContentLength:
+ axiosConfig.maxContentLength ??
+ OUTBOUND_HTTP_DEFAULT_MAX_PAYLOAD_SIZE_BYTES,
+ maxBodyLength:
+ axiosConfig.maxBodyLength ??
+ OUTBOUND_HTTP_DEFAULT_MAX_PAYLOAD_SIZE_BYTES,
+ };
+
const client = isSafeModeEnabled
? axios.create({
- ...axiosConfig,
+ ...boundedAxiosConfig,
httpAgent: createSsrfSafeAgent('http', allowedInternalHosts),
httpsAgent: createSsrfSafeAgent('https', allowedInternalHosts),
maxRedirects: Math.min(
@@ -53,7 +65,15 @@ export class SecureHttpClientService {
MAX_REDIRECTS,
),
})
- : axios.create(axiosConfig);
+ : axios.create(boundedAxiosConfig);
+
+ client.interceptors.request.use((requestConfig) => {
+ requestConfig.signal = AbortSignal.timeout(
+ requestConfig.timeout || OUTBOUND_HTTP_DEFAULT_TIMEOUT_MS,
+ );
+
+ return requestConfig;
+ });
if (isDefined(retries) && retries > 0) {
axiosRetry(client, {
@@ -62,7 +82,8 @@ export class SecureHttpClientService {
retryCondition: (error) =>
axiosRetry.isNetworkOrIdempotentRequestError(error) &&
error.code !== 'ECONNABORTED' &&
- error.code !== 'ETIMEDOUT',
+ error.code !== 'ETIMEDOUT' &&
+ error.code !== 'ERR_CANCELED',
});
}
diff --git a/packages/twenty-server/src/engine/core-modules/tool/tools/http-tool/http-tool.ts b/packages/twenty-server/src/engine/core-modules/tool/tools/http-tool/http-tool.ts
index ca5c7653749..1b260fdae41 100644
--- a/packages/twenty-server/src/engine/core-modules/tool/tools/http-tool/http-tool.ts
+++ b/packages/twenty-server/src/engine/core-modules/tool/tools/http-tool/http-tool.ts
@@ -31,14 +31,13 @@ export class HttpTool implements Tool {
const { url, method, headers, body } = parameters as HttpRequestInput;
const headersCopy = { ...headers };
const isMethodForBody = ['POST', 'PUT', 'PATCH'].includes(method);
- const timeoutSignal = AbortSignal.timeout(HTTP_TOOL_TIMEOUT_MS);
try {
const axiosConfig: AxiosRequestConfig = {
url,
method: method,
headers: headersCopy,
- signal: timeoutSignal,
+ timeout: HTTP_TOOL_TIMEOUT_MS,
maxContentLength: HTTP_TOOL_MAX_PAYLOAD_SIZE_BYTES,
maxBodyLength: HTTP_TOOL_MAX_PAYLOAD_SIZE_BYTES,
};
@@ -72,7 +71,9 @@ export class HttpTool implements Tool {
};
} catch (error) {
if (isAxiosError(error)) {
- const errorMessage = timeoutSignal.aborted
+ const isTimeout =
+ error.code === 'ERR_CANCELED' || error.code === 'ECONNABORTED';
+ const errorMessage = isTimeout
? `Request timed out after ${HTTP_TOOL_TIMEOUT_MS / 1_000}s`
: error.message;
diff --git a/packages/twenty-server/src/engine/metadata-modules/webhook/jobs/call-webhook.job.ts b/packages/twenty-server/src/engine/metadata-modules/webhook/jobs/call-webhook.job.ts
index b8f12a289cc..33ae412927a 100644
--- a/packages/twenty-server/src/engine/metadata-modules/webhook/jobs/call-webhook.job.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/webhook/jobs/call-webhook.job.ts
@@ -115,9 +115,13 @@ export class CallWebhookJob {
{
headers,
timeout: 5_000,
+ responseType: 'stream',
},
);
+ response.data.destroy();
+ response.request.destroy();
+
const success = response.status >= 200 && response.status < 300;
void this.metricsService.incrementCounterForEvent({
@@ -135,6 +139,9 @@ export class CallWebhookJob {
},
};
} catch (err) {
+ err.response?.data?.destroy();
+ err.request?.destroy();
+
const isSSRFBlocked =
err instanceof Error &&
err.message.includes('internal IP address') &&