Skip to content

Commit f6028e6

Browse files
fix: skip redacting urls meant for opening by the user (#8614)
Login urls have tokens in them and need to be shown to the user on stdout, especially if they have no browser and need to copy/paste. This builds off of #8587 and changes things to use proc-log's META notation, which is how this kind of info is sent. --------- Co-authored-by: Jolyn <jolyndenning@gmail.com>
1 parent 54fd27f commit f6028e6

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

lib/utils/display.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,11 @@ class Display {
370370
#writeOutput (level, meta, ...args) {
371371
switch (level) {
372372
case output.KEYS.standard:
373-
this.#write(this.#stdout, {}, ...args)
373+
this.#write(this.#stdout, meta, ...args)
374374
break
375375

376376
case output.KEYS.error:
377-
this.#write(this.#stderr, {}, ...args)
377+
this.#write(this.#stderr, meta, ...args)
378378
break
379379
}
380380
}

lib/utils/format.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ function STRIP_C01 (str) {
4141
return result
4242
}
4343

44-
const formatWithOptions = ({ prefix: prefixes = [], eol = '\n', ...options }, ...args) => {
44+
const formatWithOptions = ({ prefix: prefixes = [], eol = '\n', redact = true, ...options }, ...args) => {
4545
const prefix = prefixes.filter(p => p != null).join(' ')
46-
const formatted = redactLog(STRIP_C01(baseFormatWithOptions(options, ...args)))
46+
let formatted = STRIP_C01(baseFormatWithOptions(options, ...args))
47+
if (redact) {
48+
formatted = redactLog(formatted)
49+
}
4750
// Splitting could be changed to only `\n` once we are sure we only emit unix newlines.
4851
// The eol param to this function will put the correct newlines in place for the returned string.
4952
const lines = formatted.split(/\r?\n/)

lib/utils/open-url.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { open } = require('@npmcli/promise-spawn')
2-
const { output, input } = require('proc-log')
2+
const { output, input, META } = require('proc-log')
33
const { URL } = require('node:url')
44
const readline = require('node:readline/promises')
55
const { once } = require('node:events')
@@ -18,7 +18,8 @@ const outputMsg = (json, title, url) => {
1818
if (json) {
1919
output.buffer({ title, url })
2020
} else {
21-
output.standard(`${title}:\n${url}`)
21+
// These urls are sometimes specifically login urls so we have to turn off redaction to standard output
22+
output.standard(`${title}:\n${url}`, { [META]: true, redact: false })
2223
}
2324
}
2425

mock-registry/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class MockRegistry {
284284

285285
weblogin ({ token = 'npm_default-test-token' }) {
286286
const doneUrl = new URL('/npm-cli-test/done', this.origin).href
287-
const loginUrl = new URL('/npm-cli-test/login', this.origin).href
287+
const loginUrl = new URL('/npm-cli-test/login/cli/00000000-0000-0000-0000-000000000000', this.origin).href
288288
this.nock = this.nock
289289
.post(this.fullPath('/-/v1/login'), () => {
290290
return true

test/lib/commands/login.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ t.test('legacy', t => {
119119

120120
t.test('web', t => {
121121
t.test('basic login', async t => {
122-
const { npm, registry, login, rc } = await mockLogin(t, {
122+
const { outputs, npm, registry, login, rc } = await mockLogin(t, {
123123
config: { 'auth-type': 'web' },
124124
})
125125
registry.weblogin({ token: 'npm_test-token' })
@@ -128,6 +128,7 @@ t.test('web', t => {
128128
t.same(rc(), {
129129
'//registry.npmjs.org/:_authToken': 'npm_test-token',
130130
})
131+
t.match(outputs[0], '/npm-cli-test/login/cli/00000000-0000-0000-0000-000000000000')
131132
})
132133
t.test('server error', async t => {
133134
const { registry, login } = await mockLogin(t, {

0 commit comments

Comments
 (0)