Skip to content

Commit 6ed8354

Browse files
author
Zachary Haber
committed
fix: defend against bad inputs to eventLayout
1 parent 2760da8 commit 6ed8354

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function getErrorStack(logData) {
3434
* @param {import('../types').LogFacesHTTPAppender} config
3535
*/
3636
function logFacesAppender(config) {
37+
if (config.eventLayout && typeof config.eventLayout !== 'function') {
38+
throw new TypeError('eventLayout must be a function');
39+
}
40+
3741
const sender = axios.create({
3842
baseURL: config.url,
3943
timeout: config.timeout || 5000,
@@ -95,6 +99,17 @@ function logFacesAppender(config) {
9599
// no event object returned, consider the event ignored
96100
return;
97101
}
102+
if (
103+
typeof lfsEvent !== 'object' ||
104+
Array.isArray(lfsEvent) ||
105+
Object.keys(lfsEvent).length < 3
106+
) {
107+
// eslint-disable-next-line no-console
108+
console.error(
109+
`log4js.logFaces-HTTP Appender eventLayout must be an object`
110+
);
111+
return;
112+
}
98113
}
99114
if (lfsEvent.m == null) {
100115
// Add the default message on if not set

test/tap/index-test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ function setupLogging(category, enableCallStack, options) {
2121
};
2222
},
2323
};
24-
24+
/**
25+
* @type {{msg?: string, error(msg:string):void}}
26+
*/
2527
const fakeConsole = {
2628
error(msg) {
2729
this.msg = msg;
@@ -234,7 +236,40 @@ test('logFaces appender', (batch) => {
234236

235237
t.end();
236238
});
239+
batch.test('eventLayout should error if set incorrectly', (t) => {
240+
t.throws(() => {
241+
setupLogging('myCategory', false, {
242+
application: 'LFS-HTTP',
243+
url: 'http://localhost/receivers/rx1',
244+
eventLayout: 'banana',
245+
});
246+
});
247+
const setup = setupLogging('myCategory', false, {
248+
application: 'LFS-HTTP',
249+
url: 'http://localhost/receivers/rx1',
250+
eventLayout: (event) => {
251+
if (event.data[0] === 'array') {
252+
return [];
253+
}
254+
if (event.data[0] === 'string') {
255+
return 'string';
256+
}
257+
if (event.data[0] === 'object') {
258+
return {};
259+
}
260+
return undefined;
261+
},
262+
});
263+
const message =
264+
'log4js.logFaces-HTTP Appender eventLayout must be an object';
265+
['array', 'string', 'object'].forEach((input) => {
266+
setup.logger.info(input);
267+
t.equal(setup.fakeConsole.msg, message);
268+
setup.fakeConsole.msg = undefined;
269+
});
237270

271+
t.end();
272+
});
238273
batch.test('eventLayout should enable changing the results', (t) => {
239274
const setup = setupLogging('myCategory', false, {
240275
application: 'LFS-HTTP',

0 commit comments

Comments
 (0)