Skip to content

Commit

Permalink
Use process.stdout if specified by the developer (#1499)
Browse files Browse the repository at this point in the history
* Use process.stdout if specified by the developer

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* fixup: add test

Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Jul 21, 2022
1 parent 2430691 commit 55e25f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/tools.js
Expand Up @@ -303,9 +303,12 @@ function createArgsNormalizer (defaultOptions) {
}
const { enabled } = opts
if (enabled === false) opts.level = 'silent'
stream = stream || process.stdout
if (stream === process.stdout && stream.fd >= 0 && !hasBeenTampered(stream)) {
stream = buildSafeSonicBoom({ fd: stream.fd })
if (!stream) {
if (!hasBeenTampered(process.stdout)) {
stream = buildSafeSonicBoom({ fd: process.stdout.fd })
} else {
stream = process.stdout
}
}
return { opts, stream }
}
Expand Down
6 changes: 6 additions & 0 deletions test/stdout-protection.test.js
Expand Up @@ -5,6 +5,7 @@ const { join } = require('path')
const { fork } = require('child_process')
const { once } = require('./helper')
const writer = require('flush-write-stream')
const pino = require('..')

test('do not use SonicBoom is someone tampered with process.stdout.write', async ({ not }) => {
let actual = ''
Expand All @@ -17,3 +18,8 @@ test('do not use SonicBoom is someone tampered with process.stdout.write', async
await once(child, 'close')
not(actual.match(/^hack/), null)
})

test('do not use SonicBoom is someone has passed process.stdout to pino', async ({ equal }) => {
const logger = pino(process.stdout)
equal(logger[pino.symbols.streamSym], process.stdout)
})

0 comments on commit 55e25f8

Please sign in to comment.