Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: harden feedback attachment uploads

Blue Mouse7b42b057f0c8
diff --git a/backend/apps/cloud/src/user/user.controller.ts b/backend/apps/cloud/src/user/user.controller.ts
index ffca0dbfe..bacb3b1d4 100644
--- a/backend/apps/cloud/src/user/user.controller.ts
+++ b/backend/apps/cloud/src/user/user.controller.ts
@@ -9,12 +9,14 @@ import {
Delete,
HttpCode,
BadRequestException,
+ FileTypeValidator,
UseGuards,
UseInterceptors,
ConflictException,
Headers,
Ip,
NotFoundException,
+ ParseFilePipe,
UploadedFiles,
} from '@nestjs/common'
import { FilesInterceptor } from '@nestjs/platform-express'
@@ -220,22 +222,23 @@ export class UserController {
@UseInterceptors(
FilesInterceptor('attachments', FEEDBACK_ATTACHMENTS_LIMIT, {
limits: { fileSize: FEEDBACK_ATTACHMENT_MAX_SIZE },
- fileFilter: (_req, file, callback) => {
- if (!file.mimetype.startsWith('image/')) {
- return callback(
- new BadRequestException('Only image attachments are allowed'),
- false,
- )
- }
-
- callback(null, true)
- },
}),
)
async createFeedback(
@CurrentUserId() userId: string,
@Body() body: CreateFeedbackDTO,
- @UploadedFiles() files: Express.Multer.File[],
+ @UploadedFiles(
+ new ParseFilePipe({
+ fileIsRequired: false,
+ validators: [
+ new FileTypeValidator({
+ fileType: /^image\//,
+ errorMessage: 'Only image attachments are allowed',
+ }),
+ ],
+ }),
+ )
+ files: Express.Multer.File[],
@Headers() headers: Record<string, string>,
@Ip() requestIp: string,
) {
diff --git a/backend/apps/cloud/src/user/user.service.ts b/backend/apps/cloud/src/user/user.service.ts
index 993cbb827..77ea47932 100644
--- a/backend/apps/cloud/src/user/user.service.ts
+++ b/backend/apps/cloud/src/user/user.service.ts
@@ -102,6 +102,7 @@ const CURRENCY_BY_COUNTRY = {
const { PADDLE_VENDOR_ID, PADDLE_API_KEY } = process.env
const DEFAULT_CDN_URL = 'https://cdn.swetrix.com'
+const FEEDBACK_ATTACHMENT_UPLOAD_TIMEOUT_MS = 10_000
type CdnFileResponse =
| string
@@ -314,10 +315,26 @@ export class UserService {
)
}
- const response = await fetch(`${baseUrl}/file/multiple`, {
- method: 'POST',
- body: formData,
- })
+ const controller = new AbortController()
+ const timeout = setTimeout(() => {
+ controller.abort()
+ }, FEEDBACK_ATTACHMENT_UPLOAD_TIMEOUT_MS)
+
+ let response: Response
+ try {
+ response = await fetch(`${baseUrl}/file/multiple`, {
+ method: 'POST',
+ body: formData,
+ signal: controller.signal,
+ })
+ } catch (error) {
+ if (error.name === 'AbortError') {
+ throw new ServiceUnavailableException('Attachment upload timed out')
+ }
+ throw error
+ } finally {
+ clearTimeout(timeout)
+ }
if (!response.ok) {
throw new ServiceUnavailableException('Failed to upload attachments')