Skip to content

Commit

Permalink
feat: remove infer-owner (#40)
Browse files Browse the repository at this point in the history
* feat: remove infer-owner

BREAKING CHANGE: this module no longer attempts to infer a uid and gid for processes

* fixup! feat: remove infer-owner

Co-authored-by: Gar <gar+gh@danger.computer>
  • Loading branch information
nlf and wraithgar committed Oct 26, 2022
1 parent 8bc2dd1 commit 422e1b6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 57 deletions.
33 changes: 10 additions & 23 deletions lib/index.js
@@ -1,5 +1,4 @@
const { spawn } = require('child_process')
const inferOwner = require('infer-owner')

const isPipe = (stdio = 'pipe', fd) =>
stdio === 'pipe' || stdio === null ? true
Expand All @@ -8,28 +7,6 @@ const isPipe = (stdio = 'pipe', fd) =>

// 'extra' object is for decorating the error a bit more
const promiseSpawn = (cmd, args, opts = {}, extra = {}) => {
const cwd = opts.cwd || process.cwd()
const isRoot = process.getuid && process.getuid() === 0
const { uid, gid } = isRoot ? inferOwner.sync(cwd) : {}
return promiseSpawnUid(cmd, args, {
...opts,
cwd,
uid,
gid,
}, extra)
}

const stdioResult = (stdout, stderr, { stdioString, stdio }) =>
stdioString ? {
stdout: isPipe(stdio, 1) ? Buffer.concat(stdout).toString() : null,
stderr: isPipe(stdio, 2) ? Buffer.concat(stderr).toString() : null,
}
: {
stdout: isPipe(stdio, 1) ? Buffer.concat(stdout) : null,
stderr: isPipe(stdio, 2) ? Buffer.concat(stderr) : null,
}

const promiseSpawnUid = (cmd, args, opts, extra) => {
let proc
const p = new Promise((res, rej) => {
proc = spawn(cmd, args, opts)
Expand Down Expand Up @@ -72,4 +49,14 @@ const promiseSpawnUid = (cmd, args, opts, extra) => {
return p
}

const stdioResult = (stdout, stderr, { stdioString, stdio }) =>
stdioString ? {
stdout: isPipe(stdio, 1) ? Buffer.concat(stdout).toString() : null,
stderr: isPipe(stdio, 2) ? Buffer.concat(stderr).toString() : null,
}
: {
stdout: isPipe(stdio, 1) ? Buffer.concat(stdout) : null,
stderr: isPipe(stdio, 2) ? Buffer.concat(stderr) : null,
}

module.exports = promiseSpawn
3 changes: 0 additions & 3 deletions package.json
Expand Up @@ -42,8 +42,5 @@
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
"version": "4.6.2"
},
"dependencies": {
"infer-owner": "^1.0.4"
}
}
31 changes: 0 additions & 31 deletions test/promise-spawn.js
@@ -1,7 +1,6 @@
const t = require('tap')
const Minipass = require('minipass')
const EE = require('events')
const fs = require('fs')

const isPipe = (stdio = 'pipe', fd) =>
stdio === 'pipe' || stdio === null ? true
Expand Down Expand Up @@ -215,33 +214,3 @@ t.test('expose process', t => {
t.end()
setTimeout(() => p.process.exit(0))
})

t.test('infer ownership', t => {
const { lstatSync } = fs
t.teardown(() => fs.lstatSync = lstatSync)
fs.lstatSync = (path) => ({ uid: 420, gid: 69 })
const getuid = process.getuid
t.teardown(() => process.getuid = getuid)

t.test('as non-root, do not change uid/gid, regardless of arguments', t => {
process.getuid = () => 1234
return t.resolveMatch(promiseSpawn('whoami', [], { uid: 4321, gid: 9876 }), {
code: 0,
signal: null,
stdout: Buffer.from('UID undefined\nGID undefined\n'),
stderr: Buffer.alloc(0),
})
})

t.test('as root, change uid/gid to folder, regardless of arguments', t => {
process.getuid = () => 0
return t.resolveMatch(promiseSpawn('whoami', [], { uid: 4321, gid: 9876 }), {
code: 0,
signal: null,
stdout: Buffer.from('UID 420\nGID 69\n'),
stderr: Buffer.alloc(0),
})
})

t.end()
})

0 comments on commit 422e1b6

Please sign in to comment.