Skip to content

Commit 3306024

Browse files
chore: sonar issues
1 parent 64a9f6a commit 3306024

3 files changed

Lines changed: 17 additions & 9 deletions

File tree

app/src/forms/file/middleware/upload.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,21 @@ const sanitizeFilename = (filename) => {
7474
};
7575

7676
const fileSetup = (options) => {
77-
fileUploadsDir = (options && options.dir) || process.env.FILE_UPLOADS_DIR || defaultUploadsDir();
77+
fileUploadsDir = options?.dir || process.env.FILE_UPLOADS_DIR || defaultUploadsDir();
7878
try {
7979
fs.ensureDirSync(fileUploadsDir);
8080
} catch (error) {
8181
log.error(`Error creating file uploads directory '${fileUploadsDir}':`, error);
8282
throw new Error(`Could not create file uploads directory '${fileUploadsDir}'.`);
8383
}
8484

85-
maxFileSize = (options && options.maxFileSize) || process.env.FILE_UPLOADS_MAX_FILE_SIZE || '25MB';
85+
maxFileSize = options?.maxFileSize || process.env.FILE_UPLOADS_MAX_FILE_SIZE || '25MB';
8686
maxFileSize = bytes.parse(maxFileSize);
8787
if (maxFileSize === null) {
8888
throw new Error('Could not determine max file size (bytes) for file uploads.');
8989
}
9090

91-
maxFileCount = (options && options.maxFileCount) || process.env.FILE_UPLOADS_MAX_FILE_COUNT || '1';
91+
maxFileCount = options?.maxFileCount || process.env.FILE_UPLOADS_MAX_FILE_COUNT || '1';
9292
maxFileCount = parseInt(maxFileCount);
9393
if (isNaN(maxFileCount)) {
9494
maxFileCount = 1;
@@ -101,7 +101,7 @@ const fileUpload = {
101101
init(options) {
102102
let { fileUploadsDir, maxFileSize, maxFileCount } = fileSetup(options);
103103

104-
const formFieldName = (options && options.fieldName) || process.env.FILE_UPLOADS_FIELD_NAME || 'files';
104+
const formFieldName = options?.fieldName || process.env.FILE_UPLOADS_FIELD_NAME || 'files';
105105

106106
storage = multer.diskStorage({
107107
destination: function (_req, _file, callback) {

app/src/forms/file/middleware/virusScan.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const log = require('../../../components/log')(module.filename);
44
const { fileUpload } = require('./upload');
55
const uploadCleanup = require('../uploadCleanup');
66

7+
/**
8+
* Neutralizes user-controlled text before it is written to a log line by
9+
* stripping control characters (CR/LF/etc.), preventing log injection/forging.
10+
* @param {*} value - The value to sanitize (e.g. an uploaded file name).
11+
* @returns {string} - The value with control characters replaced by spaces.
12+
*/
13+
const sanitizeForLog = (value) => String(value ?? '').replace(/\p{Cc}/gu, ' ');
14+
715
/**
816
* Validates that a file path is within a specified base directory.
917
* @param {string} filePath - The file path to validate.
@@ -52,7 +60,7 @@ const scanFile = async (req, res, next) => {
5260
try {
5361
const scanResult = await clamAvScanner.scanFile(filePath);
5462

55-
log.info(`${fileName} scanned. Is infected? ${scanResult.isInfected}. Viruses: ${scanResult.viruses || 'None'}`);
63+
log.info(`${sanitizeForLog(fileName)} scanned. Is infected? ${scanResult.isInfected}. Viruses: ${scanResult.viruses || 'None'}`);
5664

5765
if (scanResult.isInfected) {
5866
await removeInfected(filePath);
@@ -61,7 +69,7 @@ const scanFile = async (req, res, next) => {
6169

6270
next();
6371
} catch (error) {
64-
log.error(`Error scanning file: ${fileName || 'unknown'}. ${error.message}`);
72+
log.error(`Error scanning file: ${sanitizeForLog(fileName) || 'unknown'}. ${error.message}`);
6573
await uploadCleanup.removeUploadedFile(filePath, 'scan-error');
6674
next(error);
6775
}

app/src/forms/file/uploadCleanup.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ const startUploadCleanupScheduler = (options = {}) => {
186186
return;
187187
}
188188

189-
const staleAgeMinutes = parseInt(options.staleAgeMinutes, 10) || 1440;
190-
const intervalMinutes = parseInt(options.intervalMinutes, 10) || 60;
191-
const batchSize = parseInt(options.batchSize, 10) || 100;
189+
const staleAgeMinutes = Number.parseInt(options.staleAgeMinutes, 10) || 1440;
190+
const intervalMinutes = Number.parseInt(options.intervalMinutes, 10) || 60;
191+
const batchSize = Number.parseInt(options.batchSize, 10) || 100;
192192

193193
const runSweep = async () => {
194194
try {

0 commit comments

Comments
 (0)