Skip to content

Commit

Permalink
fix: Do not drop perms in git when not root
Browse files Browse the repository at this point in the history
Fix: npm/cli#476
PR-URL: #23
Credit: @isaacs
Close: #23
Reviewed-by: @darcyclarke
  • Loading branch information
isaacs committed Dec 4, 2019
1 parent 4c78d76 commit 5f33040
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/util/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,17 @@ function spawnGit (gitArgs, gitOpts, opts) {
})
}

module.exports._mkOpts = mkOpts
function mkOpts (_gitOpts, opts) {
const gitOpts = {
env: gitEnv()
}
if (+opts.uid && !isNaN(opts.uid)) {
const isRoot = process.getuid && process.getuid() === 0
// don't change child process uid/gid if not root
if (+opts.uid && !isNaN(opts.uid) && isRoot) {
gitOpts.uid = +opts.uid
}
if (+opts.gid && !isNaN(opts.gid)) {
if (+opts.gid && !isNaN(opts.gid) && isRoot) {
gitOpts.gid = +opts.gid
}
Object.assign(gitOpts, _gitOpts)
Expand Down
22 changes: 22 additions & 0 deletions test/git.mkopts.uid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'
const t = require('tap')
const { _mkOpts: mkOpts } = require('../lib/util/git.js')
const getuid = process.getuid

t.test('mkOpts sets perms when root', t => {
t.teardown(() => {
process.getuid = getuid
})
process.getuid = () => 0
t.match(mkOpts({}, { uid: 1234, gid: 1234 }), { uid: 1234, gid: 1234 })
t.end()
})

t.test('mkOpts does not set perms when not root', t => {
t.teardown(() => {
process.getuid = getuid
})
process.getuid = () => 4321
t.match(mkOpts({}, { uid: 1234, gid: 1234 }), { uid: undefined, gid: undefined })
t.end()
})

0 comments on commit 5f33040

Please sign in to comment.