Skip to content

Commit

Permalink
Fix hasOwnProperty usage (pinojs#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshkel authored Mar 23, 2022
1 parent 4173ff5 commit 572ac0a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,14 @@ function asJson (obj, msg, num, time) {
data = data + chindings

let value
const notHasOwnProperty = obj.hasOwnProperty === undefined
if (formatters.log) {
obj = formatters.log(obj)
}
const wildcardStringifier = stringifiers[wildcardFirstSym]
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
Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/pretty/null-prototype.js
Original file line number Diff line number Diff line change
@@ -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')
12 changes: 12 additions & 0 deletions test/pretty.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 572ac0a

Please sign in to comment.