π¨ The ultimate agentic social media scheduling tool π€
TypeScript nextjsopen-sourceopen-source-social-media-scheduling-toolosspostiz-cloudpostiz-hostedredisscheduling-toolsocial-media-scheduling-tooltypescript
fix: cap media size on URL uploads to prevent OOM
Gilad Resisia21a7d4b4748
diff --git a/apps/backend/src/public-api/routes/v1/public.integrations.controller.ts b/apps/backend/src/public-api/routes/v1/public.integrations.controller.tsindex a835cf569..1111d2aff 100644--- a/apps/backend/src/public-api/routes/v1/public.integrations.controller.ts+++ b/apps/backend/src/public-api/routes/v1/public.integrations.controller.ts@@ -12,7 +12,10 @@ import {UseInterceptors,UsePipes,} from '@nestjs/common';-import { CustomFileValidationPipe } from '@gitroom/nestjs-libraries/upload/custom.upload.validation';+import {+ CustomFileValidationPipe,+ getMaxSize,+} from '@gitroom/nestjs-libraries/upload/custom.upload.validation';import { ApiTags } from '@nestjs/swagger';import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request';import { Organization } from '@prisma/client';@@ -107,11 +110,27 @@ export class PublicIntegrationsController {if (!response.ok) {throw new HttpException({ msg: 'Failed to fetch URL' }, 400);}++ // Guard against OOM: bail out before buffering the whole body into memory.+ // Content-Length may be absent or wrong, so we re-check the real size after+ // download too. The type isn't known yet (sniffed below), so the pre-check+ // uses the largest allowed cap (video).+ const maxDownloadSize = getMaxSize('video/mp4');+ const declaredSize = Number(response.headers.get('content-length'));+ if (declaredSize && declaredSize > maxDownloadSize) {+ throw new HttpException({ msg: 'File is too large.' }, 400);+ }+const buffer = Buffer.from(await response.arrayBuffer());const detected = await fromBuffer(buffer);if (!detected || !PUBLIC_API_ALLOWED_MIME.has(detected.mime)) {throw new HttpException({ msg: 'Unsupported file type.' }, 400);}++ if (buffer.length > getMaxSize(detected.mime)) {+ throw new HttpException({ msg: 'File is too large.' }, 400);+ }+const mimetype = detected.mime;const ext = detected.ext;diff --git a/libraries/nestjs-libraries/src/chat/tools/upload.from.url.tool.ts b/libraries/nestjs-libraries/src/chat/tools/upload.from.url.tool.tsindex ea0735a3f..03c7bee4c 100644--- a/libraries/nestjs-libraries/src/chat/tools/upload.from.url.tool.ts+++ b/libraries/nestjs-libraries/src/chat/tools/upload.from.url.tool.ts@@ -4,6 +4,7 @@ import { z } from 'zod';import { Injectable } from '@nestjs/common';import { MediaService } from '@gitroom/nestjs-libraries/database/prisma/media/media.service';import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory';+import { getMaxSize } from '@gitroom/nestjs-libraries/upload/custom.upload.validation';import { checkAuth } from '@gitroom/nestjs-libraries/chat/auth.context';import { ssrfSafeDispatcher } from '@gitroom/nestjs-libraries/dtos/webhooks/ssrf.safe.dispatcher';import { Readable } from 'stream';@@ -76,12 +77,31 @@ so the attachment passes the upload-domain validation. Returns the hosted mediareturn { error: 'Failed to fetch URL' };}+ // Guard against OOM: bail out before buffering the whole body into+ // memory. Content-Length may be absent or wrong, so we re-check the+ // real size after download too. The type isn't known yet (sniffed+ // below), so the pre-check uses the largest allowed cap (video).+ const maxDownloadSize = getMaxSize('video/mp4');+ const declaredSize = Number(response.headers.get('content-length'));+ if (declaredSize && declaredSize > maxDownloadSize) {+ return {+ error: `File is too large: ${declaredSize} bytes (max ${maxDownloadSize} bytes).`,+ };+ }+const buffer = Buffer.from(await response.arrayBuffer());const detected = await fromBuffer(buffer);if (!detected || !ALLOWED_MIME.has(detected.mime)) {return { error: 'Unsupported file type.' };}+ const maxSize = getMaxSize(detected.mime);+ if (buffer.length > maxSize) {+ return {+ error: `File is too large: ${buffer.length} bytes (max ${maxSize} bytes).`,+ };+ }+const getFile = await this.storage.uploadFile({buffer,mimetype: detected.mime,diff --git a/libraries/nestjs-libraries/src/upload/custom.upload.validation.ts b/libraries/nestjs-libraries/src/upload/custom.upload.validation.tsindex c24c95043..49c414be5 100644--- a/libraries/nestjs-libraries/src/upload/custom.upload.validation.ts+++ b/libraries/nestjs-libraries/src/upload/custom.upload.validation.ts@@ -38,7 +38,7 @@ export class CustomFileValidationPipe implements PipeTransform {throw new BadRequestException('Unsupported file type.');}- const maxSize = this.getMaxSize(detected.mime);+ const maxSize = getMaxSize(detected.mime);if (value.size > maxSize) {throw new BadRequestException(`File size exceeds the maximum allowed size of ${maxSize} bytes.`@@ -55,13 +55,14 @@ export class CustomFileValidationPipe implements PipeTransform {return value;}- private getMaxSize(mimeType: string): number {- if (mimeType.startsWith('image/')) {- return 10 * 1024 * 1024; // 10 MB- } else if (mimeType.startsWith('video/')) {- return 1024 * 1024 * 1024; // 1 GB- } else {- throw new BadRequestException('Unsupported file type.');- }+}++export function getMaxSize(mimeType: string): number {+ if (mimeType.startsWith('image/')) {+ return 10 * 1024 * 1024; // 10 MB+ } else if (mimeType.startsWith('video/')) {+ return 1024 * 1024 * 1024; // 1 GB+ } else {+ throw new BadRequestException('Unsupported file type.');}}