diff --git a/src/modules/uploader/services/uploader.service.ts b/src/modules/uploader/services/uploader.service.ts index 22d4691..8cfc1a4 100644 --- a/src/modules/uploader/services/uploader.service.ts +++ b/src/modules/uploader/services/uploader.service.ts @@ -64,9 +64,12 @@ export class UploaderService { const s3Key = this.s3Service.generateFileKey(file.originalname, fileType); + // Sanitize filename for HTTP headers (S3 metadata) + const sanitizedOriginalName = this.sanitizeFilenameForHeader(file.originalname); + // Upload to S3 const s3Result = await this.s3Service.uploadFile(file.buffer, s3Key, file.mimetype, { - originalName: file.originalname, + originalName: sanitizedOriginalName, uploadedBy: "admin", }); @@ -108,4 +111,21 @@ export class UploaderService { } return "unknown"; } + + /** + * Sanitize filename for use in HTTP headers + * Removes invalid characters that are not allowed in HTTP header values + * HTTP headers only allow ASCII printable characters (0x20-0x7E) excluding certain special chars + */ + private sanitizeFilenameForHeader(filename: string): string { + if (!filename) return ""; + + // Remove control characters (0x00-0x1F, 0x7F) and newlines/tabs + // Replace non-ASCII characters with underscore to keep the filename readable + // This ensures the header value is valid while preserving as much of the original name as possible + return filename + .replace(/[\x00-\x1F\x7F\r\n\t]/g, "") // Remove control characters, newlines, tabs + .replace(/[^\x20-\x7E]/g, "_") // Replace non-ASCII characters with underscore + .trim(); + } }