📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Refactor file handling
Blue Mouse68ff38de4f10
diff --git a/backend/apps/cloud/src/data-import/data-import.controller.ts b/backend/apps/cloud/src/data-import/data-import.controller.tsindex 52a9d26db..75bbcda9f 100644--- a/backend/apps/cloud/src/data-import/data-import.controller.ts+++ b/backend/apps/cloud/src/data-import/data-import.controller.ts@@ -86,13 +86,13 @@ export class DataImportController {const project = await this.projectService.getFullProject(projectId)this.projectService.allowedToManage(project, uid)- if (!file?.path) {+ if (!file?.filename) {throw new BadRequestException('No file uploaded')}const mapper = getMapper(dto.provider)if (!mapper) {- this.cleanupFile(file.path)+ this.cleanupFile(file.filename)throw new BadRequestException(`Unsupported provider: ${dto.provider}. Supported: ${SUPPORTED_PROVIDERS.join(', ')}`,)@@ -100,7 +100,7 @@ export class DataImportController {const ext = path.extname(file.originalname).toLowerCase()if (ext !== mapper.expectedFileExtension) {- this.cleanupFile(file.path)+ this.cleanupFile(file.filename)throw new BadRequestException(`Invalid file type. Expected a ${mapper.expectedFileExtension} file for ${dto.provider} import.`,)@@ -110,7 +110,7 @@ export class DataImportController {try {dataImport = await this.dataImportService.create(projectId, dto.provider)} catch (error) {- this.cleanupFile(file.path)+ this.cleanupFile(file.filename)throw error}@@ -122,7 +122,7 @@ export class DataImportController {importId: dataImport.importId,projectId,provider: dto.provider,- filePath: file.path,+ fileName: file.filename,},{attempts: 1,@@ -133,7 +133,7 @@ export class DataImportController {} catch (error) {this.logger.error(`Failed to enqueue import job: ${error.message}`)await this.dataImportService.deleteImportRecord(dataImport.id)- this.cleanupFile(file.path)+ this.cleanupFile(file.filename)throw new ServiceUnavailableException('Failed to start import. Please try again later.',)@@ -206,18 +206,12 @@ export class DataImportController {return { success: true }}- private cleanupFile(filePath: string): void {+ private cleanupFile(fileName: string): void {try {- const resolvedPath = path.resolve(filePath)- const relativePath = path.relative(IMPORT_TMP_DIR, resolvedPath)- const isImportTempFile =- relativePath !== '' &&- !relativePath.startsWith('..') &&- !path.isAbsolute(relativePath)-- if (!isImportTempFile) {+ const resolvedPath = this.getImportFilePath(fileName)+ if (!resolvedPath) {this.logger.warn(- `Skipped cleanup outside import temp dir: ${resolvedPath}`,+ `Skipped cleanup for invalid import file name: ${fileName}`,)return}@@ -229,4 +223,16 @@ export class DataImportController {//}}++ private getImportFilePath(fileName: string): string | null {+ if (+ !fileName ||+ fileName !== path.basename(fileName) ||+ /[\\/]/.test(fileName)+ ) {+ return null+ }++ return path.join(IMPORT_TMP_DIR, fileName)+ }}diff --git a/backend/apps/cloud/src/data-import/data-import.processor.ts b/backend/apps/cloud/src/data-import/data-import.processor.tsindex 1e7d91648..d64b66a14 100644--- a/backend/apps/cloud/src/data-import/data-import.processor.ts+++ b/backend/apps/cloud/src/data-import/data-import.processor.ts@@ -21,7 +21,7 @@ export interface DataImportJobData {importId: numberprojectId: stringprovider: string- filePath: string+ fileName: string}@Processor(DATA_IMPORT_QUEUE, { concurrency: 1 })@@ -33,7 +33,8 @@ export class DataImportProcessor extends WorkerHost {}async process(job: Job<DataImportJobData>): Promise<void> {- const { id, importId, projectId, provider, filePath } = job.data+ const { id, importId, projectId, provider, fileName } = job.data+ const filePath = this.getImportFilePath(fileName)try {this.logger.log(@@ -42,6 +43,14 @@ export class DataImportProcessor extends WorkerHost {await this.dataImportService.markProcessing(id)+ if (!filePath) {+ await this.dataImportService.markFailed(+ id,+ 'Invalid import file reference. Please upload the file again.',+ )+ return+ }+const mapper = getMapper(provider)if (!mapper) {await this.dataImportService.markFailed(@@ -150,7 +159,7 @@ export class DataImportProcessor extends WorkerHost {await this.dataImportService.markFailed(id, userMessage)}} finally {- this.cleanupFile(filePath)+ this.cleanupFile(fileName)}}@@ -165,18 +174,12 @@ export class DataImportProcessor extends WorkerHost {})}- private cleanupFile(filePath: string): void {+ private cleanupFile(fileName: string): void {try {- const resolvedPath = path.resolve(filePath)- const relativePath = path.relative(IMPORT_TMP_DIR, resolvedPath)- const isImportTempFile =- relativePath !== '' &&- !relativePath.startsWith('..') &&- !path.isAbsolute(relativePath)-- if (!isImportTempFile) {+ const resolvedPath = this.getImportFilePath(fileName)+ if (!resolvedPath) {this.logger.warn(- `Skipped cleanup outside import temp dir: ${resolvedPath}`,+ `Skipped cleanup for invalid import file name: ${fileName}`,)return}@@ -185,7 +188,19 @@ export class DataImportProcessor extends WorkerHost {fs.unlinkSync(resolvedPath)}} catch {- this.logger.warn(`Failed to clean up temp file: ${filePath}`)+ this.logger.warn(`Failed to clean up temp file: ${fileName}`)}}++ private getImportFilePath(fileName: string): string | null {+ if (+ !fileName ||+ fileName !== path.basename(fileName) ||+ /[\\/]/.test(fileName)+ ) {+ return null+ }++ return path.join(IMPORT_TMP_DIR, fileName)+ }}diff --git a/backend/apps/community/src/data-import/data-import.controller.ts b/backend/apps/community/src/data-import/data-import.controller.tsindex 7e08ba784..ed7b259d1 100644--- a/backend/apps/community/src/data-import/data-import.controller.ts+++ b/backend/apps/community/src/data-import/data-import.controller.ts@@ -82,13 +82,13 @@ export class DataImportController {const project = await this.projectService.getFullProject(projectId)this.projectService.allowedToManage(project, uid)- if (!file?.path) {+ if (!file?.filename) {throw new BadRequestException('No file uploaded')}const mapper = getMapper(dto.provider)if (!mapper) {- this.cleanupFile(file.path)+ this.cleanupFile(file.filename)throw new BadRequestException(`Unsupported provider: ${dto.provider}. Supported: ${SUPPORTED_PROVIDERS.join(', ')}`,)@@ -96,7 +96,7 @@ export class DataImportController {const ext = path.extname(file.originalname).toLowerCase()if (ext !== mapper.expectedFileExtension) {- this.cleanupFile(file.path)+ this.cleanupFile(file.filename)throw new BadRequestException(`Invalid file type. Expected a ${mapper.expectedFileExtension} file for ${dto.provider} import.`,)@@ -106,7 +106,7 @@ export class DataImportController {try {dataImport = await this.dataImportService.create(projectId, dto.provider)} catch (error) {- this.cleanupFile(file.path)+ this.cleanupFile(file.filename)throw error}@@ -117,7 +117,7 @@ export class DataImportController {importId: dataImport.id,projectId,provider: dto.provider,- filePath: file.path,+ fileName: file.filename,},{attempts: 1,@@ -128,7 +128,7 @@ export class DataImportController {} catch (error) {this.logger.error(`Failed to enqueue import job: ${error.message}`)await this.dataImportService.deleteImportRecord(projectId, dataImport.id)- this.cleanupFile(file.path)+ this.cleanupFile(file.filename)throw new ServiceUnavailableException('Failed to start import. Please try again later.',)@@ -197,18 +197,12 @@ export class DataImportController {return { success: true }}- private cleanupFile(filePath: string): void {+ private cleanupFile(fileName: string): void {try {- const resolvedPath = path.resolve(filePath)- const relativePath = path.relative(IMPORT_TMP_DIR, resolvedPath)- const isImportTempFile =- relativePath !== '' &&- !relativePath.startsWith('..') &&- !path.isAbsolute(relativePath)-- if (!isImportTempFile) {+ const resolvedPath = this.getImportFilePath(fileName)+ if (!resolvedPath) {this.logger.warn(- `Skipped cleanup outside import temp dir: ${resolvedPath}`,+ `Skipped cleanup for invalid import file name: ${fileName}`,)return}@@ -220,4 +214,16 @@ export class DataImportController {//}}++ private getImportFilePath(fileName: string): string | null {+ if (+ !fileName ||+ fileName !== path.basename(fileName) ||+ /[\\/]/.test(fileName)+ ) {+ return null+ }++ return path.join(IMPORT_TMP_DIR, fileName)+ }}diff --git a/backend/apps/community/src/data-import/data-import.processor.ts b/backend/apps/community/src/data-import/data-import.processor.tsindex ea68841af..0ede6681f 100644--- a/backend/apps/community/src/data-import/data-import.processor.ts+++ b/backend/apps/community/src/data-import/data-import.processor.ts@@ -19,7 +19,7 @@ export interface DataImportJobData {importId: numberprojectId: stringprovider: string- filePath: string+ fileName: string}@Processor(DATA_IMPORT_QUEUE, { concurrency: 1 })@@ -31,7 +31,8 @@ export class DataImportProcessor extends WorkerHost {}async process(job: Job<DataImportJobData>): Promise<void> {- const { importId, projectId, provider, filePath } = job.data+ const { importId, projectId, provider, fileName } = job.data+ const filePath = this.getImportFilePath(fileName)try {this.logger.log(@@ -40,6 +41,15 @@ export class DataImportProcessor extends WorkerHost {await this.dataImportService.markProcessing(importId, projectId)+ if (!filePath) {+ await this.dataImportService.markFailed(+ importId,+ projectId,+ 'Invalid import file reference. Please upload the file again.',+ )+ return+ }+const mapper = getMapper(provider)if (!mapper) {await this.dataImportService.markFailed(@@ -153,7 +163,7 @@ export class DataImportProcessor extends WorkerHost {)}} finally {- this.cleanupFile(filePath)+ this.cleanupFile(fileName)}}@@ -168,18 +178,12 @@ export class DataImportProcessor extends WorkerHost {})}- private cleanupFile(filePath: string): void {+ private cleanupFile(fileName: string): void {try {- const resolvedPath = path.resolve(filePath)- const relativePath = path.relative(IMPORT_TMP_DIR, resolvedPath)- const isImportTempFile =- relativePath !== '' &&- !relativePath.startsWith('..') &&- !path.isAbsolute(relativePath)-- if (!isImportTempFile) {+ const resolvedPath = this.getImportFilePath(fileName)+ if (!resolvedPath) {this.logger.warn(- `Skipped cleanup outside import temp dir: ${resolvedPath}`,+ `Skipped cleanup for invalid import file name: ${fileName}`,)return}@@ -188,7 +192,19 @@ export class DataImportProcessor extends WorkerHost {fs.unlinkSync(resolvedPath)}} catch {- this.logger.warn(`Failed to clean up temp file: ${filePath}`)+ this.logger.warn(`Failed to clean up temp file: ${fileName}`)}}++ private getImportFilePath(fileName: string): string | null {+ if (+ !fileName ||+ fileName !== path.basename(fileName) ||+ /[\\/]/.test(fileName)+ ) {+ return null+ }++ return path.join(IMPORT_TMP_DIR, fileName)+ }}