-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(git): Do not drop uid/gid when executing in root-owned directory
Fix: npm/cli#624 Fix: npm/cli#642 Fix: npm/cli#514 Infer the ownership of a git command invocation based on the cwd, if one is specified.
- Loading branch information
Showing
2 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict' | ||
const t = require('tap') | ||
|
||
process.getuid = () => 0 | ||
process.getgid = () => 0 | ||
|
||
const requireInject = require('require-inject') | ||
const fs = require('fs') | ||
const { _cwdOwner: cwdOwner } = requireInject('../lib/util/git.js', { | ||
fs: Object.assign({}, fs, { | ||
lstat: (path, cb) => { | ||
if (path === '/var/root/.npm/_cacache/bleep/boop') { | ||
return process.nextTick(() => cb(null, { uid: 0, gid: 0 })) | ||
} | ||
if (path === '/home/user/.npm/_cacache/bleep/boop') { | ||
return process.nextTick(() => cb(null, { uid: 69, gid: 420 })) | ||
} | ||
fs.lstat(path, cb) | ||
}, | ||
stat: (path, cb) => { | ||
if (path === '/var/root/.npm/_cacache/bleep/boop') { | ||
return process.nextTick(() => cb(null, { uid: 0, gid: 0 })) | ||
} | ||
if (path === '/home/user/.npm/_cacache/bleep/boop') { | ||
return process.nextTick(() => cb(null, { uid: 69, gid: 420 })) | ||
} | ||
fs.stat(path, cb) | ||
} | ||
}) | ||
}) | ||
|
||
t.test('running in root-owned folder, but with non-root uid/gid opts', t => { | ||
const gitOpts = { | ||
cwd: '/var/root/.npm/_cacache/bleep/boop' | ||
} | ||
const opts = { | ||
uid: 69, | ||
gid: 420 | ||
} | ||
return cwdOwner(gitOpts, opts).then(() => { | ||
t.strictSame(gitOpts, { | ||
cwd: '/var/root/.npm/_cacache/bleep/boop', | ||
uid: 0, | ||
gid: 0 | ||
}) | ||
}) | ||
}) | ||
|
||
t.test('running in non-root-owned folder', t => { | ||
const gitOpts = { | ||
cwd: '/home/user/.npm/_cacache/bleep/boop' | ||
} | ||
const opts = { | ||
uid: 12, | ||
gid: 34 | ||
} | ||
return cwdOwner(gitOpts, opts).then(() => { | ||
t.strictSame(gitOpts, { | ||
cwd: '/home/user/.npm/_cacache/bleep/boop', | ||
uid: 69, | ||
gid: 420 | ||
}) | ||
}) | ||
}) | ||
|
||
t.test('running without a cwd', t => { | ||
const gitOpts = {} | ||
const opts = { | ||
uid: 12, | ||
gid: 34 | ||
} | ||
return cwdOwner(gitOpts, opts).then(() => { | ||
t.strictSame(gitOpts, {}) | ||
}) | ||
}) |