fix: filename validation (cannot be directory)

This commit is contained in:
Lam Wei Li 2022-05-22 15:48:09 +08:00
parent 6de1da298e
commit d182204079
No known key found for this signature in database
GPG Key ID: 90F6ABECF080D7BF
4 changed files with 38 additions and 4 deletions

View File

@ -29,6 +29,8 @@ function mainSighupHandler() {
function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset) {
if (typeof file !== "string" || file.length === 0) {
throw new Error(`Invalid filename: ${file}`);
} else if (file.endsWith(path.sep)) {
throw new Error(`Filename is a directory: ${file}`);
}
file = path.normalize(file);
numBackups = (!numBackups && numBackups !== 0) ? 5 : numBackups;

View File

@ -164,6 +164,8 @@ class RollingFileSync {
function fileAppender(file, layout, logSize, numBackups, options, timezoneOffset) {
if (typeof file !== "string" || file.length === 0) {
throw new Error(`Invalid filename: ${file}`);
} else if (file.endsWith(path.sep)) {
throw new Error(`Filename is a directory: ${file}`);
}
file = path.normalize(file);
numBackups = (!numBackups && numBackups !== 0) ? 5 : numBackups;

View File

@ -50,7 +50,6 @@ test("log4js fileAppender", batch => {
batch.test("should give error if invalid filename", async t => {
const file = "";
const expectedError = new Error(`Invalid filename: ${file}`);
t.throws(
() =>
log4js.configure({
@ -64,7 +63,23 @@ test("log4js fileAppender", batch => {
default: { appenders: ["file"], level: "debug" }
}
}),
expectedError
new Error(`Invalid filename: ${file}`)
);
const dir = `.${path.sep}`;
t.throws(
() =>
log4js.configure({
appenders: {
file: {
type: "file",
filename: dir
}
},
categories: {
default: { appenders: ["file"], level: "debug" }
}
}),
new Error(`Filename is a directory: ${dir}`)
);
t.end();
});

View File

@ -78,7 +78,6 @@ test("log4js fileSyncAppender", batch => {
batch.test("should give error if invalid filename", async t => {
const file = "";
const expectedError = new Error(`Invalid filename: ${file}`);
t.throws(
() =>
log4js.configure({
@ -92,7 +91,23 @@ test("log4js fileSyncAppender", batch => {
default: { appenders: ["file"], level: "debug" }
}
}),
expectedError
new Error(`Invalid filename: ${file}`)
);
const dir = `.${path.sep}`;
t.throws(
() =>
log4js.configure({
appenders: {
file: {
type: "fileSync",
filename: dir
}
},
categories: {
default: { appenders: ["file"], level: "debug" }
}
}),
new Error(`Filename is a directory: ${dir}`)
);
t.end();
});