Skip to content

Commit

Permalink
fix: redact passwords from http logs
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Aug 17, 2020
1 parent c167170 commit 3c294eb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
8 changes: 6 additions & 2 deletions check-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ function logRequest (method, res, startTime, opts) {

let urlStr
try {
const { URL } = require('url')
const url = new URL(res.url)
urlStr = res.url.replace(url.password, '***')
} catch {
if (url.password) {
url.password = '***'
}
urlStr = url.toString()
} catch (er) {
urlStr = res.url
}

Expand Down
42 changes: 42 additions & 0 deletions test/check-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,48 @@ test('log x-fetch-attempts header value', t => {
t.plan(2)
})

test('log the url fetched', async t => {
const headers = new Headers()
const EE = require('events')
headers.get = header => undefined
const res = Object.assign({}, mockFetchRes, {
headers,
status: 200,
url: 'http://example.com/foo/bar/baz',
body: new EE()
})
checkResponse('get', res, 'registry', Date.now(), {
log: Object.assign({}, silentLog, {
http (header, msg) {
t.equal(header, 'fetch')
t.equal(msg, 'GET 200 http://example.com/foo/bar/baz 0ms')
}
})
})
res.body.emit('end')
})

test('redact password from log', async t => {
const headers = new Headers()
const EE = require('events')
headers.get = header => undefined
const res = Object.assign({}, mockFetchRes, {
headers,
status: 200,
url: 'http://username:password@example.com/foo/bar/baz',
body: new EE()
})
checkResponse('get', res, 'registry', Date.now(), {
log: Object.assign({}, silentLog, {
http (header, msg) {
t.equal(header, 'fetch')
t.equal(msg, 'GET 200 http://username:***@example.com/foo/bar/baz 0ms')
}
})
})
res.body.emit('end')
})

test('bad-formatted warning headers', t => {
const headers = new Headers()
headers.has = header => header === 'warning' ? 'foo' : undefined
Expand Down

0 comments on commit 3c294eb

Please sign in to comment.