diff --git a/lib/tools.js b/lib/tools.js index 91df051d0..70b8bcb1f 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -108,7 +108,6 @@ function asJson (obj, msg, num, time) { data = data + chindings let value - const notHasOwnProperty = obj.hasOwnProperty === undefined if (formatters.log) { obj = formatters.log(obj) } @@ -116,7 +115,7 @@ function asJson (obj, msg, num, time) { let propStr = '' for (const key in obj) { value = obj[key] - if ((notHasOwnProperty || obj.hasOwnProperty(key)) && value !== undefined) { + if (Object.prototype.hasOwnProperty.call(obj, key) && value !== undefined) { value = serializers[key] ? serializers[key](value) : value const stringifier = stringifiers[key] || wildcardStringifier @@ -291,7 +290,7 @@ function prettifierMetaWrapper (pretty, dest, opts) { const formattedObj = formatters.log ? formatters.log(lastObj) : lastObj const messageKey = lastLogger[messageKeySym] - if (lastMsg && formattedObj && !formattedObj.hasOwnProperty(messageKey)) { + if (lastMsg && formattedObj && !Object.prototype.hasOwnProperty.call(formattedObj, messageKey)) { formattedObj[messageKey] = lastMsg } diff --git a/test/fixtures/pretty/null-prototype.js b/test/fixtures/pretty/null-prototype.js new file mode 100644 index 000000000..a74ca2cc1 --- /dev/null +++ b/test/fixtures/pretty/null-prototype.js @@ -0,0 +1,8 @@ +global.process = { __proto__: process, pid: 123456 } +Date.now = function () { return 1459875739796 } +require('os').hostname = function () { return 'abcdefghijklmnopqr' } +const pino = require(require.resolve('./../../../')) +const log = pino({ prettyPrint: true }) +const obj = Object.create(null) +Object.assign(obj, { foo: 'bar' }) +log.info(obj, 'hello') diff --git a/test/pretty.test.js b/test/pretty.test.js index 3fe30af9d..43e25a28e 100644 --- a/test/pretty.test.js +++ b/test/pretty.test.js @@ -341,6 +341,18 @@ test('works as expected with an object with the msg prop', async ({ not }) => { not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): hello/), null) }) +test('handles objects with null prototypes', async ({ not }) => { + let actual = '' + const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'null-prototype.js')]) + + child.stdout.pipe(writer((s, enc, cb) => { + actual += s + cb() + })) + await once(child, 'close') + not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): hello\s+foo: "bar"/), null) +}) + test('should not lose stream metadata for streams with `needsMetadataGsym` flag', async ({ not }) => { const dest = new Writable({ objectMode: true,