Skip to content

Commit b72920f

Browse files
Assaf Sapirruyadorno
authored andcommitted
chore: Do not send user secret in the referer header
PR-URL: #1663 Credit: @assapir Close: #1663 Reviewed-by: @ruyadorno
1 parent 376bc08 commit b72920f

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ npm-debug.log
2626
/node_modules/.cache
2727
.DS_Store
2828
**/.DS_Store
29+
.vscode/

lib/hook.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const pudding = require('figgy-pudding')
1010
const relativeDate = require('tiny-relative-date')
1111
const Table = require('cli-table3')
1212
const validate = require('aproba')
13+
const npm = require('./npm')
1314

1415
hook.usage = [
1516
'npm hook add <pkg> <url> <secret> [--type=<type>]',
@@ -40,6 +41,10 @@ module.exports = (args, cb) => BB.try(() => hook(args)).then(
4041
err => err.code === 'EUSAGE' ? cb(err.message) : cb(err)
4142
)
4243
function hook (args) {
44+
if (args.length === 4) { // secret is passed in the args
45+
// we have the user secret in the CLI args, we need to redact it from the referer.
46+
redactUserSecret()
47+
}
4348
return otplease(npmConfig(), opts => {
4449
opts = HookConfig(opts)
4550
switch (args[0]) {
@@ -150,3 +155,11 @@ function hookName (hook) {
150155
if (hook.type === 'owner') { target = '~' + target }
151156
return target
152157
}
158+
159+
function redactUserSecret () {
160+
const referer = npm.referer
161+
if (!referer) return
162+
const splittedReferer = referer.split(' ')
163+
splittedReferer[4] = '[REDACTED]'
164+
npm.referer = splittedReferer.join(' ')
165+
}

test/tap/referer.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,63 @@ test('should send referer http header', function (t) {
2121
})
2222
})
2323
})
24+
25+
test('should redact user secret from hook add command', function (t) {
26+
http.createServer(function (q, s) {
27+
t.equal(q.headers.referer, 'hook add ~zkat [REDACTED] [REDACTED]')
28+
s.statusCode = 204
29+
s.end()
30+
this.close()
31+
}).listen(common.port, function () {
32+
var reg = `http://localhost:${common.port}`
33+
var args = [ 'hook', 'add', '~zkat', 'https://example.com', 'sekrit', '--registry', reg ]
34+
common.npm(args, {}, function (er, code) {
35+
if (er) {
36+
throw er
37+
}
38+
// should not have ended nicely, since we returned an error
39+
t.ok(code)
40+
t.end()
41+
})
42+
})
43+
})
44+
45+
test('should redact user secret from hook up command', function (t) {
46+
http.createServer(function (q, s) {
47+
t.equal(q.headers.referer, 'hook up ~zkat [REDACTED] [REDACTED]')
48+
s.statusCode = 204
49+
s.end()
50+
this.close()
51+
}).listen(common.port, function () {
52+
var reg = `http://localhost:${common.port}`
53+
var args = [ 'hook', 'up', '~zkat', 'https://example.com', 'sekrit', '--registry', reg ]
54+
common.npm(args, {}, function (er, code) {
55+
if (er) {
56+
throw er
57+
}
58+
// should not have ended nicely, since we returned an error
59+
t.ok(code)
60+
t.end()
61+
})
62+
})
63+
})
64+
65+
test('should redact user secret from hook update command', function (t) {
66+
http.createServer(function (q, s) {
67+
t.equal(q.headers.referer, 'hook update ~zkat [REDACTED] [REDACTED]')
68+
s.statusCode = 204
69+
s.end()
70+
this.close()
71+
}).listen(common.port, function () {
72+
var reg = `http://localhost:${common.port}`
73+
var args = [ 'hook', 'update', '~zkat', 'https://example.com', 'sekrit', '--registry', reg ]
74+
common.npm(args, {}, function (er, code) {
75+
if (er) {
76+
throw er
77+
}
78+
// should not have ended nicely, since we returned an error
79+
t.ok(code)
80+
t.end()
81+
})
82+
})
83+
})

0 commit comments

Comments
 (0)