Skip to content

Commit d352e27

Browse files
authored
fix: do not redact notice logs going to stdout (#8629)
These have 2fa links in them and need to include the uuids. Also un-wraps the comments in lib/utils/display.js as a small cleanup.
1 parent 67cfaf3 commit d352e27

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

lib/utils/display.js

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ const setBlocking = (stream) => {
7575
return stream
7676
}
7777

78-
// These are important
7978
// This is the key that is returned to the user for errors
8079
const ERROR_KEY = 'error'
81-
// This is the key producers use to indicate that there
82-
// is a json error that should be merged into the finished output
80+
// This is the key producers use to indicate that there is a json error that should be merged into the finished output
8381
const JSON_ERROR_KEY = 'jsonError'
8482

8583
const isPlainObject = (v) => v && typeof v === 'object' && !Array.isArray(v)
@@ -120,15 +118,12 @@ const getJsonBuffer = ({ [JSON_ERROR_KEY]: metaError }, buffer) => {
120118

121119
const res = getArrayOrObject(items)
122120

123-
// This skips any error checking since we can only set an error property
124-
// on an object that can be stringified
121+
// This skips any error checking since we can only set an error property on an object that can be stringified
125122
// XXX(BREAKING_CHANGE): remove this in favor of always returning an object with result and error keys
126123
if (isPlainObject(res) && errors.length) {
127-
// This is not ideal. JSON output has always been keyed at the root with an `error`
128-
// key, so we cant change that without it being a breaking change. At the same time
129-
// some commands output arbitrary keys at the top level of the output, such as package
130-
// names. So the output could already have the same key. The choice here is to overwrite
131-
// it with our error since that is (probably?) more important.
124+
// This is not ideal.
125+
// JSON output has always been keyed at the root with an `error` key, so we cant change that without it being a breaking change. At the same time some commands output arbitrary keys at the top level of the output, such as package names.
126+
// So the output could already have the same key. The choice here is to overwrite it with our error since that is (probably?) more important.
132127
// XXX(BREAKING_CHANGE): all json output should be keyed under well known keys, eg `result` and `error`
133128
if (res[ERROR_KEY]) {
134129
log.warn('', `overwriting existing ${ERROR_KEY} on json output`)
@@ -227,9 +222,7 @@ class Display {
227222
import('chalk'),
228223
import('supports-color'),
229224
])
230-
// we get the chalk level based on a null stream meaning chalk will only use
231-
// what it knows about the environment to get color support since we already
232-
// determined in our definitions that we want to show colors.
225+
// we get the chalk level based on a null stream meaning chalk will only use what it knows about the environment to get color support since we already determined in our definitions that we want to show colors.
233226
const level = Math.max(createSupportsColor(null).level, 1)
234227
this.#noColorChalk = new Chalk({ level: 0 })
235228
this.#stdoutColor = stdoutColor
@@ -265,8 +258,7 @@ class Display {
265258

266259
// HANDLERS
267260

268-
// Arrow function assigned to a private class field so it can be passed
269-
// directly as a listener and still reference "this"
261+
// Arrow function assigned to a private class field so it can be passed directly as a listener and still reference "this"
270262
#logHandler = withMeta((level, meta, ...args) => {
271263
switch (level) {
272264
case log.KEYS.resume:
@@ -289,8 +281,7 @@ class Display {
289281
}
290282
})
291283

292-
// Arrow function assigned to a private class field so it can be passed
293-
// directly as a listener and still reference "this"
284+
// Arrow function assigned to a private class field so it can be passed directly as a listener and still reference "this"
294285
#outputHandler = withMeta((level, meta, ...args) => {
295286
this.#json = typeof meta.json === 'boolean' ? meta.json : this.#json
296287
switch (level) {
@@ -316,18 +307,14 @@ class Display {
316307
if (this.#outputState.buffering) {
317308
this.#outputState.buffer.push([level, meta, ...args])
318309
} else {
319-
// HACK: Check if the argument looks like a run-script banner. This can be
320-
// replaced with proc-log.META in @npmcli/run-script
310+
// HACK: Check if the argument looks like a run-script banner. This can be replaced with proc-log.META in @npmcli/run-script
321311
if (typeof args[0] === 'string' && args[0].startsWith('\n> ') && args[0].endsWith('\n')) {
322312
if (this.#silent || ['exec', 'explore'].includes(this.#command)) {
323313
// Silent mode and some specific commands always hide run script banners
324314
break
325315
} else if (this.#json) {
326-
// In json mode, change output to stderr since we don't want to break json
327-
// parsing on stdout if the user is piping to jq or something.
328-
// XXX: in a future (breaking?) change it might make sense for run-script to
329-
// always output these banners with proc-log.output.error if we think they
330-
// align closer with "logging" instead of "output"
316+
// In json mode, change output to stderr since we don't want to break json parsing on stdout if the user is piping to jq or something.
317+
// XXX: in a future (breaking?) change it might make sense for run-script to always output these banners with proc-log.output.error if we think they align closer with "logging" instead of "output"
331318
level = output.KEYS.error
332319
}
333320
}
@@ -352,14 +339,12 @@ class Display {
352339
break
353340

354341
case input.KEYS.read: {
355-
// The convention when calling input.read is to pass in a single fn that returns
356-
// the promise to await. resolve and reject are provided by proc-log
342+
// The convention when calling input.read is to pass in a single fn that returns the promise to await. resolve and reject are provided by proc-log
357343
const [res, rej, p] = args
358344
return input.start(() => p()
359345
.then(res)
360346
.catch(rej)
361-
// Any call to procLog.input.read will render a prompt to the user, so we always
362-
// add a single newline of output to stdout to move the cursor to the next line
347+
// Any call to procLog.input.read will render a prompt to the user, so we always add a single newline of output to stdout to move the cursor to the next line
363348
.finally(() => output.standard('')))
364349
}
365350
}
@@ -383,10 +368,7 @@ class Display {
383368

384369
#tryWriteLog (level, meta, ...args) {
385370
try {
386-
// Also (and this is a really inexcusable kludge), we patch the
387-
// log.warn() method so that when we see a peerDep override
388-
// explanation from Arborist, we can replace the object with a
389-
// highly abbreviated explanation of what's being overridden.
371+
// Also (and this is a really inexcusable kludge), we patch the log.warn() method so that when we see a peerDep override explanation from Arborist, we can replace the object with a highly abbreviated explanation of what's being overridden.
390372
// TODO: this could probably be moved to arborist now that display is refactored
391373
const [heading, message, expl] = args
392374
if (level === log.KEYS.warn && heading === 'ERESOLVE' && expl && typeof expl === 'object') {
@@ -400,8 +382,7 @@ class Display {
400382
// if it crashed once, it might again!
401383
this.#writeLog(log.KEYS.verbose, meta, '', `attempt to log crashed`, ...args, ex)
402384
} catch (ex2) {
403-
// This happens if the object has an inspect method that crashes so just console.error
404-
// with the errors but don't do anything else that might error again.
385+
// This happens if the object has an inspect method that crashes so just console.error with the errors but don't do anything else that might error again.
405386
// eslint-disable-next-line no-console
406387
console.error(`attempt to log crashed`, ex, ex2)
407388
}
@@ -421,7 +402,12 @@ class Display {
421402
this.#logColors[level](level),
422403
title ? this.#logColors.title(title) : null,
423404
]
424-
this.#write(this.#stderr, { prefix }, ...args)
405+
const writeOpts = { prefix }
406+
// notice logs typically come from `npm-notice` headers in responses. Some of them have 2fa login links so we skip redaction.
407+
if (level === 'notice') {
408+
writeOpts.redact = false
409+
}
410+
this.#write(this.#stderr, writeOpts, ...args)
425411
}
426412
}
427413
}
@@ -480,9 +466,8 @@ class Progress {
480466
this.#render()
481467
}
482468

483-
// If we are currently rendering the spinner we clear it
484-
// before writing our line and then re-render the spinner after.
485-
// If not then all we need to do is write the line
469+
// If we are currently rendering the spinner we clear it before writing our line and then re-render the spinner after.
470+
// If not then all we need to do is write the line.
486471
write (write) {
487472
if (this.#spinning) {
488473
this.#clearSpinner()
@@ -510,8 +495,7 @@ class Progress {
510495
if (!this.#rendering) {
511496
return
512497
}
513-
// We always attempt to render immediately but we only request to move to the next
514-
// frame if it has been longer than our spinner frame duration since our last update
498+
// We always attempt to render immediately but we only request to move to the next frame if it has been longer than our spinner frame duration since our last update
515499
this.#renderFrame(Date.now() - this.#lastUpdate >= this.#spinner.duration)
516500
clearInterval(this.#interval)
517501
this.#interval = setInterval(() => this.#renderFrame(true), this.#spinner.duration)

0 commit comments

Comments
 (0)