Skip to content

util: util.debuglog() is buggy and poorly documented #13728

Closed
@vsemozhetbyt

Description

@vsemozhetbyt
  • Version: possibly all actual ones
  • Platform: possibly all
  • Subsystem: util

Doc says nothing about the acceptable format of the section strings. However, the check in the code has some silent restrictions/prerequisites:

  1. User input is not escaped, so if it contains unintentional RegExp special characters, things become unpredictable.
  2. Doc states the delimiters to be commas, but the code checks borders via \b symbol (word boundary) and this can make a mess: a section with non-[a-z0-9_] symbols at the beginning or the end becomes always unmatched, while a multiword section may trigger some unintended matchings.

This is prone to false positive/false negative effects and crashes. For example:

const util = require('util');

const debuglogMustCall_1 = util.debuglog('###');
const debuglogMustCall_2 = util.debuglog('f$oo');

const debuglogMustNotCall_1 = util.debuglog('f.oo');
const debuglogMustNotCall_2 = util.debuglog('bar');

debuglogMustCall_1('this should be logged');
debuglogMustCall_2('this should be logged too');

debuglogMustNotCall_1('this should not be logged');
debuglogMustNotCall_2('this should not be logged too');
> SET NODE_DEBUG=###,f$oo,no-bar-at-all
> node test.js

F.OO 5604: this should not be logged
BAR 5604: this should not be logged too
const util = require('util');

const debuglogTHROWS = util.debuglog('hi:)');

debuglogTHROWS('hi there!');
> SET NODE_DEBUG=hi:)
> node test.js

util.js:147
    if (new RegExp(`\\b${set}\\b`, 'i').test(debugEnviron)) {
        ^

SyntaxError: Invalid regular expression: /\bHI:)\b/: Unmatched ')'

What possibly could be done:

  1. Document this weird situation (like 'only one word [a-z0-9_] strings are allowed' or something).
  2. Or change the code: split the NODE_DEBUG value by commas and check via sections.map(toUpperCase).includes(set.toUpperCase()). This may be semver-major unfortunately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    utilIssues and PRs related to the built-in util module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions