Skip to content

Commit cb4a487

Browse files
authored
fix: put infer-owner back in (#12)
@npmcli/exec is not out or ready and we need to get the cli updated to use this again
1 parent ad35767 commit cb4a487

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

lib/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { spawn } = require('child_process')
2+
const inferOwner = require('infer-owner')
23

34
const isPipe = (stdio = 'pipe', fd) =>
45
stdio === 'pipe' || stdio === null ? true
@@ -8,9 +9,13 @@ const isPipe = (stdio = 'pipe', fd) =>
89
// 'extra' object is for decorating the error a bit more
910
const promiseSpawn = (cmd, args, opts = {}, extra = {}) => {
1011
const cwd = opts.cwd || process.cwd()
12+
const isRoot = process.getuid && process.getuid() === 0
13+
const { uid, gid } = isRoot ? inferOwner.sync(cwd) : {}
1114
return promiseSpawnUid(cmd, args, {
1215
...opts,
1316
cwd,
17+
uid,
18+
gid,
1419
}, extra)
1520
}
1621

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,8 @@
4141
"templateOSS": {
4242
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
4343
"version": "3.2.2"
44+
},
45+
"dependencies": {
46+
"infer-owner": "^1.0.4"
4447
}
4548
}

test/promise-spawn.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const t = require('tap')
22
const Minipass = require('minipass')
33
const EE = require('events')
4+
const fs = require('fs')
45

56
const isPipe = (stdio = 'pipe', fd) =>
67
stdio === 'pipe' || stdio === null ? true
@@ -214,3 +215,33 @@ t.test('expose process', t => {
214215
t.end()
215216
setTimeout(() => p.process.exit(0))
216217
})
218+
219+
t.test('infer ownership', t => {
220+
const { lstatSync } = fs
221+
t.teardown(() => fs.lstatSync = lstatSync)
222+
fs.lstatSync = (path) => ({ uid: 420, gid: 69 })
223+
const getuid = process.getuid
224+
t.teardown(() => process.getuid = getuid)
225+
226+
t.test('as non-root, do not change uid/gid, regardless of arguments', t => {
227+
process.getuid = () => 1234
228+
return t.resolveMatch(promiseSpawn('whoami', [], { uid: 4321, gid: 9876 }), {
229+
code: 0,
230+
signal: null,
231+
stdout: Buffer.from('UID undefined\nGID undefined\n'),
232+
stderr: Buffer.alloc(0),
233+
})
234+
})
235+
236+
t.test('as root, change uid/gid to folder, regardless of arguments', t => {
237+
process.getuid = () => 0
238+
return t.resolveMatch(promiseSpawn('whoami', [], { uid: 4321, gid: 9876 }), {
239+
code: 0,
240+
signal: null,
241+
stdout: Buffer.from('UID 420\nGID 69\n'),
242+
stderr: Buffer.alloc(0),
243+
})
244+
})
245+
246+
t.end()
247+
})

0 commit comments

Comments
 (0)