const jsonReplacers = require('./json-replacers');
const dataSanitizers = require('./data-sanitizers');
-const nop = () => { /**/ };
+const nop = () => undefined;
class Logger {
/**
if (this.sanitizationNeeded(data)) {
// Create copy of data so we are not changing anything important.
- data = structuredClone(data);
+ try {
+ data = structuredClone(data);
+ } catch (e) {
+ data = JSON.parse(JSON.stringify(data, replacer));
+ }
this.sanitize(data);
}
while (ancestors.length > 0 && ancestors.at(-1) !== this) {
ancestors.pop();
}
- if (ancestors.includes(value)) { // eslint-disable-line security/detect-object-injection
+ if (ancestors.includes(value)) {
return '[Circular]';
} else {
ancestors.push(value);
}
return value;
- }
+ };
}
}
describe('Logger', function () {
let config, logger, commonObject, scope, message;
+ function sanitizeCredential(data, sanitize = true) {
+ let unclean = false;
+ const credentialLength = data?.ctx?.parsedBody?.credential?.length;
+ if (credentialLength) {
+ unclean = true;
+ }
+ if (unclean && sanitize) {
+ data.ctx.parsedBody.credential = '*'.repeat(credentialLength);
+ }
+ return unclean;
+ }
+
beforeEach(function () {
config = {};
commonObject = {
});
it('sanitizes', function () {
- logger.dataSanitizers.push((data, sanitize = true) => {
- let unclean = false;
- const credentialLength = data?.ctx?.parsedBody?.credential?.length;
- if (credentialLength) {
- unclean = true;
- }
- if (unclean && sanitize) {
- data.ctx.parsedBody.credential = '*'.repeat(credentialLength);
- }
- return unclean;
- });
+ logger.dataSanitizers.push(sanitizeCredential);
logger.info(scope, message, {
ctx: {
parsedBody: {
assert(logger.backend.info.called);
assert(logger.backend.info.args[0][0].includes('[Circular]'));
});
+
+ it('covers non-serializable objects', function () {
+ logger.dataSanitizers.push(sanitizeCredential);
+ const data = {
+ ctx: {
+ parsedBody: {
+ credential: 'foo',
+ },
+ },
+ foo: new WeakMap(),
+ };
+ logger.info(scope, message, data);
+ assert(logger.backend.info.called);
+ });
+
}); // Logger
\ No newline at end of file