mirror of
https://github.com/hexastack/hexabot
synced 2025-05-31 10:57:06 +00:00
fix: filename sanitization
This commit is contained in:
parent
b66093612d
commit
c5520c96b3
@ -17,6 +17,7 @@ import {
|
||||
StreamableFile,
|
||||
} from '@nestjs/common';
|
||||
import fetch from 'node-fetch';
|
||||
import sanitizeFilename from 'sanitize-filename';
|
||||
|
||||
import { config } from '@/config';
|
||||
import { LoggerService } from '@/logger/logger.service';
|
||||
@ -203,12 +204,17 @@ export class AttachmentService extends BaseService<Attachment> {
|
||||
} else {
|
||||
const dirPath = path.join(config.parameters.uploadDir);
|
||||
const uniqueFilename = generateUniqueFilename(metadata.name);
|
||||
const filePath = path.resolve(dirPath, uniqueFilename);
|
||||
const filePath = path.resolve(dirPath, sanitizeFilename(uniqueFilename));
|
||||
|
||||
if (!filePath.startsWith(dirPath)) {
|
||||
throw new Error('Invalid file path');
|
||||
}
|
||||
|
||||
if (typeof file === 'string') {
|
||||
// For example, if the file is an instance of `Express.Multer.File` (diskStorage case)
|
||||
await fsPromises.copyFile(file, filePath);
|
||||
await fsPromises.unlink(file);
|
||||
const srcFilePath = path.resolve(file);
|
||||
await fsPromises.copyFile(srcFilePath, filePath);
|
||||
await fsPromises.unlink(srcFilePath);
|
||||
} else if (Buffer.isBuffer(file)) {
|
||||
await fsPromises.writeFile(filePath, file);
|
||||
} else if (file instanceof Readable) {
|
||||
|
Loading…
Reference in New Issue
Block a user