Skip to content

Commit b7f6e5f

Browse files
committed
Infer ownership of shrinkwrap files
Do not leave a root-owned package-lock or npm-shrinkwrap file in the project root, where it will create problems when the user tries to update it later.
1 parent 8b85eaa commit b7f6e5f

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lib/shrinkwrap.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ const writeFileAtomic = require('write-file-atomic')
2525
const unixFormatPath = require('./utils/unix-format-path.js')
2626
const isRegistry = require('./utils/is-registry.js')
2727

28+
const { chown } = require('fs')
29+
const inferOwner = require('infer-owner')
30+
const selfOwner = {
31+
uid: process.getuid && process.getuid(),
32+
gid: process.getgid && process.getgid()
33+
}
34+
2835
const PKGLOCK = 'package-lock.json'
2936
const SHRINKWRAP = 'npm-shrinkwrap.json'
3037
const PKGLOCK_VERSION = npm.lockfileVersion
@@ -217,13 +224,19 @@ function save (dir, pkginfo, opts, cb) {
217224
log.verbose('shrinkwrap', `skipping write for ${path.basename(info.path)} because there were no changes.`)
218225
cb(null, pkginfo)
219226
} else {
220-
writeFileAtomic(info.path, swdata, (err) => {
221-
if (err) return cb(err)
222-
if (opts.silent) return cb(null, pkginfo)
223-
if (!shrinkwrap && !lockfile) {
224-
log.notice('', `created a lockfile as ${path.basename(info.path)}. You should commit this file.`)
225-
}
226-
cb(null, pkginfo)
227+
inferOwner(info.path).then(owner => {
228+
writeFileAtomic(info.path, swdata, (err) => {
229+
if (err) return cb(err)
230+
if (opts.silent) return cb(null, pkginfo)
231+
if (!shrinkwrap && !lockfile) {
232+
log.notice('', `created a lockfile as ${path.basename(info.path)}. You should commit this file.`)
233+
}
234+
if (selfOwner.uid === 0 && (selfOwner.uid !== owner.uid || selfOwner.gid !== owner.gid)) {
235+
chown(info.path, owner.uid, owner.gid, er => cb(er, pkginfo))
236+
} else {
237+
cb(null, pkginfo)
238+
}
239+
})
227240
})
228241
}
229242
}

0 commit comments

Comments
 (0)