-
Notifications
You must be signed in to change notification settings - Fork 47
chore: cleanup uploaded tmp files #1927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
usingtechnology
merged 2 commits into
bcgov:main
from
usingtechnology:chore/CCP-5189_uploadfilescleanup
Jul 7, 2026
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,26 @@ | ||
| const fs = require('fs').promises; | ||
| const path = require('path'); | ||
| const os = require('os'); | ||
| const Problem = require('api-problem'); | ||
| const clamAvScanner = require('../../../components/clamAvScanner'); | ||
| const log = require('../../../components/log')(module.filename); | ||
| const { fileUpload } = require('./upload'); | ||
| const uploadCleanup = require('../uploadCleanup'); | ||
|
|
||
| /** | ||
| * Validates that a file path is within a specified base directory. | ||
| * @param {string} filePath - The file path to validate. | ||
| * @param {string} [baseDirectory=os.tmpdir()] - The base directory to restrict file operations. | ||
| * @param {string} [baseDirectory] - The base directory to restrict file operations. | ||
| * @returns {string} - The resolved and validated file path. | ||
| * @throws {Error} - Throws an error if the file path is outside the base directory. | ||
| */ | ||
| const validateFilePath = (filePath, baseDirectory = os.tmpdir()) => { | ||
| // Resolve the absolute path of the base directory | ||
| const resolvedBaseDir = path.resolve(baseDirectory); | ||
|
|
||
| // Resolve the absolute path of the file | ||
| const resolvedFilePath = path.resolve(resolvedBaseDir, filePath); | ||
|
|
||
| // Ensure the resolved file path is within the base directory | ||
| if (!resolvedFilePath.startsWith(resolvedBaseDir)) { | ||
| throw new Error(`Invalid file path: ${filePath} is outside the allowed directory.`); | ||
| } | ||
|
|
||
| return resolvedFilePath; | ||
| const validateFilePath = (filePath, baseDirectory) => { | ||
| return uploadCleanup.resolveUploadPath(filePath, baseDirectory || fileUpload.getFileUploadsDir()); | ||
| }; | ||
|
|
||
| /** | ||
| * Removes an infected file from the filesystem. | ||
| * @param {string} filePath - The path of the file to remove. | ||
| */ | ||
| const removeInfected = async (filePath) => { | ||
| try { | ||
| if (validateFilePath(filePath)) { | ||
| await fs.unlink(filePath); | ||
| log.info(`Deleted infected file: ${filePath}`); | ||
| } | ||
| } catch (error) { | ||
| log.error(`Could not delete infected file: ${filePath}. ${error.message}`); | ||
| } | ||
| await uploadCleanup.removeUploadedFile(filePath, 'infected'); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -66,21 +47,22 @@ | |
| return next(); | ||
| } | ||
|
|
||
| const { path: filePath, originalname: fileName } = req.file; | ||
|
|
||
| try { | ||
| const { path: filePath, originalname: fileName } = req.file; | ||
| const scanResult = await clamAvScanner.scanFile(filePath); | ||
|
|
||
| log.info(`${fileName} scanned. Is infected? ${scanResult.isInfected}. Viruses: ${scanResult.viruses || 'None'}`); | ||
|
|
||
| if (scanResult.isInfected) { | ||
| const validatedPath = validateFilePath(filePath); | ||
| await removeInfected(validatedPath); | ||
| await removeInfected(filePath); | ||
| return next(createVirusProblem(fileName, scanResult.viruses)); | ||
| } | ||
|
|
||
| next(); | ||
| } catch (error) { | ||
| log.error(`Error scanning file: ${req.file?.originalname || 'unknown'}. ${error.message}`); | ||
| log.error(`Error scanning file: ${fileName || 'unknown'}. ${error.message}`); | ||
|
Check warning on line 64 in app/src/forms/file/middleware/virusScan.js
|
||
| await uploadCleanup.removeUploadedFile(filePath, 'scan-error'); | ||
| next(error); | ||
| } | ||
| }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.