Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove infer-owner #40

Merged
merged 2 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})