Skip to content

Commit 5f33040

Browse files
committed
fix: Do not drop perms in git when not root
Fix: npm/cli#476 PR-URL: #23 Credit: @isaacs Close: #23 Reviewed-by: @darcyclarke
1 parent 4c78d76 commit 5f33040

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/util/git.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,17 @@ function spawnGit (gitArgs, gitOpts, opts) {
234234
})
235235
}
236236

237+
module.exports._mkOpts = mkOpts
237238
function mkOpts (_gitOpts, opts) {
238239
const gitOpts = {
239240
env: gitEnv()
240241
}
241-
if (+opts.uid && !isNaN(opts.uid)) {
242+
const isRoot = process.getuid && process.getuid() === 0
243+
// don't change child process uid/gid if not root
244+
if (+opts.uid && !isNaN(opts.uid) && isRoot) {
242245
gitOpts.uid = +opts.uid
243246
}
244-
if (+opts.gid && !isNaN(opts.gid)) {
247+
if (+opts.gid && !isNaN(opts.gid) && isRoot) {
245248
gitOpts.gid = +opts.gid
246249
}
247250
Object.assign(gitOpts, _gitOpts)

test/git.mkopts.uid.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
const t = require('tap')
3+
const { _mkOpts: mkOpts } = require('../lib/util/git.js')
4+
const getuid = process.getuid
5+
6+
t.test('mkOpts sets perms when root', t => {
7+
t.teardown(() => {
8+
process.getuid = getuid
9+
})
10+
process.getuid = () => 0
11+
t.match(mkOpts({}, { uid: 1234, gid: 1234 }), { uid: 1234, gid: 1234 })
12+
t.end()
13+
})
14+
15+
t.test('mkOpts does not set perms when not root', t => {
16+
t.teardown(() => {
17+
process.getuid = getuid
18+
})
19+
process.getuid = () => 4321
20+
t.match(mkOpts({}, { uid: 1234, gid: 1234 }), { uid: undefined, gid: undefined })
21+
t.end()
22+
})

0 commit comments

Comments
 (0)