unjs / consola

🐨 Elegant Console Logger for Node.js and Browser
Other
5.76k stars 163 forks source link

infinite loop protection #298

Open danielroe opened 3 weeks ago

danielroe commented 3 weeks ago

Describe the feature

Discovered in https://github.com/nuxt/nuxt/issues/27130, it's possible to create an infinite loop when getters call console.log, which is true for Vue.

Reproduction: https://codepen.io/danielroe/pen/GRaqZOj?editors=1111

import { consola } from "consola"

consola.wrapConsole()

const scaryObject = {
  get value () {
    console.warn('Do not access me!', scaryObject)
  }
}

consola.log(scaryObject)

I wonder if it would be worth trying to add protection against this kind of thing? Maybe JSON.stringify could be called lazily?

Additional information

pi0 commented 3 weeks ago

Util.format handles the recursive structures (but it is only for Node reporters) just to confirm you happened to have this issue with a testing/browser env right?

danielroe commented 3 weeks ago

Yes, this was in a browser env - however I could also reproduce with this code in the Node REPL:

const { consola } = require('consola')

consola.wrapConsole()

const scaryObject = {
  get value () {
    console.warn('Do not access me!', scaryObject)
  }
}

consola.log(scaryObject)