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
Forward port of 5f33040

Fix npm/cli#476 for pacote v10.

Fix #22
  • Loading branch information
isaacs committed Dec 4, 2019
1 parent ccc9e20 commit bad55cd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
5 changes: 3 additions & 2 deletions lib/util/git/opts.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const gitEnv = require('./env.js')
module.exports = (_gitOpts = {}, opts = {}) => {
const isRoot = process.getuid && process.getuid() === 0
const gitOpts = {
env: gitEnv()
}
if (+opts.uid && !isNaN(opts.uid)) {
if (isRoot && +opts.uid && !isNaN(opts.uid)) {
gitOpts.uid = +opts.uid
}
if (+opts.gid && !isNaN(opts.gid)) {
if (isRoot && +opts.gid && !isNaN(opts.gid)) {
gitOpts.gid = +opts.gid
}
Object.assign(gitOpts, _gitOpts)
Expand Down
52 changes: 38 additions & 14 deletions test/util/git/opts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
const t = require('tap')
const gitOpts = require('../../../lib/util/git/opts.js')
const gitEnv = require('../../../lib/util/git/env.js')
t.match(gitOpts({
foo: 'bar',
env: { override: 'for some reason' },
}, {
uid: 420,
gid: 69,
abc: 'def',
}), {
foo: 'bar',
env: { override: 'for some reason' },
uid: 420,
gid: 69,
abc: undefined,
}, 'copied relevant opts, not irrelevant ones')

t.match(gitOpts().env, gitEnv(), 'got the git env by default')

t.test('as root', t => {
process.getuid = () => 0
t.match(gitOpts({
foo: 'bar',
env: { override: 'for some reason' },
}, {
uid: 420,
gid: 69,
abc: 'def',
}), {
foo: 'bar',
env: { override: 'for some reason' },
uid: 420,
gid: 69,
abc: undefined,
}, 'copied relevant opts, not irrelevant ones')
t.end()
})

t.test('as non-root', t => {
process.getuid = () => 999
t.match(gitOpts({
foo: 'bar',
env: { override: 'for some reason' },
}, {
uid: 420,
gid: 69,
abc: 'def',
}), {
foo: 'bar',
env: { override: 'for some reason' },
uid: undefined,
gid: undefined,
abc: undefined,
}, 'do not set uid/gid as non-root')
t.end()
})

0 comments on commit bad55cd

Please sign in to comment.