Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vsemozhetbyt opened this issue Jun 16, 2017 · 4 comments
Closed

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

vsemozhetbyt opened this issue Jun 16, 2017 · 4 comments
Labels
util Issues and PRs related to the built-in util module.

Comments

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Jun 16, 2017

  • 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.
@vsemozhetbyt vsemozhetbyt added the util Issues and PRs related to the built-in util module. label Jun 16, 2017
@vsemozhetbyt
Copy link
Contributor Author

vsemozhetbyt commented Jun 17, 2017

It seems we have a test for only one simple section variant.

@vsemozhetbyt
Copy link
Contributor Author

cc @bnoordhuis, @cjihrig, @evanlucas
Should we address this issue? If so, what way should we prefer?

@evanlucas
Copy link
Contributor

My understanding is that this was/is meant only for use inside core, so I'm not sure we should do anything about it?

@vsemozhetbyt
Copy link
Contributor Author

vsemozhetbyt commented Jun 21, 2017

As it is documented without any precautions, it seems this is for userland debug usage as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

2 participants